@e280/stz 0.0.0-3 → 0.0.0-31

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.
Files changed (172) hide show
  1. package/README.md +285 -0
  2. package/package.json +14 -7
  3. package/s/clone/clone.test.ts +123 -123
  4. package/s/coalesce.ts +1 -0
  5. package/s/data/base-x.ts +170 -0
  6. package/s/data/base58.ts +15 -5
  7. package/s/data/base64.ts +15 -6
  8. package/s/data/base64url.ts +15 -6
  9. package/s/data/bytename/bytename.test.ts +49 -0
  10. package/s/data/{barname/barname.ts → bytename/bytename.ts} +38 -10
  11. package/s/data/bytename/index.ts +4 -0
  12. package/s/data/bytename/thumbprint.test.ts +53 -0
  13. package/s/data/bytename/thumbprint.ts +112 -0
  14. package/s/data/bytes.ts +12 -2
  15. package/s/data/data.test.ts +98 -0
  16. package/s/data/hex.ts +15 -5
  17. package/s/data/txt.ts +14 -4
  18. package/s/deadline.ts +5 -6
  19. package/s/debounce/debounce.test.ts +73 -73
  20. package/s/deep/deep.test.ts +8 -8
  21. package/s/deep/parts/equal.test.ts +123 -124
  22. package/s/{defer-promise.ts → defer.ts} +2 -2
  23. package/s/drill.ts +2 -4
  24. package/s/ev.ts +20 -0
  25. package/s/id-counter.ts +9 -0
  26. package/s/index.ts +16 -2
  27. package/s/is.ts +10 -4
  28. package/s/loopy.ts +12 -0
  29. package/s/map-g.ts +2 -2
  30. package/s/nap.ts +1 -0
  31. package/s/once.ts +14 -0
  32. package/s/pubsub.ts +24 -6
  33. package/s/queue/queue.test.ts +67 -0
  34. package/s/queue/queue.ts +21 -0
  35. package/s/repeat.ts +25 -0
  36. package/s/tests.test.ts +21 -0
  37. package/s/time.ts +22 -0
  38. package/x/clone/clone.test.d.ts +15 -1
  39. package/x/clone/clone.test.js +106 -124
  40. package/x/clone/clone.test.js.map +1 -1
  41. package/x/coalesce.d.ts +1 -0
  42. package/x/coalesce.js +1 -0
  43. package/x/coalesce.js.map +1 -1
  44. package/x/data/base-x.d.ts +47 -0
  45. package/x/data/base-x.js +147 -0
  46. package/x/data/base-x.js.map +1 -0
  47. package/x/data/base58.d.ts +5 -1
  48. package/x/data/base58.js +13 -5
  49. package/x/data/base58.js.map +1 -1
  50. package/x/data/base64.d.ts +7 -3
  51. package/x/data/base64.js +13 -5
  52. package/x/data/base64.js.map +1 -1
  53. package/x/data/base64url.d.ts +5 -1
  54. package/x/data/base64url.js +13 -5
  55. package/x/data/base64url.js.map +1 -1
  56. package/x/data/bytename/bytename.d.ts +19 -0
  57. package/x/data/{barname/barname.js → bytename/bytename.js} +27 -11
  58. package/x/data/bytename/bytename.js.map +1 -0
  59. package/x/data/bytename/bytename.test.d.ts +17 -0
  60. package/x/data/bytename/bytename.test.js +41 -0
  61. package/x/data/bytename/bytename.test.js.map +1 -0
  62. package/x/data/bytename/index.d.ts +2 -0
  63. package/x/data/bytename/index.js +3 -0
  64. package/x/data/bytename/index.js.map +1 -0
  65. package/x/data/bytename/thumbprint.d.ts +36 -0
  66. package/x/data/bytename/thumbprint.js +79 -0
  67. package/x/data/bytename/thumbprint.js.map +1 -0
  68. package/x/data/bytename/thumbprint.test.d.ts +24 -0
  69. package/x/data/bytename/thumbprint.test.js +45 -0
  70. package/x/data/bytename/thumbprint.test.js.map +1 -0
  71. package/x/data/bytename/utils/prefixes.js.map +1 -0
  72. package/x/data/bytename/utils/suffixes.js.map +1 -0
  73. package/x/data/bytes.d.ts +3 -2
  74. package/x/data/bytes.js +11 -2
  75. package/x/data/bytes.js.map +1 -1
  76. package/x/data/data.test.d.ts +69 -0
  77. package/x/data/data.test.js +77 -0
  78. package/x/data/data.test.js.map +1 -0
  79. package/x/data/hex.d.ts +8 -4
  80. package/x/data/hex.js +13 -5
  81. package/x/data/hex.js.map +1 -1
  82. package/x/data/txt.d.ts +6 -2
  83. package/x/data/txt.js +12 -4
  84. package/x/data/txt.js.map +1 -1
  85. package/x/deadline.d.ts +2 -2
  86. package/x/deadline.js +4 -4
  87. package/x/deadline.js.map +1 -1
  88. package/x/debounce/debounce.test.d.ts +8 -1
  89. package/x/debounce/debounce.test.js +70 -74
  90. package/x/debounce/debounce.test.js.map +1 -1
  91. package/x/deep/deep.test.d.ts +21 -1
  92. package/x/deep/deep.test.js +5 -9
  93. package/x/deep/deep.test.js.map +1 -1
  94. package/x/deep/parts/equal.test.d.ts +19 -1
  95. package/x/deep/parts/equal.test.js +107 -125
  96. package/x/deep/parts/equal.test.js.map +1 -1
  97. package/x/{defer-promise.d.ts → defer.d.ts} +2 -2
  98. package/x/{defer-promise.js → defer.js} +2 -2
  99. package/x/defer.js.map +1 -0
  100. package/x/drill.d.ts +1 -3
  101. package/x/drill.js +2 -4
  102. package/x/drill.js.map +1 -1
  103. package/x/ev.d.ts +6 -0
  104. package/x/ev.js +10 -0
  105. package/x/ev.js.map +1 -0
  106. package/x/id-counter.d.ts +5 -0
  107. package/x/id-counter.js +10 -0
  108. package/x/id-counter.js.map +1 -0
  109. package/x/index.d.ts +13 -2
  110. package/x/index.js +13 -2
  111. package/x/index.js.map +1 -1
  112. package/x/is.d.ts +6 -4
  113. package/x/is.js +6 -4
  114. package/x/is.js.map +1 -1
  115. package/x/loopy.d.ts +2 -0
  116. package/x/loopy.js +10 -0
  117. package/x/loopy.js.map +1 -0
  118. package/x/map-g.d.ts +3 -3
  119. package/x/map-g.js +1 -1
  120. package/x/map-g.js.map +1 -1
  121. package/x/nap.d.ts +1 -0
  122. package/x/nap.js +1 -0
  123. package/x/nap.js.map +1 -1
  124. package/x/once.d.ts +1 -0
  125. package/x/once.js +12 -0
  126. package/x/once.js.map +1 -0
  127. package/x/pubsub.d.ts +12 -1
  128. package/x/pubsub.js +7 -5
  129. package/x/pubsub.js.map +1 -1
  130. package/x/queue/queue.d.ts +4 -0
  131. package/x/queue/queue.js +14 -0
  132. package/x/queue/queue.js.map +1 -0
  133. package/x/queue/queue.test.d.ts +8 -0
  134. package/x/queue/queue.test.js +57 -0
  135. package/x/queue/queue.test.js.map +1 -0
  136. package/x/repeat.d.ts +5 -0
  137. package/x/repeat.js +23 -0
  138. package/x/repeat.js.map +1 -0
  139. package/x/tests.test.js +18 -0
  140. package/x/tests.test.js.map +1 -0
  141. package/x/time.d.ts +18 -0
  142. package/x/time.js +19 -0
  143. package/x/time.js.map +1 -0
  144. package/readme.md +0 -85
  145. package/s/data/anka.ts +0 -66
  146. package/s/data/barname/badge.ts +0 -78
  147. package/s/data/barname/demo.ts +0 -49
  148. package/s/data/barname/index.ts +0 -4
  149. package/x/data/anka.d.ts +0 -5
  150. package/x/data/anka.js +0 -53
  151. package/x/data/anka.js.map +0 -1
  152. package/x/data/barname/badge.d.ts +0 -25
  153. package/x/data/barname/badge.js +0 -64
  154. package/x/data/barname/badge.js.map +0 -1
  155. package/x/data/barname/barname.d.ts +0 -7
  156. package/x/data/barname/barname.js.map +0 -1
  157. package/x/data/barname/demo.js +0 -45
  158. package/x/data/barname/demo.js.map +0 -1
  159. package/x/data/barname/index.d.ts +0 -2
  160. package/x/data/barname/index.js +0 -3
  161. package/x/data/barname/index.js.map +0 -1
  162. package/x/data/barname/utils/prefixes.js.map +0 -1
  163. package/x/data/barname/utils/suffixes.js.map +0 -1
  164. package/x/defer-promise.js.map +0 -1
  165. /package/{license → LICENSE} +0 -0
  166. /package/s/data/{barname → bytename}/utils/prefixes.ts +0 -0
  167. /package/s/data/{barname → bytename}/utils/suffixes.ts +0 -0
  168. /package/x/data/{barname → bytename}/utils/prefixes.d.ts +0 -0
  169. /package/x/data/{barname → bytename}/utils/prefixes.js +0 -0
  170. /package/x/data/{barname → bytename}/utils/suffixes.d.ts +0 -0
  171. /package/x/data/{barname → bytename}/utils/suffixes.js +0 -0
  172. /package/x/{data/barname/demo.d.ts → tests.test.d.ts} +0 -0
