Skip to content

Commit

Permalink
Merge pull request #67 from jokester/refine
Browse files Browse the repository at this point in the history
Refine
  • Loading branch information
jokester authored May 25, 2024
2 parents c1679ba + 9464427 commit 32b5595
Show file tree
Hide file tree
Showing 33 changed files with 236 additions and 258 deletions.
34 changes: 15 additions & 19 deletions jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
"name": "@ihate-work/ts-commonutil",
"version": "0.1.0",
"exports": {
"./algebra/monoid": "./src/algebra/monoid.ts",
"./algebra/topological-sort": "./src/algebra/topological-sort.ts",
"./algebra/total-ordered": "./src/algebra/total-ordered.ts",
"./binary-conversion/array-buffer": "./src/binary-conversion/array-buffer.ts",
"./binary-conversion/string": "./src/binary-conversion/string.ts",
"./binary-conversion/web": "./src/binary-conversion/web.ts",
"./bytes/array-buffer": "./src/bytes/array-buffer.ts",
"./bytes/string": "./src/bytes/string.ts",
"./bytes/web": "./src/bytes/web.ts",
"./collection/btree": "./src/collection/btree.ts",
"./collection/default-map": "./src/collection/default-map.ts",
"./collection/iterables": "./src/collection/iterables.ts",
"./collection/maps": "./src/collection/maps.ts",
"./collection/min-heap": "./src/collection/min-heap.ts",
"./collection/multiset": "./src/collection/multiset.ts",
"./collection/segment-tree": "./src/collection/segment-tree.ts",
Expand All @@ -20,17 +17,10 @@
"./concurrency/deferred": "./src/concurrency/deferred.ts",
"./concurrency/lazy-thenable": "./src/concurrency/lazy-thenable.ts",
"./concurrency/lease": "./src/concurrency/lease.ts",
"./concurrency/lockable": "./src/concurrency/lockable.ts",
"./concurrency/promise-container": "./src/concurrency/promise-container.ts",
"./concurrency/resource-pool": "./src/concurrency/resource-pool.ts",
"./concurrency/tic-toc": "./src/concurrency/tic-toc.ts",
"./concurrency/timing": "./src/concurrency/timing.ts",
"./event-emitter/typed-event-emitter": "./src/event-emitter/typed-event-emitter.ts",
"./fp-ts/monoid": "./src/fp-ts/monoid.ts",
"./frontend/dummy-img": "./src/frontend/dummy-img.ts",
"./frontend/fps-observable": "./src/frontend/fps-observable.ts",
"./frontend/measure-img": "./src/frontend/measure-img.ts",
"./frontend/observe-dom-mutation": "./src/frontend/observe-dom-mutation.ts",
"./index": "./src/index.ts",
"./logging/console-logger": "./src/logging/console-logger.ts",
"./logging/get-debug-namespace": "./src/logging/get-debug-namespace.ts",
Expand All @@ -39,6 +29,8 @@
"./node/fsp": "./src/node/fsp.ts",
"./node/index": "./src/node/index.ts",
"./node/subprocess": "./src/node/subprocess.ts",
"./random/number": "./src/random/number.ts",
"./random/series": "./src/random/series.ts",
"./react/component/font-awesome": "./src/react/component/font-awesome.tsx",
"./react/component/inspect-render-count": "./src/react/component/inspect-render-count.tsx",
"./react/component/pre-json": "./src/react/component/pre-json.tsx",
Expand All @@ -54,17 +46,21 @@
"./react/hook/use-promised": "./src/react/hook/use-promised.ts",
"./react/hook/use-versioned-memo": "./src/react/hook/use-versioned-memo.ts",
"./react/mobx/use-derived": "./src/react/mobx/use-derived.ts",
"./react/mobx/use-mobx-derived": "./src/react/mobx/use-mobx-derived.ts",
"./react/util/prop-of": "./src/react/util/prop-of.ts",
"./rxjs/distributions": "./src/rxjs/distributions.ts",
"./rxjs/stochastic-processes": "./src/rxjs/stochastic-processes.ts",
"./rxjs/poisson-observable": "./src/rxjs/poisson-observable.ts",
"./stress/chunk": "./src/stress/chunk.ts",
"./stress/groupBy": "./src/stress/groupBy.ts",
"./stress/sortBy": "./src/stress/sortBy.ts",
"./text/chunk-to-lines": "./src/text/chunk-to-lines.ts",
"./text/random-string": "./src/text/random-string.ts",
"./type/freeze": "./src/type/freeze.ts",
"./type/index": "./src/type/index.ts",
"./type/omit": "./src/type/omit.ts",
"./util/lru": "./src/util/lru.ts",
"./util/proxys": "./src/util/proxys.ts",
"./util/randoms": "./src/util/randoms.ts",
"./util/with-retry": "./src/util/with-retry.ts"
"./util/with-retry": "./src/util/with-retry.ts",
"./web/dummy-img": "./src/web/dummy-img.ts",
"./web/fps-observable": "./src/web/fps-observable.ts",
"./web/measure-img": "./src/web/measure-img.ts",
"./web/observe-dom-mutation": "./src/web/observe-dom-mutation.ts"
}
}
9 changes: 9 additions & 0 deletions obsolete/algebra/promise.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { liftA2 } from './promise';

