@guillaume-docquier/tools-ts 8.6.1 → 9.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.
- package/README.md +4 -3
- package/dist/Sort.d.ts +20 -9
- package/dist/Sort.js +36 -14
- package/dist/Sort.js.map +1 -1
- package/dist/entry.tools-ts.d.ts +1 -1
- package/dist/entry.tools-ts.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,9 +49,10 @@ intervals.
|
|
|
49
49
|
|
|
50
50
|
Use this tool when code needs shared ordering helpers.
|
|
51
51
|
|
|
52
|
-
| Export
|
|
53
|
-
|
|
|
54
|
-
| `Sort`
|
|
52
|
+
| Export | What it is | Use it when |
|
|
53
|
+
| ------------ | ------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------- |
|
|
54
|
+
| `Sort` | Direction-aware numeric and property comparators, plus comparator composition. | You want a shared sorting utility instead of rewriting compare callbacks at call sites. |
|
|
55
|
+
| `Comparator` | Helper type to define comparators compatible with the Sort utilities. | You define sorting comparators. |
|
|
55
56
|
|
|
56
57
|
### Randomness
|
|
57
58
|
|
package/dist/Sort.d.ts
CHANGED
|
@@ -1,18 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
declare enum SortDirection {
|
|
2
|
+
/**
|
|
3
|
+
* From smallest to biggest.
|
|
4
|
+
*/
|
|
5
|
+
Ascending = "Ascending",
|
|
2
6
|
/**
|
|
3
|
-
*
|
|
7
|
+
* From biggest to smallest.
|
|
4
8
|
*/
|
|
5
|
-
|
|
9
|
+
Descending = "Descending"
|
|
10
|
+
}
|
|
11
|
+
export type Comparator<T> = (first: T, second: T) => number;
|
|
12
|
+
export declare const Sort: {
|
|
13
|
+
Direction: typeof SortDirection;
|
|
6
14
|
/**
|
|
7
|
-
*
|
|
15
|
+
* Composes comparators.
|
|
8
16
|
*/
|
|
9
|
-
|
|
17
|
+
compose: <T>(...comparators: ReadonlyArray<Comparator<T>>) => Comparator<T>;
|
|
10
18
|
/**
|
|
11
|
-
*
|
|
19
|
+
* Compares numbers in the selected direction.
|
|
20
|
+
* Defaults to Ascending.
|
|
12
21
|
*/
|
|
13
|
-
|
|
22
|
+
numeric: (direction?: SortDirection) => Comparator<number>;
|
|
14
23
|
/**
|
|
15
|
-
*
|
|
24
|
+
* Compares records by a numeric property in the selected direction.
|
|
25
|
+
* Defaults to Ascending.
|
|
16
26
|
*/
|
|
17
|
-
|
|
27
|
+
byNumericProperty: <TProperty extends PropertyKey>(property: TProperty, direction?: SortDirection) => <TRecord extends Record<TProperty, number>>(first: TRecord, second: TRecord) => number;
|
|
18
28
|
};
|
|
29
|
+
export {};
|
package/dist/Sort.js
CHANGED
|
@@ -1,27 +1,49 @@
|
|
|
1
|
-
|
|
1
|
+
var SortDirection;
|
|
2
|
+
(function (SortDirection) {
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
+
* From smallest to biggest.
|
|
4
5
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
SortDirection["Ascending"] = "Ascending";
|
|
7
|
+
/**
|
|
8
|
+
* From biggest to smallest.
|
|
9
|
+
*/
|
|
10
|
+
SortDirection["Descending"] = "Descending";
|
|
11
|
+
})(SortDirection || (SortDirection = {}));
|
|
12
|
+
export const Sort = {
|
|
13
|
+
Direction: SortDirection,
|
|
8
14
|
/**
|
|
9
|
-
*
|
|
15
|
+
* Composes comparators.
|
|
10
16
|
*/
|
|
11
|
-
|
|
12
|
-
return
|
|
17
|
+
compose: (...comparators) => {
|
|
18
|
+
return (first, second) => {
|
|
19
|
+
for (const comparator of comparators) {
|
|
20
|
+
const comparison = comparator(first, second);
|
|
21
|
+
if (comparison !== 0) {
|
|
22
|
+
return comparison;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return 0;
|
|
26
|
+
};
|
|
13
27
|
},
|
|
14
28
|
/**
|
|
15
|
-
*
|
|
29
|
+
* Compares numbers in the selected direction.
|
|
30
|
+
* Defaults to Ascending.
|
|
16
31
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
32
|
+
numeric: (direction = SortDirection.Ascending) => {
|
|
33
|
+
switch (direction) {
|
|
34
|
+
case SortDirection.Ascending:
|
|
35
|
+
return (first, second) => first - second;
|
|
36
|
+
case SortDirection.Descending:
|
|
37
|
+
return (first, second) => second - first;
|
|
38
|
+
}
|
|
19
39
|
},
|
|
20
40
|
/**
|
|
21
|
-
*
|
|
41
|
+
* Compares records by a numeric property in the selected direction.
|
|
42
|
+
* Defaults to Ascending.
|
|
22
43
|
*/
|
|
23
|
-
|
|
24
|
-
|
|
44
|
+
byNumericProperty: (property, direction = SortDirection.Ascending) => {
|
|
45
|
+
const compare = Sort.numeric(direction);
|
|
46
|
+
return (first, second) => compare(first[property], second[property]);
|
|
25
47
|
},
|
|
26
48
|
};
|
|
27
49
|
//# sourceMappingURL=Sort.js.map
|
package/dist/Sort.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Sort.js","sourceRoot":"","sources":["../src/Sort.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"Sort.js","sourceRoot":"","sources":["../src/Sort.ts"],"names":[],"mappings":"AAAA,IAAK,aAUJ;AAVD,WAAK,aAAa;IAChB;;OAEG;IACH,wCAAuB,CAAA;IAEvB;;OAEG;IACH,0CAAyB,CAAA;AAC3B,CAAC,EAVI,aAAa,KAAb,aAAa,QAUjB;AAID,MAAM,CAAC,MAAM,IAAI,GAAG;IAClB,SAAS,EAAE,aAAa;IAExB;;OAEG;IACH,OAAO,EAAE,CAAI,GAAG,WAAyC,EAAiB,EAAE;QAC1E,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;YACvB,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE,CAAC;gBACrC,MAAM,UAAU,GAAG,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;gBAC5C,IAAI,UAAU,KAAK,CAAC,EAAE,CAAC;oBACrB,OAAO,UAAU,CAAA;gBACnB,CAAC;YACH,CAAC;YAED,OAAO,CAAC,CAAA;QACV,CAAC,CAAA;IACH,CAAC;IAED;;;OAGG;IACH,OAAO,EAAE,CAAC,SAAS,GAAkB,aAAa,CAAC,SAAS,EAAsB,EAAE;QAClF,QAAQ,SAAS,EAAE,CAAC;YAClB,KAAK,aAAa,CAAC,SAAS;gBAC1B,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,CAAC,KAAK,GAAG,MAAM,CAAA;YAC1C,KAAK,aAAa,CAAC,UAAU;gBAC3B,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,CAAC,MAAM,GAAG,KAAK,CAAA;QAC5C,CAAC;IACH,CAAC;IAED;;;OAGG;IACH,iBAAiB,EAAE,CAAgC,QAAmB,EAAE,SAAS,GAAkB,aAAa,CAAC,SAAS,EAAE,EAAE;QAC5H,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;QACvC,OAAO,CAA4C,KAAc,EAAE,MAAe,EAAU,EAAE,CAC5F,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAA;IAC9C,CAAC;CACF,CAAA"}
|
package/dist/entry.tools-ts.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export type { LogContext } from "./logging/log-context/LogContext.js";
|
|
|
25
25
|
export type { LoggerContext } from "./logging/log-context/LoggerContext.js";
|
|
26
26
|
export type { LogContextProvider } from "./logging/log-context/LogContextProvider.js";
|
|
27
27
|
export { debounce } from "./debounce.js";
|
|
28
|
-
export { Sort } from "./Sort.js";
|
|
28
|
+
export { Sort, type Comparator } from "./Sort.js";
|
|
29
29
|
export { Rng, type RngState } from "./rng/Rng.js";
|
|
30
30
|
export type { Generator } from "./rng/Generator.js";
|
|
31
31
|
export { createGeneratorStub } from "./rng/Generator.stub.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{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