@@ -0,0 +1,98 @@
1
+
2
+ import {Science, test, expect} from "@e280/science"
3
+ import {ob} from "../ob.js"
4
+ import {Txt} from "./txt.js"
5
+ import {Hex} from "./hex.js"
6
+ import {Bytes} from "./bytes.js"
7
+ import {BaseX} from "./base-x.js"
8
+ import {Base58} from "./base58.js"
9
+ import {Base64} from "./base64.js"
10
+ import {Base64url} from "./base64url.js"
11
+
12
+ const sampleBytes = Hex.toBytes("9960cd633a46acfe8307d8a400e842da0d930a75fb8188e0f5da264e4b6b4e5b")
13
+
14
+ type ByteUtil = {
15
+ toBytes: (string: string) => Uint8Array
16
+ fromBytes: (bytes: Uint8Array) => string
17
+ }
18
+
19
+ type NumberUtil = {
20
+ toInteger: (s: string) => number
21
+ fromInteger: (n: number) => string
22
+ }
23
+
24
+ function testBytes(util: ByteUtil) {
25
+ return async() => {
26
+ const string = util.fromBytes(sampleBytes)
27
+ const recreated = util.toBytes(string)
28
+ expect(Bytes.eq(sampleBytes, recreated)).ok()
29
+ }
30
+ }
31
+
32
+ function testNumbers(util: NumberUtil) {
33
+ return {
34
+ positive: async() => {
35
+ const original = 123456789
36
+ const string = util.fromInteger(original)
37
+ const recreated = util.toInteger(string)
38
+ expect(original).is(recreated)
39
+ expect(util.toInteger("")).is(0)
40
+ },
41
+ negative: async() => {
42
+ const original = -123456789
43
+ const string = util.fromInteger(original)
44
+ const recreated = util.toInteger(string)
45
+ expect(original).is(recreated)
46
+ expect(util.toInteger("")).is(0)
47
+ },
48
+ }
49
+ }
50
+
51
+ function testBoth(util: ByteUtil & NumberUtil) {
52
+ return {
53
+ bytes: testBytes(util),
54
+ numbers: testNumbers(util),
55
+ }
56
+ }
57
+
58
+ function testCompat(alpha: ByteUtil, bravo: ByteUtil) {
59
+ return async() => {
60
+ expect(alpha.fromBytes(sampleBytes))
61
+ .is(bravo.fromBytes(sampleBytes))
62
+ }
63
+ }
64
+
65
+ export default Science.suite({
66
+ "Base58": testBytes(Base58),
67
+ "Base64": testBytes(Base64),
68
+ "Base64url": testBytes(Base64url),
69
+ "Hex": testBytes(Hex),
70
+
71
+ "Txt": test(async() => {
72
+ const original = `build or die. 💻>☠️ 命火工`
73
+ const bytes = Txt.toBytes(original)
74
+ const recreated = Txt.fromBytes(bytes)
75
+ expect(original).is(recreated)
76
+ }),
77
+
78
+ "BaseX": Science.suite({
79
+ lexicons: Science.suite(
80
+ ob(BaseX.lexicons).map(lex => testBoth(new BaseX(lex)))
81
+ ),
82
+ compat: Science.suite({
83
+ "Hex": testCompat(Hex, new BaseX(BaseX.lexicons.hex)),
84
+ "Base64": testCompat(Base64, new BaseX(BaseX.lexicons.base64)),
85
+ "Base64url": testCompat(Base64url, new BaseX(BaseX.lexicons.base64url)),
86
+ }),
87
+ "base64 has padding": test(async() => {
88
+ const s = new BaseX(BaseX.lexicons.base64).fromBytes(sampleBytes)
89
+ expect(s.endsWith("=")).ok()
90
+ }),
91
+ "antagonistic base64 positive integer": test(async() => {
92
+ const base64url = new BaseX(BaseX.lexicons.base64url)
93
+ expect(base64url.toInteger(base64url.fromInteger(62))).is(62)
94
+ expect(base64url.toInteger(base64url.fromInteger(4030))).is(4030)
95
+ }),
96
+ }),
97
+ })
98
+
package/s/data/hex.ts CHANGED
@@ -11,14 +11,14 @@
11
11
 
