Skip to content

Commit

Permalink
fix(snoval): rename pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
snomiao committed Jun 15, 2022
1 parent f2f9abd commit 3b86bf7
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 76 deletions.
1 change: 0 additions & 1 deletion packages/async-value/package.json

This file was deleted.

8 changes: 0 additions & 8 deletions packages/async-value/src/index.test.ts

This file was deleted.

55 changes: 0 additions & 55 deletions packages/async-value/src/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/bothforage/package.json
Expand Up @@ -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",
Expand Down
5 changes: 1 addition & 4 deletions packages/junction-move/package.json
Expand Up @@ -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 <snomiao@gmail.com>",
"type": "module",
Expand Down
5 changes: 1 addition & 4 deletions packages/md-aggregate/package.json
Expand Up @@ -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 <snomiao@gmail.com>",
"type": "module",
Expand Down
9 changes: 7 additions & 2 deletions packages/outer-join-by/package.json
Expand Up @@ -6,8 +6,8 @@
"aoj",
"join"
],
"license": "ISC",
"author": "",
"license": "GPLv3",
"author": "snomiao <snomiao@gmail.com>",
"type": "module",
"exports": {
".": {
Expand All @@ -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"
}
}
2 changes: 1 addition & 1 deletion 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",
Expand Down
10 changes: 10 additions & 0 deletions 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 )
41 changes: 41 additions & 0 deletions 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 <snomiao@gmail.com>",
"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"
}
}
25 changes: 25 additions & 0 deletions packages/snoval/src/asyncFn.ts
@@ -0,0 +1,25 @@
import type { Conder, Iterater, Looper, PromiseLiker, Reducer } from "./types";
export async function asyncMap<V, R>(a: PromiseLiker<V[]>, f: Iterater<V, R>) {
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<V, R>(a: PromiseLiker<V[]>, f: Reducer<V, R>, 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<V, R>(a: PromiseLiker<V[]>, f: Iterater<V, R>) {
let i = 0;
a = await a;
for await (const v of a) await f(v, i++, a);
}
export async function asyncWhile<V, R>(cond: Conder<V>, f: Looper<V>) {
let i = 0;
let v = null;
while ((v = await cond())) await f(v, i++);
}
28 changes: 28 additions & 0 deletions 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],
);
});
38 changes: 38 additions & 0 deletions 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<V>(value: PromiseLiker<V[]>) {
const map = <R>(fn: Iterater<V, R>) => asyncArray(asyncMap(value, fn));
const forEach = <R>(fn: Iterater<V, R>) => asyncValue(asyncForEach(value, fn));
const reduce = <R>(fn: Reducer<V, R>) => asyncReduce(value, fn);
return { ...asyncValue(value), map, forEach, reduce };
}
export default function asyncValue<V>(value: PromiseLiker<V>) {
const pipe = <R>(f: { (v: V): PromiseLiker<R> }) =>
asyncValue(Promise.resolve(value).then((e) => f.call(e)));
const arrayPipe = <R extends any[]>(f: { (v: V): PromiseLiker<R> }) =>
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<void>((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 = <T>(fn: () => Promise<T>): Promise<T> => lock().then(fn).finally(unlock());
return { wait };
}
5 changes: 5 additions & 0 deletions packages/snoval/src/types.ts
@@ -0,0 +1,5 @@
export type PromiseLiker<T> = PromiseLike<T> | T;
export type Iterater<V, R> = { (v: V, i: number, a: V[]): PromiseLiker<R> };
export type Reducer<V, R> = { (r: R, v: V, i: number, a: V[]): PromiseLiker<R> };
export type Looper<V> = { (v: V, i: number): PromiseLiker<void> };
export type Conder<R> = { (): PromiseLiker<R> };

0 comments on commit 3b86bf7

Please sign in to comment.