@e280/stz 0.2.0-1 → 0.2.0-4

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
@@ -9,40 +9,77 @@ zero dependencies.
9
9
 
10
10
  <br/>
11
11
 
12
- ## 🧰 STZ PRIMITIVES
12
+ ## 🥨 stz primitives
13
13
 
14
- ### 🍏 MapG
15
- > extended js map
14
+ ### 🍏 `pub` and `sub`
15
+ > ergonomic event emitters
16
16
 
17
17
  ```ts
18
- import {MapG} from "@e280/stz"
19
-
20
- const map = new MapG<number, string>([
21
- [1, "hello"],
22
- [2, "world"],
23
- ])
18
+ import {pub, sub} from "@e280/stz"
24
19
  ```
25
20
 
26
- - `map.require(key)` — returns the value for key — if missing, throw an error
21
+ #### `pub`
22
+ - make a publisher fn
27
23
  ```ts
28
- const value = map.require(1)
29
- // "hello"
24
+ // create a pub fn
25
+ const sendMessage = pub<[string]>()
26
+
27
+ // subscribe to it
28
+ sendMessage.subscribe(m => console.log(m))
29
+
30
+ // publish to it
31
+ sendMessage("hello")
30
32
  ```
31
- - `map.guarantee(key, make)` — returns the value for `key` — if missing, run `make` to set the value and return it
33
+
34
+ #### `sub`
35
+ - make a subscriber fn — *it's just like pub, except it's flipsy-reversey!*
32
36
  ```ts
33
- const value = map.guarantee(3, () => "rofl")
34
- // "rofl"
35
- ```
37
+ // create a sub fn
38
+ const onMessage = sub<[string]>()
36
39
 
37
- ### 🍏 nap
38
- > sleep for some milliseconds
40
+ // subscribe to it
41
+ onMessage(m => console.log(m))
39
42
 
40
- ```ts
41
- import {nap} from "@e280/stz"
43
+ // publish to it
44
+ onMessage.publish("hello")
45
+ ```
42
46
 
43
- await nap(900)
44
- // wait for 900 milliseconds
45
- ```
47
+ #### pub vs sub
48
+ - pub and sub both have the same facilities
49
+ - `.publish`
50
+ - `.subscribe`
51
+ - `.on`
52
+ - `.next`
53
+ - `.clear`
54
+ - i seem to use `sub` more often
55
+
56
+ #### the more you know, about pubsub
57
+ - publish actually returns a promise, to wait for all async subscribers
58
+ ```ts
59
+ await onMessage.publish("hello")
60
+ ```
61
+ - subscribe returns a fn to unsubscribe
62
+ ```ts
63
+ const unsubscribe = onMessage(() => {})
64
+ unsubscribe()
65
+ ```
66
+ - `.clear()` to wipe all subscribed listeners
67
+ ```ts
68
+ onMessage.clear()
69
+ ```
70
+ - `.next(fn?)` is a better way to do .once..
71
+ - you can use it like a .once:
72
+ ```ts
73
+ onMessage.next(message => {})
74
+ ```
75
+ - but it also gives you a promise like this:
76
+ ```ts
77
+ const [message] = await onMessage.next()
78
+ ```
79
+ - of course the promise can be used like this:
80
+ ```ts
81
+ onMessage.next().then(([message]) => {})
82
+ ```
46
83
 
47
84
  ### 🍏 defer
48
85
  > defer the resolve/reject of a promise to the outside
@@ -66,62 +103,54 @@ const deferred = defer()
66
103
  await deferred.promise
67
104
  ```
68
105
 
69
- ### 🍏 `pub` and `sub`
70
- > ergonomic event emitters
106
+ ### 🍏 nap
107
+ > sleep for some milliseconds
71
108
 
72
109
  ```ts
73
- import {pub, sub} from "@e280/stz"
110
+ import {nap} from "@e280/stz"
111
+
112
+ await nap(900)
113
+ // wait for 900 milliseconds
74
114
  ```
75
115
 
