@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,51 @@
1
+ /**
2
+ * A maybe can contain a value or nothing.
3
+ */
4
+ export type Maybe<T> = Just<T> | Nothing;
5
+ /**
6
+ * A maybe that contains a value.
7
+ */
8
+ export interface Just<T> {
9
+ readonly tag: "Just";
10
+ readonly value: T;
11
+ }
12
+ /**
13
+ * A maybe that contains nothing.
14
+ */
15
+ export interface Nothing {
16
+ readonly tag: "Nothing";
17
+ }
18
+ /**
19
+ * Creates a maybe that contains a value.
20
+ */
21
+ export declare const Just: <T>(value: T) => Maybe<T>;
22
+ /**
23
+ * Checks if a maybe contains a value.
24
+ */
25
+ export declare const isJust: <T>(result: Maybe<T>) => result is Just<T>;
26
+ /**
27
+ * Creates a maybe that contains nothing.
28
+ */
29
+ export declare const Nothing: Maybe<never>;
30
+ /**
31
+ * Checks if a maybe contains nothing.
32
+ */
33
+ export declare const isNothing: <T>(result: Maybe<T>) => result is Nothing;
34
+ /**
35
+ * Creates a maybe from a nullable value.
36
+ */
37
+ export declare const fromNullable: <T>(value: T) => Maybe<NonNullable<T>>;
38
+ /**
39
+ * Reduces a maybe to a value of a common type.
40
+ */
41
+ export declare const reduce: <T, R>(maybe: Maybe<T>, def: R, f: (value: T) => R) => R;
42
+ /**
43
+ * Maps the value of a maybe to a new value.
44
+ */
45
+ export declare const map: <T, U>(maybe: Maybe<T>, f: (value: T) => U) => Maybe<U>;
46
+ /**
47
+ * Combines two maybes into one maybe. If both maybes contain a value, the
48
+ * values are combined using the provided function. If at least one maybe
49
+ * contains nothing, nothing is returned.
50
+ */
51
+ export declare const combine: <T1, T2, TR>(maybe1: Maybe<T1>, maybe2: Maybe<T2>, f: (value1: T1, value2: T2) => TR) => Maybe<TR>;
package/dist/maybe.js ADDED
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Creates a maybe that contains a value.
3
+ */
4
+ export const Just = (value) => ({ tag: "Just", value });
5
+ /**
6
+ * Checks if a maybe contains a value.
7
+ */
8
+ export const isJust = (result) => result.tag === "Just";
9
+ /**
10
+ * Creates a maybe that contains nothing.
11
+ */
12
+ export const Nothing = { tag: "Nothing" };
13
+ /**
14
+ * Checks if a maybe contains nothing.
15
+ */
16
+ export const isNothing = (result) => result.tag === "Nothing";
17
+ /**
18
+ * Creates a maybe from a nullable value.
19
+ */
20
+ export const fromNullable = (value) => value === null || value === undefined ? Nothing : Just(value);
21
+ /**
22
+ * Reduces a maybe to a value of a common type.
23
+ */
24
+ export const reduce = (maybe, def, f) => isJust(maybe) ? f(maybe.value) : def;
25
+ /**
26
+ * Maps the value of a maybe to a new value.
27
+ */
28
+ export const map = (maybe, f) => isJust(maybe) ? Just(f(maybe.value)) : Nothing;
29
+ /**
30
+ * Combines two maybes into one maybe. If both maybes contain a value, the
31
+ * values are combined using the provided function. If at least one maybe
32
+ * contains nothing, nothing is returned.
33
+ */
34
+ export const combine = (maybe1, maybe2, f) => isJust(maybe1) && isJust(maybe2)
35
+ ? Just(f(maybe1.value, maybe2.value))
36
+ : Nothing;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Extracts `null` and `undefined` from a type.
3
+ */
4
+ export type Nullish<T = null | undefined> = T extends null | undefined ? T : never;
5
+ export interface AnyNonNullish {
6
+ }
7
+ /**
8
+ * Checks if a value is `null` or `undefined`.
9
+ */
10
+ export declare const isNullish: <T>(value: T) => value is Exclude<T, NonNullable<T>>;
11
+ /**
12
+ * Checks if a value is not `null` or `undefined`.
13
+ */
14
+ export declare const isNotNullish: <T>(value: T) => value is NonNullable<T>;
15
+ /**
16
+ * Maps a value to another value if it is not `null` or `undefined`.
17
+ */
18
+ export declare const mapNullable: <T, U>(value: T, map: (value: NonNullable<T>) => U) => U | Nullish<T>;
19
+ /**
20
+ * Maps a value to another value if it is not `null` or `undefined`, otherwise
21
+ * returns a default value.
22
+ */
23
+ export declare const mapNullableDefault: <T, U>(value: T, map: (value: NonNullable<T>) => U, defaultValue: U) => U;
24
+ /**
25
+ * Returns an array, containing the value if it is not `null` or `undefined`.
26
+ *
27
+ * This can be useful in combination with the spread operator or
28
+ * `Array.prototype.flatMap`.
29
+ * @example
30
+ * nullableToArray(2) // [2]
31
+ * nullableToArray(undefined) // []
32
+ *
33
+ * [...nullableToArray(2)] // [2]
34
+ * [1, ...nullableToArray(2)] // [1, 2]
35
+ * [1, ...nullableToArray(undefined)] // [1]
36
+ */
37
+ export declare const nullableToArray: <T>(value: T) => NonNullable<T>[];
38
+ /**
39
+ * Returns the value if it matches the given predicate, otherwise `undefined`.
40
+ */
41
+ export declare const ensure: {
42
+ /**
43
+ * Returns the value if it matches the given predicate, otherwise `undefined`.
44
+ */
45
+ <T, T1 extends T>(value: T, predicate: (value: T) => value is T1): T1 | undefined;
46
+ /**
47
+ * Returns the value if it matches the given predicate, otherwise `undefined`.
48
+ */
49
+ <T>(value: T, predicate: (value: T) => boolean): T | undefined;
50
+ };
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Checks if a value is `null` or `undefined`.
3
+ */
4
+ export const isNullish = (value) => value === null || value === undefined;
5
+ /**
6
+ * Checks if a value is not `null` or `undefined`.
7
+ */
8
+ export const isNotNullish = (value) => !isNullish(value);
9
+ /**
10
+ * Maps a value to another value if it is not `null` or `undefined`.
11
+ */
12
+ export const mapNullable = (value, map) => (isNotNullish(value) ? map(value) : value);
13
+ /**
14
+ * Maps a value to another value if it is not `null` or `undefined`, otherwise
15
+ * returns a default value.
16
+ */
17
+ export const mapNullableDefault = (value, map, defaultValue) => (isNotNullish(value) ? map(value) : defaultValue);
18
+ /**
19
+ * Returns an array, containing the value if it is not `null` or `undefined`.
20
+ *
21
+ * This can be useful in combination with the spread operator or
22
+ * `Array.prototype.flatMap`.
23
+ * @example
24
+ * nullableToArray(2) // [2]
25
+ * nullableToArray(undefined) // []
26
+ *
27
+ * [...nullableToArray(2)] // [2]
28
+ * [1, ...nullableToArray(2)] // [1, 2]
29
+ * [1, ...nullableToArray(undefined)] // [1]
30
+ */
31
+ export const nullableToArray = (value) => isNotNullish(value) ? [value] : [];
32
+ /**
33
+ * Returns the value if it matches the given predicate, otherwise `undefined`.
34
+ */
35
+ export const ensure = (value, predicate) => predicate(value) ? value : undefined;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Returns a random integer between `0` and `max` (inclusive).
3
+ */
4
+ export declare const randomInt: (max: number) => number;
5
+ /**
6
+ * Returns a random integer between `min` and `max` (inclusive).
7
+ */
8
+ export declare const randomIntRange: (min: number, max: number) => number;
9
+ /**
10
+ * Returns if the given number is even.
11
+ */
12
+ export declare const even: (x: number) => boolean;
13
+ /**
14
+ * Returns if the given number is odd.
15
+ */
16
+ export declare const odd: (x: number) => boolean;
package/dist/number.js ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Returns a random integer between `0` and `max` (inclusive).
3
+ */
4
+ export const randomInt = (max) => Math.floor(Math.random() * (max + 1));
5
+ /**
6
+ * Returns a random integer between `min` and `max` (inclusive).
7
+ */
8
+ export const randomIntRange = (min, max) => Math.floor(Math.random() * (max + 1 - min)) + min;
9
+ /**
10
+ * Returns if the given number is even.
11
+ */
12
+ export const even = (x) => x % 2 === 0;
13
+ /**
14
+ * Returns if the given number is odd.
15
+ */
16
+ export const odd = (x) => x % 2 !== 0;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Maps all own properties of an object to a new object. Returning `undefined`
3
+ * from the mapping function will omit the property from the result.
4
+ */
5
+ export declare const mapObject: <T extends object, U>(object: T, map: (value: T[keyof T], key: keyof T) => U | undefined) => { [key in keyof T]: Exclude<U, undefined>; };
6
+ /**
7
+ * Sorts the keys of an object based on the order of the provided keys array.
8
+ *
9
+ * Keys not present in the keys array will be placed at the end in their original order.
10
+ */
11
+ export declare const sortObjectKeysByIndex: (obj: Record<string, unknown>, keys: string[]) => Record<string, unknown>;
12
+ /**
13
+ * Sorts the keys of an object using the provided comparison function.
14
+ *
15
+ * By default, it sorts the keys in ascending lexicographical order.
16
+ */
17
+ export declare const sortObjectKeys: (obj: Record<string, unknown>, fn?: (a: string, b: string) => number) => Record<string, unknown>;
18
+ /**
19
+ * Merges two objects. In case of key conflicts, the `solveConflict` function
20
+ * is used to determine the value for the conflicting key.
21
+ */
22
+ export declare const mergeObjects: <T>(obj1: Record<string, T>, obj2: Record<string, T>, solveConflict: (a: T, b: T) => T) => Record<string, T>;
23
+ /**
24
+ * Keeps only the given keys in an object.
25
+ */
26
+ export declare const onlyKeys: <T extends object, K extends keyof T>(obj: T, ...keys: K[]) => Pick<T, K>;
27
+ /**
28
+ * Determines whether an object has a property with the specified name.
29
+ *
30
+ * Provides a type guard to assert the presence of the key, in contrast to the native `Object.hasOwn`.
31
+ */
32
+ export declare const hasKey: <T extends object, K extends PropertyKey>(obj: T, key: K) => obj is T & Record<K, unknown>;
33
+ /**
34
+ * Omits all keys with `undefined` values from an object.
35
+ */
36
+ export declare const omitUndefinedKeys: <T extends object>(obj: T) => T;
37
+ /**
38
+ * Omits the given keys from an object.
39
+ */
40
+ export declare const omitKeys: <T extends object, K extends keyof T>(obj: T, ...keys: K[]) => Omit<T, K>;
package/dist/object.js ADDED
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Maps all own properties of an object to a new object. Returning `undefined`
3
+ * from the mapping function will omit the property from the result.
4
+ */
5
+ export const mapObject = (object, map) => {
6
+ const result = {};
7
+ for (const key in object) {
8
+ if (Object.hasOwn(object, key)) {
9
+ const newValue = map(object[key], key);
10
+ if (newValue !== undefined) {
11
+ result[key] = newValue;
12
+ }
13
+ }
14
+ }
15
+ return result;
16
+ };
17
+ /**
18
+ * Sorts the keys of an object based on the order of the provided keys array.
19
+ *
20
+ * Keys not present in the keys array will be placed at the end in their original order.
21
+ */
22
+ export const sortObjectKeysByIndex = (obj, keys) => Object.fromEntries([
23
+ ...keys.flatMap((key) => obj[key] === undefined ? [] : [[key, obj[key]]]),
24
+ ...Object.entries(obj).filter(([key]) => !keys.includes(key)),
25
+ ]);
26
+ /**
27
+ * Sorts the keys of an object using the provided comparison function.
28
+ *
29
+ * By default, it sorts the keys in ascending lexicographical order.
30
+ */
31
+ export const sortObjectKeys = (obj, fn = (a, b) => a.localeCompare(b)) => Object.fromEntries(Object.entries(obj).sort(([keyA], [keyB]) => fn(keyA, keyB)));
32
+ /**
33
+ * Merges two objects. In case of key conflicts, the `solveConflict` function
34
+ * is used to determine the value for the conflicting key.
35
+ */
36
+ export const mergeObjects = (obj1, obj2, solveConflict) => Object.entries(obj2).reduce((acc, [key, value]) => ({
37
+ ...acc,
38
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
39
+ [key]: Object.hasOwn(acc, key) ? solveConflict(acc[key], value) : value,
40
+ }), obj1);
41
+ /**
42
+ * Keeps only the given keys in an object.
43
+ */
44
+ export const onlyKeys = (obj, ...keys) => Object.fromEntries(Object.entries(obj).filter(([key]) => keys.includes(key)));
45
+ /**
46
+ * Determines whether an object has a property with the specified name.
47
+ *
48
+ * Provides a type guard to assert the presence of the key, in contrast to the native `Object.hasOwn`.
49
+ */
50
+ export const hasKey = (obj, key) => Object.hasOwn(obj, key);
51
+ /**
52
+ * Omits all keys with `undefined` values from an object.
53
+ */
54
+ export const omitUndefinedKeys = (obj) => Object.fromEntries(Object.entries(obj).filter(([, value]) => value !== undefined));
55
+ /**
56
+ * Omits the given keys from an object.
57
+ */
58
+ export const omitKeys = (obj, ...keys) => Object.fromEntries(Object.entries(obj).filter(([key]) => !keys.includes(key)));
@@ -0,0 +1,30 @@
1
+ import { type AnyNonNullish } from "./nullable.js";
2
+ /**
3
+ * The type of a compare function that can be used to sort values.
4
+ * @returns A negative number if `a` should be sorted before `b`, a positive
5
+ * number if `a` should be sorted after `b`, or zero if `a` and `b` are equal.
6
+ */
7
+ export type Compare<T> = (a: T, b: T) => number;
8
+ /**
9
+ * Build a compare function that nests multiple compare functions. The functions
10
+ * are applied in order, so the first function is the primary sort key, the
11
+ * second function is the secondary sort key, and so on.
12
+ */
13
+ export declare const reduceCompare: <T>(...compares: Compare<T>[]) => Compare<T>;
14
+ /**
15
+ * Compare function for numbers that sorts them in ascending order.
16
+ */
17
+ export declare const compareNumber: Compare<number>;
18
+ /**
19
+ * Compare function for {@link Date} objects in ascending order.
20
+ */
21
+ export declare const compareDate: Compare<Date>;
22
+ /**
23
+ * Higher-order compare function that extends a compare function to also handle
24
+ * `null` and `undefined` values. Nullish values are always sorted last.
25
+ */
26
+ export declare const compareNullish: <T extends AnyNonNullish>(compare: Compare<T>) => (a: T | null | undefined, b: T | null | undefined) => number;
27
+ /**
28
+ * A function that reverses the order of a compare function.
29
+ */
30
+ export declare const reverse: <T>(compare: Compare<T>) => Compare<T>;
@@ -0,0 +1,46 @@
1
+ import { isNullish } from "./nullable.js";
2
+ /**
3
+ * Build a compare function that nests multiple compare functions. The functions
4
+ * are applied in order, so the first function is the primary sort key, the
5
+ * second function is the secondary sort key, and so on.
6
+ */
7
+ export const reduceCompare = (...compares) => (a, b) => {
8
+ for (const compare of compares) {
9
+ const result = compare(a, b);
10
+ if (result !== 0) {
11
+ return result;
12
+ }
13
+ }
14
+ return 0;
15
+ };
16
+ /**
17
+ * Compare function for numbers that sorts them in ascending order.
18
+ */
19
+ export const compareNumber = (a, b) => a - b;
20
+ /**
21
+ * Compare function for {@link Date} objects in ascending order.
22
+ */
23
+ export const compareDate = (a, b) => a.getTime() - b.getTime();
24
+ /**
25
+ * Higher-order compare function that extends a compare function to also handle
26
+ * `null` and `undefined` values. Nullish values are always sorted last.
27
+ */
28
+ export const compareNullish = (compare) => (a, b) => {
29
+ if (isNullish(a) && isNullish(b)) {
30
+ return 0;
31
+ }
32
+ if (isNullish(a)) {
33
+ return 1;
34
+ }
35
+ if (isNullish(b)) {
36
+ return -1;
37
+ }
38
+ return compare(a, b);
39
+ };
40
+ /**
41
+ * A function that reverses the order of a compare function.
42
+ */
43
+ export const reverse = (compare) => (a, b) => {
44
+ const res = compare(a, b);
45
+ return res === 0 ? 0 : -res;
46
+ };
@@ -0,0 +1,37 @@
1
+ /**
2
+ * A pair that specifies the lower (including) and upper (including) bounds of a
3
+ * contiguous subrange of integer values.
4
+ */
5
+ export type RangeBounds = [lowerBound: number, upperBound: number];
6
+ /**
7
+ * The list of values in the subrange defined by a bounding pair.
8
+ * @throws {RangeError} If the upper bound is lower than the lower bound.
9
+ */
10
+ export declare const range: {
11
+ (...args: RangeBounds): number[];
12
+ (bounds: RangeBounds): number[];
13
+ };
14
+ /**
15
+ * Returns a range of numbers between and including two numbers. The order of
16
+ * the arguments does not matter.
17
+ * @param start The first number in the range.
18
+ * @param end The last number in the range.
19
+ */
20
+ export declare const rangeSafe: {
21
+ (...args: RangeBounds): number[];
22
+ (bounds: RangeBounds): number[];
23
+ };
24
+ /**
25
+ * Checks whether the passed `value` is within the range specified by the passed
26
+ * `bounds`.
27
+ */
28
+ export declare const isInRange: (bounds: RangeBounds, value: number) => boolean;
29
+ /**
30
+ * Returns the index of a value in a range.
31
+ * @throws {RangeError} If the passed `value` is not within the range specified.
32
+ */
33
+ export declare const indexInRange: (bounds: RangeBounds, value: number) => number;
34
+ /**
35
+ * Returns the size of the range defined by the passed bounds.
36
+ */
37
+ export declare const rangeSize: (bounds: RangeBounds) => number;
package/dist/range.js ADDED
@@ -0,0 +1,41 @@
1
+ const normalizeBounds = (bounds) => bounds.length === 1 ? bounds[0] : bounds;
2
+ /**
3
+ * The list of values in the subrange defined by a bounding pair.
4
+ * @throws {RangeError} If the upper bound is lower than the lower bound.
5
+ */
6
+ export const range = (...args) => {
7
+ const [start, end] = normalizeBounds(args);
8
+ if (start > end) {
9
+ throw new RangeError("The upper bound must be greater than or equal to the lower bound.");
10
+ }
11
+ return Array.from({ length: end - start + 1 }, (_, i) => i + start);
12
+ };
13
+ /**
14
+ * Returns a range of numbers between and including two numbers. The order of
15
+ * the arguments does not matter.
16
+ * @param start The first number in the range.
17
+ * @param end The last number in the range.
18
+ */
19
+ export const rangeSafe = (...args) => {
20
+ const [start, end] = normalizeBounds(args);
21
+ return start > end ? range(end, start) : range(start, end);
22
+ };
23
+ /**
24
+ * Checks whether the passed `value` is within the range specified by the passed
25
+ * `bounds`.
26
+ */
27
+ export const isInRange = (bounds, value) => value >= bounds[0] && value <= bounds[1];
28
+ /**
29
+ * Returns the index of a value in a range.
30
+ * @throws {RangeError} If the passed `value` is not within the range specified.
31
+ */
32
+ export const indexInRange = (bounds, value) => {
33
+ if (!isInRange(bounds, value)) {
34
+ throw new RangeError(`indexInRange: index for ${value.toString()} is out of range (${bounds[0].toString()}...${bounds[1].toString()})`);
35
+ }
36
+ return value - bounds[0];
37
+ };
38
+ /**
39
+ * Returns the size of the range defined by the passed bounds.
40
+ */
41
+ export const rangeSize = (bounds) => bounds[0] <= bounds[1] ? bounds[1] - bounds[0] + 1 : 0;
@@ -0,0 +1,28 @@
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 ADDED
@@ -0,0 +1,35 @@
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
+ };
@@ -0,0 +1,54 @@
1
+ /**
2
+ * A result is either a value or an error.
3
+ */
4
+ export type Result<T, E> = Ok<T> | Error<E>;
5
+ /**
6
+ * A result that contains a value.
7
+ */
8
+ export interface Ok<T> {
9
+ readonly tag: "Ok";
10
+ readonly value: T;
11
+ }
12
+ /**
13
+ * A result that contains an error.
14
+ */
15
+ export interface Error<E> {
16
+ readonly tag: "Error";
17
+ readonly error: E;
18
+ }
19
+ /**
20
+ * Creates a result that contains a value.
21
+ */
22
+ export declare const ok: <T>(value: T) => Result<T, never>;
23
+ /**
24
+ * Checks if a result contains a value.
25
+ */
26
+ export declare const isOk: <T, E>(result: Result<T, E>) => result is Ok<T>;
27
+ /**
28
+ * Creates a result that contains an error.
29
+ */
30
+ export declare const error: <E>(error: E) => Result<never, E>;
31
+ /**
32
+ * Checks if a result contains an error.
33
+ */
34
+ export declare const isError: <T, E>(result: Result<T, E>) => result is Error<E>;
35
+ /**
36
+ * Reduces a result to a value of a common type.
37
+ */
38
+ export declare const reduce: <T, E, R>(result: Result<T, E>, fok: (value: T) => R, ferror: (error: E) => R) => R;
39
+ /**
40
+ * Maps the value of a result to a new value.
41
+ */
42
+ export declare const map: <T, U, E>(result: Result<T, E>, f: (value: T) => U) => Result<U, E>;
43
+ /**
44
+ * Maps an error to a new error.
45
+ */
46
+ export declare const mapError: <T, E, F>(result: Result<T, E>, f: (value: E) => F) => Result<T, F>;
47
+ /**
48
+ * Combines two results into one.
49
+ *
50
+ * If both results are Ok, applies `fok` to their values and returns an Ok result.
51
+ * If both results are Error, applies `ferror` to their errors and returns an Error result.
52
+ * If one result is Ok and the other is Error, returns the Error result.
53
+ */
54
+ export declare const combine: <T1, T2, TR, E1, E2, ER>(result1: Result<T1, E1>, result2: Result<T2, E2>, fok: (value1: T1, value2: T2) => TR, ferror: (error1: E1, error2: E2) => ER) => Result<TR, E1 | E2 | ER>;
package/dist/result.js ADDED
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Creates a result that contains a value.
3
+ */
4
+ export const ok = (value) => ({ tag: "Ok", value });
5
+ /**
6
+ * Checks if a result contains a value.
7
+ */
8
+ export const isOk = (result) => result.tag === "Ok";
9
+ /**
10
+ * Creates a result that contains an error.
11
+ */
12
+ export const error = (error) => ({
13
+ tag: "Error",
14
+ error,
15
+ });
16
+ /**
17
+ * Checks if a result contains an error.
18
+ */
19
+ export const isError = (result) => result.tag === "Error";
20
+ /**
21
+ * Reduces a result to a value of a common type.
22
+ */
23
+ export const reduce = (result, fok, ferror) => (isOk(result) ? fok(result.value) : ferror(result.error));
24
+ /**
25
+ * Maps the value of a result to a new value.
26
+ */
27
+ export const map = (result, f) => (isOk(result) ? ok(f(result.value)) : result);
28
+ /**
29
+ * Maps an error to a new error.
30
+ */
31
+ export const mapError = (result, f) => (isError(result) ? error(f(result.error)) : result);
32
+ /**
33
+ * Combines two results into one.
34
+ *
35
+ * If both results are Ok, applies `fok` to their values and returns an Ok result.
36
+ * If both results are Error, applies `ferror` to their errors and returns an Error result.
37
+ * If one result is Ok and the other is Error, returns the Error result.
38
+ */
39
+ export const combine = (result1, result2, fok, ferror) => isOk(result1)
40
+ ? isOk(result2)
41
+ ? ok(fok(result1.value, result2.value))
42
+ : result2
43
+ : isOk(result2)
44
+ ? result1
45
+ : error(ferror(result1.error, result2.error));
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Converts a number to a roman numeral.
3
+ */
4
+ export declare const romanize: (num: number) => string;