@tempots/std 0.9.0

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 (132) hide show
  1. package/dist/arrays.d.ts +49 -0
  2. package/dist/arrays.js +249 -0
  3. package/dist/async-result.d.ts +37 -0
  4. package/dist/async-result.js +75 -0
  5. package/dist/bigint.d.ts +18 -0
  6. package/dist/bigint.js +110 -0
  7. package/dist/booleans.d.ts +23 -0
  8. package/dist/booleans.js +68 -0
  9. package/dist/colors/cmyk.d.ts +21 -0
  10. package/dist/colors/cmyk.js +54 -0
  11. package/dist/colors/convert.d.ts +283 -0
  12. package/dist/colors/convert.js +742 -0
  13. package/dist/colors/hsl.d.ts +24 -0
  14. package/dist/colors/hsl.js +56 -0
  15. package/dist/colors/hsla.d.ts +18 -0
  16. package/dist/colors/hsla.js +35 -0
  17. package/dist/colors/hsluv.d.ts +24 -0
  18. package/dist/colors/hsluv.js +56 -0
  19. package/dist/colors/hsv.d.ts +24 -0
  20. package/dist/colors/hsv.js +54 -0
  21. package/dist/colors/lab.d.ts +24 -0
  22. package/dist/colors/lab.js +54 -0
  23. package/dist/colors/lch.d.ts +19 -0
  24. package/dist/colors/lch.js +44 -0
  25. package/dist/colors/luv.d.ts +19 -0
  26. package/dist/colors/luv.js +45 -0
  27. package/dist/colors/rgb.d.ts +13 -0
  28. package/dist/colors/rgb.js +47 -0
  29. package/dist/colors/rgba.d.ts +12 -0
  30. package/dist/colors/rgba.js +44 -0
  31. package/dist/colors/srgb.d.ts +24 -0
  32. package/dist/colors/srgb.js +51 -0
  33. package/dist/colors/xyz.d.ts +19 -0
  34. package/dist/colors/xyz.js +41 -0
  35. package/dist/edit.d.ts +20 -0
  36. package/dist/edit.js +29 -0
  37. package/dist/equals.d.ts +3 -0
  38. package/dist/equals.js +122 -0
  39. package/dist/functions.d.ts +20 -0
  40. package/dist/functions.js +38 -0
  41. package/dist/json.d.ts +14 -0
  42. package/dist/json.js +33 -0
  43. package/dist/match.d.ts +16 -0
  44. package/dist/match.js +45 -0
  45. package/dist/maybe.d.ts +9 -0
  46. package/dist/maybe.js +25 -0
  47. package/dist/memoize.d.ts +1 -0
  48. package/dist/memoize.js +9 -0
  49. package/dist/newtype.d.ts +28 -0
  50. package/dist/newtype.js +29 -0
  51. package/dist/numbers.d.ts +104 -0
  52. package/dist/numbers.js +183 -0
  53. package/dist/objects.d.ts +9 -0
  54. package/dist/objects.js +33 -0
  55. package/dist/ord.d.ts +19 -0
  56. package/dist/ord.js +73 -0
  57. package/dist/reg-exps.d.ts +10 -0
  58. package/dist/reg-exps.js +43 -0
  59. package/dist/result.d.ts +31 -0
  60. package/dist/result.js +95 -0
  61. package/dist/strings.d.ts +314 -0
  62. package/dist/strings.js +685 -0
  63. package/dist/types/assert.d.ts +12 -0
  64. package/dist/types/assert.js +13 -0
  65. package/dist/types/differentiate.d.ts +13 -0
  66. package/dist/types/differentiate.js +14 -0
  67. package/dist/types/functions.d.ts +22 -0
  68. package/dist/types/functions.js +13 -0
  69. package/dist/types/generic.d.ts +9 -0
  70. package/dist/types/generic.js +13 -0
  71. package/dist/types/objects.d.ts +50 -0
  72. package/dist/types/objects.js +13 -0
  73. package/dist/types/tuples.d.ts +44 -0
  74. package/dist/types/tuples.js +24 -0
  75. package/dist/types/utility.d.ts +2 -0
  76. package/dist/types/utility.js +1 -0
  77. package/dist/uuid.d.ts +13 -0
  78. package/dist/uuid.js +56 -0
  79. package/dist/validation.d.ts +23 -0
  80. package/dist/validation.js +44 -0
  81. package/package.json +36 -0
  82. package/src/arrays.ts +296 -0
  83. package/src/async-result.ts +103 -0
  84. package/src/bigint.ts +111 -0
  85. package/src/booleans.ts +73 -0
  86. package/src/colors/cmyk.ts +84 -0
  87. package/src/colors/convert.ts +1093 -0
  88. package/src/colors/hsl.ts +73 -0
  89. package/src/colors/hsla.ts +45 -0
  90. package/src/colors/hsluv.ts +73 -0
  91. package/src/colors/hsv.ts +75 -0
  92. package/src/colors/lab.ts +69 -0
  93. package/src/colors/lch.ts +53 -0
  94. package/src/colors/luv.ts +56 -0
  95. package/src/colors/rgb.ts +55 -0
  96. package/src/colors/rgba.ts +53 -0
  97. package/src/colors/srgb.ts +72 -0
  98. package/src/colors/xyz.ts +52 -0
  99. package/src/edit.ts +29 -0
  100. package/src/equals.ts +116 -0
  101. package/src/functions.ts +108 -0
  102. package/src/json.ts +52 -0
  103. package/src/match.ts +88 -0
  104. package/src/maybe.ts +32 -0
  105. package/src/memoize.ts +9 -0
  106. package/src/newtype.ts +59 -0
  107. package/src/numbers.ts +222 -0
  108. package/src/objects.ts +47 -0
  109. package/src/ord.ts +79 -0
  110. package/src/reg-exps.ts +48 -0
  111. package/src/result.ts +140 -0
  112. package/src/strings.ts +768 -0
  113. package/src/types/assert.ts +96 -0
  114. package/src/types/differentiate.ts +89 -0
  115. package/src/types/functions.ts +114 -0
  116. package/src/types/generic.ts +42 -0
  117. package/src/types/objects.ts +212 -0
  118. package/src/types/tuples.ts +244 -0
  119. package/src/types/utility.ts +3 -0
  120. package/src/uuid.ts +61 -0
  121. package/src/validation.ts +69 -0
  122. package/test/arrays.spec.ts +410 -0
  123. package/test/colors.spec.ts +406 -0
  124. package/test/commmon.ts +9 -0
  125. package/test/equals.spec.ts +165 -0
  126. package/test/functions.spec.ts +9 -0
  127. package/test/index.d.ts +20 -0
  128. package/test/objects.spec.ts +22 -0
  129. package/test/reg-exps.spec.ts +33 -0
  130. package/test/strings.spec.ts +333 -0
  131. package/test/uuid.spec.ts +35 -0
  132. package/tsconfig.json +19 -0
