@planet-matrix/mobius-model 0.3.0 → 0.4.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.
Files changed (82) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +4 -1
  3. package/dist/index.js +4 -2
  4. package/dist/index.js.map +18 -3
  5. package/package.json +3 -3
  6. package/scripts/build.ts +4 -4
  7. package/src/basic/README.md +143 -0
  8. package/src/basic/array.ts +872 -0
  9. package/src/basic/bigint.ts +114 -0
  10. package/src/basic/boolean.ts +180 -0
  11. package/src/basic/error.ts +51 -0
  12. package/src/basic/function.ts +453 -0
  13. package/src/basic/helper.ts +276 -0
  14. package/src/basic/index.ts +15 -0
  15. package/src/basic/is.ts +320 -0
  16. package/src/basic/number.ts +178 -0
  17. package/src/basic/object.ts +58 -0
  18. package/src/basic/promise.ts +464 -0
  19. package/src/basic/regexp.ts +7 -0
  20. package/src/basic/stream.ts +140 -0
  21. package/src/basic/string.ts +308 -0
  22. package/src/basic/symbol.ts +164 -0
  23. package/src/basic/temporal.ts +224 -0
  24. package/src/index.ts +2 -0
  25. package/src/type/README.md +330 -0
  26. package/src/type/array.ts +5 -0
  27. package/src/type/boolean.ts +471 -0
  28. package/src/type/class.ts +419 -0
  29. package/src/type/function.ts +1519 -0
  30. package/src/type/helper.ts +135 -0
  31. package/src/type/index.ts +14 -0
  32. package/src/type/intersection.ts +93 -0
  33. package/src/type/is.ts +247 -0
  34. package/src/type/iteration.ts +233 -0
  35. package/src/type/number.ts +732 -0
  36. package/src/type/object.ts +788 -0
  37. package/src/type/path.ts +73 -0
  38. package/src/type/string.ts +1004 -0
  39. package/src/type/tuple.ts +2424 -0
  40. package/src/type/union.ts +108 -0
  41. package/tests/unit/basic/array.spec.ts +290 -0
  42. package/tests/unit/basic/bigint.spec.ts +50 -0
  43. package/tests/unit/basic/boolean.spec.ts +74 -0
  44. package/tests/unit/basic/error.spec.ts +32 -0
  45. package/tests/unit/basic/function.spec.ts +175 -0
  46. package/tests/unit/basic/helper.spec.ts +118 -0
  47. package/tests/unit/basic/number.spec.ts +74 -0
  48. package/tests/unit/basic/object.spec.ts +15 -0
  49. package/tests/unit/basic/promise.spec.ts +232 -0
  50. package/tests/unit/basic/regexp.spec.ts +11 -0
  51. package/tests/unit/basic/stream.spec.ts +120 -0
  52. package/tests/unit/basic/string.spec.ts +74 -0
  53. package/tests/unit/basic/symbol.spec.ts +72 -0
  54. package/tests/unit/basic/temporal.spec.ts +78 -0
  55. package/dist/index.d.ts +0 -2
  56. package/dist/index.d.ts.map +0 -1
  57. package/dist/reactor/index.d.ts +0 -3
  58. package/dist/reactor/index.d.ts.map +0 -1
  59. package/dist/reactor/reactor-core/flags.d.ts +0 -99
  60. package/dist/reactor/reactor-core/flags.d.ts.map +0 -1
  61. package/dist/reactor/reactor-core/index.d.ts +0 -4
  62. package/dist/reactor/reactor-core/index.d.ts.map +0 -1
  63. package/dist/reactor/reactor-core/primitive.d.ts +0 -276
  64. package/dist/reactor/reactor-core/primitive.d.ts.map +0 -1
  65. package/dist/reactor/reactor-core/reactive-system.d.ts +0 -241
  66. package/dist/reactor/reactor-core/reactive-system.d.ts.map +0 -1
  67. package/dist/reactor/reactor-operators/branch.d.ts +0 -19
  68. package/dist/reactor/reactor-operators/branch.d.ts.map +0 -1
  69. package/dist/reactor/reactor-operators/convert.d.ts +0 -30
  70. package/dist/reactor/reactor-operators/convert.d.ts.map +0 -1
  71. package/dist/reactor/reactor-operators/create.d.ts +0 -26
  72. package/dist/reactor/reactor-operators/create.d.ts.map +0 -1
  73. package/dist/reactor/reactor-operators/filter.d.ts +0 -269
  74. package/dist/reactor/reactor-operators/filter.d.ts.map +0 -1
  75. package/dist/reactor/reactor-operators/index.d.ts +0 -8
  76. package/dist/reactor/reactor-operators/index.d.ts.map +0 -1
  77. package/dist/reactor/reactor-operators/join.d.ts +0 -48
  78. package/dist/reactor/reactor-operators/join.d.ts.map +0 -1
  79. package/dist/reactor/reactor-operators/map.d.ts +0 -165
  80. package/dist/reactor/reactor-operators/map.d.ts.map +0 -1
  81. package/dist/reactor/reactor-operators/utility.d.ts +0 -48
  82. package/dist/reactor/reactor-operators/utility.d.ts.map +0 -1
