atomic-fns 0.3.5 → 0.4.3

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 (109) hide show
  1. package/README.md +1 -12
  2. package/{core → dist}/collections/BTree.d.ts +6 -6
  3. package/{core → dist}/collections/BTree.js +17 -17
  4. package/{core → dist}/collections/Heap.d.ts +3 -3
  5. package/{core → dist}/collections/Heap.js +0 -0
  6. package/dist/collections/LRUCache.d.ts +80 -0
  7. package/dist/collections/LRUCache.js +126 -0
  8. package/{core → dist}/collections/SortedMap.d.ts +2 -2
  9. package/{core → dist}/collections/SortedMap.js +0 -0
  10. package/{core → dist}/collections/SortedSet.d.ts +3 -3
  11. package/{core → dist}/collections/SortedSet.js +1 -1
  12. package/{core → dist}/collections/SortedTree.d.ts +3 -3
  13. package/{core → dist}/collections/SortedTree.js +1 -1
  14. package/{core → dist}/collections/SplayTree.d.ts +5 -5
  15. package/{core → dist}/collections/SplayTree.js +0 -0
  16. package/{core → dist}/collections/abc.d.ts +0 -0
  17. package/{core → dist}/collections/abc.js +0 -0
  18. package/{core → dist}/collections/deque.d.ts +7 -7
  19. package/{core → dist}/collections/deque.js +6 -8
  20. package/{core → dist}/collections/frozenset.d.ts +0 -0
  21. package/{core → dist}/collections/frozenset.js +3 -6
  22. package/{core → dist}/collections/index.d.ts +207 -105
  23. package/{core → dist}/collections/index.js +179 -436
  24. package/{core → dist}/functools/index.d.ts +30 -16
  25. package/{core → dist}/functools/index.js +34 -12
  26. package/dist/globals/decimal.d.ts +142 -0
  27. package/dist/globals/decimal.js +371 -0
  28. package/{core → dist}/globals/index.d.ts +26 -25
  29. package/{core → dist}/globals/index.js +16 -37
  30. package/dist/i18n/currency.d.ts +24 -0
  31. package/dist/i18n/currency.js +85 -0
  32. package/dist/i18n/date/index.d.ts +204 -0
  33. package/dist/i18n/date/index.js +530 -0
  34. package/dist/i18n/date/utils.d.ts +65 -0
  35. package/dist/i18n/date/utils.js +290 -0
  36. package/dist/i18n/duration.d.ts +112 -0
  37. package/dist/i18n/duration.js +392 -0
  38. package/dist/i18n/format.d.ts +26 -0
  39. package/dist/i18n/format.js +495 -0
  40. package/{core → dist}/i18n/index.d.ts +2 -1
  41. package/{core → dist}/i18n/index.js +2 -1
  42. package/{core → dist}/index.d.ts +0 -0
  43. package/{core → dist}/index.js +0 -0
  44. package/{core → dist}/itertools/index.d.ts +74 -46
  45. package/{core → dist}/itertools/index.js +28 -56
  46. package/{core → dist}/math/index.d.ts +9 -10
  47. package/{core → dist}/math/index.js +3 -1
  48. package/{core → dist}/operators/index.d.ts +4 -4
  49. package/{core → dist}/operators/index.js +2 -8
  50. package/{core → dist}/random/index.d.ts +1 -1
  51. package/{core → dist}/random/index.js +5 -5
  52. package/package.json +28 -42
  53. package/.editorconfig +0 -12
  54. package/.esdoc.json +0 -27
  55. package/.eslintignore +0 -1
  56. package/.eslintrc.json +0 -35
  57. package/.firebaserc +0 -5
  58. package/.prettierrc +0 -19
  59. package/core/collections/BTree.ts +0 -1340
  60. package/core/collections/Heap.ts +0 -189
  61. package/core/collections/LRUCache.d.ts +0 -152
  62. package/core/collections/LRUCache.js +0 -409
  63. package/core/collections/LRUCache.ts +0 -486
  64. package/core/collections/SortedMap.ts +0 -146
  65. package/core/collections/SortedSet.ts +0 -110
  66. package/core/collections/SortedTree.ts +0 -160
  67. package/core/collections/SplayTree.ts +0 -354
  68. package/core/collections/abc.ts +0 -176
  69. package/core/collections/deque.ts +0 -338
  70. package/core/collections/frozenset.ts +0 -37
  71. package/core/collections/index.ts +0 -1024
  72. package/core/collections/test.html +0 -10
  73. package/core/collections/typedArrays.d.ts +0 -31
  74. package/core/collections/typedArrays.js +0 -143
  75. package/core/collections/typedArrays.ts +0 -163
  76. package/core/functools/index.ts +0 -260
  77. package/core/globals/index.ts +0 -481
  78. package/core/i18n/date/format.d.ts +0 -16
  79. package/core/i18n/date/format.js +0 -307
  80. package/core/i18n/date/format.ts +0 -343
  81. package/core/i18n/date/index.d.ts +0 -224
  82. package/core/i18n/date/index.js +0 -387
  83. package/core/i18n/date/index.ts +0 -471
  84. package/core/i18n/date/test.html +0 -67
  85. package/core/i18n/index.ts +0 -8
  86. package/core/index.ts +0 -8
  87. package/core/itertools/index.ts +0 -494
  88. package/core/math/index.ts +0 -152
  89. package/core/operators/index.ts +0 -310
  90. package/core/random/index.ts +0 -66
  91. package/firebase.json +0 -10
  92. package/jasmine.json +0 -14
  93. package/tests/BTree.spec.ts +0 -143
  94. package/tests/Cache.spec.ts +0 -161
  95. package/tests/Heap.spec.ts +0 -66
  96. package/tests/SortedMap.spec.ts +0 -144
  97. package/tests/SortedSet.spec.ts +0 -84
  98. package/tests/SortedTree.spec.ts +0 -164
  99. package/tests/SplayTree.spec.ts +0 -121
  100. package/tests/collections.spec.ts +0 -288
  101. package/tests/deque.spec.ts +0 -107
  102. package/tests/globals.spec.ts +0 -270
  103. package/tests/itertools.spec.ts +0 -266
  104. package/tests/math.spec.ts +0 -25
  105. package/tests/operators.spec.ts +0 -40
  106. package/tests/random.spec.ts +0 -46
  107. package/tsconfig.json +0 -126
  108. package/tsconfig.node.json +0 -18
  109. package/tsconfig.xo.json +0 -9