describe('liftA2', () => {
it('lifts a sync function to promise', async () => {
const liftedPlus = liftA2((a: number, b: number) => a + b);
const sum = await liftedPlus(3, Promise.resolve(5));
expect(sum).toEqual(8);
});
});
13 changes: 13 additions & 0 deletions obsolete/algebra/promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/**
* lift2 f to Promise
*/
export function liftA2<T1, T2, T3>(f: (a1: T1, a2: T2) => T3) {
async function transformed(pa1: PromiseLike<T1> | T1, pa2: PromiseLike<T2> | T2): Promise<T3> {
const v1 = await pa1;
const v2 = await pa2;
return Promise.resolve(f(v1, v2));
}

return transformed;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PromiseContainer, PromiseState } from './promise-container';
import { wait } from './timing';
import { TicToc } from './tic-toc';
import { wait } from '../../src/concurrency/timing';
import { TicToc } from '../../src/concurrency/tic-toc';

describe('promise-container', () => {
it('keeps promised value', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Doomed } from './timing';
import { Doomed } from '../../src/concurrency/timing';

export enum PromiseState {
pending = 'pending',
Expand Down
22 changes: 22 additions & 0 deletions obsolete/node/read-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Read stream until end
* @deprecated use 'node:stream/consumers' instead.
* @see https://nodejs.org/api/webstreams.html#streamconsumersjsonstream
*/
export function readStream(stream: NodeJS.ReadableStream | NodeJS.ReadStream): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
const buffers = [] as Buffer[];

stream.once('end', () => resolve(Buffer.concat(buffers)));

stream.on('error', reject);

stream.on('data', (chunk: string | Buffer) => {
if (Buffer.isBuffer(chunk)) {
buffers.push(chunk);
} /* (typeof chunk === "string") */ else {
buffers.push(Buffer.from(chunk));
}
});
});
}
153 changes: 67 additions & 86 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"fp-ts": "^2",
"gts": "^5.2.0",
"jest": "^29",
"jsr": "^0.12.4",
"loglevel": "^1.8.0",
"mobx": "^6.6.1",
"react": "^18.2.0",
Expand All @@ -57,35 +58,25 @@
"winston": "^3.8.0"
},
"exports": {
"./lib/algebra/monoid": {
"import": "./lib/__esm/algebra/monoid.js",
"require": "./lib/algebra/monoid.js",
"type": "./lib/algebra/monoid.d.ts"
},
"./lib/algebra/topological-sort": {
"import": "./lib/__esm/algebra/topological-sort.js",
"require": "./lib/algebra/topological-sort.js",
"type": "./lib/algebra/topological-sort.d.ts"
},
"./lib/algebra/total-ordered": {
"import": "./lib/__esm/algebra/total-ordered.js",
"require": "./lib/algebra/total-ordered.js",
"type": "./lib/algebra/total-ordered.d.ts"
},
"./lib/binary-conversion/array-buffer": {
"import": "./lib/__esm/binary-conversion/array-buffer.js",
"require": "./lib/binary-conversion/array-buffer.js",
"type": "./lib/binary-conversion/array-buffer.d.ts"
"./lib/bytes/array-buffer": {
"import": "./lib/__esm/bytes/array-buffer.js",
"require": "./lib/bytes/array-buffer.js",
"type": "./lib/bytes/array-buffer.d.ts"
},
"./lib/binary-conversion/string": {
"import": "./lib/__esm/binary-conversion/string.js",
"require": "./lib/binary-conversion/string.js",
"type": "./lib/binary-conversion/string.d.ts"
"./lib/bytes/string": {
"import": "./lib/__esm/bytes/string.js",
"require": "./lib/bytes/string.js",
"type": "./lib/bytes/string.d.ts"
},
"./lib/binary-conversion/web": {
"import": "./lib/__esm/binary-conversion/web.js",
"require": "./lib/binary-conversion/web.js",
"type": "./lib/binary-conversion/web.d.ts"
"./lib/bytes/web": {
"import": "./lib/__esm/bytes/web.js",
"require": "./lib/bytes/web.js",
"type": "./lib/bytes/web.d.ts"
},
"./lib/collection/btree": {
"import": "./lib/__esm/collection/btree.js",
Expand All @@ -102,11 +93,6 @@
"require": "./lib/collection/iterables.js",
"type": "./lib/collection/iterables.d.ts"
},
"./lib/collection/maps": {
"import": "./lib/__esm/collection/maps.js",
"require": "./lib/collection/maps.js",
"type": "./lib/collection/maps.d.ts"
},
"./lib/collection/min-heap": {
"import": "./lib/__esm/collection/min-heap.js",
"require": "./lib/collection/min-heap.js",
Expand Down Expand Up @@ -147,16 +133,6 @@
"require": "./lib/concurrency/lease.js",
"type": "./lib/concurrency/lease.d.ts"
},
"./lib/concurrency/lockable": {
"import": "./lib/__esm/concurrency/lockable.js",
"require": "./lib/concurrency/lockable.js",
"type": "./lib/concurrency/lockable.d.ts"
},
"./lib/concurrency/promise-container": {
"import": "./lib/__esm/concurrency/promise-container.js",
"require": "./lib/concurrency/promise-container.js",
"type": "./lib/concurrency/promise-container.d.ts"
},
"./lib/concurrency/resource-pool": {
"import": "./lib/__esm/concurrency/resource-pool.js",
"require": "./lib/concurrency/resource-pool.js",
Expand All @@ -172,36 +148,11 @@
"require": "./lib/concurrency/timing.js",
"type": "./lib/concurrency/timing.d.ts"
},
"./lib/event-emitter/typed-event-emitter": {
"import": "./lib/__esm/event-emitter/typed-event-emitter.js",
"require": "./lib/event-emitter/typed-event-emitter.js",
"type": "./lib/event-emitter/typed-event-emitter.d.ts"
},
"./lib/fp-ts/monoid": {
"import": "./lib/__esm/fp-ts/monoid.js",
"require": "./lib/fp-ts/monoid.js",
"type": "./lib/fp-ts/monoid.d.ts"
},
"./lib/frontend/dummy-img": {
"import": "./lib/__esm/frontend/dummy-img.js",
"require": "./lib/frontend/dummy-img.js",
"type": "./lib/frontend/dummy-img.d.ts"
},
"./lib/frontend/fps-observable": {
"import": "./lib/__esm/frontend/fps-observable.js",
"require": "./lib/frontend/fps-observable.js",
"type": "./lib/frontend/fps-observable.d.ts"
},
"./lib/frontend/measure-img": {
"import": "./lib/__esm/frontend/measure-img.js",
"require": "./lib/frontend/measure-img.js",
"type": "./lib/frontend/measure-img.d.ts"
},
"./lib/frontend/observe-dom-mutation": {
"import": "./lib/__esm/frontend/observe-dom-mutation.js",
"require": "./lib/frontend/observe-dom-mutation.js",
"type": "./lib/frontend/observe-dom-mutation.d.ts"
},
"./lib/index": {
"import": "./lib/__esm/index.js",
"require": "./lib/index.js",
Expand Down Expand Up @@ -242,6 +193,16 @@
"require": "./lib/node/subprocess.js",
"type": "./lib/node/subprocess.d.ts"
},
"./lib/random/number": {
"import": "./lib/__esm/random/number.js",
"require": "./lib/random/number.js",
"type": "./lib/random/number.d.ts"
},
"./lib/random/series": {
"import": "./lib/__esm/random/series.js",
"require": "./lib/random/series.js",
"type": "./lib/random/series.d.ts"
},
"./lib/react/component/font-awesome": {
"import": "./lib/__esm/react/component/font-awesome.jsx",
"require": "./lib/react/component/font-awesome.jsx",
Expand Down Expand Up @@ -317,20 +278,35 @@
"require": "./lib/react/mobx/use-derived.js",
"type": "./lib/react/mobx/use-derived.d.ts"
},
"./lib/react/mobx/use-mobx-derived": {
"import": "./lib/__esm/react/mobx/use-mobx-derived.js",
"require": "./lib/react/mobx/use-mobx-derived.js",
"type": "./lib/react/mobx/use-mobx-derived.d.ts"
},
"./lib/react/util/prop-of": {
"import": "./lib/__esm/react/util/prop-of.js",
"require": "./lib/react/util/prop-of.js",
"type": "./lib/react/util/prop-of.d.ts"
},
"./lib/rxjs/distributions": {
"import": "./lib/__esm/rxjs/distributions.js",
"require": "./lib/rxjs/distributions.js",
"type": "./lib/rxjs/distributions.d.ts"
"./lib/rxjs/poisson-observable": {
"import": "./lib/__esm/rxjs/poisson-observable.js",
"require": "./lib/rxjs/poisson-observable.js",
"type": "./lib/rxjs/poisson-observable.d.ts"
},
"./lib/stress/chunk": {
"import": "./lib/__esm/stress/chunk.js",
"require": "./lib/stress/chunk.js",
"type": "./lib/stress/chunk.d.ts"
},
"./lib/stress/groupBy": {
"import": "./lib/__esm/stress/groupBy.js",
"require": "./lib/stress/groupBy.js",
"type": "./lib/stress/groupBy.d.ts"
},
"./lib/rxjs/stochastic-processes": {
"import": "./lib/__esm/rxjs/stochastic-processes.js",
"require": "./lib/rxjs/stochastic-processes.js",
"type": "./lib/rxjs/stochastic-processes.d.ts"
"./lib/stress/sortBy": {
"import": "./lib/__esm/stress/sortBy.js",
"require": "./lib/stress/sortBy.js",
"type": "./lib/stress/sortBy.d.ts"
},
"./lib/text/chunk-to-lines": {
"import": "./lib/__esm/text/chunk-to-lines.js",
Expand All @@ -357,29 +333,34 @@
"require": "./lib/type/omit.js",
"type": "./lib/type/omit.d.ts"
},
"./lib/util/lru": {
"import": "./lib/__esm/util/lru.js",
"require": "./lib/util/lru.js",
"type": "./lib/util/lru.d.ts"
},
"./lib/util/proxys": {
"import": "./lib/__esm/util/proxys.js",
"require": "./lib/util/proxys.js",
"type": "./lib/util/proxys.d.ts"
},
"./lib/util/randoms": {
"import": "./lib/__esm/util/randoms.js",
"require": "./lib/util/randoms.js",
"type": "./lib/util/randoms.d.ts"
},
"./lib/util/with-retry": {
"import": "./lib/__esm/util/with-retry.js",
"require": "./lib/util/with-retry.js",
"type": "./lib/util/with-retry.d.ts"
},
"./lib/web/dummy-img": {
"import": "./lib/__esm/web/dummy-img.js",
"require": "./lib/web/dummy-img.js",
"type": "./lib/web/dummy-img.d.ts"
},
"./lib/web/fps-observable": {
"import": "./lib/__esm/web/fps-observable.js",
"require": "./lib/web/fps-observable.js",
"type": "./lib/web/fps-observable.d.ts"
},
"./lib/web/measure-img": {
"import": "./lib/__esm/web/measure-img.js",
"require": "./lib/web/measure-img.js",
"type": "./lib/web/measure-img.d.ts"
},
"./lib/web/observe-dom-mutation": {
"import": "./lib/__esm/web/observe-dom-mutation.js",
"require": "./lib/web/observe-dom-mutation.js",
"type": "./lib/web/observe-dom-mutation.d.ts"
}
},
"resolutions": {
"@types/node": "18",
"@types/react": "^18"
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/concurrency/deferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Deferred<T> implements PromiseLike<T> {
return d;
}

private readonly _promise = new Promise<T>((fulfill, reject) => {
readonly #promise = new Promise<T>((fulfill, reject) => {
this.#fulfill = (v: T | PromiseLike<T>) => {
fulfill(v);
this.#resolved = true;
Expand All @@ -38,7 +38,7 @@ export class Deferred<T> implements PromiseLike<T> {
return this.#resolved;
}

readonly then = this._promise.then.bind(this._promise);
readonly then = this.#promise.then.bind(this.#promise);

/**
* @param v the value
Expand Down
Empty file removed src/concurrency/lockable.ts
Empty file.
2 changes: 1 addition & 1 deletion src/node/fsp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promises as fsPromised } from 'fs';
import fsPromised from 'node:fs/promises';
import { chunkToLines } from '../text/chunk-to-lines';

export const readText = (filename: string, encoding: BufferEncoding = 'utf-8'): Promise<string> =>
Expand Down
Loading

0 comments on commit 32b5595

Please sign in to comment.