76
- - make a publisher fn
77
- ```ts
78
- // create a pub fn
79
- const sendMessage = pub<[string]>()
116
+ ### 🍏 MapG
117
+ > extended js Map
80
118
 
81
- // subscribe to it
82
- sendMessage.sub(m => console.log(m))
119
+ - many say it's *"The Deluxe Mapping Experience"*
120
+ ```ts
121
+ import {MapG} from "@e280/stz"
83
122
 
84
- // publish to it
85
- sendMessage("hello")
123
+ const map = new MapG<number, string>([
124
+ [1, "hello"],
125
+ [2, "world"],
126
+ ])
86
127
  ```
87
- - make a subscriber fn *(see how it's just the reverse of pub?)*
128
+ - `map.require(key)` returns the value for key.. if missing, throw an error
88
129
  ```ts
89
- // create a sub fn
90
- const onMessage = sub<[string]>()
91
-
92
- // subscribe to it
93
- onMessage(m => console.log(m))
94
-
95
- // publish to it
96
- onMessage.pub("hello")
130
+ const value = map.require(1)
131
+ // "hello"
97
132
  ```
98
- - the pub and sub are the same, but have differing invoke signatures
99
- - i seem to use `sub` more often
100
- - both have some extra functionality
133
+ - `map.guarantee(key, makeFn)` — returns the value for `key`.. if missing, run `makeFn` to set and return the value
101
134
  ```ts
102
- // pub fns return a promise, to wait for all async subscribers
103
- await sendMessage("hello")
104
- await onMessage.pub("hello")
105
-
106
- // sub fns return an unsub fn
107
- const unsub1 = onMessage(m => console.log(m))
108
- unsub1() // unsubscribe that listener
109
-
110
- const unsub2 = sendMessage.sub(m => console.log(m))
111
- unsub2() // unsubscribe that listener
135
+ const value = map.guarantee(3, () => "rofl")
136
+ // "rofl"
137
+ ```
112
138
 
113
- // you can clear all subscribers from a pub or a sub
114
- sendMessage.clear()
115
- onMessage.clear()
139
+ ### 🍏 SetG
140
+ > extended js Set
141
+ - `new SetG<T>()`
142
+ - `set.adds(item1, item2, item3)` — add multiple items without a for-loop
143
+ - `set.deletes(item1, item2, item3)` — add multiple items without a for-loop
116
144
 
117
- // instead of a 'once' fn we simply await next()
118
- await onMessage.next()
119
- await sendMessage.next()
120
- ```
145
+ ### 🍏 WeakMapG
146
+ > extended js WeakMap
147
+ - `new WeakMapG<K, V>()`
148
+ - `weakMap.require(key)` — returns value for key.. if missing, throw an error
149
+ - `weakMap.guarantee(key, makeFn)` — returns the value for key.. if missing, run `makeFn` to set and return the value
121
150
 
122
151
  <br/>
123
152
 
124
- ## 🧰 STZ FN TOOLS
153
+ ## 🥨 stz fn tools
125
154
 
126
155
  ### 🍏 `queue(fn)`
127
156
  > execute calls in sequence (not concurrent)
@@ -211,7 +240,7 @@ stop()
211
240
 
212
241
  <br/>
213
242
 
214
- ## 🧰 STZ DATA UTILITIES
243
+ ## 🥨 stz data utilities
215
244
 
216
245
  ### 🍏 Hex
217
246
  > convert to/from hexadecimal string format
@@ -330,7 +359,7 @@ import {Bytename} from "@e280/stz"
330
359
 
331
360
  <br/>
332
361
 
333
- ## 💖 stz is made with open source love
362
+ ## 💖 stz is by e280
334
363
  reward us with github stars
335
364
  build with us at https://e280.org/ but only if you're cool
336
365
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e280/stz",
3
- "version": "0.2.0-1",
3
+ "version": "0.2.0-4",
4
4
  "description": "everyday ts fns for everything",
5
5
  "license": "MIT",
6
6
  "author": "Chase Moskal <chasemoskal@gmail.com>",
