ts-data-forge 1.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/LICENSE +201 -0
- package/README.md +534 -0
- package/package.json +101 -0
- package/src/array/array-utils-creation.test.mts +443 -0
- package/src/array/array-utils-modification.test.mts +197 -0
- package/src/array/array-utils-overload-type-error.test.mts +149 -0
- package/src/array/array-utils-reducing-value.test.mts +425 -0
- package/src/array/array-utils-search.test.mts +169 -0
- package/src/array/array-utils-set-op.test.mts +335 -0
- package/src/array/array-utils-slice-clamped.test.mts +113 -0
- package/src/array/array-utils-slicing.test.mts +316 -0
- package/src/array/array-utils-transformation.test.mts +790 -0
- package/src/array/array-utils-validation.test.mts +492 -0
- package/src/array/array-utils.mts +4000 -0
- package/src/array/array.test.mts +146 -0
- package/src/array/index.mts +2 -0
- package/src/array/tuple-utils.mts +519 -0
- package/src/array/tuple-utils.test.mts +518 -0
- package/src/collections/imap-mapped.mts +801 -0
- package/src/collections/imap-mapped.test.mts +860 -0
- package/src/collections/imap.mts +651 -0
- package/src/collections/imap.test.mts +932 -0
- package/src/collections/index.mts +6 -0
- package/src/collections/iset-mapped.mts +889 -0
- package/src/collections/iset-mapped.test.mts +1187 -0
- package/src/collections/iset.mts +682 -0
- package/src/collections/iset.test.mts +1084 -0
- package/src/collections/queue.mts +390 -0
- package/src/collections/queue.test.mts +282 -0
- package/src/collections/stack.mts +423 -0
- package/src/collections/stack.test.mts +225 -0
- package/src/expect-type.mts +206 -0
- package/src/functional/index.mts +4 -0
- package/src/functional/match.mts +300 -0
- package/src/functional/match.test.mts +177 -0
- package/src/functional/optional.mts +733 -0
- package/src/functional/optional.test.mts +619 -0
- package/src/functional/pipe.mts +212 -0
- package/src/functional/pipe.test.mts +85 -0
- package/src/functional/result.mts +1134 -0
- package/src/functional/result.test.mts +777 -0
- package/src/globals.d.mts +38 -0
- package/src/guard/has-key.mts +119 -0
- package/src/guard/has-key.test.mts +219 -0
- package/src/guard/index.mts +7 -0
- package/src/guard/is-non-empty-string.mts +108 -0
- package/src/guard/is-non-empty-string.test.mts +91 -0
- package/src/guard/is-non-null-object.mts +106 -0
- package/src/guard/is-non-null-object.test.mts +90 -0
- package/src/guard/is-primitive.mts +165 -0
- package/src/guard/is-primitive.test.mts +102 -0
- package/src/guard/is-record.mts +153 -0
- package/src/guard/is-record.test.mts +112 -0
- package/src/guard/is-type.mts +450 -0
- package/src/guard/is-type.test.mts +496 -0
- package/src/guard/key-is-in.mts +163 -0
- package/src/guard/key-is-in.test.mts +19 -0
- package/src/index.mts +10 -0
- package/src/iterator/index.mts +1 -0
- package/src/iterator/range.mts +120 -0
- package/src/iterator/range.test.mts +33 -0
- package/src/json/index.mts +1 -0
- package/src/json/json.mts +711 -0
- package/src/json/json.test.mts +628 -0
- package/src/number/branded-types/finite-number.mts +354 -0
- package/src/number/branded-types/finite-number.test.mts +135 -0
- package/src/number/branded-types/index.mts +26 -0
- package/src/number/branded-types/int.mts +278 -0
- package/src/number/branded-types/int.test.mts +140 -0
- package/src/number/branded-types/int16.mts +192 -0
- package/src/number/branded-types/int16.test.mts +170 -0
- package/src/number/branded-types/int32.mts +193 -0
- package/src/number/branded-types/int32.test.mts +170 -0
- package/src/number/branded-types/non-negative-finite-number.mts +223 -0
- package/src/number/branded-types/non-negative-finite-number.test.mts +188 -0
- package/src/number/branded-types/non-negative-int16.mts +187 -0
- package/src/number/branded-types/non-negative-int16.test.mts +201 -0
- package/src/number/branded-types/non-negative-int32.mts +187 -0
- package/src/number/branded-types/non-negative-int32.test.mts +204 -0
- package/src/number/branded-types/non-zero-finite-number.mts +229 -0
- package/src/number/branded-types/non-zero-finite-number.test.mts +198 -0
- package/src/number/branded-types/non-zero-int.mts +167 -0
- package/src/number/branded-types/non-zero-int.test.mts +177 -0
- package/src/number/branded-types/non-zero-int16.mts +196 -0
- package/src/number/branded-types/non-zero-int16.test.mts +195 -0
- package/src/number/branded-types/non-zero-int32.mts +196 -0
- package/src/number/branded-types/non-zero-int32.test.mts +197 -0
- package/src/number/branded-types/non-zero-safe-int.mts +196 -0
- package/src/number/branded-types/non-zero-safe-int.test.mts +232 -0
- package/src/number/branded-types/non-zero-uint16.mts +189 -0
- package/src/number/branded-types/non-zero-uint16.test.mts +199 -0
- package/src/number/branded-types/non-zero-uint32.mts +189 -0
- package/src/number/branded-types/non-zero-uint32.test.mts +199 -0
- package/src/number/branded-types/positive-finite-number.mts +241 -0
- package/src/number/branded-types/positive-finite-number.test.mts +204 -0
- package/src/number/branded-types/positive-int.mts +304 -0
- package/src/number/branded-types/positive-int.test.mts +176 -0
- package/src/number/branded-types/positive-int16.mts +188 -0
- package/src/number/branded-types/positive-int16.test.mts +197 -0
- package/src/number/branded-types/positive-int32.mts +188 -0
- package/src/number/branded-types/positive-int32.test.mts +197 -0
- package/src/number/branded-types/positive-safe-int.mts +187 -0
- package/src/number/branded-types/positive-safe-int.test.mts +210 -0
- package/src/number/branded-types/positive-uint16.mts +188 -0
- package/src/number/branded-types/positive-uint16.test.mts +203 -0
- package/src/number/branded-types/positive-uint32.mts +188 -0
- package/src/number/branded-types/positive-uint32.test.mts +203 -0
- package/src/number/branded-types/safe-int.mts +291 -0
- package/src/number/branded-types/safe-int.test.mts +170 -0
- package/src/number/branded-types/safe-uint.mts +187 -0
- package/src/number/branded-types/safe-uint.test.mts +176 -0
- package/src/number/branded-types/uint.mts +179 -0
- package/src/number/branded-types/uint.test.mts +158 -0
- package/src/number/branded-types/uint16.mts +186 -0
- package/src/number/branded-types/uint16.test.mts +170 -0
- package/src/number/branded-types/uint32.mts +218 -0
- package/src/number/branded-types/uint32.test.mts +170 -0
- package/src/number/enum/index.mts +2 -0
- package/src/number/enum/int8.mts +344 -0
- package/src/number/enum/int8.test.mts +180 -0
- package/src/number/enum/uint8.mts +293 -0
- package/src/number/enum/uint8.test.mts +164 -0
- package/src/number/index.mts +4 -0
- package/src/number/num.mts +604 -0
- package/src/number/num.test.mts +242 -0
- package/src/number/refined-number-utils.mts +566 -0
- package/src/object/index.mts +1 -0
- package/src/object/object.mts +447 -0
- package/src/object/object.test.mts +124 -0
- package/src/others/cast-mutable.mts +113 -0
- package/src/others/cast-readonly.mts +192 -0
- package/src/others/cast-readonly.test.mts +89 -0
- package/src/others/if-then.mts +98 -0
- package/src/others/if-then.test.mts +75 -0
- package/src/others/index.mts +7 -0
- package/src/others/map-nullable.mts +172 -0
- package/src/others/map-nullable.test.mts +297 -0
- package/src/others/memoize-function.mts +196 -0
- package/src/others/memoize-function.test.mts +168 -0
- package/src/others/tuple.mts +160 -0
- package/src/others/tuple.test.mts +11 -0
- package/src/others/unknown-to-string.mts +215 -0
- package/src/others/unknown-to-string.test.mts +114 -0
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { expectType } from '../expect-type.mjs';
|
|
2
|
+
import { Optional } from '../functional/index.mjs';
|
|
3
|
+
import { Arr } from './array-utils.mjs';
|
|
4
|
+
|
|
5
|
+
describe('Arr slicing', () => {
|
|
6
|
+
describe('head', () => {
|
|
7
|
+
test('case 1', () => {
|
|
8
|
+
const xs = [1, 2, 3] as const;
|
|
9
|
+
const head = Arr.head(xs);
|
|
10
|
+
|
|
11
|
+
expectType<typeof head, Optional.Some<1>>('=');
|
|
12
|
+
|
|
13
|
+
expect(Optional.isSome(head)).toBe(true);
|
|
14
|
+
if (Optional.isSome(head)) {
|
|
15
|
+
expect(head.value).toBe(1);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('case 2', () => {
|
|
20
|
+
const xs: MutableNonEmptyArray<number> = [1, 2, 3];
|
|
21
|
+
const head = Arr.head(xs);
|
|
22
|
+
|
|
23
|
+
expectType<typeof head, Optional.Some<number>>('=');
|
|
24
|
+
|
|
25
|
+
expect(Optional.isSome(head)).toBe(true);
|
|
26
|
+
if (Optional.isSome(head)) {
|
|
27
|
+
expect(head.value).toBe(1);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('case 3', () => {
|
|
32
|
+
const mut_xs: number[] = [1, 2, 3];
|
|
33
|
+
const head = Arr.head(mut_xs);
|
|
34
|
+
|
|
35
|
+
expectType<typeof head, Optional<number>>('=');
|
|
36
|
+
|
|
37
|
+
expect(Optional.isSome(head)).toBe(true);
|
|
38
|
+
if (Optional.isSome(head)) {
|
|
39
|
+
expect(head.value).toBe(1);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('case 4', () => {
|
|
44
|
+
const xs = [] as const;
|
|
45
|
+
|
|
46
|
+
const head = Arr.head(xs);
|
|
47
|
+
|
|
48
|
+
expectType<typeof head, Optional.None>('=');
|
|
49
|
+
|
|
50
|
+
expect(Optional.isNone(head)).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('should return none for empty array', () => {
|
|
54
|
+
const result = Arr.head([]);
|
|
55
|
+
expect(Optional.isNone(result)).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('should work with single element array', () => {
|
|
59
|
+
const result = Arr.head([42]);
|
|
60
|
+
expect(Optional.isSome(result)).toBe(true);
|
|
61
|
+
if (Optional.isSome(result)) {
|
|
62
|
+
expect(result.value).toBe(42);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe('last', () => {
|
|
68
|
+
test('case 1', () => {
|
|
69
|
+
const xs = [1, 2, 3] as const;
|
|
70
|
+
const last = Arr.last(xs);
|
|
71
|
+
|
|
72
|
+
expectType<typeof last, Optional.Some<3>>('=');
|
|
73
|
+
|
|
74
|
+
expect(Optional.isSome(last)).toBe(true);
|
|
75
|
+
if (Optional.isSome(last)) {
|
|
76
|
+
expect(last.value).toBe(3);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('case 2', () => {
|
|
81
|
+
const xs: MutableNonEmptyArray<number> = [1, 2, 3];
|
|
82
|
+
const last = Arr.last(xs);
|
|
83
|
+
|
|
84
|
+
expectType<typeof last, Optional.Some<number>>('=');
|
|
85
|
+
|
|
86
|
+
expect(Optional.isSome(last)).toBe(true);
|
|
87
|
+
if (Optional.isSome(last)) {
|
|
88
|
+
expect(last.value).toBe(3);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('case 3', () => {
|
|
93
|
+
const mut_xs: number[] = [1, 2, 3];
|
|
94
|
+
const last = Arr.last(mut_xs);
|
|
95
|
+
|
|
96
|
+
expectType<typeof last, Optional<number>>('=');
|
|
97
|
+
|
|
98
|
+
expect(Optional.isSome(last)).toBe(true);
|
|
99
|
+
if (Optional.isSome(last)) {
|
|
100
|
+
expect(last.value).toBe(3);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('case 4', () => {
|
|
105
|
+
const xs = [] as const;
|
|
106
|
+
|
|
107
|
+
const last = Arr.last(xs);
|
|
108
|
+
|
|
109
|
+
expectType<typeof last, Optional.None>('=');
|
|
110
|
+
|
|
111
|
+
expect(Optional.isNone(last)).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('should return none for empty array', () => {
|
|
115
|
+
const result = Arr.last([]);
|
|
116
|
+
expect(Optional.isNone(result)).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('should work with single element array', () => {
|
|
120
|
+
const result = Arr.last([42]);
|
|
121
|
+
expect(Optional.isSome(result)).toBe(true);
|
|
122
|
+
if (Optional.isSome(result)) {
|
|
123
|
+
expect(result.value).toBe(42);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('tail', () => {
|
|
129
|
+
test('should return all elements except the first', () => {
|
|
130
|
+
const array = [1, 2, 3, 4] as const;
|
|
131
|
+
const result = Arr.tail(array);
|
|
132
|
+
expectType<typeof result, readonly [2, 3, 4]>('=');
|
|
133
|
+
expect(result).toStrictEqual([2, 3, 4]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('should work with single element array', () => {
|
|
137
|
+
const array = [1] as const;
|
|
138
|
+
const result = Arr.tail(array);
|
|
139
|
+
expectType<typeof result, readonly []>('=');
|
|
140
|
+
expect(result).toStrictEqual([]);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('should work with empty array', () => {
|
|
144
|
+
const array = [] as const;
|
|
145
|
+
const result = Arr.tail(array);
|
|
146
|
+
expectType<typeof result, readonly []>('=');
|
|
147
|
+
expect(result).toStrictEqual([]);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test('should return all elements except the first', () => {
|
|
151
|
+
const array = [1, 2, 3, 4] as const;
|
|
152
|
+
const result = Arr.tail(array);
|
|
153
|
+
expectType<typeof result, readonly [2, 3, 4]>('=');
|
|
154
|
+
expect(result).toStrictEqual([2, 3, 4]);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test('should work with single element array', () => {
|
|
158
|
+
const array = [1] as const;
|
|
159
|
+
const result = Arr.tail(array);
|
|
160
|
+
expectType<typeof result, readonly []>('=');
|
|
161
|
+
expect(result).toStrictEqual([]);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test('should work with empty array', () => {
|
|
165
|
+
const array = [] as const;
|
|
166
|
+
const result = Arr.tail(array);
|
|
167
|
+
expectType<typeof result, readonly []>('=');
|
|
168
|
+
expect(result).toStrictEqual([]);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('butLast', () => {
|
|
173
|
+
test('readonly number[] type', () => {
|
|
174
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
175
|
+
const butLast = Arr.butLast(xs);
|
|
176
|
+
|
|
177
|
+
expectType<typeof butLast, readonly number[]>('=');
|
|
178
|
+
|
|
179
|
+
expect(butLast).toStrictEqual([1, 2]);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('should return all elements except the last', () => {
|
|
183
|
+
const array = [1, 2, 3, 4] as const;
|
|
184
|
+
const result = Arr.butLast(array);
|
|
185
|
+
expectType<typeof result, readonly [1, 2, 3]>('=');
|
|
186
|
+
expect(result).toStrictEqual([1, 2, 3]);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('should work with single element array', () => {
|
|
190
|
+
const array = [1] as const;
|
|
191
|
+
const result = Arr.butLast(array);
|
|
192
|
+
expectType<typeof result, readonly []>('=');
|
|
193
|
+
expect(result).toStrictEqual([]);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test('should work with empty array', () => {
|
|
197
|
+
const array = [] as const;
|
|
198
|
+
const result = Arr.butLast(array);
|
|
199
|
+
expectType<typeof result, readonly []>('=');
|
|
200
|
+
expect(result).toStrictEqual([]);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test('should return all elements except the last', () => {
|
|
204
|
+
const array = [1, 2, 3, 4] as const;
|
|
205
|
+
const result = Arr.butLast(array);
|
|
206
|
+
expectType<typeof result, readonly [1, 2, 3]>('=');
|
|
207
|
+
expect(result).toStrictEqual([1, 2, 3]);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test('should work with single element array', () => {
|
|
211
|
+
const array = [1] as const;
|
|
212
|
+
const result = Arr.butLast(array);
|
|
213
|
+
expectType<typeof result, readonly []>('=');
|
|
214
|
+
expect(result).toStrictEqual([]);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test('should work with empty array', () => {
|
|
218
|
+
const array = [] as const;
|
|
219
|
+
const result = Arr.butLast(array);
|
|
220
|
+
expectType<typeof result, readonly []>('=');
|
|
221
|
+
expect(result).toStrictEqual([]);
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
describe('take', () => {
|
|
226
|
+
{
|
|
227
|
+
const xs = [1, 2, 3] as const;
|
|
228
|
+
const t = Arr.take(xs, 2);
|
|
229
|
+
|
|
230
|
+
expectType<typeof t, readonly [1, 2]>('=');
|
|
231
|
+
|
|
232
|
+
test('case 1', () => {
|
|
233
|
+
expect(t).toStrictEqual([1, 2]);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
{
|
|
237
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
238
|
+
const t = Arr.take(xs, 2);
|
|
239
|
+
|
|
240
|
+
expectType<typeof t, readonly number[]>('=');
|
|
241
|
+
|
|
242
|
+
test('case 2', () => {
|
|
243
|
+
expect(t).toStrictEqual([1, 2]);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
describe('takeLast', () => {
|
|
249
|
+
{
|
|
250
|
+
const xs = [1, 2, 3] as const;
|
|
251
|
+
const t = Arr.takeLast(xs, 2);
|
|
252
|
+
|
|
253
|
+
expectType<typeof t, readonly [2, 3]>('=');
|
|
254
|
+
|
|
255
|
+
test('case 1', () => {
|
|
256
|
+
expect(t).toStrictEqual([2, 3]);
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
{
|
|
260
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
261
|
+
const t = Arr.takeLast(xs, 2);
|
|
262
|
+
|
|
263
|
+
expectType<typeof t, readonly number[]>('=');
|
|
264
|
+
|
|
265
|
+
test('case 2', () => {
|
|
266
|
+
expect(t).toStrictEqual([2, 3]);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
describe('skip', () => {
|
|
272
|
+
{
|
|
273
|
+
const xs = [1, 2, 3] as const;
|
|
274
|
+
const t = Arr.skip(xs, 2);
|
|
275
|
+
|
|
276
|
+
expectType<typeof t, readonly [3]>('=');
|
|
277
|
+
|
|
278
|
+
test('case 1', () => {
|
|
279
|
+
expect(t).toStrictEqual([3]);
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
{
|
|
283
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
284
|
+
const t = Arr.skip(xs, 2);
|
|
285
|
+
|
|
286
|
+
expectType<typeof t, readonly number[]>('=');
|
|
287
|
+
|
|
288
|
+
test('case 2', () => {
|
|
289
|
+
expect(t).toStrictEqual([3]);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
describe('skipLast', () => {
|
|
295
|
+
{
|
|
296
|
+
const xs = [1, 2, 3] as const;
|
|
297
|
+
const t = Arr.skipLast(xs, 2);
|
|
298
|
+
|
|
299
|
+
expectType<typeof t, readonly [1]>('=');
|
|
300
|
+
|
|
301
|
+
test('case 1', () => {
|
|
302
|
+
expect(t).toStrictEqual([1]);
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
{
|
|
306
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
307
|
+
const t = Arr.skipLast(xs, 2);
|
|
308
|
+
|
|
309
|
+
expectType<typeof t, readonly number[]>('=');
|
|
310
|
+
|
|
311
|
+
test('case 2', () => {
|
|
312
|
+
expect(t).toStrictEqual([1]);
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
});
|