@@ -0,0 +1,114 @@
1
+ import { isBigInt } from "./is.ts"
2
+
3
+ /**
4
+ * Check whether a value is zero as a bigint.
5
+ *
6
+ * @example
7
+ * ```
8
+ * // Expect: true
9
+ * const example1 = bigintIsZero(0n)
10
+ * // Expect: false
11
+ * const example2 = bigintIsZero(2n)
12
+ * ```
13
+ */
14
+ export const bigintIsZero = (value: unknown): value is 0n => {
15
+ return isBigInt(value) && value === 0n
16
+ }
17
+
18
+ /**
19
+ * Check whether a value is a positive bigint.
20
+ *
21
+ * @example
22
+ * ```
23
+ * // Expect: true
24
+ * const example1 = bigintIsPositive(3n)
25
+ * // Expect: false
26
+ * const example2 = bigintIsPositive(-1n)
27
+ * ```
28
+ */
29
+ export const bigintIsPositive = (value: unknown): value is bigint => {
30
+ return isBigInt(value) && value > 0n
31
+ }
32
+
33
+ /**
34
+ * Check whether a value is a negative bigint.
35
+ *
36
+ * @example
37
+ * ```
38
+ * // Expect: true
39
+ * const example1 = bigintIsNegative(-3n)
40
+ * // Expect: false
41
+ * const example2 = bigintIsNegative(1n)
42
+ * ```
43
+ */
44
+ export const bigintIsNegative = (value: unknown): value is bigint => {
45
+ return isBigInt(value) && value < 0n
46
+ }
47
+
48
+ /**
49
+ * Get the absolute value of a bigint.
50
+ *
51
+ * @example
52
+ * ```
53
+ * // Expect: 5n
54
+ * const example1 = bigintAbs(-5n)
55
+ * // Expect: 2n
56
+ * const example2 = bigintAbs(2n)
57
+ * ```
58
+ */
59
+ export const bigintAbs = (value: bigint): bigint => {
60
+ return value < 0n ? -value : value
61
+ }
62
+
63
+ /**
64
+ * Get the smaller of two bigint values.
65
+ *
66
+ * @example
67
+ * ```
68
+ * // Expect: 2n
69
+ * const example1 = bigintMinOf(2n, 9n)
70
+ * // Expect: -1n
71
+ * const example2 = bigintMinOf(5n, -1n)
72
+ * ```
73
+ */
74
+ export const bigintMinOf = (a: bigint, b: bigint): bigint => {
75
+ return a < b ? a : b
76
+ }
77
+
78
+ /**
79
+ * Get the larger of two bigint values.
80
+ *
81
+ * @example
82
+ * ```
83
+ * // Expect: 9n
84
+ * const example1 = bigintMaxOf(2n, 9n)
85
+ * // Expect: 5n
86
+ * const example2 = bigintMaxOf(5n, -1n)
87
+ * ```
88
+ */
89
+ export const bigintMaxOf = (a: bigint, b: bigint): bigint => {
90
+ return a > b ? a : b
91
+ }
92
+
93
+ /**
94
+ * Clamp a bigint between two bounds.
95
+ *
96
+ * @example
97
+ * ```
98
+ * // Expect: 5n
99
+ * const example1 = bigintClamp(0n, 10n, 5n)
100
+ * // Expect: 0n
101
+ * const example2 = bigintClamp(0n, 10n, -3n)
102
+ * ```
103
+ */
104
+ export const bigintClamp = (min: bigint, max: bigint, value: bigint): bigint => {
105
+ const minValue = bigintMinOf(min, max)
106
+ const maxValue = bigintMaxOf(min, max)
107
+ if (value < minValue) {
108
+ return minValue
109
+ }
110
+ if (value > maxValue) {
111
+ return maxValue
112
+ }
113
+ return value
114
+ }
@@ -0,0 +1,180 @@
1
+ import type {
2
+ AnyFunctionOfReturn, BooleanNot, CastBoolean
3
+ } from "#Source/type/index.ts"
4
+
5
+ /**
6
+ * Convert any value to a boolean.
7
+ *
8
+ * @example
9
+ * ```
10
+ * // Expect: true
11
+ * const example1 = booleanFrom(1)
12
+ * // Expect: false
13
+ * const example2 = booleanFrom(0)
14
+ * ```
15
+ *
16
+ * @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Truthy}
17
+ * @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Falsy}
18
+ */
19
+ export const booleanFrom = (value: unknown): boolean => Boolean(value)
20
+
21
+ /**
22
+ * Invert a boolean value.
23
+ *
24
+ * @example
25
+ * ```
26
+ * // Expect: false
27
+ * const example1 = booleanNot(true)
28
+ * // Expect: true
29
+ * const example2 = booleanNot(false)
30
+ * ```
31
+ *
32
+ * @see {@link https://en.wikipedia.org/wiki/Logic_gate}
33
+ */
34
+ export function booleanNot(b: true): false
35
+ export function booleanNot(b: false): true
36
+ // catch all overload, used in `pipe` or `compose` or likewise situations.
37
+ export function booleanNot(b: boolean): boolean
38
+ export function booleanNot(b: unknown): boolean
39
+ export function booleanNot(b: unknown): boolean { return !booleanFrom(b) }
40
+
41
+ /**
42
+ * Perform logical AND.
43
+ *
44
+ * @example
45
+ * ```
46
+ * // Expect: false
47
+ * const example1 = booleanAnd(true, false)
48
+ * // Expect: true
49
+ * const example2 = booleanAnd(true, true)
50
+ * ```
51
+ *
52
+ * @see {@link https://en.wikipedia.org/wiki/Logic_gate}
53
+ */
54
+ export const booleanAnd = (x: unknown, y: unknown): boolean => booleanFrom(x) && booleanFrom(y)
55
+
56
+ /**
57
+ * Perform logical OR.
58
+ *
59
+ * @example
60
+ * ```
61
+ * // Expect: true
62
+ * const example1 = booleanOr(true, false)
63
+ * // Expect: false
64
+ * const example2 = booleanOr(false, false)
65
+ * ```
66
+ *
67
+ * @see {@link https://en.wikipedia.org/wiki/Logic_gate}
68
+ */
69
+ export const booleanOr = (x: unknown, y: unknown): boolean => booleanFrom(x) || booleanFrom(y)
70
+
71
+ /**
72
+ * Perform logical NAND.
73
+ *
74
+ * @example
75
+ * ```
76
+ * // Expect: true
77
+ * const example1 = booleanNand(true, false)
78
+ * // Expect: false
79
+ * const example2 = booleanNand(true, true)
80
+ * ```
81
+ *
82
+ * @see {@link https://en.wikipedia.org/wiki/Logic_gate}
83
+ */
84
+ export const booleanNand = (x: unknown, y: unknown): boolean => !booleanAnd(x, y)
85
+
86
+ /**
87
+ * Perform logical NOR.
88
+ *
89
+ * @example
90
+ * ```
91
+ * // Expect: false
92
+ * const example1 = booleanNor(true, false)
93
+ * // Expect: true
94
+ * const example2 = booleanNor(false, false)
95
+ * ```
96
+ *
97
+ * @see {@link https://en.wikipedia.org/wiki/Logic_gate}
98
+ */
99
+ export const booleanNor = (x: unknown, y: unknown): boolean => !booleanOr(x, y)
100
+
101
+ /**
102
+ * Perform logical XOR.
103
+ *
104
+ * @example
105
+ * ```
106
+ * // Expect: true
107
+ * const example1 = booleanXor(true, false)
108
+ * // Expect: false
109
+ * const example2 = booleanXor(true, true)
110
+ * ```
111
+ *
112
+ * @see {@link https://en.wikipedia.org/wiki/Logic_gate}
113
+ */
114
+ export const booleanXor = (x: unknown, y: unknown): boolean => booleanFrom(x) !== booleanFrom(y)
115
+
116
+ /**
117
+ * Perform logical XNOR.
118
+ *
119
+ * @example
120
+ * ```
121
+ * // Expect: false
122
+ * const example1 = booleanXnor(true, false)
123
+ * // Expect: true
124
+ * const example2 = booleanXnor(true, true)
125
+ * ```
126
+ *
127
+ * @see {@link https://en.wikipedia.org/wiki/Logic_gate}
128
+ */
129
+ export const booleanXnor = (x: unknown, y: unknown): boolean => !booleanXor(x, y)
130
+
131
+ export type BooleanInvertFunctionReturn<FN extends AnyFunctionOfReturn<boolean>> =
132
+ // oxlint-disable-next-line no-explicit-any
133
+ FN extends (...args: any[]) => infer R ? (...args: Parameters<FN>) => BooleanNot<CastBoolean<R>> : never
134
+ /**
135
+ * Create a complement function that inverts a boolean-returning function.
136
+ *
137
+ * @example
138
+ * ```
139
+ * const isEven = (value: number) => value % 2 === 0
140
+ * const isOdd = booleanComplement(isEven)
141
+ * // Expect: true
142
+ * const example1 = isOdd(3)
143
+ * // Expect: false
144
+ * const example2 = isOdd(4)
145
+ * ```
146
+ */
147
+ export const booleanComplement = <FN extends AnyFunctionOfReturn<boolean>>(fn: FN): BooleanInvertFunctionReturn<FN> =>
148
+ // oxlint-disable-next-line no-explicit-any no-unsafe-type-assertion no-unsafe-argument
149
+ ((...args: any[]) => booleanNot(fn(...args))) as any as BooleanInvertFunctionReturn<FN>
150
+
151
+ /**
152
+ * Check whether the value is truthy.
153
+ *
154
+ * @example
155
+ * ```
156
+ * // Expect: true
157
+ * const example1 = booleanIsTruthy("ok")
158
+ * // Expect: false
159
+ * const example2 = booleanIsTruthy("")
160
+ * ```
161
+ *
162
+ * @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Truthy}
163
+ * @see {@link booleanIsFalsy}
164
+ */
165
+ export const booleanIsTruthy = (v: unknown): boolean => booleanFrom(v)
166
+ /**
167
+ * Check whether the value is falsy.
168
+ *
169
+ * @example
170
+ * ```
171
+ * // Expect: true
172
+ * const example1 = booleanIsFalsy(0)
173
+ * // Expect: false
174
+ * const example2 = booleanIsFalsy("0")
175
+ * ```
176
+ *
177
+ * @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Falsy}
178
+ * @see {@link booleanIsTruthy}
179
+ */
180
+ export const booleanIsFalsy = (v: unknown): boolean => !booleanIsTruthy(v)
@@ -0,0 +1,51 @@
1
+ import { isError } from "./is.ts"
2
+
3
+ const internalErrorMessages = new Set([
4
+ "network error", // Chrome
5
+ "Failed to fetch", // Chrome
6
+ "NetworkError when attempting to fetch resource.", // Firefox
7
+ "The Internet connection appears to be offline.", // Safari 16
8
+ "Load failed", // Safari 17+
9
+ "Network request failed", // `cross-fetch`
10
+ "fetch failed", // Undici (Node.js)
11
+ "terminated", // Undici (Node.js)
12
+ ])
13
+
14
+ /**
15
+ * Checks if an error is a network error.
16
+ *
17
+ * @see {@link https://github.com/sindresorhus/is-network-error}
18
+ */
19
+ export const errorIsNetworkError = (error: unknown): error is Error => {
20
+ if (error === null || error === undefined) {
21
+ return false
22
+ }
23
+
24
+ if (isError(error) === false) {
25
+ return false
26
+ }
27
+
28
+ if (error.name !== "TypeError" || typeof error.message !== "string") {
29
+ return false
30
+ }
31
+
32
+ // We do an extra check for Safari 17+ as it has a very generic error message.
33
+ // Network errors in Safari have no stack.
34
+ if (error.message === "Load failed") {
35
+ return error.stack === undefined
36
+ }
37
+
38
+ return internalErrorMessages.has(error.message)
39
+ }
40
+
41
+ /**
42
+ * Stringifies an exception into a readable format.
43
+ */
44
+ export const errorStringifyException = (exception: unknown): string => {
45
+ if (isError(exception)) {
46
+ return `${exception.name}: ${exception.message}`
47
+ }
48
+ else {
49
+ return `${String(exception)}`
50
+ }
51
+ }