@@ -0,0 +1,410 @@
1
+ /*
2
+ Copyright 2019 Google LLC
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+ https://www.apache.org/licenses/LICENSE-2.0
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
12
+ */
13
+
14
+ import { describe, expect, test } from 'vitest'
15
+ import { applyOperations, type DiffOperations, diffOperations, map, head, tail, numbersRange, fill, makeCompare } from '../src/arrays'
16
+ import { compare as compareString } from '../src/strings'
17
+
18
+ describe('arrays:map', () => {
19
+ test('should work with empty arrays', () => {
20
+ expect(map([], a => a)).toEqual([])
21
+ })
22
+
23
+ test('should work with any array', () => {
24
+ expect(map([1, 2, 3], a => a + 1)).toEqual([2, 3, 4])
25
+ })
26
+ })
27
+
28
+ describe('arrays:head', () => {
29
+ test('should return nothing if the array is empy', () => {
30
+ expect(head([])).not.toBeDefined()
31
+ })
32
+
33
+ test('should return the first element', () => {
34
+ expect(head([1])).toEqual(1)
35
+ expect(head([1, 2])).toEqual(1)
36
+ expect(head([1, 2, 3])).toEqual(1)
37
+ })
38
+ })
39
+
40
+ describe('arrays:tail', () => {
41
+ test('should return nothing if the array is empy', () => {
42
+ expect(tail([])).toEqual([])
43
+ })
44
+
45
+ test('should return all the elements except for the first', () => {
46
+ expect(tail([1])).toEqual([])
47
+ expect(tail([1, 2])).toEqual([2])
48
+ expect(tail([1, 2, 3])).toEqual([2, 3])
49
+ })
50
+ })
51
+
52
+ describe('arrays', () => {
53
+ test('numberRange', () => {
54
+ expect(numbersRange(4)).toEqual([0, 1, 2, 3])
55
+ expect(numbersRange(4, 1)).toEqual([1, 2, 3, 4])
56
+ })
57
+
58
+ test('fill', () => {
59
+ expect(fill(4, 'x')).toEqual(['x', 'x', 'x', 'x'])
60
+ })
61
+ })
62
+
63
+ describe('arrays:makeCompare', () => {
64
+ test('should compare arrays of the same length', () => {
65
+ const tests = [
66
+ { a: ['a'], b: ['b'], r: -1 },
67
+ { a: ['b'], b: ['a'], r: 1 },
68
+ { a: ['a'], b: ['a'], r: 0 },
69
+ { a: ['a', 'b'], b: ['a', 'b'], r: 0 }
70
+ ]
71
+
72
+ const compare = makeCompare(compareString, true)
73
+ tests.forEach(test => {
74
+ expect(compare(test.a, test.b)).toBe(test.r)
75
+ })
76
+ })
77
+
78
+ test('should compare arrays with different lengths', () => {
79
+ const tests = [
80
+ { a: [], b: ['a'], r: -1 },
81
+ { a: ['a'], b: [], r: 1 },
82
+ { a: ['a'], b: ['a', 'b'], r: -1 },
83
+ { a: ['b'], b: ['a', 'b'], r: -1 },
84
+ { a: ['b', 'b'], b: ['a', 'b', 'c'], r: -1 }
85
+ ]
86
+
87
+ let compare = makeCompare(compareString, true)
88
+ tests.forEach(test => {
89
+ expect(compare(test.a, test.b)).toBe(test.r)
90
+ })
91
+
92
+ compare = makeCompare(compareString, false)
93
+ tests.forEach(test => {
94
+ expect(compare(test.a, test.b)).toBe(test.r * -1)
95
+ })
96
+ })
97
+ })
98
+
99
+ const cases: Array<{
100
+ a: string[]
101
+ b: string[]
102
+ operations: DiffOperations<string>
103
+ }> = [
104
+ {
105
+ a: [],
106
+ b: [],
107
+ operations: {
108
+ removals: [],
109
+ swaps: [],
110
+ inserts: []
111
+ }
112
+ },
113
+ {
114
+ a: ['a'],
115
+ b: ['a'],
116
+ operations: {
117
+ removals: [],
118
+ swaps: [],
119
+ inserts: []
120
+ }
121
+ },
122
+ {
123
+ a: ['a'],
124
+ b: ['b'],
125
+ operations: {
126
+ removals: [{ at: 0, qt: 1 }],
127
+ swaps: [],
128
+ inserts: [{ at: 0, values: ['b'] }]
129
+ }
130
+ },
131
+ {
132
+ a: ['a', 'b', 'c'],
133
+ b: ['b'],
134
+ operations: {
135
+ removals: [
136
+ { at: 2, qt: 1 },
137
+ { at: 0, qt: 1 }
138
+ ],
139
+ swaps: [],
140
+ inserts: []
141
+ }
142
+ },
143
+ {
144
+ a: ['a', 'b', 'c'],
145
+ b: ['d'],
146
+ operations: {
147
+ removals: [{ at: 0, qt: 3 }],
148
+ swaps: [],
149
+ inserts: [{ at: 0, values: ['d'] }]
150
+ }
151
+ },
152
+ {
153
+ a: ['a', 'b', 'c', 'd'],
154
+ b: ['a', 'd'],
155
+ operations: {
156
+ removals: [{ at: 1, qt: 2 }],
157
+ swaps: [],
158
+ inserts: []
159
+ }
160
+ },
161
+ {
162
+ a: ['a', 'b', 'c', 'd'],
163
+ b: ['x', 'd'],
164
+ operations: {
165
+ removals: [{ at: 0, qt: 3 }],
166
+ swaps: [],
167
+ inserts: [{ at: 0, values: ['x'] }]
168
+ }
169
+ },
170
+ {
171
+ a: ['a', 'b', 'c', 'd'],
172
+ b: ['x', 'b', 'y', 'd'],
173
+ operations: {
174
+ removals: [
175
+ { at: 2, qt: 1 },
176
+ { at: 0, qt: 1 }
177
+ ],
178
+ swaps: [],
179
+ inserts: [
180
+ { at: 0, values: ['x'] },
181
+ { at: 2, values: ['y'] }
182
+ ]
183
+ }
184
+ },
185
+ {
186
+ a: ['b'],
187
+ b: ['a', 'b', 'c'],
188
+ operations: {
189
+ removals: [],
190
+ swaps: [],
191
+ inserts: [
192
+ { at: 0, values: ['a'] },
193
+ { at: 2, values: ['c'] }
194
+ ]
195
+ }
196
+ },
197
+ {
198
+ a: ['d'],
199
+ b: ['a', 'b', 'c'],
200
+ operations: {
201
+ removals: [{ at: 0, qt: 1 }],
202
+ swaps: [],
203
+ inserts: [{ at: 0, values: ['a', 'b', 'c'] }]
204
+ }
205
+ },
206
+ {
207
+ a: ['a', 'd'],
208
+ b: ['a', 'b', 'c', 'd'],
209
+ operations: {
210
+ removals: [],
211
+ swaps: [],
212
+ inserts: [{ at: 1, values: ['b', 'c'] }]
213
+ }
214
+ },
215
+ {
216
+ a: ['x', 'd'],
217
+ b: ['a', 'b', 'c', 'd'],
218
+ operations: {
219
+ removals: [{ at: 0, qt: 1 }],
220
+ swaps: [],
221
+ inserts: [{ at: 0, values: ['a', 'b', 'c'] }]
222
+ }
223
+ },
224
+ {
225
+ a: ['a', 'b', 'c', 'd', 'e'],
226
+ b: ['b', 'x', 'd'],
227
+ operations: {
228
+ removals: [
229
+ { at: 4, qt: 1 },
230
+ { at: 2, qt: 1 },
231
+ { at: 0, qt: 1 }
232
+ ],
233
+ swaps: [],
234
+ inserts: [{ at: 1, values: ['x'] }]
235
+ }
236
+ },
237
+ {
238
+ a: ['b', 'x', 'd'],
239
+ b: ['a', 'b', 'c', 'd', 'e'],
240
+ operations: {
241
+ removals: [{ at: 1, qt: 1 }],
242
+ swaps: [],
243
+ inserts: [
244
+ { at: 0, values: ['a'] },
245
+ { at: 2, values: ['c'] },
246
+ { at: 4, values: ['e'] }
247
+ ]
248
+ }
249
+ },
250
+ {
251
+ a: ['a', 'b', 'c'],
252
+ b: ['c', 'b', 'a'],
253
+ operations: {
254
+ removals: [],
255
+ swaps: [{ from: 0, to: 2 }],
256
+ inserts: []
257
+ }
258
+ },
259
+ {
260
+ a: ['a', 'b', 'c'],
261
+ b: ['c', 'x', 'b', 'a'],
262
+ operations: {
263
+ removals: [],
264
+ swaps: [{ from: 0, to: 2 }],
265
+ inserts: [{ at: 1, values: ['x'] }]
266
+ }
267
+ },
268
+ {
269
+ a: ['c', 'x', 'b', 'a'],
270
+ b: ['a', 'b', 'c'],
271
+ operations: {
272
+ removals: [{ at: 1, qt: 1 }],
273
+ swaps: [{ from: 0, to: 2 }],
274
+ inserts: []
275
+ }
276
+ },
277
+ {
278
+ a: ['a', 'b', 'c', 'x'],
279
+ b: ['c', 'x', 'y', 'z', 'a', 'b'],
280
+ operations: {
281
+ removals: [],
282
+ swaps: [
283
+ { from: 0, to: 2 },
284
+ { from: 1, to: 3 }
285
+ ],
286
+ inserts: [{ at: 2, values: ['y', 'z'] }]
287
+ }
288
+ },
289
+ {
290
+ a: ['c', 'x', 'y', 'z', 'a', 'b'],
291
+ b: ['a', 'b', 'c', 'x'],
292
+ operations: {
293
+ removals: [{ at: 2, qt: 2 }],
294
+ swaps: [
295
+ { from: 0, to: 2 },
296
+ { from: 1, to: 3 }
297
+ ],
298
+ inserts: []
299
+ }
300
+ }
301
+ ]
302
+
303
+ const js = JSON.stringify
304
+
305
+ describe('array helpers', () => {
306
+ test('diffOperations', () => {
307
+ for (const { a, b, operations } of cases) {
308
+ const res = diffOperations(a, b, v => v)
309
+ try {
310
+ expect(res).toEqual(operations)
311
+ } catch {
312
+ throw new Error(
313
+ `Expected ${js(a)} and ${js(b)} to produce\n${js(
314
+ operations
315
+ )}\nbut got\n${js(res)} instead`
316
+ )
317
+ }
318
+ }
319
+ })
320
+
321
+ test('applyOperations', () => {
322
+ for (const { a, b, operations } of cases) {
323
+ const res = applyOperations(operations, a)
324
+ try {
325
+ expect(res).toEqual(b)
326
+ } catch {
327
+ throw new Error(
328
+ `Expected ${js(operations)} applied to ${js(a)} to produce\n${js(
329
+ b
330
+ )}\nbut got\n${js(res)} instead`
331
+ )
332
+ }
333
+ }
334
+ })
335
+
336
+ const roundtrips: Array<[string[], string[]]> = [
337
+ [[], []],
338
+ [['a'], ['b']],
339
+ [['a', 'b'], ['b']],
340
+ [['a', 'b'], ['x']],
341
+ [['a', 'b', 'c'], ['b']],
342
+ [['a', 'b', 'c'], ['c']],
343
+ [['a', 'b', 'c'], ['x']],
344
+ [['a', 'b', 'c'], ['a']],
345
+ [
346
+ ['a', 'b', 'c'],
347
+ ['c', 'b']
348
+ ],
349
+ [
350
+ ['a', 'b', 'c'],
351
+ ['c', 'a']
352
+ ],
353
+ [
354
+ ['a', 'b', 'c'],
355
+ ['a', 'b']
356
+ ],
357
+ [
358
+ ['a', 'b', 'c'],
359
+ ['a', 'b', 'c']
360
+ ],
361
+ [
362
+ ['a', 'b', 'c'],
363
+ ['c', 'b', 'a']
364
+ ],
365
+ [
366
+ ['a', 'b', 'c'],
367
+ ['a', 'c', 'b']
368
+ ],
369
+ [
370
+ ['a', 'b', 'c'],
371
+ ['b', 'c', 'a']
372
+ ],
373
+ [
374
+ ['a', 'b', 'c'],
375
+ ['a', 'b', 'c', 'd', 'e', 'f']
376
+ ],
377
+ [
378
+ ['c', 'a', 'b'],
379
+ ['a', 'b', 'c']
380
+ ]
381
+ ]
382
+
383
+ test('diffOperations and applyOperations roundtrips', () => {
384
+ for (const [a, b] of roundtrips) {
385
+ const ops = diffOperations(a, b, v => v)
386
+ const res = applyOperations(ops, a)
387
+ try {
388
+ expect(res).toEqual(b)
389
+ } catch {
390
+ throw new Error(
391
+ `${js(a)} and ${js(b)} produced ${js(ops)} but generated ${js(res)}`
392
+ )
393
+ }
394
+ }
395
+
396
+ for (const [b, a] of roundtrips) {
397
+ const ops = diffOperations(a, b, v => v)
398
+ const res = applyOperations(ops, a)
399
+ try {
400
+ expect(res).toEqual(b)
401
+ } catch {
402
+ throw new Error(
403
+ `Swapped ${js(a)} and ${js(b)} produced ${js(ops)} but generated ${js(
404
+ res
405
+ )}`
406
+ )
407
+ }
408
+ }
409
+ })
410
+ })