@e280/stz 0.0.0-28 → 0.0.0-29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -74,9 +74,9 @@ standard library of environment-agnostic typescript functions we use basically e
74
74
  sendMessage.clear()
75
75
  onMessage.clear()
76
76
 
77
- // you can subscribe to only one next call
78
- onMessage.once(m => console.log(m))
79
- sendMessage.once(m => console.log(m))
77
+ // instead of a 'once' fn we simply await next()
78
+ await onMessage.next()
79
+ await sendMessage.next()
80
80
  ```
81
81
 
82
82
  <br/>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e280/stz",
3
- "version": "0.0.0-28",
3
+ "version": "0.0.0-29",
4
4
  "description": "everyday ts fns for everything",
5
5
  "license": "MIT",
6
6
  "author": "Chase Moskal <chasemoskal@gmail.com>",
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "devDependencies": {
26
26
  "@e280/science": "^0.0.5",
27
- "@types/node": "^24.0.7",
27
+ "@types/node": "^24.1.0",
28
28
  "npm-run-all": "^4.1.5",
29
29
  "typescript": "^5.8.3"
30
30
  },
package/s/defer.ts CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  /** a promise which can be resolved from the outside */
3
- export type Deferred<R> = {
3
+ export type Deferred<R = void> = {
4
4
  promise: Promise<R>
5
5
  resolve: (result: R) => void
6
6
  reject: (reason: any) => void
@@ -10,7 +10,7 @@ export type Deferred<R> = {
10
10
  }
11
11
 
12
12
  /** returns a deferred promise with exposed resolve and reject fns */
13
- export function defer<R>(): Deferred<R> {
13
+ export function defer<R = void>(): Deferred<R> {
14
14
  let resolve!: (result: R) => void
15
15
  let reject!: (reason: any) => void
16
16
 
package/s/ev.ts ADDED
@@ -0,0 +1,20 @@
1
+
2
+ export type EvFn = (...a: any[]) => void
3
+
4
+ export type EvTarget<N extends string> = {
5
+ addEventListener(name: N, fn: EvFn): void
6
+ removeEventListener(name: N, fn: EvFn): void
7
+ }
8
+
9
+ export function ev<N extends string>(target: EvTarget<N>, fns: Record<N, EvFn>) {
10
+ const entries = Object.entries(fns) as [N, EvFn][]
11
+
12
+ for (const [name, fn] of entries)
13
+ target.addEventListener(name, fn)
14
+
15
+ return () => {
16
+ for (const [name, fn] of entries)
17
+ target.removeEventListener(name, fn)
18
+ }
19
+ }
20
+
package/s/index.ts CHANGED
@@ -20,6 +20,7 @@ export * from "./dedupe.js"
20
20
  export * from "./defer.js"
21
21
  export * from "./drill.js"
22
22
  export * from "./escape-regex.js"
23
+ export * from "./ev.js"
23
24
  export * from "./hat.js"
24
25
  export * from "./id-counter.js"
25
26
  export * from "./is.js"
@@ -27,8 +28,9 @@ export * from "./loopy.js"
27
28
  export * from "./map-g.js"
28
29
  export * from "./nap.js"
29
30
  export * from "./ob.js"
31
+ export * from "./once.js"
30
32
  export * from "./pipe.js"
31
- export * from "./repeatly.js"
33
+ export * from "./repeat.js"
32
34
  export * from "./pubsub.js"
33
35
  export * from "./time.js"
34
36
  export * from "./trash.js"
package/s/once.ts ADDED
@@ -0,0 +1,14 @@
1
+
2
+ export function once<Fn extends (...a: any[]) => any>(fn: Fn) {
3
+ let done = false
4
+ let ret: any
5
+
6
+ return ((...a) => {
7
+ if (!done) {
8
+ done = true
9
+ ret = fn(...a)
10
+ }
11
+ return ret
12
+ }) as Fn
13
+ }
14
+
package/s/pubsub.ts CHANGED
@@ -17,8 +17,8 @@ export interface Xub<A extends any[] = []> {
17
17
  */
18
18
  on(fn: Listener<A>): () => void
19
19
 
20
- /** subscribe a listener function that unsubscribes itself after being invoked. */
21
- once(): Promise<A>
20
+ /** wait for the next published value */
21
+ next(): Promise<A>
22
22
 
23
23
  /** wipe all listeners attached to this. */
24
24
  clear(): void
@@ -46,7 +46,7 @@ export function xub<A extends any[] = []>() {
46
46
  await Promise.all([...set].map(fn => fn(...a)))
47
47
  }
48
48
 
49
- async function once() {
49
+ async function next() {
50
50
  const {promise, resolve} = defer<A>()
51
51
  const unsubscribe = sub((...a) => {
52
52
  resolve(a)
@@ -62,13 +62,13 @@ export function xub<A extends any[] = []>() {
62
62
  sub.pub = pub
63
63
  sub.sub = sub
64
64
  sub.on = sub
65
- sub.once = once
65
+ sub.next = next
66
66
  sub.clear = clear
67
67
 
68
68
  pub.pub = pub
69
69
  pub.sub = sub
70
70
  pub.on = sub
71
- pub.once = once
71
+ pub.next = next
72
72
  pub.clear = clear
73
73
 
74
74
  return [pub, sub] as [Pub<A>, Sub<A>]
package/s/repeat.ts ADDED
@@ -0,0 +1,25 @@
1
+
2
+ /**
3
+ * repeat the given async function over and over.
4
+ * - consider using `nap` in your fn to create a delay
5
+ */
6
+ export function repeat(fn: (stop: () => void) => Promise<void>) {
7
+ let timeout: any
8
+ let stopped = false
9
+
10
+ const stop = () => {
11
+ stopped = true
12
+ clearTimeout(timeout)
13
+ }
14
+
15
+ const tick = async() => {
16
+ if (stopped) return
17
+ await fn(stop)
18
+ if (stopped) return
19
+ timeout = setTimeout(tick, 0)
20
+ }
21
+
22
+ tick()
23
+ return stop
24
+ }
25
+
package/x/defer.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /** a promise which can be resolved from the outside */
2
- export type Deferred<R> = {
2
+ export type Deferred<R = void> = {
3
3
  promise: Promise<R>;
4
4
  resolve: (result: R) => void;
5
5
  reject: (reason: any) => void;
@@ -7,4 +7,4 @@ export type Deferred<R> = {
7
7
  entangle: (outsidePromise: Promise<R>) => Promise<R>;
8
8
  };
9
9
  /** returns a deferred promise with exposed resolve and reject fns */
10
- export declare function defer<R>(): Deferred<R>;
10
+ export declare function defer<R = void>(): Deferred<R>;
package/x/ev.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export type EvFn = (...a: any[]) => void;
2
+ export type EvTarget<N extends string> = {
3
+ addEventListener(name: N, fn: EvFn): void;
4
+ removeEventListener(name: N, fn: EvFn): void;
5
+ };
6
+ export declare function ev<N extends string>(target: EvTarget<N>, fns: Record<N, EvFn>): () => void;
package/x/ev.js ADDED
@@ -0,0 +1,10 @@
1
+ export function ev(target, fns) {
2
+ const entries = Object.entries(fns);
3
+ for (const [name, fn] of entries)
4
+ target.addEventListener(name, fn);
5
+ return () => {
6
+ for (const [name, fn] of entries)
7
+ target.removeEventListener(name, fn);
8
+ };
9
+ }
10
+ //# sourceMappingURL=ev.js.map
package/x/ev.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ev.js","sourceRoot":"","sources":["../s/ev.ts"],"names":[],"mappings":"AAQA,MAAM,UAAU,EAAE,CAAmB,MAAmB,EAAE,GAAoB;IAC7E,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAgB,CAAA;IAElD,KAAK,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,OAAO;QAC/B,MAAM,CAAC,gBAAgB,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;IAElC,OAAO,GAAG,EAAE;QACX,KAAK,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,OAAO;YAC/B,MAAM,CAAC,mBAAmB,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;IACtC,CAAC,CAAA;AACF,CAAC"}
package/x/index.d.ts CHANGED
@@ -16,6 +16,7 @@ export * from "./dedupe.js";
16
16
  export * from "./defer.js";
17
17
  export * from "./drill.js";
18
18
  export * from "./escape-regex.js";
19
+ export * from "./ev.js";
19
20
  export * from "./hat.js";
20
21
  export * from "./id-counter.js";
21
22
  export * from "./is.js";
@@ -23,8 +24,9 @@ export * from "./loopy.js";
23
24
  export * from "./map-g.js";
24
25
  export * from "./nap.js";
25
26
  export * from "./ob.js";
27
+ export * from "./once.js";
26
28
  export * from "./pipe.js";
27
- export * from "./repeatly.js";
29
+ export * from "./repeat.js";
28
30
  export * from "./pubsub.js";
29
31
  export * from "./time.js";
30
32
  export * from "./trash.js";
package/x/index.js CHANGED
@@ -16,6 +16,7 @@ export * from "./dedupe.js";
16
16
  export * from "./defer.js";
17
17
  export * from "./drill.js";
18
18
  export * from "./escape-regex.js";
19
+ export * from "./ev.js";
19
20
  export * from "./hat.js";
20
21
  export * from "./id-counter.js";
21
22
  export * from "./is.js";
@@ -23,8 +24,9 @@ export * from "./loopy.js";
23
24
  export * from "./map-g.js";
24
25
  export * from "./nap.js";
25
26
  export * from "./ob.js";
27
+ export * from "./once.js";
26
28
  export * from "./pipe.js";
27
- export * from "./repeatly.js";
29
+ export * from "./repeat.js";
28
30
  export * from "./pubsub.js";
29
31
  export * from "./time.js";
30
32
  export * from "./trash.js";
package/x/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../s/index.ts"],"names":[],"mappings":"AACA,cAAc,0BAA0B,CAAA;AACxC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA;AAE7B,cAAc,wBAAwB,CAAA;AACtC,cAAc,qBAAqB,CAAA;AAEnC,cAAc,gBAAgB,CAAA;AAE9B,cAAc,eAAe,CAAA;AAC7B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA;AACjC,cAAc,UAAU,CAAA;AACxB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,SAAS,CAAA;AACvB,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../s/index.ts"],"names":[],"mappings":"AACA,cAAc,0BAA0B,CAAA;AACxC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,qBAAqB,CAAA;AACnC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA;AAE7B,cAAc,wBAAwB,CAAA;AACtC,cAAc,qBAAqB,CAAA;AAEnC,cAAc,gBAAgB,CAAA;AAE9B,cAAc,eAAe,CAAA;AAC7B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA;AACjC,cAAc,SAAS,CAAA;AACvB,cAAc,UAAU,CAAA;AACxB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,SAAS,CAAA;AACvB,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA"}
package/x/once.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function once<Fn extends (...a: any[]) => any>(fn: Fn): Fn;
package/x/once.js ADDED
@@ -0,0 +1,12 @@
1
+ export function once(fn) {
2
+ let done = false;
3
+ let ret;
4
+ return ((...a) => {
5
+ if (!done) {
6
+ done = true;
7
+ ret = fn(...a);
8
+ }
9
+ return ret;
10
+ });
11
+ }
12
+ //# sourceMappingURL=once.js.map
package/x/once.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"once.js","sourceRoot":"","sources":["../s/once.ts"],"names":[],"mappings":"AACA,MAAM,UAAU,IAAI,CAAkC,EAAM;IAC3D,IAAI,IAAI,GAAG,KAAK,CAAA;IAChB,IAAI,GAAQ,CAAA;IAEZ,OAAO,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE;QAChB,IAAI,CAAC,IAAI,EAAE,CAAC;YACX,IAAI,GAAG,IAAI,CAAA;YACX,GAAG,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;QACf,CAAC;QACD,OAAO,GAAG,CAAA;IACX,CAAC,CAAO,CAAA;AACT,CAAC"}
package/x/pubsub.d.ts CHANGED
@@ -9,8 +9,8 @@ export interface Xub<A extends any[] = []> {
9
9
  * @alias sub
10
10
  */
11
11
  on(fn: Listener<A>): () => void;
12
- /** subscribe a listener function that unsubscribes itself after being invoked. */
13
- once(): Promise<A>;
12
+ /** wait for the next published value */
13
+ next(): Promise<A>;
14
14
  /** wipe all listeners attached to this. */
15
15
  clear(): void;
16
16
  }
package/x/pubsub.js CHANGED
@@ -8,7 +8,7 @@ export function xub() {
8
8
  async function pub(...a) {
9
9
  await Promise.all([...set].map(fn => fn(...a)));
10
10
  }
11
- async function once() {
11
+ async function next() {
12
12
  const { promise, resolve } = defer();
13
13
  const unsubscribe = sub((...a) => {
14
14
  resolve(a);
@@ -22,12 +22,12 @@ export function xub() {
22
22
  sub.pub = pub;
23
23
  sub.sub = sub;
24
24
  sub.on = sub;
25
- sub.once = once;
25
+ sub.next = next;
26
26
  sub.clear = clear;
27
27
  pub.pub = pub;
28
28
  pub.sub = sub;
29
29
  pub.on = sub;
30
- pub.once = once;
30
+ pub.next = next;
31
31
  pub.clear = clear;
32
32
  return [pub, sub];
33
33
  }
package/x/repeat.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * repeat the given async function over and over.
3
+ * - consider using `nap` in your fn to create a delay
4
+ */
5
+ export declare function repeat(fn: (stop: () => void) => Promise<void>): () => void;
package/x/repeat.js ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * repeat the given async function over and over.
3
+ * - consider using `nap` in your fn to create a delay
4
+ */
5
+ export function repeat(fn) {
6
+ let timeout;
7
+ let stopped = false;
8
+ const stop = () => {
9
+ stopped = true;
10
+ clearTimeout(timeout);
11
+ };
12
+ const tick = async () => {
13
+ if (stopped)
14
+ return;
15
+ await fn(stop);
16
+ if (stopped)
17
+ return;
18
+ timeout = setTimeout(tick, 0);
19
+ };
20
+ tick();
21
+ return stop;
22
+ }
23
+ //# sourceMappingURL=repeat.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"repeat.js","sourceRoot":"","sources":["../s/repeat.ts"],"names":[],"mappings":"AACA;;;GAGG;AACH,MAAM,UAAU,MAAM,CAAC,EAAuC;IAC7D,IAAI,OAAY,CAAA;IAChB,IAAI,OAAO,GAAG,KAAK,CAAA;IAEnB,MAAM,IAAI,GAAG,GAAG,EAAE;QACjB,OAAO,GAAG,IAAI,CAAA;QACd,YAAY,CAAC,OAAO,CAAC,CAAA;IACtB,CAAC,CAAA;IAED,MAAM,IAAI,GAAG,KAAK,IAAG,EAAE;QACtB,IAAI,OAAO;YAAE,OAAM;QACnB,MAAM,EAAE,CAAC,IAAI,CAAC,CAAA;QACd,IAAI,OAAO;YAAE,OAAM;QACnB,OAAO,GAAG,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,CAAA;IAC9B,CAAC,CAAA;IAED,IAAI,EAAE,CAAA;IACN,OAAO,IAAI,CAAA;AACZ,CAAC"}
package/s/repeatly.ts DELETED
@@ -1,19 +0,0 @@
1
-
2
- export function repeatly(milliseconds: number, fn: () => Promise<void>) {
3
- let timeout: any
4
- let stop = false
5
-
6
- async function tick() {
7
- if (stop) return undefined
8
- await fn()
9
- timeout = setTimeout(tick, milliseconds)
10
- }
11
-
12
- timeout = setTimeout(tick, milliseconds)
13
-
14
- return () => {
15
- stop = true
16
- clearTimeout(timeout)
17
- }
18
- }
19
-
package/x/repeatly.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function repeatly(milliseconds: number, fn: () => Promise<void>): () => void;
package/x/repeatly.js DELETED
@@ -1,16 +0,0 @@
1
- export function repeatly(milliseconds, fn) {
2
- let timeout;
3
- let stop = false;
4
- async function tick() {
5
- if (stop)
6
- return undefined;
7
- await fn();
8
- timeout = setTimeout(tick, milliseconds);
9
- }
10
- timeout = setTimeout(tick, milliseconds);
11
- return () => {
12
- stop = true;
13
- clearTimeout(timeout);
14
- };
15
- }
16
- //# sourceMappingURL=repeatly.js.map
package/x/repeatly.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"repeatly.js","sourceRoot":"","sources":["../s/repeatly.ts"],"names":[],"mappings":"AACA,MAAM,UAAU,QAAQ,CAAC,YAAoB,EAAE,EAAuB;IACrE,IAAI,OAAY,CAAA;IAChB,IAAI,IAAI,GAAG,KAAK,CAAA;IAEhB,KAAK,UAAU,IAAI;QAClB,IAAI,IAAI;YAAE,OAAO,SAAS,CAAA;QAC1B,MAAM,EAAE,EAAE,CAAA;QACV,OAAO,GAAG,UAAU,CAAC,IAAI,EAAE,YAAY,CAAC,CAAA;IACzC,CAAC;IAED,OAAO,GAAG,UAAU,CAAC,IAAI,EAAE,YAAY,CAAC,CAAA;IAExC,OAAO,GAAG,EAAE;QACX,IAAI,GAAG,IAAI,CAAA;QACX,YAAY,CAAC,OAAO,CAAC,CAAA;IACtB,CAAC,CAAA;AACF,CAAC"}