@elyukai/utils 0.1.2

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 (67) hide show
  1. package/AUTHORS +1 -0
  2. package/CHANGELOG.md +46 -0
  3. package/LICENSE +373 -0
  4. package/README.md +1 -0
  5. package/dist/array/filters.d.ts +14 -0
  6. package/dist/array/filters.js +24 -0
  7. package/dist/array/generators.d.ts +9 -0
  8. package/dist/array/generators.js +13 -0
  9. package/dist/array/groups.d.ts +24 -0
  10. package/dist/array/groups.js +45 -0
  11. package/dist/array/modify.d.ts +21 -0
  12. package/dist/array/modify.js +57 -0
  13. package/dist/array/nonEmpty.d.ts +23 -0
  14. package/dist/array/nonEmpty.js +15 -0
  15. package/dist/array/reductions.d.ts +39 -0
  16. package/dist/array/reductions.js +58 -0
  17. package/dist/array/sets.d.ts +17 -0
  18. package/dist/array/sets.js +19 -0
  19. package/dist/array.d.ts +4 -0
  20. package/dist/array.js +5 -0
  21. package/dist/async.d.ts +11 -0
  22. package/dist/async.js +11 -0
  23. package/dist/classList.d.ts +5 -0
  24. package/dist/classList.js +17 -0
  25. package/dist/compare.d.ts +44 -0
  26. package/dist/compare.js +73 -0
  27. package/dist/date.d.ts +5 -0
  28. package/dist/date.js +5 -0
  29. package/dist/dictionary/native.d.ts +116 -0
  30. package/dist/dictionary/native.js +142 -0
  31. package/dist/dictionary.d.ts +115 -0
  32. package/dist/dictionary.js +178 -0
  33. package/dist/equality.d.ts +44 -0
  34. package/dist/equality.js +53 -0
  35. package/dist/function.d.ts +48 -0
  36. package/dist/function.js +52 -0
  37. package/dist/lazy.d.ts +16 -0
  38. package/dist/lazy.js +32 -0
  39. package/dist/math.d.ts +47 -0
  40. package/dist/math.js +56 -0
  41. package/dist/maybe.d.ts +51 -0
  42. package/dist/maybe.js +36 -0
  43. package/dist/nullable.d.ts +50 -0
  44. package/dist/nullable.js +35 -0
  45. package/dist/number.d.ts +16 -0
  46. package/dist/number.js +16 -0
  47. package/dist/object.d.ts +40 -0
  48. package/dist/object.js +58 -0
  49. package/dist/ordering.d.ts +30 -0
  50. package/dist/ordering.js +46 -0
  51. package/dist/range.d.ts +37 -0
  52. package/dist/range.js +41 -0
  53. package/dist/regex.d.ts +28 -0
  54. package/dist/regex.js +35 -0
  55. package/dist/result.d.ts +54 -0
  56. package/dist/result.js +45 -0
  57. package/dist/roman.d.ts +4 -0
  58. package/dist/roman.js +47 -0
  59. package/dist/string/number.d.ts +31 -0
  60. package/dist/string/number.js +40 -0
  61. package/dist/string/regex.d.ts +28 -0
  62. package/dist/string/regex.js +38 -0
  63. package/dist/string.d.ts +32 -0
  64. package/dist/string.js +96 -0
  65. package/dist/typeSafety.d.ts +38 -0
  66. package/dist/typeSafety.js +33 -0
  67. package/package.json +46 -0
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Introduces a Dictionary class that represents an immutable mapping from
3
+ * strings to values.
4
+ */
5
+ import type { AnyNonNullish } from "./nullable.js";
6
+ /**
7
+ * An immutable dictionary mapping strings to values.
8
+ */
9
+ export declare class Dictionary<T extends AnyNonNullish | null> {
10
+ private record;
11
+ /**
12
+ * Creates a new dictionary from an indexed object.
13
+ */
14
+ constructor(record: Record<string, T>);
15
+ /**
16
+ * An empty dictionary.
17
+ */
18
+ static empty: Dictionary<never>;
19
+ /**
20
+ * Constructs a dictionary from an array of key-value pairs.
21
+ */
22
+ static fromEntries<T extends AnyNonNullish | null>(entries: [string, T][]): Dictionary<T>;
23
+ /**
24
+ * Returns the value associated with the given key, or `undefined` if the key does not exist.
25
+ */
26
+ get(key: string): T | undefined;
27
+ /**
28
+ * Returns the value associated with the given key mapped through the given function, or `undefined` if the key does not exist.
29
+ */
30
+ getMap<U>(key: string, mapFn: (value: T) => U): U | undefined;
31
+ /**
32
+ * Checks if the dictionary has the given key.
33
+ */
34
+ has(key: string): boolean;
35
+ /**
36
+ * Sets the given key to the given value.
37
+ *
38
+ * If the key already exists, its value will be overwritten.
39
+ */
40
+ set(key: string, value: T): Dictionary<T>;
41
+ /**
42
+ * Removes the given key from the dictionary.
43
+ *
44
+ * If the key does not exist, the same dictionary instance is returned.
45
+ */
46
+ remove(key: string): Dictionary<T>;
47
+ /**
48
+ * The number of entries in the dictionary.
49
+ */
50
+ get size(): number;
51
+ /**
52
+ * Returns an array of key-value pairs in the dictionary.
53
+ */
54
+ entries(): [string, T][];
55
+ /**
56
+ * Returns an array of values in the dictionary.
57
+ */
58
+ values(): T[];
59
+ /**
60
+ * Returns an array of keys in the dictionary.
61
+ */
62
+ keys(): string[];
63
+ /**
64
+ * Calls the given function for each key-value pair in the dictionary.
65
+ */
66
+ forEach(fn: (value: T, key: string) => void): void;
67
+ /**
68
+ * Calls the given async function for each key-value pair in the dictionary.
69
+ *
70
+ * The calls are made sequentially.
71
+ */
72
+ forEachAsync(fn: (value: T, key: string) => Promise<void>): Promise<void>;
73
+ /**
74
+ * Create, modify, or remove the value for the given key based on its current value.
75
+ *
76
+ * The value passed to `modifyFn` will be `undefined` if the key does not exist.
77
+ * If the `modifyFn` returns `undefined`, the key will be removed from the dictionary.
78
+ * Otherwise, the key will be set to the new value returned by `modifyFn`.
79
+ */
80
+ modify(key: string, modifyFn: (currentValue: T | undefined) => T | undefined): Dictionary<T>;
81
+ /**
82
+ * Returns the first value that matches the given predicate, or `undefined` if
83
+ * no such value exists.
84
+ */
85
+ find<U extends T>(predicate: (value: T, key: string) => value is U): U | undefined;
86
+ /**
87
+ * Returns the first value that matches the given predicate, or `undefined` if
88
+ * no such value exists.
89
+ */
90
+ find(predicate: (value: T, key: string) => boolean): T | undefined;
91
+ /**
92
+ * Returns the first key that matches the given predicate, or `undefined` if no such key exists.
93
+ */
94
+ findKey(predicate: (value: T, key: string) => boolean): string | undefined;
95
+ /**
96
+ * Returns the first key-value pair that matches the given predicate, or `undefined` if no such key-value pair exists.
97
+ */
98
+ findEntry<U extends T>(predicate: (value: T, key: string) => value is U): [key: string, value: U] | undefined;
99
+ /**
100
+ * Returns the first key-value pair that matches the given predicate, or `undefined` if no such key-value pair exists.
101
+ */
102
+ findEntry(predicate: (value: T, key: string) => boolean): [key: string, value: T] | undefined;
103
+ /**
104
+ * Applies the given mapping function to each key-value pair in the dictionary and returns the first non-`undefined` result, or `undefined` if no such result exists.
105
+ */
106
+ mapFirst<U extends AnyNonNullish | null>(mapFn: (value: T, key: string) => U | undefined): U | undefined;
107
+ /**
108
+ * Applies a function to every key-value pair in the dictionary.
109
+ */
110
+ map<U extends AnyNonNullish | null>(mapFn: (value: T, key: string) => U): Dictionary<U>;
111
+ /**
112
+ * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
113
+ */
114
+ reduce<U>(reducer: (acc: U, value: T, key: string) => U, initialValue: U): U;
115
+ }
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Introduces a Dictionary class that represents an immutable mapping from
3
+ * strings to values.
4
+ */
5
+ import { omitKeys } from "./object.js";
6
+ /**
7
+ * An immutable dictionary mapping strings to values.
8
+ */
9
+ export class Dictionary {
10
+ record;
11
+ /**
12
+ * Creates a new dictionary from an indexed object.
13
+ */
14
+ constructor(record) {
15
+ this.record = record;
16
+ }
17
+ /**
18
+ * An empty dictionary.
19
+ */
20
+ static empty = new Dictionary({});
21
+ /**
22
+ * Constructs a dictionary from an array of key-value pairs.
23
+ */
24
+ static fromEntries(entries) {
25
+ return new Dictionary(Object.fromEntries(entries));
26
+ }
27
+ /**
28
+ * Returns the value associated with the given key, or `undefined` if the key does not exist.
29
+ */
30
+ get(key) {
31
+ return this.record[key];
32
+ }
33
+ /**
34
+ * Returns the value associated with the given key mapped through the given function, or `undefined` if the key does not exist.
35
+ */
36
+ getMap(key, mapFn) {
37
+ const value = this.get(key);
38
+ return value === undefined ? undefined : mapFn(value);
39
+ }
40
+ /**
41
+ * Checks if the dictionary has the given key.
42
+ */
43
+ has(key) {
44
+ return Object.prototype.hasOwnProperty.call(this.record, key);
45
+ }
46
+ /**
47
+ * Sets the given key to the given value.
48
+ *
49
+ * If the key already exists, its value will be overwritten.
50
+ */
51
+ set(key, value) {
52
+ return new Dictionary({ ...this.record, [key]: value });
53
+ }
54
+ /**
55
+ * Removes the given key from the dictionary.
56
+ *
57
+ * If the key does not exist, the same dictionary instance is returned.
58
+ */
59
+ remove(key) {
60
+ if (this.has(key)) {
61
+ return new Dictionary(omitKeys(this.record, key));
62
+ }
63
+ else {
64
+ return this;
65
+ }
66
+ }
67
+ /**
68
+ * The number of entries in the dictionary.
69
+ */
70
+ get size() {
71
+ return Object.keys(this.record).length;
72
+ }
73
+ /**
74
+ * Returns an array of key-value pairs in the dictionary.
75
+ */
76
+ entries() {
77
+ return Object.entries(this.record);
78
+ }
79
+ /**
80
+ * Returns an array of values in the dictionary.
81
+ */
82
+ values() {
83
+ return Object.values(this.record);
84
+ }
85
+ /**
86
+ * Returns an array of keys in the dictionary.
87
+ */
88
+ keys() {
89
+ return Object.keys(this.record);
90
+ }
91
+ /**
92
+ * Calls the given function for each key-value pair in the dictionary.
93
+ */
94
+ forEach(fn) {
95
+ for (const [key, value] of Object.entries(this.record)) {
96
+ fn(value, key);
97
+ }
98
+ }
99
+ /**
100
+ * Calls the given async function for each key-value pair in the dictionary.
101
+ *
102
+ * The calls are made sequentially.
103
+ */
104
+ async forEachAsync(fn) {
105
+ for (const [key, value] of Object.entries(this.record)) {
106
+ await fn(value, key);
107
+ }
108
+ }
109
+ /**
110
+ * Create, modify, or remove the value for the given key based on its current value.
111
+ *
112
+ * The value passed to `modifyFn` will be `undefined` if the key does not exist.
113
+ * If the `modifyFn` returns `undefined`, the key will be removed from the dictionary.
114
+ * Otherwise, the key will be set to the new value returned by `modifyFn`.
115
+ */
116
+ modify(key, modifyFn) {
117
+ const currentValue = this.get(key);
118
+ const newValue = modifyFn(currentValue);
119
+ if (newValue === undefined) {
120
+ return this.remove(key);
121
+ }
122
+ else {
123
+ return this.set(key, newValue);
124
+ }
125
+ }
126
+ /**
127
+ * Returns the first value that matches the given predicate, or `undefined` if
128
+ * no such value exists.
129
+ */
130
+ find(predicate) {
131
+ return this.findEntry(predicate)?.[1];
132
+ }
133
+ /**
134
+ * Returns the first key that matches the given predicate, or `undefined` if no such key exists.
135
+ */
136
+ findKey(predicate) {
137
+ return this.findEntry(predicate)?.[0];
138
+ }
139
+ /**
140
+ * Returns the first key-value pair that matches the given predicate, or `undefined` if no such key-value pair exists.
141
+ */
142
+ findEntry(predicate) {
143
+ for (const [key, value] of Object.entries(this.record)) {
144
+ if (predicate(value, key)) {
145
+ return [key, value];
146
+ }
147
+ }
148
+ return undefined;
149
+ }
150
+ /**
151
+ * Applies the given mapping function to each key-value pair in the dictionary and returns the first non-`undefined` result, or `undefined` if no such result exists.
152
+ */
153
+ mapFirst(mapFn) {
154
+ for (const [key, value] of Object.entries(this.record)) {
155
+ const mapped = mapFn(value, key);
156
+ if (mapped !== undefined) {
157
+ return mapped;
158
+ }
159
+ }
160
+ return undefined;
161
+ }
162
+ /**
163
+ * Applies a function to every key-value pair in the dictionary.
164
+ */
165
+ map(mapFn) {
166
+ const newRecord = {};
167
+ for (const [key, value] of Object.entries(this.record)) {
168
+ newRecord[key] = mapFn(value, key);
169
+ }
170
+ return new Dictionary(newRecord);
171
+ }
172
+ /**
173
+ * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
174
+ */
175
+ reduce(reducer, initialValue) {
176
+ return Object.entries(this.record).reduce((acc, [key, item]) => reducer(acc, item, key), initialValue);
177
+ }
178
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * A function that compares two values for equality.
3
+ */
4
+ export type Equality<T> = (a: T, b: T) => boolean;
5
+ /**
6
+ * A function that compares two numbers for equality.
7
+ */
8
+ export type NumberEquality = Equality<number>;
9
+ /**
10
+ * Checks if the first number is less than the second number.
11
+ */
12
+ export declare const lt: NumberEquality;
13
+ /**
14
+ * Checks if the first number is less than or equal to the second number.
15
+ */
16
+ export declare const lte: NumberEquality;
17
+ /**
18
+ * Checks if the first number is greater than the second number.
19
+ */
20
+ export declare const gt: NumberEquality;
21
+ /**
22
+ * Checks if the first number is greater than or equal to the second number.
23
+ */
24
+ export declare const gte: NumberEquality;
25
+ /**
26
+ * Checks if two values are (shallowly) equal.
27
+ */
28
+ export declare const equal: <T>(a: T, b: T) => boolean;
29
+ /**
30
+ * Checks if two numbers are not (shallowly) equal.
31
+ */
32
+ export declare const notEqual: <T>(a: T, b: T) => boolean;
33
+ /**
34
+ * Checks if two arrays are equal. The values are compared shallowly.
35
+ *
36
+ * Use {@link deepEqual} for a deep equality check.
37
+ */
38
+ export declare const arrayEqual: <T extends number | boolean | string | symbol | null | undefined>(arr1: T[], arr2: T[]) => boolean;
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;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Checks if the first number is less than the second number.
3
+ */
4
+ export const lt = (a, b) => a < b;
5
+ /**
6
+ * Checks if the first number is less than or equal to the second number.
7
+ */
8
+ export const lte = (a, b) => a <= b;
9
+ /**
10
+ * Checks if the first number is greater than the second number.
11
+ */
12
+ export const gt = (a, b) => a > b;
13
+ /**
14
+ * Checks if the first number is greater than or equal to the second number.
15
+ */
16
+ export const gte = (a, b) => a >= b;
17
+ /**
18
+ * Checks if two values are (shallowly) equal.
19
+ */
20
+ export const equal = (a, b) => Object.is(a, b);
21
+ /**
22
+ * Checks if two numbers are not (shallowly) equal.
23
+ */
24
+ export const notEqual = (a, b) => !Object.is(a, b);
25
+ /**
26
+ * Checks if two arrays are equal. The values are compared shallowly.
27
+ *
28
+ * Use {@link deepEqual} for a deep equality check.
29
+ */
30
+ export const arrayEqual = (arr1, arr2) => arr1.length === arr2.length &&
31
+ arr1.every((value, index) => equal(value, arr2[index]));
32
+ /**
33
+ * Checks two values for value equality. This is a deep equality check that
34
+ * works for all types, including objects and arrays. For objects, it only
35
+ * compares all enumerable keys, no other properties or the prototype chain.
36
+ */
37
+ export const deepEqual = (a, b) => {
38
+ if (equal(a, b)) {
39
+ return true;
40
+ }
41
+ if (typeof a === "object" &&
42
+ typeof b === "object" &&
43
+ a !== null &&
44
+ b !== null) {
45
+ const keys = Object.keys(a);
46
+ if (keys.length !== Object.keys(b).length) {
47
+ return false;
48
+ }
49
+ return keys.every((key) => key in b &&
50
+ deepEqual(a[key], b[key]));
51
+ }
52
+ return false;
53
+ };
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Returns a function that always returns the given value.
3
+ * @param value The value to return from the new function.
4
+ * @returns A new function that always returns the given value.
5
+ */
6
+ export declare const constant: <T>(value: T) => () => T;
7
+ /**
8
+ * Returns a function that applies the given predicates to the given value and
9
+ * returns `true` if all predicates return `true`.
10
+ */
11
+ export declare const andEvery: <T>(...predicates: ((value: T) => boolean)[]) => (value: T) => boolean;
12
+ export declare function orSome<T, U extends T, V extends T>(f: (value: T) => value is U, g: (value: T) => value is V): (value: T) => value is U | V;
13
+ export declare function orSome<T>(...fns: ((value: T) => boolean)[]): (value: T) => boolean;
14
+ /**
15
+ * Returns a function that applies the given predicate to the given value and
16
+ * returns the negated result.
17
+ */
18
+ export declare const not: <T>(predicate: (value: T) => boolean) => (value: T) => boolean;
19
+ /**
20
+ * Combines two values while applying an accessor to each value before combining them.
21
+ *
22
+ * This is useful for scenarios where you want to combine complex objects based on a specific property or derived value.
23
+ *
24
+ * @param accessor A function that extracts the value to be used for combination from each input.
25
+ * @param combinator A function that combines the two extracted values.
26
+ * @returns A new function that takes two inputs, applies the accessor to each, and combines the results using the combinator.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * interface Person {
31
+ * name: string;
32
+ * age: number;
33
+ * }
34
+ *
35
+ * const alice: Person = { name: "Alice", age: 30 };
36
+ * const bob: Person = { name: "Bob", age: 25 };
37
+ * const people: Person[] = [alice, bob];
38
+ *
39
+ * const compareByAge = on(
40
+ * (person: Person) => person.age,
41
+ * (age1, age2) => age1 - age2
42
+ * );
43
+ *
44
+ * const sortedPeople = people.sort(compareByAge);
45
+ * // Result: [{ name: "Bob", age: 25 }, { name: "Alice", age: 30 }]
46
+ * ```
47
+ */
48
+ export declare const on: <T, U, V>(accessor: (value: T) => U, combinator: (a: U, b: U) => V) => ((a: T, b: T) => V);
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Returns a function that always returns the given value.
3
+ * @param value The value to return from the new function.
4
+ * @returns A new function that always returns the given value.
5
+ */
6
+ export const constant = (value) => () => value;
7
+ /**
8
+ * Returns a function that applies the given predicates to the given value and
9
+ * returns `true` if all predicates return `true`.
10
+ */
11
+ export const andEvery = (...predicates) => (value) => predicates.every((predicate) => predicate(value));
12
+ /**
13
+ * Returns a function that combines predicate functions disjunctionally.
14
+ */
15
+ export function orSome(...predicates) {
16
+ return (value) => predicates.some((predicate) => predicate(value));
17
+ }
18
+ /**
19
+ * Returns a function that applies the given predicate to the given value and
20
+ * returns the negated result.
21
+ */
22
+ export const not = (predicate) => (value) => !predicate(value);
23
+ /**
24
+ * Combines two values while applying an accessor to each value before combining them.
25
+ *
26
+ * This is useful for scenarios where you want to combine complex objects based on a specific property or derived value.
27
+ *
28
+ * @param accessor A function that extracts the value to be used for combination from each input.
29
+ * @param combinator A function that combines the two extracted values.
30
+ * @returns A new function that takes two inputs, applies the accessor to each, and combines the results using the combinator.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * interface Person {
35
+ * name: string;
36
+ * age: number;
37
+ * }
38
+ *
39
+ * const alice: Person = { name: "Alice", age: 30 };
40
+ * const bob: Person = { name: "Bob", age: 25 };
41
+ * const people: Person[] = [alice, bob];
42
+ *
43
+ * const compareByAge = on(
44
+ * (person: Person) => person.age,
45
+ * (age1, age2) => age1 - age2
46
+ * );
47
+ *
48
+ * const sortedPeople = people.sort(compareByAge);
49
+ * // Result: [{ name: "Bob", age: 25 }, { name: "Alice", age: 30 }]
50
+ * ```
51
+ */
52
+ export const on = (accessor, combinator) => (a, b) => combinator(accessor(a), accessor(b));
package/dist/lazy.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * A lazy value that is only evaluated when it is needed.
3
+ */
4
+ export declare class Lazy<T> {
5
+ #private;
6
+ private constructor();
7
+ /**
8
+ * Creates a new lazy value.
9
+ */
10
+ static of<T>(f: () => T): Lazy<T>;
11
+ /**
12
+ * The value of the lazy value. If the value has not been evaluated
13
+ * yet, it will be evaluated and cached.
14
+ */
15
+ get value(): T;
16
+ }
package/dist/lazy.js ADDED
@@ -0,0 +1,32 @@
1
+ import { assertExhaustive } from "./typeSafety.js";
2
+ /**
3
+ * A lazy value that is only evaluated when it is needed.
4
+ */
5
+ export class Lazy {
6
+ #state;
7
+ constructor(f) {
8
+ this.#state = { kind: "unevaluated", f };
9
+ }
10
+ /**
11
+ * Creates a new lazy value.
12
+ */
13
+ static of(f) {
14
+ return new Lazy(f);
15
+ }
16
+ /**
17
+ * The value of the lazy value. If the value has not been evaluated
18
+ * yet, it will be evaluated and cached.
19
+ */
20
+ get value() {
21
+ switch (this.#state.kind) {
22
+ case "unevaluated": {
23
+ this.#state = { kind: "evaluated", value: this.#state.f() };
24
+ return this.#state.value;
25
+ }
26
+ case "evaluated":
27
+ return this.#state.value;
28
+ default:
29
+ return assertExhaustive(this.#state);
30
+ }
31
+ }
32
+ }
package/dist/math.d.ts ADDED
@@ -0,0 +1,47 @@
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 ADDED
@@ -0,0 +1,56 @@
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;