@fluidframework/core-utils 2.0.0-rc.2.0.2 → 2.0.0-rc.3.0.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.
@@ -1,45 +0,0 @@
1
- /* Excluded from this release type: assert */
2
-
3
- /* Excluded from this release type: compareArrays */
4
-
5
- /* Excluded from this release type: Deferred */
6
-
7
- /* Excluded from this release type: delay */
8
-
9
- /* Excluded from this release type: Heap */
10
-
11
- /* Excluded from this release type: IComparer */
12
-
13
- /* Excluded from this release type: IHeapNode */
14
-
15
- /* Excluded from this release type: IPromiseTimer */
16
-
17
- /* Excluded from this release type: IPromiseTimerResult */
18
-
19
- /* Excluded from this release type: isObject */
20
-
21
- /* Excluded from this release type: isPromiseLike */
22
-
23
- /* Excluded from this release type: ITimer */
24
-
25
- /* Excluded from this release type: Lazy */
26
-
27
- /* Excluded from this release type: LazyPromise */
28
-
29
- /* Excluded from this release type: NumberComparer */
30
-
31
- /* Excluded from this release type: PromiseCache */
32
-
33
- /* Excluded from this release type: PromiseCacheExpiry */
34
-
35
- /* Excluded from this release type: PromiseCacheOptions */
36
-
37
- /* Excluded from this release type: PromiseTimer */
38
-
39
- /* Excluded from this release type: setLongTimeout */
40
-
41
- /* Excluded from this release type: Timer */
42
-
43
- /* Excluded from this release type: unreachableCase */
44
-
45
- export { }
@@ -1,432 +0,0 @@
1
- /**
2
- * A browser friendly assert library.
3
- * Use this instead of the 'assert' package, which has a big impact on bundle sizes.
4
- * @param condition - The condition that should be true, if the condition is false an error will be thrown.
5
- * Only use this API when `false` indicates a logic error in the problem and thus a bug that should be fixed.
6
- * @param message - The message to include in the error when the condition does not hold.
7
- * A number should not be specified manually: use a string.
8
- * Before a release, policy-check should be run, which will convert any asserts still using strings to
9
- * use numbered error codes instead.
10
- * @alpha
11
- */
12
- export declare function assert(condition: boolean, message: string | number): asserts condition;
13
-
14
- /**
15
- * Compare two arrays. Returns true if their elements are equivalent and in the same order.
16
- *
17
- * @alpha
18
- *
19
- * @param left - The first array to compare
20
- * @param right - The second array to compare
21
- * @param comparator - The function used to check if two `T`s are equivalent.
22
- * Defaults to `Object.is()` equality (a shallow compare where NaN = NaN and -0 ≠ 0)
23
- */
24
- export declare const compareArrays: <T>(left: readonly T[], right: readonly T[], comparator?: (leftItem: T, rightItem: T, index: number) => boolean) => boolean;
25
-
26
- /**
27
- * A deferred creates a promise and the ability to resolve or reject it
28
- * @alpha
29
- */
30
- export declare class Deferred<T> {
31
- private readonly p;
32
- private res;
33
- private rej;
34
- private completed;
35
- constructor();
36
- /**
37
- * Returns whether the underlying promise has been completed
38
- */
39
- get isCompleted(): boolean;
40
- /**
41
- * Retrieves the underlying promise for the deferred
42
- *
43
- * @returns the underlying promise
44
- */
45
- get promise(): Promise<T>;
46
- /**
47
- * Resolves the promise
48
- *
49
- * @param value - the value to resolve the promise with
50
- */
51
- resolve(value: T | PromiseLike<T>): void;
52
- /**
53
- * Rejects the promise
54
- *
55
- * @param value - the value to reject the promise with
56
- */
57
- reject(error: any): void;
58
- }
59
-
60
- /**
61
- * Returns a promise that resolves after `timeMs`.
62
- * @param timeMs - Time in milliseconds to wait.
63
- * @internal
64
- */
65
- export declare const delay: (timeMs: number) => Promise<void>;
66
-
67
- /**
68
- * Ordered {@link https://en.wikipedia.org/wiki/Heap_(data_structure) | Heap} data structure implementation.
69
- * @internal
70
- */
71
- export declare class Heap<T> {
72
- comp: IComparer<T>;
73
- private L;
74
- /**
75
- * Creates an instance of `Heap` with comparer.
76
- * @param comp - A comparer that specify how elements are ordered.
77
- */
78
- constructor(comp: IComparer<T>);
79
- /**
80
- * Return the smallest element in the heap as determined by the order of the comparer
81
- *
82
- * @returns Heap node containing the smallest element
83
- */
84
- peek(): IHeapNode<T> | undefined;
85
- /**
86
- * Get and remove the smallest element in the heap as determined by the order of the comparer
87
- *
88
- * @returns The smallest value in the heap
89
- */
90
- get(): T | undefined;
91
- /**
92
- * Add a value to the heap
93
- *
94
- * @param x - value to add
95
- * @returns The heap node that contains the value
96
- */
97
- add(x: T): IHeapNode<T>;
98
- /**
99
- * Allows for the Heap to be updated after a node's value changes.
100
- */
101
- update(node: IHeapNode<T>): void;
102
- /**
103
- * Removes the given node from the heap.
104
- *
105
- * @param node - The node to remove from the heap.
106
- */
107
- remove(node: IHeapNode<T>): void;
108
- /**
109
- * Get the number of elements in the Heap.
110
- *
111
- * @returns The number of elements in the Heap.
112
- */
113
- count(): number;
114
- private fixup;
115
- private isGreaterThanParent;
116
- private fixdown;
117
- private swap;
118
- }
119
-
120
- /**
121
- * Interface for a comparer.
122
- * @internal
123
- */
124
- export declare interface IComparer<T> {
125
- /**
126
- * The minimum value of type T.
127
- */
128
- min: T;
129
- /**
130
- * Compare the two value
131
- *
132
- * @returns 0 if the value is equal, negative number if a is smaller then b, positive number otherwise
133
- */
134
- compare(a: T, b: T): number;
135
- }
136
-
137
- /**
138
- * Interface to a node in {@link Heap}.
139
- * @internal
140
- */
141
- export declare interface IHeapNode<T> {
142
- value: T;
143
- position: number;
144
- }
145
-
146
- /**
147
- * Timer which offers a promise that fulfills when the timer
148
- * completes.
149
- * @internal
150
- */
151
- export declare interface IPromiseTimer extends ITimer {
152
- /**
153
- * Starts the timer and returns a promise that
154
- * resolves when the timer times out or is canceled.
155
- */
156
- start(): Promise<IPromiseTimerResult>;
157
- }
158
-
159
- /**
160
- * @internal
161
- */
162
- export declare interface IPromiseTimerResult {
163
- timerResult: "timeout" | "cancel";
164
- }
165
-
166
- /**
167
- * Determines if an arbitrary value is an object
168
- * @param value - The value to check to see if it is an object
169
- * @returns True if the passed value is an object
170
- *
171
- * @internal
172
- */
173
- export declare const isObject: (value: unknown) => value is object;
174
-
175
- /**
176
- * Determines if an arbitrary value is a promise
177
- * @param value - The value to check to see if it is a promise
178
- * @returns True if the passed value is a promise
179
- *
180
- * @internal
181
- */
182
- export declare const isPromiseLike: (value: unknown) => value is PromiseLike<unknown>;
183
-
184
- /**
185
- * @internal
186
- */
187
- export declare interface ITimer {
188
- /**
189
- * True if timer is currently running
190
- */
191
- readonly hasTimer: boolean;
192
- /**
193
- * Starts the timer
194
- */
195
- start(): void;
196
- /**
197
- * Cancels the timer if already running
198
- */
199
- clear(): void;
200
- }
201
-
202
- /**
203
- * Helper class for lazy initialized values. Ensures the value is only generated once, and remain immutable.
204
- * @internal
205
- */
206
- export declare class Lazy<T> {
207
- private readonly valueGenerator;
208
- private _value;
209
- private _evaluated;
210
- /**
211
- * Instantiates an instance of Lazy<T>.
212
- * @param valueGenerator - The function that will generate the value when value is accessed the first time.
213
- */
214
- constructor(valueGenerator: () => T);
215
- /**
216
- * Return true if the value as been generated, otherwise false.
217
- */
218
- get evaluated(): boolean;
219
- /**
220
- * Get the value. If this is the first call the value will be generated.
221
- */
222
- get value(): T;
223
- }
224
-
225
- /**
226
- * A lazy evaluated promise. The execute function is delayed until
227
- * the promise is used, e.g. await, then, catch ...
228
- * The execute function is only called once.
229
- * All calls are then proxied to the promise returned by the execute method.
230
- * @alpha
231
- */
232
- export declare class LazyPromise<T> implements Promise<T> {
233
- private readonly execute;
234
- get [Symbol.toStringTag](): string;
235
- private result;
236
- constructor(execute: () => Promise<T>);
237
- then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
238
- catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): Promise<T | TResult>;
239
- finally(onfinally?: (() => void) | null | undefined): Promise<T>;
240
- private getPromise;
241
- }
242
-
243
- /**
244
- * A comparer for numbers.
245
- * @internal
246
- */
247
- export declare const NumberComparer: IComparer<number>;
248
-
249
- /**
250
- * A specialized cache for async work, allowing you to safely cache the promised result of some async work
251
- * without fear of running it multiple times or losing track of errors.
252
- * @alpha
253
- */
254
- export declare class PromiseCache<TKey, TResult> {
255
- private readonly cache;
256
- private readonly gc;
257
- private readonly removeOnError;
258
- /**
259
- * Create the PromiseCache with the given options, with the following defaults:
260
- *
261
- * expiry: indefinite, removeOnError: true for all errors
262
- */
263
- constructor({ expiry, removeOnError, }?: PromiseCacheOptions);
264
- /**
265
- * Check if there's anything cached at the given key
266
- */
267
- has(key: TKey): boolean;
268
- /**
269
- * Get the Promise for the given key, or undefined if it's not found.
270
- * Extend expiry if applicable.
271
- */
272
- get(key: TKey): Promise<TResult> | undefined;
273
- /**
274
- * Remove the Promise for the given key, returning true if it was found and removed
275
- */
276
- remove(key: TKey): boolean;
277
- /**
278
- * Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.
279
- * Returns a Promise for the added or existing async work being done at that key.
280
- * @param key - key name where to store the async work
281
- * @param asyncFn - the async work to do and store, if not already in progress under the given key
282
- */
283
- addOrGet(key: TKey, asyncFn: () => Promise<TResult>): Promise<TResult>;
284
- /**
285
- * Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.
286
- * Returns false if the cache already contained an entry at that key, and true otherwise.
287
- * @param key - key name where to store the async work
288
- * @param asyncFn - the async work to do and store, if not already in progress under the given key
289
- */
290
- add(key: TKey, asyncFn: () => Promise<TResult>): boolean;
291
- /**
292
- * Try to add the given value, without overwriting an existing cache entry at that key.
293
- * Returns a Promise for the added or existing async work being done at that key.
294
- * @param key - key name where to store the async work
295
- * @param value - value to store
296
- */
297
- addValueOrGet(key: TKey, value: TResult): Promise<TResult>;
298
- /**
299
- * Try to add the given value, without overwriting an existing cache entry at that key.
300
- * Returns false if the cache already contained an entry at that key, and true otherwise.
301
- * @param key - key name where to store the value
302
- * @param value - value to store
303
- */
304
- addValue(key: TKey, value: TResult): boolean;
305
- }
306
-
307
- /**
308
- * Three supported expiry policies:
309
- * - indefinite: entries don't expire and must be explicitly removed
310
- * - absolute: entries expire after the given duration in MS, even if accessed multiple times in the mean time
311
- * - sliding: entries expire after the given duration in MS of inactivity (i.e. get resets the clock)
312
- * @alpha
313
- */
314
- export declare type PromiseCacheExpiry = {
315
- policy: "indefinite";
316
- } | {
317
- policy: "absolute" | "sliding";
318
- durationMs: number;
319
- };
320
-
321
- /**
322
- * Options for configuring the {@link PromiseCache}
323
- * @alpha
324
- */
325
- export declare interface PromiseCacheOptions {
326
- /**
327
- * Common expiration policy for all items added to this cache
328
- */
329
- expiry?: PromiseCacheExpiry;
330
- /**
331
- * If the stored Promise is rejected with a particular error, should the given key be removed?
332
- */
333
- removeOnError?: (error: any) => boolean;
334
- }
335
-
336
- /**
337
- * This class is a wrapper over setTimeout and clearTimeout which
338
- * makes it simpler to keep track of recurring timeouts with the
339
- * same handlers and timeouts, while also providing a promise that
340
- * resolves when it times out.
341
- * @internal
342
- */
343
- export declare class PromiseTimer implements IPromiseTimer {
344
- private deferred?;
345
- private readonly timer;
346
- /**
347
- * {@inheritDoc Timer.hasTimer}
348
- */
349
- get hasTimer(): boolean;
350
- constructor(defaultTimeout: number, defaultHandler: () => void);
351
- /**
352
- * {@inheritDoc IPromiseTimer.start}
353
- */
354
- start(ms?: number, handler?: () => void): Promise<IPromiseTimerResult>;
355
- clear(): void;
356
- protected wrapHandler(handler: () => void): void;
357
- }
358
-
359
- /**
360
- * Sets timeouts like the setTimeout function allowing timeouts to exceed the setTimeout's max timeout limit.
361
- * Timeouts may not be exactly accurate due to browser implementations and the OS.
362
- * https://stackoverflow.com/questions/21097421/what-is-the-reason-javascript-settimeout-is-so-inaccurate
363
- * @param timeoutFn - Executed when the timeout expires
364
- * @param timeoutMs - Duration of the timeout in milliseconds
365
- * @param setTimeoutIdFn - Executed to update the timeout if multiple timeouts are required when
366
- * timeoutMs greater than maxTimeout
367
- * @returns The initial timeout
368
- * @internal
369
- */
370
- export declare function setLongTimeout(timeoutFn: () => void, timeoutMs: number, setTimeoutIdFn?: (timeoutId: ReturnType<typeof setTimeout>) => void): ReturnType<typeof setTimeout>;
371
-
372
- /**
373
- * This class is a thin wrapper over setTimeout and clearTimeout which
374
- * makes it simpler to keep track of recurring timeouts with the same
375
- * or similar handlers and timeouts. This class supports long timeouts
376
- * or timeouts exceeding (2^31)-1 ms or approximately 24.8 days.
377
- * @internal
378
- */
379
- export declare class Timer implements ITimer {
380
- private readonly defaultTimeout;
381
- private readonly defaultHandler;
382
- private readonly getCurrentTick;
383
- /**
384
- * Returns true if the timer is running.
385
- */
386
- get hasTimer(): boolean;
387
- private runningState;
388
- constructor(defaultTimeout: number, defaultHandler: () => void, getCurrentTick?: () => number);
389
- /**
390
- * Calls setTimeout and tracks the resulting timeout.
391
- * @param ms - overrides default timeout in ms
392
- * @param handler - overrides default handler
393
- */
394
- start(ms?: number, handler?: () => void): void;
395
- /**
396
- * Calls clearTimeout on the underlying timeout if running.
397
- */
398
- clear(): void;
399
- /**
400
- * Restarts the timer with the new handler and duration.
401
- * If a new handler is passed, the original handler may
402
- * never execute.
403
- * This is a potentially more efficient way to clear and start
404
- * a new timer.
405
- * @param ms - overrides previous or default timeout in ms
406
- * @param handler - overrides previous or default handler
407
- */
408
- restart(ms?: number, handler?: () => void): void;
409
- private startCore;
410
- private handler;
411
- private calculateRemainingTime;
412
- }
413
-
414
- /**
415
- * This function can be used to assert at compile time that a given value has type never.
416
- * One common usage is in the default case of a switch block,
417
- * to ensure that all cases are explicitly handled.
418
- *
419
- * Example:
420
- * ```typescript
421
- * const bool: true | false = ...;
422
- * switch(bool) {
423
- * case true: {...}
424
- * case false: {...}
425
- * default: unreachableCase(bool);
426
- * }
427
- * ```
428
- * @internal
429
- */
430
- export declare function unreachableCase(_: never, message?: string): never;
431
-
432
- export { }
@@ -1,21 +0,0 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
- import { strict } from "node:assert";
6
- import { assert } from "@fluidframework/core-utils";
7
- describe("Assert", () => {
8
- it("Validate Shortcode Format", async () => {
9
- // short codes should be hex, and at least 3 chars
10
- for (const shortCode of ["0x000", "0x03a", "0x200", "0x4321"]) {
11
- try {
12
- assert(false, Number.parseInt(shortCode, 16));
13
- }
14
- catch (error) {
15
- strict(error instanceof Error, "not an error");
16
- strict.strictEqual(error.message, shortCode, "incorrect short code format");
17
- }
18
- }
19
- });
20
- });
21
- //# sourceMappingURL=assert.spec.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"assert.spec.js","sourceRoot":"","sources":["../../src/test/assert.spec.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AAEpD,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACvB,EAAE,CAAC,2BAA2B,EAAE,KAAK,IAAI,EAAE;QAC1C,kDAAkD;QAClD,KAAK,MAAM,SAAS,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAC,EAAE;YAC9D,IAAI;gBACH,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,CAAC;aAC9C;YAAC,OAAO,KAAc,EAAE;gBACxB,MAAM,CAAC,KAAK,YAAY,KAAK,EAAE,cAAc,CAAC,CAAC;gBAC/C,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC,OAAO,EAAE,SAAS,EAAE,6BAA6B,CAAC,CAAC;aAC5E;SACD;IACF,CAAC,CAAC,CAAC;AACJ,CAAC,CAAC,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { strict } from \"node:assert\";\nimport { assert } from \"@fluidframework/core-utils\";\n\ndescribe(\"Assert\", () => {\n\tit(\"Validate Shortcode Format\", async () => {\n\t\t// short codes should be hex, and at least 3 chars\n\t\tfor (const shortCode of [\"0x000\", \"0x03a\", \"0x200\", \"0x4321\"]) {\n\t\t\ttry {\n\t\t\t\tassert(false, Number.parseInt(shortCode, 16));\n\t\t\t} catch (error: unknown) {\n\t\t\t\tstrict(error instanceof Error, \"not an error\");\n\t\t\t\tstrict.strictEqual(error.message, shortCode, \"incorrect short code format\");\n\t\t\t}\n\t\t}\n\t});\n});\n"]}
@@ -1,105 +0,0 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
- import { benchmark, BenchmarkType } from "@fluid-tools/benchmark";
6
- import { compareArrays } from "@fluidframework/core-utils";
7
- const a4 = Array.from({ length: 4 }).fill(0);
8
- const a1024 = Array.from({ length: 1024 }).fill(0);
9
- const comparisons = [
10
- ["trivial rejection based on length", a1024, a1024.slice(1)],
11
- ["trivial acceptance based on ref", a1024, a1024],
12
- ["compare empty", [], []],
13
- [`compare ${a4.length} items`, [...a4], [...a4]],
14
- [`compare ${a1024.length} items`, [...a1024], [...a1024]],
15
- ];
16
- function compareWithFor(left, right) {
17
- if (left.length !== right.length) {
18
- return false;
19
- }
20
- for (let index = 0; index < left.length; index++) {
21
- if (left[index] !== right[index]) {
22
- return false;
23
- }
24
- }
25
- return true;
26
- }
27
- function compareWithEvery(left, right) {
28
- return (left.length === right.length && left.every((leftItem, index) => leftItem === right[index]));
29
- }
30
- function compareWithObjectIs(left, right) {
31
- return (left.length === right.length &&
32
- left.every((leftItem, index) => Object.is(leftItem, right[index])));
33
- }
34
- describe("compareArrays()", () => {
35
- describe.skip("baseline", () => {
36
- describe("using for-loop", () => {
37
- for (const [title, left, right] of comparisons) {
38
- benchmark({
39
- type: BenchmarkType.Measurement,
40
- title: `${title}`,
41
- before: () => { },
42
- benchmarkFn: () => {
43
- compareWithFor(left, right);
44
- },
45
- });
46
- }
47
- });
48
- describe("using Array.every()", () => {
49
- for (const [title, left, right] of comparisons) {
50
- benchmark({
51
- type: BenchmarkType.Measurement,
52
- title: `${title}`,
53
- before: () => { },
54
- benchmarkFn: () => {
55
- compareWithEvery(left, right);
56
- },
57
- });
58
- }
59
- });
60
- describe("using Object.is()", () => {
61
- for (const [title, left, right] of comparisons) {
62
- benchmark({
63
- type: BenchmarkType.Measurement,
64
- title: `${title}`,
65
- before: () => { },
66
- benchmarkFn: () => {
67
- compareWithObjectIs(left, right);
68
- },
69
- });
70
- }
71
- });
72
- });
73
- describe("no callback", () => {
74
- for (const [title, left, right] of comparisons) {
75
- benchmark({
76
- type: BenchmarkType.Measurement,
77
- title: `${title}`,
78
- before: () => { },
79
- benchmarkFn: () => {
80
- compareArrays(left, right);
81
- },
82
- });
83
- }
84
- });
85
- describe("with callback", () => {
86
- let sum = 0;
87
- for (const [title, left, right] of comparisons) {
88
- benchmark({
89
- type: BenchmarkType.Measurement,
90
- title: `${title}`,
91
- benchmarkFn: () => {
92
- compareArrays(left, right, (leftItem, rightItem, index) => {
93
- sum += index;
94
- return Object.is(leftItem, rightItem);
95
- });
96
- },
97
- after: () => {
98
- // Paranoid usage of 'sum' to prevent dead code optimization.
99
- console.log(`after: ${sum}`);
100
- },
101
- });
102
- }
103
- });
104
- });
105
- //# sourceMappingURL=compareArrays.bench.spec.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"compareArrays.bench.spec.js","sourceRoot":"","sources":["../../../src/test/bench/compareArrays.bench.spec.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAClE,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,MAAM,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC7C,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAEnD,MAAM,WAAW,GAAqC;IACrD,CAAC,mCAAmC,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC5D,CAAC,iCAAiC,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* Licensed under the MIT License.\n */\n\nimport { benchmark, BenchmarkType } from \"@fluid-tools/benchmark\";\nimport { compareArrays } from \"@fluidframework/core-utils\";\n\nconst a4 = Array.from({ length: 4 }).fill(0);\nconst a1024 = Array.from({ length: 1024 }).fill(0);\n\nconst comparisons: [string, unknown[], unknown[]][] = [\n\t[\"trivial rejection based on length\", a1024, a1024.slice(1)],\n\t[\"trivial acceptance based on ref\", a1024, a1024],\n\t[\"compare empty\", [], []],\n\t[`compare ${a4.length} items`, [...a4], [...a4]],\n\t[`compare ${a1024.length} items`, [...a1024], [...a1024]],\n];\n\nfunction compareWithFor<T>(left: readonly T[], right: readonly T[]): boolean {\n\tif (left.length !== right.length) {\n\t\treturn false;\n\t}\n\n\tfor (let index = 0; index < left.length; index++) {\n\t\tif (left[index] !== right[index]) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\nfunction compareWithEvery<T>(left: readonly T[], right: readonly T[]): boolean {\n\treturn (\n\t\tleft.length === right.length && left.every((leftItem, index) => leftItem === right[index])\n\t);\n}\n\nfunction compareWithObjectIs<T>(left: readonly T[], right: readonly T[]): boolean {\n\treturn (\n\t\tleft.length === right.length &&\n\t\tleft.every((leftItem, index) => Object.is(leftItem, right[index]))\n\t);\n}\n\ndescribe(\"compareArrays()\", () => {\n\tdescribe.skip(\"baseline\", () => {\n\t\tdescribe(\"using for-loop\", () => {\n\t\t\tfor (const [title, left, right] of comparisons) {\n\t\t\t\tbenchmark({\n\t\t\t\t\ttype: BenchmarkType.Measurement,\n\t\t\t\t\ttitle: `${title}`,\n\t\t\t\t\tbefore: () => {},\n\t\t\t\t\tbenchmarkFn: () => {\n\t\t\t\t\t\tcompareWithFor(left, right);\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\n\t\tdescribe(\"using Array.every()\", () => {\n\t\t\tfor (const [title, left, right] of comparisons) {\n\t\t\t\tbenchmark({\n\t\t\t\t\ttype: BenchmarkType.Measurement,\n\t\t\t\t\ttitle: `${title}`,\n\t\t\t\t\tbefore: () => {},\n\t\t\t\t\tbenchmarkFn: () => {\n\t\t\t\t\t\tcompareWithEvery(left, right);\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\n\t\tdescribe(\"using Object.is()\", () => {\n\t\t\tfor (const [title, left, right] of comparisons) {\n\t\t\t\tbenchmark({\n\t\t\t\t\ttype: BenchmarkType.Measurement,\n\t\t\t\t\ttitle: `${title}`,\n\t\t\t\t\tbefore: () => {},\n\t\t\t\t\tbenchmarkFn: () => {\n\t\t\t\t\t\tcompareWithObjectIs(left, right);\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t});\n\n\tdescribe(\"no callback\", () => {\n\t\tfor (const [title, left, right] of comparisons) {\n\t\t\tbenchmark({\n\t\t\t\ttype: BenchmarkType.Measurement,\n\t\t\t\ttitle: `${title}`,\n\t\t\t\tbefore: () => {},\n\t\t\t\tbenchmarkFn: () => {\n\t\t\t\t\tcompareArrays(left, right);\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\t});\n\n\tdescribe(\"with callback\", () => {\n\t\tlet sum = 0;\n\n\t\tfor (const [title, left, right] of comparisons) {\n\t\t\tbenchmark({\n\t\t\t\ttype: BenchmarkType.Measurement,\n\t\t\t\ttitle: `${title}`,\n\n\t\t\t\tbenchmarkFn: () => {\n\t\t\t\t\tcompareArrays(left, right, (leftItem, rightItem, index) => {\n\t\t\t\t\t\tsum += index;\n\t\t\t\t\t\treturn Object.is(leftItem, rightItem);\n\t\t\t\t\t});\n\t\t\t\t},\n\n\t\t\t\tafter: () => {\n\t\t\t\t\t// Paranoid usage of 'sum' to prevent dead code optimization.\n\t\t\t\t\tconsole.log(`after: ${sum}`);\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\t});\n});\n"]}
@@ -1,43 +0,0 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
- import { strict as assert } from "node:assert";
6
- import { compareArrays } from "@fluidframework/core-utils";
7
- const o = { o: "o" };
8
- const s = Symbol("s");
9
- const tests = [
10
- [[], [], true],
11
- [[0], [], false],
12
- [[], [0], false],
13
- [[0], [0], true],
14
- [[1], [0], false],
15
- [[0], [1], false],
16
- [[0, 1], [0], false],
17
- [[0], [0, 1], false],
18
- [[0, 1], [0, 2], false],
19
- [[0, 1], [-1, 1], false],
20
- [[0, 1], [0, 1], true],
21
- // Object.is() semantics:
22
- [[Number.NaN], [Number.NaN], true],
23
- [[0], [-0], false],
24
- // eslint-disable-next-line unicorn/no-null
25
- [[null], [undefined], false],
26
- [[""], [0], false],
27
- [[{}], [{}], false],
28
- [[o], [o], true],
29
- [[Symbol("sl")], [Symbol("sr")], false],
30
- [[s], [s], true],
31
- ];
32
- describe("compareArrays()", () => {
33
- function check(left, right, expected) {
34
- it(`${JSON.stringify(left)} and ${JSON.stringify(right)} must ${expected ? "" : "not "}be equal`, () => {
35
- const actual = compareArrays(left, right);
36
- assert.equal(actual, expected);
37
- });
38
- }
39
- for (const [left, right, expected] of tests) {
40
- check(left, right, expected);
41
- }
42
- });
43
- //# sourceMappingURL=compareArrays.spec.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"compareArrays.spec.js","sourceRoot":"","sources":["../../src/test/compareArrays.spec.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,IAAI,MAAM,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC;AACrB,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;AAEtB,MAAM,KAAK,GAAsC;IAChD,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC;IACd,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC;IAChB,CAAC,EAA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,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,KAAK,CAAC;IACvC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC;CAChB,CAAC;AAEF,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAChC,SAAS,KAAK,CAAI,IAAS,EAAE,KAAU,EAAE,QAAiB;QACzD,EAAE,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,SACtD,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MACjB,UAAU,EAAE,GAAG,EAAE;YAChB,MAAM,MAAM,GAAG,aAAa,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YAE1C,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAChC,CAAC,CAAC,CAAC;IACJ,CAAC;IAED,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,EAAE,QAAQ,CAAC,IAAI,KAAK,EAAE;QAC5C,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;KAC7B;AACF,CAAC,CAAC,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { strict as assert } from \"node:assert\";\nimport { compareArrays } from \"@fluidframework/core-utils\";\n\nconst o = { o: \"o\" };\nconst s = Symbol(\"s\");\n\nconst tests: [unknown[], unknown[], boolean][] = [\n\t[[], [], true],\n\t[[0], [], false],\n\t[[], [0], false],\n\t[[0], [0], true],\n\t[[1], [0], false],\n\t[[0], [1], false],\n\n\t[[0, 1], [0], false],\n\t[[0], [0, 1], false],\n\t[[0, 1], [0, 2], false],\n\t[[0, 1], [-1, 1], false],\n\t[[0, 1], [0, 1], true],\n\n\t// Object.is() semantics:\n\t[[Number.NaN], [Number.NaN], true],\n\t[[0], [-0], false],\n\n\t// eslint-disable-next-line unicorn/no-null\n\t[[null], [undefined], false],\n\t[[\"\"], [0], false],\n\t[[{}], [{}], false],\n\t[[o], [o], true],\n\t[[Symbol(\"sl\")], [Symbol(\"sr\")], false],\n\t[[s], [s], true],\n];\n\ndescribe(\"compareArrays()\", () => {\n\tfunction check<T>(left: T[], right: T[], expected: boolean): void {\n\t\tit(`${JSON.stringify(left)} and ${JSON.stringify(right)} must ${\n\t\t\texpected ? \"\" : \"not \"\n\t\t}be equal`, () => {\n\t\t\tconst actual = compareArrays(left, right);\n\n\t\t\tassert.equal(actual, expected);\n\t\t});\n\t}\n\n\tfor (const [left, right, expected] of tests) {\n\t\tcheck(left, right, expected);\n\t}\n});\n"]}