diff --git a/packages/async-value/package.json b/packages/async-value/package.json deleted file mode 100644 index 9e26dfee..00000000 --- a/packages/async-value/package.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/packages/async-value/src/index.test.ts b/packages/async-value/src/index.test.ts deleted file mode 100644 index b2dea58b..00000000 --- a/packages/async-value/src/index.test.ts +++ /dev/null @@ -1,8 +0,0 @@ -import asyncValue from "./index"; -(async function () { - console.log( - await asyncValue(12) - .pipe((v) => v * 12) // 144 - .valueOf(), - ); -})(); diff --git a/packages/async-value/src/index.ts b/packages/async-value/src/index.ts deleted file mode 100644 index 2b4119b1..00000000 --- a/packages/async-value/src/index.ts +++ /dev/null @@ -1,55 +0,0 @@ -type PromiseLiker = Promise | PromiseLike | T; -type Iterator = { (v: V, i: number, a: V[]): PromiseLiker }; -type Reducer = { (v: V, w: W, i: number, a: V[]): PromiseLiker }; -type Looper = { (v: V, i: number): PromiseLiker }; -type Conder = { (): PromiseLiker }; -[].reduc; -export async function asyncMap(a: PromiseLiker, f: Iterator) { - const r = []; - let i = 0; - a = await a; - for await (const v of a) r.push(await f(v, i++, a)); - return r; -} -export async function asyncForEach(a: PromiseLiker, f: Iterator) { - let i = 0; - a = await a; - for await (const v of a) await f(v, i++, a); -} -export async function asyncWhile(cond: Conder, f: Looper) { - let i = 0; - let v = null; - while ((v = await cond())) await f(v, i++); -} -export function asyncArray(value: PromiseLiker) { - // const pipe = (f: { (v: V[]): PromiseLiker }) => asyncArray(Promise.resolve(value).then(f)); - const map = (fn: Iterator) => asyncArray(asyncMap(value, fn)); - const forEach = (fn: Iterator) => asyncValue(asyncForEach(value, fn)); - const reduce = (fn: Iterator) => asyncArray(asyncReduce(value, fn)); - // (async function() { - // (await value). - // })(); - return { ...asyncValue(value), map, forEach, reduce }; -} -export default function asyncValue(value: PromiseLiker) { - const pipe = (f: { (v: V): PromiseLiker }) => asyncValue(Promise.resolve(value).then(f)); - const valueOf = async () => await value; - return { - valueOf, - pipe, - }; -} -/** - * @example const { lock, unlock } = useLockers(n); - */ -export function useLockers(n = 1) { - const resolves = []; - const lock = () => new Promise((resolve) => (n-- ? resolve() : resolves.push(resolve))); - const unlock = () => (n++, resolves.shift()?.()); - return { lock, unlock }; -} -export function useLimiter(n = 1) { - const { lock, unlock } = useLockers(n); - const wait = (fn: () => Promise): Promise => lock().then(fn).finally(unlock()); - return { wait }; -} diff --git a/packages/bothforage/package.json b/packages/bothforage/package.json index ded1d0fb..c62a954a 100644 --- a/packages/bothforage/package.json +++ b/packages/bothforage/package.json @@ -5,7 +5,7 @@ "keywords": [], "homepage": "https://github.com/snomiao/js/tree/main/packages/bothforage#readme", "bugs": { - "url": "https://github.com/snomiao/js/issues?q=is%3Aissue+is%3Aopen+bothforage" + "url": "https://github.com/snomiao/js/issues?q=is%3Aissue+is%3Aopen+@snomiao/bothforage" }, "repository": "https://github.com/snomiao/js/tree/main/packages/bothforage", "license": "GPLv3", diff --git a/packages/junction-move/package.json b/packages/junction-move/package.json index ecf61205..0635894d 100644 --- a/packages/junction-move/package.json +++ b/packages/junction-move/package.json @@ -13,10 +13,7 @@ "bugs": { "url": "https://github.com/snomiao/js/issues?q=is%3Aissue+is%3Aopen+junction-move" }, - "repository": { - "type": "git", - "url": "git+https://github.com/snomiao/js.git#main" - }, + "repository": "https://github.com/snomiao/js/tree/main/packages/junction-move", "license": "GPLv3", "author": "snomiao ", "type": "module", diff --git a/packages/md-aggregate/package.json b/packages/md-aggregate/package.json index 70a64134..801e7e40 100644 --- a/packages/md-aggregate/package.json +++ b/packages/md-aggregate/package.json @@ -14,10 +14,7 @@ "bugs": { "url": "https://github.com/snomiao/js/issues?q=is%3Aissue+is%3Aopen+md-aggregate" }, - "repository": { - "type": "git", - "url": "git+https://github.com/snomiao/js.git#main" - }, + "repository": "https://github.com/snomiao/js/tree/main/packages/md-aggregate", "license": "GPLv3", "author": "snomiao ", "type": "module", diff --git a/packages/outer-join-by/package.json b/packages/outer-join-by/package.json index 527d9362..060e801a 100644 --- a/packages/outer-join-by/package.json +++ b/packages/outer-join-by/package.json @@ -6,8 +6,8 @@ "aoj", "join" ], - "license": "ISC", - "author": "", + "license": "GPLv3", + "author": "snomiao ", "type": "module", "exports": { ".": { @@ -34,5 +34,10 @@ "mocha": "^10.0.0", "snobuild": "^2.0.0", "tsx": "^3.4.2" + }, + "repository": "https://github.com/snomiao/js/tree/main/packages/outer-join-by", + "homepage": "https://github.com/snomiao/js/tree/main/packages/outer-join-by#readme", + "bugs": { + "url": "https://github.com/snomiao/js/issues?q=is%3Aissue+is%3Aopen+outer-join-by" } } diff --git a/packages/snobuild/package.json b/packages/snobuild/package.json index b331c794..3f8a6b90 100644 --- a/packages/snobuild/package.json +++ b/packages/snobuild/package.json @@ -1,6 +1,6 @@ { "name": "snobuild", - "version": "2.1.1", + "version": "2.1.2", "description": "Easy way to use esbuild.", "keywords": [ "cli", diff --git a/packages/snoval/README.md b/packages/snoval/README.md new file mode 100644 index 00000000..3c756dfe --- /dev/null +++ b/packages/snoval/README.md @@ -0,0 +1,10 @@ +# async-value (WIP) + +Async Value pipelines (WIP) + +## Usage + +## Reference + +- [tubo - npm]( https://www.npmjs.com/package/tubo ) +- [Ramda Documentation]( https://ramdajs.com/docs/#pipe ) \ No newline at end of file diff --git a/packages/snoval/package.json b/packages/snoval/package.json new file mode 100644 index 00000000..731e8b3a --- /dev/null +++ b/packages/snoval/package.json @@ -0,0 +1,41 @@ +{ + "name": "snoval", + "version": "1.0.0", + "description": "Async Value pipelines (WIP)", + "keywords": [ + "async", + "value", + "functional", + "pipeline" + ], + "homepage": "https://github.com/snomiao/js/tree/main/packages/async-value#readme", + "bugs": { + "url": "https://github.com/snomiao/js/issues?q=is%3Aissue+is%3Aopen+async-value" + }, + "repository": "https://github.com/snomiao/js/tree/main/packages/async-value", + "license": "GPLv3", + "author": "snomiao ", + "type": "module", + "exports": { + ".": { + "require": "./lib/index.cjs", + "import": "./lib/index.js" + } + }, + "main": "lib/index.cjs", + "module": "lib/index.js", + "types": "./lib/index.d.ts", + "files": [ + "lib" + ], + "scripts": { + "build": "snobuild --lib", + "prepack": "npm run build", + "test": "mocha --require tsx ./src/**/*.test.ts" + }, + "devDependencies": { + "@types/mocha": "^9.1.1", + "mocha": "^10.0.0", + "snobuild": "^2.1.1" + } +} diff --git a/packages/snoval/src/asyncFn.ts b/packages/snoval/src/asyncFn.ts new file mode 100644 index 00000000..8ddf6d4d --- /dev/null +++ b/packages/snoval/src/asyncFn.ts @@ -0,0 +1,25 @@ +import type { Conder, Iterater, Looper, PromiseLiker, Reducer } from "./types"; +export async function asyncMap(a: PromiseLiker, f: Iterater) { + const r = []; + let i = 0; + a = await a; + for await (const v of a) r.push(await f(v, i++, a)); + return r; +} +export async function asyncReduce(a: PromiseLiker, f: Reducer, r?: R) { + a = await a; + if (r === undefined) r = a[0] as unknown as R; + let i = 0; + for await (const v of a) r = await f(r, v, i++, a); + return r; +} +export async function asyncForEach(a: PromiseLiker, f: Iterater) { + let i = 0; + a = await a; + for await (const v of a) await f(v, i++, a); +} +export async function asyncWhile(cond: Conder, f: Looper) { + let i = 0; + let v = null; + while ((v = await cond())) await f(v, i++); +} diff --git a/packages/snoval/src/index.test.ts b/packages/snoval/src/index.test.ts new file mode 100644 index 00000000..807b4fca --- /dev/null +++ b/packages/snoval/src/index.test.ts @@ -0,0 +1,28 @@ +import { strictEqual } from "assert"; +import { asyncMap } from "./asyncFn"; +import asyncValue from "./index"; +it("number pipe valueOf", async () => { + strictEqual( + await asyncValue(12) + .pipe((v) => v * 12) + .valueOf(), + 144, + ); +}); + +it("number pipe value", async () => { + strictEqual(await asyncValue(12).pipe((v) => v * 12).value, 144); +}); + +it("number multi pipe value", async () => { + strictEqual( + await asyncValue(1) + .pipe((n) => n / 2) + .pipe((n) => n.toString()) + .pipe((s) => s.split(".")) + .pipe((a) => asyncMap(a, Number)) + // .pipe(Array.prototype.reverse) + .value, + [5, 0], + ); +}); diff --git a/packages/snoval/src/index.ts b/packages/snoval/src/index.ts new file mode 100644 index 00000000..e1a638eb --- /dev/null +++ b/packages/snoval/src/index.ts @@ -0,0 +1,38 @@ +import { asyncForEach, asyncMap, asyncReduce } from "./asyncFn"; +import { Iterater, PromiseLiker, Reducer } from "./types"; +export * from "./asyncFn"; + +export function asyncArray(value: PromiseLiker) { + const map = (fn: Iterater) => asyncArray(asyncMap(value, fn)); + const forEach = (fn: Iterater) => asyncValue(asyncForEach(value, fn)); + const reduce = (fn: Reducer) => asyncReduce(value, fn); + return { ...asyncValue(value), map, forEach, reduce }; +} +export default function asyncValue(value: PromiseLiker) { + const pipe = (f: { (v: V): PromiseLiker }) => + asyncValue(Promise.resolve(value).then((e) => f.call(e))); + const arrayPipe = (f: { (v: V): PromiseLiker }) => + asyncArray(Promise.resolve(value).then(f)); + const valueOf = async () => await value; + return { + value, + valueOf, + pipe, + arrayPipe, + }; +} +// flow control +/** + * @example const { lock, unlock } = useLockers(n); + */ +export function useLockers(n = 1) { + const resolves = []; + const lock = () => new Promise((resolve) => (n-- ? resolve() : resolves.push(resolve))); + const unlock = () => (n++, resolves.shift()?.()); + return { lock, unlock }; +} +export function useLimiter(n = 1) { + const { lock, unlock } = useLockers(n); + const wait = (fn: () => Promise): Promise => lock().then(fn).finally(unlock()); + return { wait }; +} diff --git a/packages/snoval/src/types.ts b/packages/snoval/src/types.ts new file mode 100644 index 00000000..10791034 --- /dev/null +++ b/packages/snoval/src/types.ts @@ -0,0 +1,5 @@ +export type PromiseLiker = PromiseLike | T; +export type Iterater = { (v: V, i: number, a: V[]): PromiseLiker }; +export type Reducer = { (r: R, v: V, i: number, a: V[]): PromiseLiker }; +export type Looper = { (v: V, i: number): PromiseLiker }; +export type Conder = { (): PromiseLiker };