package/s/drill.ts CHANGED
@@ -11,7 +11,7 @@ export function drill<xResult>(
11
11
 
12
12
  for (const key of path) {
13
13
  current = current[key]
14
- if (is.unset(current))
14
+ if (is.sad(current))
15
15
  break
16
16
  }
17
17
 
package/s/is.ts CHANGED
@@ -1,9 +1,12 @@
1
1
 
2
2
  export const is = Object.freeze({
3
- set: <X>(x: X): x is NonNullable<X> =>
3
+
4
+ /** not undefined or null */
5
+ happy: <X>(x: X): x is NonNullable<X> =>
4
6
  x !== undefined && x !== null,
5
7
 
6
- unset: (x: any): x is (undefined | null) =>
8
+ /** undefined or null */
9
+ sad: (x: any): x is (undefined | null) =>
7
10
  x === undefined || x === null,
8
11
 
9
12
  boolean: (x: any): x is boolean =>
package/s/pubsub.ts CHANGED
@@ -61,9 +61,10 @@ export function xub<A extends any[] = []>() {
61
61
  return subscribe(fn)
62
62
  }
63
63
 
64
- async function next() {
64
+ async function next(fn?: Listener<A>) {
65
65
  const {promise, resolve} = defer<A>()
66
- const unsubscribe = sub((...a) => {
66
+ const unsubscribe = sub(async(...a) => {
67
+ if (fn) await fn(...a)
67
68
  resolve(a)
68
69
  unsubscribe()
69
70
  })
package/x/drill.js CHANGED
@@ -4,7 +4,7 @@ export function drill(object, path) {
4
4
  let current = object;
5
5
  for (const key of path) {
6
6
  current = current[key];
7
- if (is.unset(current))
7
+ if (is.sad(current))
8
8
  break;
9
9
  }
10
10
  return current;
package/x/drill.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"drill.js","sourceRoot":"","sources":["../s/drill.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,EAAE,EAAC,MAAM,SAAS,CAAA;AAE1B,qEAAqE;AACrE,MAAM,UAAU,KAAK,CACnB,MAA4B,EAC5B,IAAc;IAGf,IAAI,OAAO,GAAQ,MAAM,CAAA;IAEzB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACxB,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAA;QACtB,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC;YACpB,MAAK;IACP,CAAC;IAED,OAAO,OAAO,CAAA;AACf,CAAC"}
1
+ {"version":3,"file":"drill.js","sourceRoot":"","sources":["../s/drill.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,EAAE,EAAC,MAAM,SAAS,CAAA;AAE1B,qEAAqE;AACrE,MAAM,UAAU,KAAK,CACnB,MAA4B,EAC5B,IAAc;IAGf,IAAI,OAAO,GAAQ,MAAM,CAAA;IAEzB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACxB,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAA;QACtB,IAAI,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC;YAClB,MAAK;IACP,CAAC;IAED,OAAO,OAAO,CAAA;AACf,CAAC"}
package/x/is.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export declare const is: Readonly<{
2
- set: <X>(x: X) => x is NonNullable<X>;
3
- unset: (x: any) => x is (undefined | null);
2
+ /** not undefined or null */
3
+ happy: <X>(x: X) => x is NonNullable<X>;
4
+ /** undefined or null */
5
+ sad: (x: any) => x is (undefined | null);
4
6
  boolean: (x: any) => x is boolean;
5
7
  number: (x: any) => x is number;
6
8
  string: (x: any) => x is string;
package/x/is.js CHANGED
@@ -1,6 +1,8 @@
1
1
  export const is = Object.freeze({
2
- set: (x) => x !== undefined && x !== null,
3
- unset: (x) => x === undefined || x === null,
2
+ /** not undefined or null */
3
+ happy: (x) => x !== undefined && x !== null,
4
+ /** undefined or null */
5
+ sad: (x) => x === undefined || x === null,
4
6
  boolean: (x) => typeof x === "boolean",
5
7
  number: (x) => typeof x === "number",
6
8
  string: (x) => typeof x === "string",
package/x/is.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"is.js","sourceRoot":"","sources":["../s/is.ts"],"names":[],"mappings":"AACA,MAAM,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC;IAC/B,GAAG,EAAE,CAAI,CAAI,EAAuB,EAAE,CACrC,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI;IAE9B,KAAK,EAAE,CAAC,CAAM,EAA2B,EAAE,CAC1C,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI;IAE9B,OAAO,EAAE,CAAC,CAAM,EAAgB,EAAE,CACjC,OAAO,CAAC,KAAK,SAAS;IAEvB,MAAM,EAAE,CAAC,CAAM,EAAe,EAAE,CAC/B,OAAO,CAAC,KAAK,QAAQ;IAEtB,MAAM,EAAE,CAAC,CAAM,EAAe,EAAE,CAC/B,OAAO,CAAC,KAAK,QAAQ;IAEtB,MAAM,EAAE,CAAC,CAAM,EAAe,EAAE,CAC/B,OAAO,CAAC,KAAK,QAAQ;IAEtB,MAAM,EAAE,CAAI,CAAI,EAAgC,EAAE,CACjD,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI;IAEpC,KAAK,EAAE,CAAC,CAAc,EAAc,EAAE,CACrC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;IAEjB,EAAE,EAAE,CAAC,CAAM,EAA6B,EAAE,CACzC,OAAO,CAAC,KAAK,UAAU;IAExB,MAAM,EAAE,CAAC,CAAM,EAAe,EAAE,CAC/B,OAAO,CAAC,KAAK,QAAQ;CACtB,CAAC,CAAA"}
1
+ {"version":3,"file":"is.js","sourceRoot":"","sources":["../s/is.ts"],"names":[],"mappings":"AACA,MAAM,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC;IAE/B,4BAA4B;IAC5B,KAAK,EAAE,CAAI,CAAI,EAAuB,EAAE,CACvC,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI;IAE9B,wBAAwB;IACxB,GAAG,EAAE,CAAC,CAAM,EAA2B,EAAE,CACxC,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI;IAE9B,OAAO,EAAE,CAAC,CAAM,EAAgB,EAAE,CACjC,OAAO,CAAC,KAAK,SAAS;IAEvB,MAAM,EAAE,CAAC,CAAM,EAAe,EAAE,CAC/B,OAAO,CAAC,KAAK,QAAQ;IAEtB,MAAM,EAAE,CAAC,CAAM,EAAe,EAAE,CAC/B,OAAO,CAAC,KAAK,QAAQ;IAEtB,MAAM,EAAE,CAAC,CAAM,EAAe,EAAE,CAC/B,OAAO,CAAC,KAAK,QAAQ;IAEtB,MAAM,EAAE,CAAI,CAAI,EAAgC,EAAE,CACjD,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI;IAEpC,KAAK,EAAE,CAAC,CAAc,EAAc,EAAE,CACrC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;IAEjB,EAAE,EAAE,CAAC,CAAM,EAA6B,EAAE,CACzC,OAAO,CAAC,KAAK,UAAU;IAExB,MAAM,EAAE,CAAC,CAAM,EAAe,EAAE,CAC/B,OAAO,CAAC,KAAK,QAAQ;CACtB,CAAC,CAAA"}
package/x/pubsub.js CHANGED
@@ -15,9 +15,11 @@ export function xub() {
15
15
  function sub(fn) {
16
16
  return subscribe(fn);
17
17
  }
18
- async function next() {
18
+ async function next(fn) {
19
19
  const { promise, resolve } = defer();
20
- const unsubscribe = sub((...a) => {
20
+ const unsubscribe = sub(async (...a) => {
21
+ if (fn)
22
+ await fn(...a);
21
23
  resolve(a);
22
24
  unsubscribe();
23
25
  });
package/x/pubsub.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"pubsub.js","sourceRoot":"","sources":["../s/pubsub.ts"],"names":[],"mappings":"A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