12
12
  import {Bytes} from "./bytes.js"
13
13
 
14
- export const Hex = {
15
- string(bytes: Uint8Array) {
14
+ export const Hex = Object.freeze({
15
+ fromBytes(bytes: Uint8Array) {
16
16
  return [...bytes]
17
17
  .map(byte => byte.toString(16).padStart(2, "0"))
18
18
  .join("")
19
19
  },
20
20
 
21
- bytes(string: string) {
21
+ toBytes(string: string) {
22
22
  if (string.length % 2 !== 0)
23
23
  throw new Error("must have even number of hex characters")
24
24
  const bytes = new Uint8Array(string.length / 2)
@@ -29,7 +29,17 @@ export const Hex = {
29
29
 
30
30
  /** generate a random hex string. byteCount defaults to 32. */
31
31
  random(byteCount = 32) {
32
- return this.string(Bytes.random(byteCount))
32
+ return this.fromBytes(Bytes.random(byteCount))
33
+ },
34
+
35
+ /** @deprecated renamed to `fromBytes` */
36
+ string(bytes: Uint8Array) {
37
+ return Hex.fromBytes(bytes)
38
+ },
39
+
40
+ /** @deprecated renamed to `toBytes` */
41
+ bytes(string: string) {
42
+ return Hex.toBytes(string)
33
43
  },
34
- }
44
+ })
35
45
 
package/s/data/txt.ts CHANGED
@@ -1,11 +1,21 @@
1
1
 
2
- export const Txt = {
3
- string(bytes: Uint8Array) {
2
+ export const Txt = Object.freeze({
3
+ fromBytes(bytes: Uint8Array) {
4
4
  return new TextDecoder().decode(bytes)
5
5
  },
6
6
 
7
- bytes(string: string) {
7
+ toBytes(string: string) {
8
8
  return new TextEncoder().encode(string)
9
9
  },
10
- }
10
+
11
+ /** @deprecated renamed to `fromBytes` */
12
+ string(bytes: Uint8Array) {
13
+ return Txt.fromBytes(bytes)
14
+ },
15
+
16
+ /** @deprecated renamed to `toBytes` */
17
+ bytes(string: string) {
18
+ return Txt.toBytes(string)
19
+ },
20
+ })
11
21
 
package/s/deadline.ts CHANGED
@@ -1,26 +1,25 @@
1
1
 
2
2
  export class DeadlineError extends Error {
3
3
  name = this.constructor.name
4
- constructor(public milliseconds: number, message: string) {
5
- super(`${message}, timed out in ${(milliseconds / 1000).toFixed(1)} seconds`)
4
+ constructor(public milliseconds: number) {
5
+ super(`deadline exceeded (${(milliseconds / 1000).toFixed(1)} seconds)`)
6
6
  }
7
7
  }
8
8
 
9
9
  /** set a deadline for a fn to do something, will reject with a `DeadlineError` if it takes too long */
10
- export function deadline<R>(milliseconds: number, message: string, fn: () => Promise<R>) {
10
+ export function deadline<R>(milliseconds: number, fn: () => Promise<R>) {
11
11
  if (milliseconds <= 0 || milliseconds === Infinity)
12
12
  return fn()
13
13
 
14
14
  return new Promise<R>((resolve, reject) => {
15
-
16
15
  const id = setTimeout(
17
- () => reject(new DeadlineError(milliseconds, message)),
16
+ () => reject(new DeadlineError(milliseconds)),
18
17
  milliseconds,
19
18
  )
20
-
21
19
  fn()
22
20
  .then(resolve)
23
21
  .catch(reject)
24
22
  .finally(() => clearTimeout(id))
25
23
  })
26
24
  }
25
+
@@ -1,73 +1,73 @@
1
- //
2
- // import {nap} from "../nap.js"
3
- // import {Suite, expect} from "cynic"
4
- // import {debounce as debounce} from "./debounce.js"
5
- //
6
- // export default <Suite>{
7
- // async "reduces many calls into one"() {
8
- // let count = 0
9
- // const increment = debounce(100, () => {
10
- // count += 1
11
- // })
12
- // increment()
13
- // increment()
14
- // increment()
15
- // await nap(200)
16
- // expect(count).equals(1)
17
- // },
18
- // async "timer gets reset on each call"() {
19
- // let count = 0
20
- // const increment = debounce(100, () => {
21
- // count += 1
22
- // })
23
- // increment()
24
- // await nap(50)
25
- // increment()
26
- // await nap(50)
27
- // increment()
28
- // await nap(50)
29
- // increment()
30
- // await nap(50)
31
- // increment()
32
- // expect(count).equals(0)
33
- // await nap(101)
34
- // expect(count).equals(1)
35
- // },
36
- // async "provides a promise that resolves when done"() {
37
- // let count = 0
38
- // const increment = debounce(100, () => {
39
- // count += 1
40
- // })
41
- // let done = false
42
- // increment()
43
- // .then(() => done = true)
44
- // await nap(50)
45
- // increment()
46
- // await nap(50)
47
- // increment()
48
- // await nap(50)
49
- // increment()
50
- // await nap(50)
51
- // increment()
52
- // expect(done).equals(false)
53
- // await nap(201)
54
- // expect(done).equals(true)
55
- // },
56
- // async "can wait multiple rounds"() {
57
- // let count = 0
58
- // const increment = debounce(100, () => {
59
- // count += 1
60
- // })
61
- // await increment()
62
- // expect(count).equals(1)
63
- // await increment()
64
- // expect(count).equals(2)
65
- // },
66
- // async "errors are passed into promise chain"() {
67
- // const err = debounce(1, () => {
68
- // throw new Error()
69
- // })
70
- // await expect(async() => err()).throws()
71
- // },
72
- // }
73
- //
1
+
2
+ import {nap} from "../nap.js"
3
+ import {Science, expect} from "@e280/science"
4
+ import {debounce as debounce} from "./debounce.js"
5
+
6
+ export default Science.suite({
7
+ async "reduces many calls into one"() {
8
+ let count = 0
9
+ const increment = debounce(100, () => {
10
+ count += 1
11
+ })
12
+ increment()
13
+ increment()
14
+ increment()
15
+ await nap(200)
16
+ expect(count).is(1)
17
+ },
18
+ async "timer gets reset on each call"() {
19
+ let count = 0
20
+ const increment = debounce(100, () => {
21
+ count += 1
22
+ })
23
+ increment()
24
+ await nap(50)
25
+ increment()
26
+ await nap(50)
27
+ increment()
28
+ await nap(50)
29
+ increment()
30
+ await nap(50)
31
+ increment()
32
+ expect(count).is(0)
33
+ await nap(101)
34
+ expect(count).is(1)
35
+ },
36
+ async "provides a promise that resolves when done"() {
37
+ let count = 0
38
+ const increment = debounce(100, () => {
39
+ count += 1
40
+ })
41
+ let done = false
42
+ increment()
43
+ .then(() => done = true)
44
+ await nap(50)
45
+ increment()
46
+ await nap(50)
47
+ increment()
48
+ await nap(50)
49
+ increment()
50
+ await nap(50)
51
+ increment()
52
+ expect(done).is(false)
53
+ await nap(201)
54
+ expect(done).is(true)
55
+ },
56
+ async "can wait multiple rounds"() {
57
+ let count = 0
58
+ const increment = debounce(100, () => {
59
+ count += 1
60
+ })
61
+ await increment()
62
+ expect(count).is(1)
63
+ await increment()
64
+ expect(count).is(2)
65
+ },
66
+ async "errors are passed into promise chain"() {
67
+ const err = debounce(1, () => {
68
+ throw new Error()
69
+ })
70
+ await expect(async() => err()).throwsAsync()
71
+ },
72
+ })
73
+
@@ -1,8 +1,8 @@
1
- //
2
- // import {Suite} from "cynic"
3
- // import deepEqual from "./parts/equal.test.js"
4
- //
5
- // export default <Suite>{
6
- // deepEqual,
7
- // }
8
- //
1
+
2
+ import {Science} from "@e280/science"
3
+ import deepEqual from "./parts/equal.test.js"
4
+
5
+ export default Science.suite({
6
+ deepEqual,
7
+ })
8
+
@@ -1,124 +1,123 @@
1
- //
2
- // import {Suite, expect} from "cynic"
3
- // import {deepEqual} from "./equal.js"
4
- //
5
- // export default <Suite>{
6
- //
7
- // async "compare primitive types"() {
8
- // expect(deepEqual(123, 123)).equals(true)
9
- // expect(deepEqual("abc", "abc")).equals(true)
10
- // expect(deepEqual(true, true)).equals(true)
11
- // expect(deepEqual(123, "123")).equals(false)
12
- // expect(deepEqual(true, 1)).equals(false)
13
- // },
14
- //
15
- // async "compare arrays"() {
16
- // const array1 = [1, 2, 3]
17
- // const array2 = [1, 2, 3]
18
- // const array3 = [1, 2, 4]
19
- // expect(deepEqual(array1, array2)).equals(true)
20
- // expect(deepEqual(array1, array3)).equals(false)
21
- // },
22
- //
23
- // async "compare plain objects"() {
24
- // const obj1 = { foo: "bar", baz: 123 }
25
- // const obj2 = { foo: "bar", baz: 123 }
26
- // const obj3 = { foo: "bar", baz: 124 }
27
- // expect(deepEqual(obj1, obj2)).equals(true)
28
- // expect(deepEqual(obj1, obj3)).equals(false)
29
- // },
30
- //
31
- // async "compare nested objects"() {
32
- // const obj1 = { foo: "bar", inner: { baz: 123, deeper: { qux: true }}}
33
- // const obj2 = { foo: "bar", inner: { baz: 123, deeper: { qux: true }}}
34
- // const obj3 = { foo: "bar", inner: { baz: 124, deeper: { qux: true }}}
35
- // expect(deepEqual(obj1, obj2)).equals(true)
36
- // expect(deepEqual(obj1, obj3)).equals(false)
37
- // },
38
- //
39
- // async "compare array of objects"() {
40
- // const arr1 = [{ foo: "bar" }, { baz: 123 }, { qux: true }]
41
- // const arr2 = [{ foo: "bar" }, { baz: 123 }, { qux: true }]
42
- // const arr3 = [{ foo: "bar" }, { baz: 123 }, { qux: false }]
43
- // expect(deepEqual(arr1, arr2)).equals(true)
44
- // expect(deepEqual(arr1, arr3)).equals(false)
45
- // },
46
- //
47
- // async "compare null and undefined"() {
48
- // expect(deepEqual(null, null)).equals(true)
49
- // expect(deepEqual(undefined, undefined)).equals(true)
50
- // expect(deepEqual(null, undefined)).equals(false)
51
- // },
52
- //
53
- // async "compare with nested arrays"() {
54
- // const obj1 = { foo: "bar", array: [1, 2, [3, 4]] }
55
- // const obj2 = { foo: "bar", array: [1, 2, [3, 4]] }
56
- // const obj3 = { foo: "bar", array: [1, 2, [3, 5]] }
57
- // expect(deepEqual(obj1, obj2)).equals(true)
58
- // expect(deepEqual(obj1, obj3)).equals(false)
59
- // },
60
- //
61
- // async "circularity is forbidden"() {
62
- // const obj1 = {alpha: 1} as any
63
- // const obj2 = {alpha: 1} as any
64
- // obj1.bravo = obj1
65
- // obj2.bravo = obj2
66
- // expect(() => deepEqual(obj1, obj2)).throws()
67
- // },
68
- //
69
- // async "duplicate references are allowed"() {
70
- // const dupe = {a: 123}
71
- // const obj1 = {alpha: 1, dupe}
72
- // const obj2 = {alpha: 1, dupe}
73
- // const obj3 = {alpha: 1, dupe: {a: 0}}
74
- // expect(deepEqual(obj1, obj2)).equals(true)
75
- // expect(deepEqual(obj1, obj3)).equals(false)
76
- // },
77
- //
78
- // "maps and sets": {
79
- // async "compare maps"() {
80
- // const map1 = new Map([['key1', 'value1'], ['key2', 'value2']])
81
- // const map2 = new Map([['key1', 'value1'], ['key2', 'value2']])
82
- // const map3 = new Map([['key1', 'value1'], ['key2', 'value3']])
83
- // expect(deepEqual(map1, map2)).equals(true)
84
- // expect(deepEqual(map1, map3)).equals(false)
85
- // },
86
- //
87
- // async "compare sets"() {
88
- // const set1 = new Set([1, 2, 3])
89
- // const set2 = new Set([1, 2, 3])
90
- // const set3 = new Set([1, 2, 4])
91
- // expect(deepEqual(set1, set2)).equals(true)
92
- // expect(deepEqual(set1, set3)).equals(false)
93
- // },
94
- //
95
- // async "compare nested maps and sets"() {
96
- // const obj1 = { map: new Map([['key', new Set([1, 2, 3])]]) }
97
- // const obj2 = { map: new Map([['key', new Set([1, 2, 3])]]) }
98
- // const obj3 = { map: new Map([['key', new Set([1, 2, 4])]]) }
99
- // expect(deepEqual(obj1, obj2)).equals(true)
100
- // expect(deepEqual(obj1, obj3)).equals(false)
101
- // },
102
- //
103
- // async "compare complex structures"() {
104
- // const map1 = new Map([['set', new Set([{ a: 1 }, { b: 2 }])]])
105
- // const map2 = new Map([['set', new Set([{ a: 1 }, { b: 2 }])]])
106
- // const map3 = new Map([['set', new Set([{ a: 1 }, { b: 3 }])]])
107
- // expect(deepEqual(map1, map2)).equals(true)
108
- // expect(deepEqual(map1, map3)).equals(false)
109
- // },
110
- //
111
- // async "compare edge cases"() {
112
- // const emptyMap1 = new Map()
113
- // const emptyMap2 = new Map()
114
- // const emptySet1 = new Set()
115
- // const emptySet2 = new Set()
116
- // const mixedMap1 = new Map([['b', 2], ['a', 1]])
117
- // const mixedMap2 = new Map([['a', 1], ['b', 2]])
118
- // expect(deepEqual(emptyMap1, emptyMap2)).equals(true)
119
- // expect(deepEqual(emptySet1, emptySet2)).equals(true)
120
- // expect(deepEqual(mixedMap1, mixedMap2)).equals(true)
121
- // },
122
- // },
123
- // }
124
- //
1
+
2
+ import {deepEqual} from "./equal.js"
3
+ import {Science, expect} from "@e280/science"
4
+
5
+ export default Science.suite({
6
+ async "compare primitive types"() {
7
+ expect(deepEqual(123, 123)).is(true)
8
+ expect(deepEqual("abc", "abc")).is(true)
9
+ expect(deepEqual(true, true)).is(true)
10
+ expect(deepEqual(123, "123")).is(false)
11
+ expect(deepEqual(true, 1)).is(false)
12
+ },
13
+
14
+ async "compare arrays"() {
15
+ const array1 = [1, 2, 3]
16
+ const array2 = [1, 2, 3]
17
+ const array3 = [1, 2, 4]
18
+ expect(deepEqual(array1, array2)).is(true)
19
+ expect(deepEqual(array1, array3)).is(false)
20
+ },
21
+
22
+ async "compare plain objects"() {
23
+ const obj1 = { foo: "bar", baz: 123 }
24
+ const obj2 = { foo: "bar", baz: 123 }
25
+ const obj3 = { foo: "bar", baz: 124 }
26
+ expect(deepEqual(obj1, obj2)).is(true)
27
+ expect(deepEqual(obj1, obj3)).is(false)
28
+ },
29
+
30
+ async "compare nested objects"() {
31
+ const obj1 = { foo: "bar", inner: { baz: 123, deeper: { qux: true }}}
32
+ const obj2 = { foo: "bar", inner: { baz: 123, deeper: { qux: true }}}
33
+ const obj3 = { foo: "bar", inner: { baz: 124, deeper: { qux: true }}}
34
+ expect(deepEqual(obj1, obj2)).is(true)
35
+ expect(deepEqual(obj1, obj3)).is(false)
36
+ },
37
+
38
+ async "compare array of objects"() {
39
+ const arr1 = [{ foo: "bar" }, { baz: 123 }, { qux: true }]
40
+ const arr2 = [{ foo: "bar" }, { baz: 123 }, { qux: true }]
41
+ const arr3 = [{ foo: "bar" }, { baz: 123 }, { qux: false }]
42
+ expect(deepEqual(arr1, arr2)).is(true)
43
+ expect(deepEqual(arr1, arr3)).is(false)
44
+ },
45
+
46
+ async "compare null and undefined"() {
47
+ expect(deepEqual(null, null)).is(true)
48
+ expect(deepEqual(undefined, undefined)).is(true)
49
+ expect(deepEqual(null, undefined)).is(false)
50
+ },
51
+
52
+ async "compare with nested arrays"() {
53
+ const obj1 = { foo: "bar", array: [1, 2, [3, 4]] }
54
+ const obj2 = { foo: "bar", array: [1, 2, [3, 4]] }
55
+ const obj3 = { foo: "bar", array: [1, 2, [3, 5]] }
56
+ expect(deepEqual(obj1, obj2)).is(true)
57
+ expect(deepEqual(obj1, obj3)).is(false)
58
+ },
59
+
60
+ async "circularity is forbidden"() {
61
+ const obj1 = {alpha: 1} as any
62
+ const obj2 = {alpha: 1} as any
63
+ obj1.bravo = obj1
64
+ obj2.bravo = obj2
65
+ expect(() => deepEqual(obj1, obj2)).throws()
66
+ },
67
+
68
+ async "duplicate references are allowed"() {
69
+ const dupe = {a: 123}
70
+ const obj1 = {alpha: 1, dupe}
71
+ const obj2 = {alpha: 1, dupe}
72
+ const obj3 = {alpha: 1, dupe: {a: 0}}
73
+ expect(deepEqual(obj1, obj2)).is(true)
74
+ expect(deepEqual(obj1, obj3)).is(false)
75
+ },
76
+
77
+ "maps and sets": {
78
+ async "compare maps"() {
79
+ const map1 = new Map([['key1', 'value1'], ['key2', 'value2']])
80
+ const map2 = new Map([['key1', 'value1'], ['key2', 'value2']])
81
+ const map3 = new Map([['key1', 'value1'], ['key2', 'value3']])
82
+ expect(deepEqual(map1, map2)).is(true)
83
+ expect(deepEqual(map1, map3)).is(false)
84
+ },
85
+
86
+ async "compare sets"() {
87
+ const set1 = new Set([1, 2, 3])
88
+ const set2 = new Set([1, 2, 3])
89
+ const set3 = new Set([1, 2, 4])
90
+ expect(deepEqual(set1, set2)).is(true)
91
+ expect(deepEqual(set1, set3)).is(false)
92
+ },
93
+
94
+ async "compare nested maps and sets"() {
95
+ const obj1 = { map: new Map([['key', new Set([1, 2, 3])]]) }
96
+ const obj2 = { map: new Map([['key', new Set([1, 2, 3])]]) }
97
+ const obj3 = { map: new Map([['key', new Set([1, 2, 4])]]) }
98
+ expect(deepEqual(obj1, obj2)).is(true)
99
+ expect(deepEqual(obj1, obj3)).is(false)
100
+ },
101
+
102
+ async "compare complex structures"() {
103
+ const map1 = new Map([['set', new Set([{ a: 1 }, { b: 2 }])]])
104
+ const map2 = new Map([['set', new Set([{ a: 1 }, { b: 2 }])]])
105
+ const map3 = new Map([['set', new Set([{ a: 1 }, { b: 3 }])]])
106
+ expect(deepEqual(map1, map2)).is(true)
107
+ expect(deepEqual(map1, map3)).is(false)
108
+ },
109
+
110
+ async "compare edge cases"() {
111
+ const emptyMap1 = new Map()
112
+ const emptyMap2 = new Map()
113
+ const emptySet1 = new Set()
114
+ const emptySet2 = new Set()
115
+ const mixedMap1 = new Map([['b', 2], ['a', 1]])
116
+ const mixedMap2 = new Map([['a', 1], ['b', 2]])
117
+ expect(deepEqual(emptyMap1, emptyMap2)).is(true)
118
+ expect(deepEqual(emptySet1, emptySet2)).is(true)
119
+ expect(deepEqual(mixedMap1, mixedMap2)).is(true)
120
+ },
121
+ },
122
+ })
123
+
@@ -1,6 +1,6 @@
1
1
 
2
2
  /** a promise which can be resolved from the outside */
3
- export type DeferredPromise<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 DeferredPromise<R> = {
10
10
  }
11
11
 
12
12
  /** returns a deferred promise with exposed resolve and reject fns */
13
- export function deferPromise<R>(): DeferredPromise<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/drill.ts CHANGED
@@ -1,9 +1,7 @@
1
1
 
2
2
  import {is} from "./is.js"
3
3
 
4
- /**
5
- * return a value within an object tree, found at the given path.
6
- */
4
+ /** return a value within an object tree, found at the given path. */
7
5
  export function drill<xResult>(
8
6
  object: {[key: string]: any},
9
7
  path: string[],
@@ -13,7 +11,7 @@ export function drill<xResult>(
13
11
 
14
12
  for (const key of path) {
15
13
  current = current[key]
16
- if (is.unavailable(current))
14
+ if (is.unset(current))
17
15
  break
18
16
  }
19
17