@@ -1,310 +0,0 @@
1
- /**
2
- * Generic operations for both values and objects.
3
- *
4
- * @module Operators
5
- */
6
-
7
- import { call, isObject } from '../globals/index.js'
8
-
9
- /** This is the identity function. It always returns the same value that was passed in */
10
- export const id = (x) => x
11
-
12
- /**
13
- * Called to implement truth value testing.
14
- * @param x
15
- * @returns {boolean} `x.bool()` if exists or `!!x`.
16
- */
17
- export const bool = (x) => {
18
- const r = call(x, 'bool')
19
- if (r != null) return r
20
- return !!x
21
- }
22
-
23
- /**
24
- * Returns `true` if `x` is a falsy value.
25
- * @see {@link bool}
26
- */
27
- export const not = (x) => !bool(x)
28
-
29
- /**
30
- * Returns `true` if `x` is an instance of class `y`
31
- * @param x The instance object
32
- * @param y The parent class
33
- * @returns {boolean}
34
- */
35
- export const isinstance = (x, y) => x instanceof y
36
-
37
- /** Describes a function used to compare two values.
38
- *
39
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters Array.sort()}
40
- */
41
- export type Comparer<T = any> = (x: T, y: T) => number
42
-
43
- /**
44
- * Checks whether `x` is a comparable type and returns the result of `x.compare(y)`.
45
- * Otherwise the return value of the compare function is equivalent to `obj === other ? 0 : obj < other ? -1 : 1`
46
- * @param {*} x An initial value
47
- * @param {*} y Other value to compare
48
- * @returns {number} The comparison result
49
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters Array.sort()}
50
- * @see {@link eq}
51
- * @see {@link lt}
52
- */
53
- export function compare(x, y): number {
54
- if (x === y || eq(x, y)) return 0
55
- const op = call(x, 'compare', y)
56
- if (typeof op === 'number') return op
57
- if (x < y || lt(x, y)) return -1
58
- return 1
59
- }
60
-
61
- /**
62
- * Compares two values to check if they are the same. If `x` is an object with an `eq` method, it returns `x.eq(y)`.
63
- * Otherwise it returns the result of `x === y`.
64
- * @param {*} x
65
- * @param {*} y
66
- * @returns {boolean} `true` if `x` and `y` are equal or `false` otherwise.
67
- */
68
- export function eq(x, y) {
69
- return Object.is(x, y) || call(x, 'eq', y) != null
70
- }
71
-
72
- /**
73
- * Compares two values to check if `x` is strictly less than `y`. If `x` is an object with a `lt` method, it returns `x.lt(y)`.
74
- * Otherwise it returns the result of `x < y`.
75
- * @param {*} x
76
- * @param {*} y
77
- * @returns {boolean} `true` if `x` is strictly less than `y` or `false` otherwise.
78
- */
79
- export function lt(x, y) {
80
- return x < y || call(x, 'lt', y) != null
81
- }
82
-
83
- /**
84
- * Compares two values to check if `x <= y`. If `x` is an object with a `lte` method, it returns `x.lte(y)`, also checks `x.eq(y)` and `x.lt(y)`.
85
- * Otherwise it returns the result of `x <= y`.
86
- * @param {*} x
87
- * @param {*} y
88
- * @returns {boolean} `true` if `x` is less than or equal to `y` or `false` otherwise.
89
- * @see {@link eq}
90
- * @see {@link lt}
91
- */
92
- export function lte(x, y) {
93
- if (x <= y || call(x, 'lte', y)) return true
94
- if (x === y || eq(x, y)) return true
95
- if (x < y || lt(x, y)) return true
96
- return false
97
- }
98
-
99
- /**
100
- * Compares two values to check if `x` is strictly greater than `y`. If `x` is an object with a `gt` method, it returns `x.gt(y)`.
101
- * Otherwise it returns the result of `x > y`.
102
- * @param {*} x
103
- * @param {*} y
104
- * @returns {boolean} `true` if `x` is strictly greater than `y` or `false` otherwise.
105
- */
106
- export function gt(x, y) {
107
- return x > y || call(x, 'gt', y)
108
- }
109
-
110
- /**
111
- * Compares two values to check if `x >= y`. If `x` is an object with a `gte` method, it returns `x.gte(y)`, also checks `x.eq(y)` and `x.gt(y)`.
112
- * Otherwise it returns the result of `x >= y`.
113
- * @param {*} x
114
- * @param {*} y
115
- * @returns {boolean} `true` if `x` is less greater than or equal to `y` or `false` otherwise.
116
- * @see {@link eq}
117
- * @see {@link gt}
118
- */
119
- export function gte(x, y) {
120
- if (x <= y || call(x, 'gte', y)) return true
121
- if (x === y || eq(x, y)) return true
122
- if (x > y || gt(x, y)) return true
123
- return false
124
- }
125
-
126
- /**
127
- * The addition (+) operator. If `x` is an object with an `add` method, it returns `x.add(y)`.
128
- * Otherwise it returns the result of `x + y`.
129
- * @param {*} x
130
- * @param {*} y
131
- * @returns {*} x + y
132
- */
133
- export function add(x, y) {
134
- const op = call(x, 'add', y)
135
- if (op != null) return op
136
- return x + y
137
- }
138
-
139
- /**
140
- * The subtraction (-) operator. If `x` is an object with a `sub` method, it returns `x.sub(y)`.
141
- * Otherwise it returns the result of `x - y`.
142
- * @param {*} x
143
- * @param {*} y
144
- * @returns {*} x - y
145
- */
146
- export function sub(x, y) {
147
- const op = call(x, 'sub', y)
148
- if (op != null) return op
149
- return x - y
150
- }
151
-
152
- /**
153
- * The multiplication (*) operator. If `x` is an object with an `mul` method, it returns `x.mul(y)`.
154
- * Otherwise it returns the result of `x * y`.
155
- * @param {*} x
156
- * @param {*} y
157
- * @returns {*} x * y
158
- */
159
- export function mul(x, y) {
160
- const op = call(x, 'mul', y)
161
- if (op != null) return op
162
- return x * y
163
- }
164
- /**
165
- * The division (/) operator. If `x` is an object with a `div` method, it returns `x.div(y)`.
166
- * Otherwise it returns the result of `x / y`.
167
- * @param {*} x
168
- * @param {*} y
169
- * @returns {*} x / y
170
- */
171
- export function div(x, y) {
172
- const op = call(x, 'div', y)
173
- if (op != null) return op
174
- return x / y
175
- }
176
- /**
177
- * The modulo (%) operator. If `x` is an object with a `mod` method, it returns `x.mod(y)`.
178
- * Otherwise it returns the result of `x % y`.
179
- * @param {*} x
180
- * @param {*} y
181
- * @returns {*} x % y
182
- */
183
- export function mod(x, y) {
184
- const op = call(x, 'mod', y)
185
- if (op != null) return op
186
- return x % y
187
- }
188
-
189
- /**
190
- * The power (**) operator. If `x` is an object with a `pow` method, it returns `x.pow(y)`.
191
- * Otherwise it returns the result of `x ** y`.
192
- * @param {*} x
193
- * @param {*} y
194
- * @returns {*} x ** y
195
- */
196
- export function pow(x, y) {
197
- const op = call(x, 'pow', y)
198
- if (op != null) return op
199
- return x ** y
200
- }
201
-
202
- /**
203
- * Performs a shallow comparison of two objects or arrays to check if they have the same keys, length and values.
204
- * @param obj
205
- * @param other
206
- * @returns `true` if the objects are considered equal.
207
- * @see {@link deepEqual}
208
- */
209
- export function shallowEqual(obj, other) {
210
- if (obj === other) return true
211
-
212
- if (!obj || !other) return false
213
-
214
- if (Array.isArray(obj)) {
215
- // compare the arrays
216
- if (obj.length !== other.length) return false
217
-
218
- for (let i = 0; i < obj.length; i++) {
219
- if (obj[i] !== other[i]) {
220
- return false
221
- }
222
- }
223
- return true
224
- } else if (isObject(obj)) {
225
- // compare the object keys
226
- const objKeys = Object.keys(obj)
227
- const otherKeys = Object.keys(other)
228
-
229
- if (objKeys.length !== otherKeys.length) return false
230
-
231
- for (const key of objKeys) {
232
- if (!Object.prototype.hasOwnProperty.call(other, key) || obj[key] !== other[key]) {
233
- return false
234
- }
235
- }
236
- return true
237
- }
238
-
239
- return false
240
- }
241
-
242
- /**
243
- * Performs a recursive comparison of two objects or arrays to check if they have the same keys, length and values. Unlike {@link shallowEqual} this method compares nested values also.
244
- * @param obj
245
- * @param other
246
- * @returns `true` if the objects are considered equal.
247
- */
248
- export function deepEqual(obj, other, checker = eq, seen = new WeakSet()) {
249
- if (checker(obj, other)) return true
250
- if (!obj || !other) return false
251
- if (obj.prototype !== other.prototype) return false
252
- if (seen.has(obj) && seen.has(other)) return false
253
-
254
- if (Array.isArray(obj)) {
255
- // compare the arrays
256
- if (obj.length !== other.length) return false
257
-
258
- for (let i = 0; i < obj.length; i++) {
259
- if (!deepEqual(obj[i], other[i], checker)) {
260
- return false
261
- }
262
- }
263
-
264
- return true
265
- }
266
-
267
- if (obj instanceof Set) {
268
- if (obj.size !== other.size) return false
269
- for (const value of obj.values()) {
270
- if (!other.has(value)) return false
271
- }
272
- return true
273
- }
274
-
275
- if (obj instanceof Map) {
276
- // compare keys and values
277
- if (obj.size !== other.size) return false
278
- seen.add(obj)
279
- seen.add(other)
280
- for (const key of obj.keys()) {
281
- if (!other.has(key)) return false
282
- if (!deepEqual(obj.get(key), other.get(key), checker, seen)) {
283
- return false
284
- }
285
- }
286
- return true
287
- }
288
-
289
- if (isObject(obj)) {
290
- // compare the object own enumerable keys
291
- const objKeys = Object.keys(obj)
292
- const otherKeys = Object.keys(other)
293
-
294
- if (objKeys.length !== otherKeys.length) return false
295
-
296
- seen.add(obj)
297
- seen.add(other)
298
- for (const key of objKeys) {
299
- if (
300
- !Object.prototype.hasOwnProperty.call(other, key) ||
301
- !deepEqual(obj[key], other[key], checker, seen)
302
- ) {
303
- return false
304
- }
305
- }
306
- return true
307
- }
308
-
309
- return false
310
- }
@@ -1,66 +0,0 @@
1
- /**
2
- * This module implements pseudo-random generators for various distributions.
3
- *
4
- * @module Random
5
- */
6
-
7
- import { isObject } from '../globals/index.js'
8
-
9
- /**
10
- * Returns a random float between a [min, max) value (max exclusive).
11
- * @param {number} min
12
- * @param {number} max
13
- * @returns {number}
14
- */
15
- export const random = (min = 0, max = 1) => Math.random() * (max - min) + min
16
-
17
- /**
18
- * Returns a random integer between a [min, max] value (inclusive).
19
- * @param {number} min
20
- * @param {number} max
21
- * @returns {number}
22
- */
23
- export const randomInt = (min = 0, max = 1) => Math.round(random(min, max))
24
-
25
- /**
26
- * Returns the item at some random index in a sequence.
27
- * @returns {T}
28
- * @template T
29
- */
30
- export const choice = <T>(arr: T[]) => {
31
- if (!arr || !arr.length) return
32
- return arr[randomInt(0, arr.length - 1)]
33
- }
34
-
35
- /**
36
- * Returns a random sample of size k from a list of items.
37
- * @param {Array} arr
38
- * @param {number} size
39
- * @returns A new array with `size` random elements from `arr`.
40
- * @template T
41
- */
42
- export function sample<T>(arr: T[], size: number) {
43
- if (!arr || !arr.length || arr.length < size) return []
44
- return shuffle(arr.slice()).slice(-size)
45
- }
46
-
47
- /**
48
- * Shuffles a collection elements in-place. Uses the Fisher-Yates algorithm for uniform shuffling. If collection is an Object it returns shuffled `Object.values()`.
49
- *
50
- * @param {Array} arr
51
- * @param {number} size
52
- * @returns The shuffled array.
53
- * @template T
54
- */
55
- export function shuffle<T>(arr: T[], size?: number) {
56
- if (!arr || !arr.length) return arr
57
- if (isObject(arr)) arr = Object.values(arr)
58
- if (size == null) size = arr.length - 1
59
- for (let i = 0; i <= size; i++) {
60
- const j = randomInt(i, size)
61
- const temp = arr[j]
62
- arr[j] = arr[i]
63
- arr[i] = temp
64
- }
65
- return arr
66
- }
package/firebase.json DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "hosting": {
3
- "public": "docs",
4
- "ignore": [
5
- "firebase.json",
6
- "**/.*",
7
- "**/node_modules/**"
8
- ]
9
- }
10
- }
package/jasmine.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "reporters": [
3
- {
4
- "name": "jasmine-spec-reporter#SpecReporter",
5
- "options": {
6
- "displayStacktrace": "all"
7
- }
8
- }
9
- ],
10
- "spec_dir": "tests",
11
- "spec_files": [
12
- "**/*spec.ts"
13
- ]
14
- }
@@ -1,143 +0,0 @@
1
- import assert from 'assert'
2
- import { BTree } from '../core/collections/BTree.js'
3
- import { times } from '../core/itertools/index.js'
4
-
5
- describe('BTree', () => {
6
- it('get', () => {
7
- let tree = new BTree()
8
-
9
- for (let i = 100; i > 0; i--) {
10
- tree.set(i, i * 2)
11
- assert(tree.get(i) == i * 2)
12
- }
13
- })
14
-
15
- it('add', () => {
16
- let tree = new BTree()
17
-
18
- for (let i = 100; i > 0; i--) {
19
- tree.add(i)
20
- assert(tree.contains(i))
21
- assert(tree.get(i) === null)
22
- }
23
- })
24
-
25
- it('set', () => {
26
- let tree = new BTree()
27
-
28
- for (let i = 100; i > 0; i--) {
29
- tree.set(i, i)
30
- assert(tree.contains(i))
31
- }
32
-
33
- assert(tree.size === 100)
34
- })
35
-
36
- it('remove', () => {
37
- let tree = new BTree()
38
- for (let i = 100; i > 0; i--) {
39
- tree.set(i, i)
40
- }
41
- assert(tree.contains(100))
42
- tree.remove(100)
43
- assert(tree.contains(100) === false)
44
- })
45
-
46
- it('clear', () => {
47
- let tree = new BTree()
48
- for (let i = 100; i > 0; i--) {
49
- tree.set(i, i)
50
- }
51
- tree.clear()
52
- assert(tree.size === 0)
53
- assert(tree.contains(100) === false)
54
- })
55
-
56
- it('iterates in order', () => {
57
- let tree = new BTree()
58
- let entries: any[] = []
59
- for (let i = 9; i >= 0; i--) {
60
- tree.set(i, i * 2)
61
- entries.push([i, i * 2])
62
- }
63
- entries.sort((x, y) => x[0] - y[0])
64
- assert.deepEqual([...tree], entries)
65
- })
66
-
67
- it('minKey', () => {
68
- let tree = new BTree()
69
- let keys: number[] = []
70
- for (let i = 10; i >= 0; i--) {
71
- keys.push(i)
72
- tree.set(i, i * 2)
73
- }
74
- keys.sort(tree._compare)
75
- assert(tree.minKey() === keys[0])
76
- })
77
-
78
- it('maxKey', () => {
79
- let tree = new BTree()
80
- let keys: number[] = []
81
- for (let i = 10; i >= 0; i--) {
82
- keys.push(i)
83
- tree.set(i, i * 2)
84
- }
85
- keys.sort(tree._compare)
86
- assert(tree.maxKey() === keys[keys.length - 1])
87
- })
88
-
89
- it('clone', () => {
90
- let tree = new BTree()
91
- let entries: any[] = []
92
- for (let i = 100; i > 0; i--) {
93
- tree.set(i, i)
94
- entries.push([i, i])
95
- }
96
- let t2 = tree.clone()
97
- assert.deepEqual([...t2.reversed()], entries)
98
- })
99
-
100
- it('freeze', () => {
101
- let tree = new BTree()
102
- for (let i = 10; i > 0; i--) {
103
- tree.set(i, i)
104
- }
105
- tree.freeze()
106
- assert.throws(tree.clear)
107
- assert.throws(() => tree.add(11))
108
- })
109
-
110
- it('reversed', () => {
111
- let tree = new BTree()
112
- let entries: any[] = []
113
- for (let i = 100; i > 0; i--) {
114
- tree.set(i, i)
115
- entries.push([i, i])
116
- }
117
- assert.deepEqual([...tree.reversed()], entries)
118
- })
119
-
120
- it('keys', () => {
121
- let tree = new BTree()
122
- for (let i = 9; i >= 0; i--) {
123
- tree.set(i, i)
124
- }
125
- assert.deepEqual([...tree.keys()], [...times(10, (i) => i)])
126
- })
127
-
128
- it('values', () => {
129
- let tree = new BTree()
130
- for (let i = 9; i >= 0; i--) {
131
- tree.set(i, i * 2)
132
- }
133
- assert.deepEqual([...tree.values()], [...times(10, (i) => i * 2)])
134
- })
135
-
136
- it('entries', () => {
137
- let tree = new BTree()
138
- for (let i = 9; i >= 0; i--) {
139
- tree.set(i, i * 2)
140
- }
141
- assert.deepEqual([...tree.entries()], [...times(10, (i) => [i, i * 2])])
142
- })
143
- })
@@ -1,161 +0,0 @@
1
- import assert from 'assert'
2
- import { Cache, LRUCache } from '../core/collections/LRUCache.js'
3
-
4
- describe('Cache', () => {
5
- it('empty', () => {
6
- let cache = new Cache()
7
- assert(cache.size === 0)
8
- })
9
-
10
- it('contains', () => {
11
- let cache = new Cache(2)
12
- assert(cache.capacity === 2)
13
- assert(!cache.contains(1))
14
- cache.set(1, 'foo')
15
- cache.set(2, 'bar')
16
- assert(cache.contains(1))
17
- assert(cache.contains(2))
18
- })
19
-
20
- it('evicts LRU keys', () => {
21
- let cache = new Cache(2)
22
- assert(cache.capacity === 2)
23
- cache.set(1, 'foo')
24
- cache.set(2, 'bar')
25
- assert(cache.get(1) === 'foo')
26
- cache.set(3, 'other')
27
- assert(cache.get(2) === undefined)
28
- assert.deepEqual([...cache.keys()], [3, 1])
29
- })
30
-
31
- it('keys', () => {
32
- let cache = new Cache(5)
33
- for (const value of [1, 2, 3, 4, 5]) {
34
- cache.set(value, value)
35
- }
36
- assert.deepEqual([...cache.keys()], [5, 4, 3, 2, 1])
37
- })
38
-
39
- it('values', () => {
40
- let cache = new Cache(5)
41
- for (const value of [1, 2, 3, 4, 5]) {
42
- cache.set(value, value)
43
- }
44
- assert.deepEqual([...cache.keys()], [5, 4, 3, 2, 1])
45
- })
46
-
47
- it('entries', () => {
48
- let cache = new Cache(3)
49
- for (const value of [1, 2, 3]) {
50
- cache.set(value, 0)
51
- }
52
- assert.deepEqual(
53
- [...cache.entries()],
54
- [
55
- [3, 0],
56
- [2, 0],
57
- [1, 0]
58
- ]
59
- )
60
- })
61
-
62
- it('iterator', () => {
63
- let cache = new Cache(3)
64
- for (const value of [1, 2, 3]) {
65
- cache.set(value, 0)
66
- }
67
- let values = [...cache]
68
- assert.deepEqual(values, [...cache.entries()])
69
- })
70
- })
71
-
72
- describe('LRUCache', () => {
73
- it('empty', () => {
74
- let cache = new LRUCache()
75
- assert(cache.size === 0)
76
- })
77
-
78
- it('contains', () => {
79
- let cache = new LRUCache(2)
80
- assert(cache.capacity === 2)
81
- assert(!cache.contains(1))
82
- cache.set(1, 'foo')
83
- cache.set(2, 'bar')
84
- assert(cache.contains(1))
85
- assert(cache.contains(2))
86
- })
87
-
88
- it('evicts LRU keys', () => {
89
- let cache = new LRUCache(2)
90
- assert(cache.capacity === 2)
91
- cache.set(1, 'foo')
92
- cache.set(2, 'bar')
93
- assert(cache.get(1) === 'foo')
94
- cache.set(3, 'other')
95
- assert(cache.get(2) === undefined)
96
- assert.deepEqual([...cache.keys()], [3, 1])
97
- })
98
-
99
- it('keys', () => {
100
- let cache = new LRUCache(5)
101
- for (const value of [1, 2, 3, 4, 5]) {
102
- cache.set(value, value)
103
- }
104
- assert.deepEqual([...cache.keys()], [5, 4, 3, 2, 1])
105
- })
106
-
107
- it('values', () => {
108
- let cache = new LRUCache(5)
109
- for (const value of [1, 2, 3, 4, 5]) {
110
- cache.set(value, value)
111
- }
112
- assert.deepEqual([...cache.keys()], [5, 4, 3, 2, 1])
113
- })
114
-
115
- it('entries', () => {
116
- let cache = new LRUCache(3)
117
- for (const value of [1, 2, 3]) {
118
- cache.set(value, 0)
119
- }
120
- assert.deepEqual(
121
- [...cache.entries()],
122
- [
123
- [3, 0],
124
- [2, 0],
125
- [1, 0]
126
- ]
127
- )
128
- })
129
-
130
- it('iterator', () => {
131
- let cache = new LRUCache(3)
132
- for (const value of [1, 2, 3]) {
133
- cache.set(value, 0)
134
- }
135
- let values = [...cache]
136
- assert.deepEqual(values, [...cache.entries()])
137
- })
138
-
139
- it('delete', () => {
140
- let cache = new LRUCache()
141
- for (const value of [1, 2, 3]) {
142
- cache.set(value, 0)
143
- }
144
- assert(cache.contains(1))
145
- assert(cache.delete(1) === true)
146
- assert(!cache.contains(1))
147
- // try deleting again
148
- assert(cache.delete(1) === false)
149
- })
150
-
151
- it('remove', () => {
152
- let cache = new LRUCache()
153
- for (const value of [1, 2, 3]) {
154
- cache.set(value, 0)
155
- }
156
- assert(cache.contains(1))
157
- assert(cache.remove(1) === 0)
158
- assert(!cache.contains(1))
159
- assert(cache.delete(2))
160
- })
161
- })