@elyukai/utils 0.1.2 → 0.1.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.
- package/CHANGELOG.md +2 -0
- package/README.md +14 -0
- package/dist/array/filters.d.ts +4 -0
- package/dist/array/filters.js +4 -0
- package/dist/array/generators.d.ts +4 -0
- package/dist/array/generators.js +4 -0
- package/dist/array/groups.d.ts +4 -0
- package/dist/array/groups.js +4 -0
- package/dist/array/modify.d.ts +4 -0
- package/dist/array/modify.js +4 -0
- package/dist/array/nonEmpty.d.ts +1 -0
- package/dist/array/nonEmpty.js +1 -0
- package/dist/array/reductions.d.ts +4 -0
- package/dist/array/reductions.js +4 -0
- package/dist/array/sets.d.ts +4 -0
- package/dist/array/sets.js +4 -0
- package/dist/async.d.ts +4 -0
- package/dist/async.js +4 -0
- package/dist/classList.d.ts +4 -0
- package/dist/classList.js +4 -0
- package/dist/dictionary/native.d.ts +1 -0
- package/dist/dictionary/native.js +1 -0
- package/dist/dictionary.d.ts +2 -2
- package/dist/dictionary.js +2 -2
- package/dist/equality.d.ts +4 -0
- package/dist/equality.js +4 -0
- package/dist/function.d.ts +4 -0
- package/dist/function.js +4 -0
- package/dist/lazy.d.ts +4 -0
- package/dist/lazy.js +4 -0
- package/dist/maybe.d.ts +4 -0
- package/dist/maybe.js +4 -0
- package/dist/nullable.d.ts +4 -0
- package/dist/nullable.js +4 -0
- package/dist/number.d.ts +4 -0
- package/dist/number.js +4 -0
- package/dist/object.d.ts +4 -0
- package/dist/object.js +4 -0
- package/dist/ordering.d.ts +4 -0
- package/dist/ordering.js +4 -0
- package/dist/range.d.ts +4 -0
- package/dist/range.js +4 -0
- package/dist/result.d.ts +4 -0
- package/dist/result.js +4 -0
- package/dist/roman.d.ts +4 -0
- package/dist/roman.js +4 -0
- package/dist/string/number.d.ts +4 -0
- package/dist/string/number.js +4 -0
- package/dist/string/regex.d.ts +4 -0
- package/dist/string/regex.js +4 -0
- package/dist/string.d.ts +4 -0
- package/dist/string.js +4 -0
- package/dist/typeSafety.d.ts +4 -0
- package/dist/typeSafety.js +4 -0
- package/package.json +2 -1
- package/dist/array.d.ts +0 -4
- package/dist/array.js +0 -5
- package/dist/compare.d.ts +0 -44
- package/dist/compare.js +0 -73
- package/dist/date.d.ts +0 -5
- package/dist/date.js +0 -5
- package/dist/math.d.ts +0 -47
- package/dist/math.js +0 -56
- package/dist/regex.d.ts +0 -28
- package/dist/regex.js +0 -35
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [0.1.3](https://github.com/elyukai/ts-utils/compare/v0.1.2...v0.1.3) (2026-01-24)
|
|
6
|
+
|
|
5
7
|
## [0.1.2](https://github.com/elyukai/ts-utils/compare/v0.1.1...v0.1.2) (2026-01-24)
|
|
6
8
|
|
|
7
9
|
|
package/README.md
CHANGED
|
@@ -1 +1,15 @@
|
|
|
1
1
|
# General TypeScript Helpers
|
|
2
|
+
|
|
3
|
+
This package provides a range of utility functions that I personally often need, which is why I bundled them in a package.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { isNotEmpty } from "@elyukai/utils/array/nonEmpty"
|
|
7
|
+
|
|
8
|
+
const extractFirstNumberDoubled = (arr: number[]): number | undefined => {
|
|
9
|
+
if (isNotEmpty(arr)) {
|
|
10
|
+
return arr[0] * 2 // accessing index 0 is safe!
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return undefined
|
|
14
|
+
}
|
|
15
|
+
```
|
package/dist/array/filters.d.ts
CHANGED
package/dist/array/filters.js
CHANGED
package/dist/array/generators.js
CHANGED
package/dist/array/groups.d.ts
CHANGED
package/dist/array/groups.js
CHANGED
package/dist/array/modify.d.ts
CHANGED
package/dist/array/modify.js
CHANGED
package/dist/array/nonEmpty.d.ts
CHANGED
package/dist/array/nonEmpty.js
CHANGED
package/dist/array/reductions.js
CHANGED
package/dist/array/sets.d.ts
CHANGED
package/dist/array/sets.js
CHANGED
package/dist/async.d.ts
CHANGED
package/dist/async.js
CHANGED
package/dist/classList.d.ts
CHANGED
package/dist/classList.js
CHANGED
package/dist/dictionary.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Introduces a Dictionary class that represents an immutable mapping from
|
|
3
|
-
*
|
|
2
|
+
* Introduces a Dictionary class that represents an immutable mapping from strings to values.
|
|
3
|
+
* @module
|
|
4
4
|
*/
|
|
5
5
|
import type { AnyNonNullish } from "./nullable.js";
|
|
6
6
|
/**
|
package/dist/dictionary.js
CHANGED
package/dist/equality.d.ts
CHANGED
package/dist/equality.js
CHANGED
package/dist/function.d.ts
CHANGED
package/dist/function.js
CHANGED
package/dist/lazy.d.ts
CHANGED
package/dist/lazy.js
CHANGED
package/dist/maybe.d.ts
CHANGED
package/dist/maybe.js
CHANGED
package/dist/nullable.d.ts
CHANGED
package/dist/nullable.js
CHANGED
package/dist/number.d.ts
CHANGED
package/dist/number.js
CHANGED
package/dist/object.d.ts
CHANGED
package/dist/object.js
CHANGED
package/dist/ordering.d.ts
CHANGED
package/dist/ordering.js
CHANGED
package/dist/range.d.ts
CHANGED
package/dist/range.js
CHANGED
package/dist/result.d.ts
CHANGED
package/dist/result.js
CHANGED
package/dist/roman.d.ts
CHANGED
package/dist/roman.js
CHANGED
package/dist/string/number.d.ts
CHANGED
package/dist/string/number.js
CHANGED
package/dist/string/regex.d.ts
CHANGED
package/dist/string/regex.js
CHANGED
package/dist/string.d.ts
CHANGED
package/dist/string.js
CHANGED
package/dist/typeSafety.d.ts
CHANGED
package/dist/typeSafety.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elyukai/utils",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "A set of JavaScript helper functions.",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"@types/node": "^25.0.10",
|
|
37
37
|
"commit-and-tag-version": "^12.6.1",
|
|
38
38
|
"eslint": "^9.39.2",
|
|
39
|
+
"eslint-plugin-jsdoc": "^62.4.1",
|
|
39
40
|
"glob": "^13.0.0",
|
|
40
41
|
"prettier": "^3.8.1",
|
|
41
42
|
"tsx": "^4.21.0",
|
package/dist/array.d.ts
DELETED
package/dist/array.js
DELETED
package/dist/compare.d.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The type of a compare function that can be used to sort values.
|
|
3
|
-
* @returns A negative number if `a` should be sorted before `b`, a positive
|
|
4
|
-
* number if `a` should be sorted after `b`, or zero if `a` and `b` are equal.
|
|
5
|
-
*/
|
|
6
|
-
export type Compare<T> = (a: T, b: T) => number;
|
|
7
|
-
/**
|
|
8
|
-
* Build a compare function for values that are nested inside other values. The
|
|
9
|
-
* nested value is getting extracted by the provided accessor function and then
|
|
10
|
-
* compared using the provided compare function. An optional `reverse` parameter
|
|
11
|
-
* can be used to reverse the sort order.
|
|
12
|
-
*/
|
|
13
|
-
export declare const compareAt: <T, U>(accessor: (value: T) => U, compare: (a: U, b: U) => number, reverse?: boolean) => Compare<T>;
|
|
14
|
-
/**
|
|
15
|
-
* Build a compare function that nests multiple compare functions. The functions
|
|
16
|
-
* are applied in order, so the first function is the primary sort key, the
|
|
17
|
-
* second function is the secondary sort key, and so on.
|
|
18
|
-
*/
|
|
19
|
-
export declare const reduceCompare: <T>(...compares: Compare<T>[]) => Compare<T>;
|
|
20
|
-
/**
|
|
21
|
-
* Compare function for numbers that sorts them in ascending order.
|
|
22
|
-
*/
|
|
23
|
-
export declare const numAsc: Compare<number>;
|
|
24
|
-
/**
|
|
25
|
-
* Higher-order compare function that extends a compare function to also handle
|
|
26
|
-
* `null` and `undefined` values. Nullish values are always sorted first.
|
|
27
|
-
*/
|
|
28
|
-
export declare const compareNullish: <T extends NonNullable<unknown>>(compare: Compare<T>) => (a: T | null | undefined, b: T | null | undefined) => number;
|
|
29
|
-
/**
|
|
30
|
-
* A function that compares two values for equality.
|
|
31
|
-
*/
|
|
32
|
-
export type Equality<T> = (a: T, b: T) => boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Build an equality function for values that are nested inside other values.
|
|
35
|
-
* The nested value is getting extracted by the provided accessor function and
|
|
36
|
-
* then compared using the provided equality function.
|
|
37
|
-
*/
|
|
38
|
-
export declare const equalityAt: <T, U>(accessor: (value: T) => U, equality: Equality<U>) => Equality<T>;
|
|
39
|
-
/**
|
|
40
|
-
* Checks two values for value equality. This is a deep equality check that
|
|
41
|
-
* works for all types, including objects and arrays. For objects, it only
|
|
42
|
-
* compares all enumerable keys, no other properties or the prototype chain.
|
|
43
|
-
*/
|
|
44
|
-
export declare const deepEqual: <T>(a: T, b: T) => boolean;
|
package/dist/compare.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { isNullish } from "./nullable.js";
|
|
2
|
-
/**
|
|
3
|
-
* Build a compare function for values that are nested inside other values. The
|
|
4
|
-
* nested value is getting extracted by the provided accessor function and then
|
|
5
|
-
* compared using the provided compare function. An optional `reverse` parameter
|
|
6
|
-
* can be used to reverse the sort order.
|
|
7
|
-
*/
|
|
8
|
-
export const compareAt = (accessor, compare, reverse = false) => (a, b) => {
|
|
9
|
-
const result = compare(accessor(a), accessor(b));
|
|
10
|
-
return reverse ? -result : result;
|
|
11
|
-
};
|
|
12
|
-
/**
|
|
13
|
-
* Build a compare function that nests multiple compare functions. The functions
|
|
14
|
-
* are applied in order, so the first function is the primary sort key, the
|
|
15
|
-
* second function is the secondary sort key, and so on.
|
|
16
|
-
*/
|
|
17
|
-
export const reduceCompare = (...compares) => (a, b) => {
|
|
18
|
-
for (const compare of compares) {
|
|
19
|
-
const result = compare(a, b);
|
|
20
|
-
if (result !== 0) {
|
|
21
|
-
return result;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return 0;
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* Compare function for numbers that sorts them in ascending order.
|
|
28
|
-
*/
|
|
29
|
-
export const numAsc = (a, b) => a - b;
|
|
30
|
-
/**
|
|
31
|
-
* Higher-order compare function that extends a compare function to also handle
|
|
32
|
-
* `null` and `undefined` values. Nullish values are always sorted first.
|
|
33
|
-
*/
|
|
34
|
-
export const compareNullish = (compare) => (a, b) => {
|
|
35
|
-
if (isNullish(a) && isNullish(b)) {
|
|
36
|
-
return 0;
|
|
37
|
-
}
|
|
38
|
-
if (isNullish(a)) {
|
|
39
|
-
return -1;
|
|
40
|
-
}
|
|
41
|
-
if (isNullish(b)) {
|
|
42
|
-
return 1;
|
|
43
|
-
}
|
|
44
|
-
return compare(a, b);
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Build an equality function for values that are nested inside other values.
|
|
48
|
-
* The nested value is getting extracted by the provided accessor function and
|
|
49
|
-
* then compared using the provided equality function.
|
|
50
|
-
*/
|
|
51
|
-
export const equalityAt = (accessor, equality) => (a, b) => equality(accessor(a), accessor(b));
|
|
52
|
-
/**
|
|
53
|
-
* Checks two values for value equality. This is a deep equality check that
|
|
54
|
-
* works for all types, including objects and arrays. For objects, it only
|
|
55
|
-
* compares all enumerable keys, no other properties or the prototype chain.
|
|
56
|
-
*/
|
|
57
|
-
export const deepEqual = (a, b) => {
|
|
58
|
-
if (a === b) {
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
if (typeof a === "object" &&
|
|
62
|
-
typeof b === "object" &&
|
|
63
|
-
a !== null &&
|
|
64
|
-
b !== null) {
|
|
65
|
-
const keys = Object.keys(a);
|
|
66
|
-
if (keys.length !== Object.keys(b).length) {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
return keys.every((key) => key in b &&
|
|
70
|
-
deepEqual(a[key], b[key]));
|
|
71
|
-
}
|
|
72
|
-
return false;
|
|
73
|
-
};
|
package/dist/date.d.ts
DELETED
package/dist/date.js
DELETED
package/dist/math.d.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The minus sign.
|
|
3
|
-
*/
|
|
4
|
-
export declare const minus = "\u2212";
|
|
5
|
-
/**
|
|
6
|
-
* The plus/minus sign.
|
|
7
|
-
*/
|
|
8
|
-
export declare const plusMinus = "\u00B1";
|
|
9
|
-
/**
|
|
10
|
-
* Forces signing on the given number, returning `undefined` on zero.
|
|
11
|
-
*/
|
|
12
|
-
export declare const signIgnoreZero: (x: number) => string | undefined;
|
|
13
|
-
/**
|
|
14
|
-
* Forces signing on the given number.
|
|
15
|
-
*/
|
|
16
|
-
export declare const sign: (x: number) => string;
|
|
17
|
-
/**
|
|
18
|
-
* Returns the sign of the given number. Returns `undefined` if the number is
|
|
19
|
-
* zero.
|
|
20
|
-
*/
|
|
21
|
-
export declare const signStr: (x: number) => string | undefined;
|
|
22
|
-
/**
|
|
23
|
-
* Converts a string to an integer. If the string is not a valid integer, it
|
|
24
|
-
* returns `undefined`.
|
|
25
|
-
*/
|
|
26
|
-
export declare const parseInt: (str: string) => number | undefined;
|
|
27
|
-
/**
|
|
28
|
-
* Converts a string to a natural number. If the string is not a valid natural
|
|
29
|
-
* number, it returns `undefined`.
|
|
30
|
-
*/
|
|
31
|
-
export declare const parseNat: (str: string) => number | undefined;
|
|
32
|
-
/**
|
|
33
|
-
* Returns a random integer between `0` and `max` (inclusive).
|
|
34
|
-
*/
|
|
35
|
-
export declare const randomInt: (max: number) => number;
|
|
36
|
-
/**
|
|
37
|
-
* Returns a random integer between `min` and `max` (inclusive).
|
|
38
|
-
*/
|
|
39
|
-
export declare const randomIntRange: (min: number, max: number) => number;
|
|
40
|
-
/**
|
|
41
|
-
* Returns if the given number is even.
|
|
42
|
-
*/
|
|
43
|
-
export declare const even: (x: number) => boolean;
|
|
44
|
-
/**
|
|
45
|
-
* Returns if the given number is odd.
|
|
46
|
-
*/
|
|
47
|
-
export declare const odd: (x: number) => boolean;
|
package/dist/math.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { isInteger, isNaturalNumber } from "./regex.js";
|
|
2
|
-
/**
|
|
3
|
-
* The minus sign.
|
|
4
|
-
*/
|
|
5
|
-
export const minus = "−";
|
|
6
|
-
/**
|
|
7
|
-
* The plus/minus sign.
|
|
8
|
-
*/
|
|
9
|
-
export const plusMinus = "\xB1";
|
|
10
|
-
/**
|
|
11
|
-
* Forces signing on the given number, returning `undefined` on zero.
|
|
12
|
-
*/
|
|
13
|
-
export const signIgnoreZero = (x) => x > 0
|
|
14
|
-
? `+${x.toString()}`
|
|
15
|
-
: x < 0
|
|
16
|
-
? `${minus}\u2060${Math.abs(x).toString()}`
|
|
17
|
-
: undefined;
|
|
18
|
-
/**
|
|
19
|
-
* Forces signing on the given number.
|
|
20
|
-
*/
|
|
21
|
-
export const sign = (x) => x > 0
|
|
22
|
-
? `+${x.toString()}`
|
|
23
|
-
: x < 0
|
|
24
|
-
? `${minus}\u2060${Math.abs(x).toString()}`
|
|
25
|
-
: "0";
|
|
26
|
-
/**
|
|
27
|
-
* Returns the sign of the given number. Returns `undefined` if the number is
|
|
28
|
-
* zero.
|
|
29
|
-
*/
|
|
30
|
-
export const signStr = (x) => x > 0 ? "+" : x < 0 ? minus : undefined;
|
|
31
|
-
/**
|
|
32
|
-
* Converts a string to an integer. If the string is not a valid integer, it
|
|
33
|
-
* returns `undefined`.
|
|
34
|
-
*/
|
|
35
|
-
export const parseInt = (str) => str.length > 0 && isInteger(str) ? Number.parseInt(str, 10) : undefined;
|
|
36
|
-
/**
|
|
37
|
-
* Converts a string to a natural number. If the string is not a valid natural
|
|
38
|
-
* number, it returns `undefined`.
|
|
39
|
-
*/
|
|
40
|
-
export const parseNat = (str) => str.length > 0 && isNaturalNumber(str) ? Number.parseInt(str, 10) : undefined;
|
|
41
|
-
/**
|
|
42
|
-
* Returns a random integer between `0` and `max` (inclusive).
|
|
43
|
-
*/
|
|
44
|
-
export const randomInt = (max) => Math.floor(Math.random() * (max + 1));
|
|
45
|
-
/**
|
|
46
|
-
* Returns a random integer between `min` and `max` (inclusive).
|
|
47
|
-
*/
|
|
48
|
-
export const randomIntRange = (min, max) => Math.floor(Math.random() * (max + 1 - min)) + min;
|
|
49
|
-
/**
|
|
50
|
-
* Returns if the given number is even.
|
|
51
|
-
*/
|
|
52
|
-
export const even = (x) => x % 2 === 0;
|
|
53
|
-
/**
|
|
54
|
-
* Returns if the given number is odd.
|
|
55
|
-
*/
|
|
56
|
-
export const odd = (x) => x % 2 !== 0;
|
package/dist/regex.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks if the provided string is a string representation of a natural number.
|
|
3
|
-
* @param test The string to test.
|
|
4
|
-
*/
|
|
5
|
-
export declare const isNaturalNumber: (test: string) => boolean;
|
|
6
|
-
/**
|
|
7
|
-
* Checks if the provided string is a string representation of an integer.
|
|
8
|
-
* @param test The string to test.
|
|
9
|
-
*/
|
|
10
|
-
export declare const isInteger: (test: string) => boolean;
|
|
11
|
-
/**
|
|
12
|
-
* Checks if the provided string is a string representation of a floating-point
|
|
13
|
-
* number. Both `.` and `,` are accepted as decimal separators.
|
|
14
|
-
* @param test The string to test.
|
|
15
|
-
*/
|
|
16
|
-
export declare const isFloat: (test: string) => boolean;
|
|
17
|
-
/**
|
|
18
|
-
* Checks if the provided string either is an empty string or passes the given
|
|
19
|
-
* test function.
|
|
20
|
-
* @param check The test function to apply if the string is not empty.
|
|
21
|
-
* @param test The string to test.
|
|
22
|
-
*/
|
|
23
|
-
export declare const isEmptyOr: (check: (test: string) => boolean, test: string) => boolean;
|
|
24
|
-
/**
|
|
25
|
-
* Checks if the provided string is a valid URL.
|
|
26
|
-
* @param test The string to test.
|
|
27
|
-
*/
|
|
28
|
-
export declare const isUrl: (test: string) => boolean;
|
package/dist/regex.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks if the provided string is a string representation of a natural number.
|
|
3
|
-
* @param test The string to test.
|
|
4
|
-
*/
|
|
5
|
-
export const isNaturalNumber = (test) => /^(?:0|[1-9][0-9]*)$/u.test(test);
|
|
6
|
-
/**
|
|
7
|
-
* Checks if the provided string is a string representation of an integer.
|
|
8
|
-
* @param test The string to test.
|
|
9
|
-
*/
|
|
10
|
-
export const isInteger = (test) => /^(?:0|-?[1-9][0-9]*)$/u.test(test);
|
|
11
|
-
/**
|
|
12
|
-
* Checks if the provided string is a string representation of a floating-point
|
|
13
|
-
* number. Both `.` and `,` are accepted as decimal separators.
|
|
14
|
-
* @param test The string to test.
|
|
15
|
-
*/
|
|
16
|
-
export const isFloat = (test) => /^(?:(?:0|-?[1-9][0-9]*)(?:[.,][0-9]+)?)$/u.test(test);
|
|
17
|
-
/**
|
|
18
|
-
* Checks if the provided string either is an empty string or passes the given
|
|
19
|
-
* test function.
|
|
20
|
-
* @param check The test function to apply if the string is not empty.
|
|
21
|
-
* @param test The string to test.
|
|
22
|
-
*/
|
|
23
|
-
export const isEmptyOr = (check, test) => test === "" || check(test);
|
|
24
|
-
/**
|
|
25
|
-
* Checks if the provided string is a valid URL.
|
|
26
|
-
* @param test The string to test.
|
|
27
|
-
*/
|
|
28
|
-
export const isUrl = (test) => {
|
|
29
|
-
try {
|
|
30
|
-
return typeof new URL(test) === "object";
|
|
31
|
-
}
|
|
32
|
-
catch {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
};
|