ts-fortress 9.2.1 → 10.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/README.md +4 -4
- package/dist/array/bounded-length-tuple.d.mts +20 -0
- package/dist/array/bounded-length-tuple.d.mts.map +1 -0
- package/dist/array/{array-bounded-length.mjs → bounded-length-tuple.mjs} +4 -4
- package/dist/array/bounded-length-tuple.mjs.map +1 -0
- package/dist/array/fixed-length-tuple.d.mts +8 -0
- package/dist/array/fixed-length-tuple.d.mts.map +1 -0
- package/dist/array/{array-of-length.mjs → fixed-length-tuple.mjs} +5 -5
- package/dist/array/fixed-length-tuple.mjs.map +1 -0
- package/dist/array/index.d.mts +5 -4
- package/dist/array/index.d.mts.map +1 -1
- package/dist/array/index.mjs +5 -4
- package/dist/array/index.mjs.map +1 -1
- package/dist/array/length-constrained-array.d.mts +82 -0
- package/dist/array/length-constrained-array.d.mts.map +1 -0
- package/dist/array/length-constrained-array.mjs +215 -0
- package/dist/array/length-constrained-array.mjs.map +1 -0
- package/dist/array/max-length-tuple.d.mts +12 -0
- package/dist/array/max-length-tuple.d.mts.map +1 -0
- package/dist/array/{array-at-most-length.mjs → max-length-tuple.mjs} +4 -4
- package/dist/array/max-length-tuple.mjs.map +1 -0
- package/dist/array/min-length-tuple.d.mts +12 -0
- package/dist/array/min-length-tuple.d.mts.map +1 -0
- package/dist/array/{array-at-least-length.mjs → min-length-tuple.mjs} +4 -4
- package/dist/array/min-length-tuple.mjs.map +1 -0
- package/dist/compose/intersection.mjs +1 -1
- package/dist/compose/intersection.mjs.map +1 -1
- package/dist/compose/union.mjs +1 -1
- package/dist/compose/union.mjs.map +1 -1
- package/dist/entry-point.mjs +5 -4
- package/dist/entry-point.mjs.map +1 -1
- package/dist/index.mjs +5 -4
- package/dist/index.mjs.map +1 -1
- package/dist/primitives/bigint.mjs +1 -1
- package/dist/primitives/bigint.mjs.map +1 -1
- package/dist/primitives/number.mjs +1 -1
- package/dist/primitives/number.mjs.map +1 -1
- package/dist/primitives/string.d.mts +18 -16
- package/dist/primitives/string.d.mts.map +1 -1
- package/dist/primitives/string.mjs +1 -1
- package/dist/primitives/string.mjs.map +1 -1
- package/dist/record/at.mjs +1 -1
- package/dist/record/at.mjs.map +1 -1
- package/dist/record/merge-records.mjs +1 -1
- package/dist/record/merge-records.mjs.map +1 -1
- package/dist/record/valueof.mjs +1 -1
- package/dist/record/valueof.mjs.map +1 -1
- package/package.json +3 -3
- package/src/array/{array-bounded-length.mts → bounded-length-tuple.mts} +22 -22
- package/src/array/{array-bounded-length.test.mts → bounded-length-tuple.test.mts} +20 -20
- package/src/array/{array-of-length.mts → fixed-length-tuple.mts} +11 -8
- package/src/array/{array-of-length.test.mts → fixed-length-tuple.test.mts} +8 -8
- package/src/array/index.mts +5 -4
- package/src/array/length-constrained-array.mts +514 -0
- package/src/array/length-constrained-array.test.mts +229 -0
- package/src/array/{array-at-most-length.mts → max-length-tuple.mts} +12 -9
- package/src/array/{array-at-most-length.test.mts → max-length-tuple.test.mts} +11 -11
- package/src/array/{array-at-least-length.mts → min-length-tuple.mts} +12 -9
- package/src/array/{array-at-least-length.test.mts → min-length-tuple.test.mts} +12 -15
- package/src/compose/intersection.mts +1 -1
- package/src/compose/union.mts +1 -1
- package/src/primitives/bigint.mts +1 -1
- package/src/primitives/number.mts +1 -1
- package/src/primitives/string.mts +24 -29
- package/src/primitives/string.test.mts +27 -20
- package/src/record/at.mts +1 -1
- package/src/record/merge-records.mts +1 -1
- package/src/record/valueof.mts +1 -1
- package/dist/array/array-at-least-length.d.mts +0 -12
- package/dist/array/array-at-least-length.d.mts.map +0 -1
- package/dist/array/array-at-least-length.mjs.map +0 -1
- package/dist/array/array-at-most-length.d.mts +0 -12
- package/dist/array/array-at-most-length.d.mts.map +0 -1
- package/dist/array/array-at-most-length.mjs.map +0 -1
- package/dist/array/array-bounded-length.d.mts +0 -20
- package/dist/array/array-bounded-length.d.mts.map +0 -1
- package/dist/array/array-bounded-length.mjs.map +0 -1
- package/dist/array/array-of-length.d.mts +0 -8
- package/dist/array/array-of-length.d.mts.map +0 -1
- package/dist/array/array-of-length.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -1042,10 +1042,10 @@ type ValidationError = Readonly<{
|
|
|
1042
1042
|
- `t.array(elementType)` - Array validation
|
|
1043
1043
|
- `t.nonEmptyArray(elementType)` - Non-empty array validation
|
|
1044
1044
|
- `t.tuple([t1, t2, ..., tN])` - Fixed-length tuple validation
|
|
1045
|
-
- `t.
|
|
1046
|
-
- `t.
|
|
1047
|
-
- `t.
|
|
1048
|
-
- `t.
|
|
1045
|
+
- `t.fixedLengthTuple(size, elementType)` - Fixed-length array validation
|
|
1046
|
+
- `t.minLengthTuple(size, elementType)` - Array validation with a minimum length
|
|
1047
|
+
- `t.maxLengthTuple(size, elementType)` - Array validation with a maximum length
|
|
1048
|
+
- `t.boundedLengthTuple(min, max, elementType)` - Array validation with a length between `min` and `max` (inclusive)
|
|
1049
1049
|
|
|
1050
1050
|
### Objects
|
|
1051
1051
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type BoundedLengthTuple, type MaxLengthTuple, type MinLengthTuple, type StructuralPrefixLength } from 'ts-type-forge';
|
|
2
|
+
import { type Type } from '../type.mjs';
|
|
3
|
+
export type { BoundedLengthTuple } from 'ts-type-forge';
|
|
4
|
+
export declare function boundedLengthTuple<A, Min extends StructuralPrefixLength, Max extends StructuralPrefixLength>(min: Min, max: Max, elementType: Type<A>, options?: Partial<Readonly<{
|
|
5
|
+
typeName: string;
|
|
6
|
+
defaultValue: BoundedLengthTuple<Min, Max, A>;
|
|
7
|
+
}>>): Type<BoundedLengthTuple<Min, Max, A>>;
|
|
8
|
+
export declare function boundedLengthTuple<A, Min extends StructuralPrefixLength>(min: Min, max: number, elementType: Type<A>, options?: Partial<Readonly<{
|
|
9
|
+
typeName: string;
|
|
10
|
+
defaultValue: MinLengthTuple<Min, A>;
|
|
11
|
+
}>>): Type<MinLengthTuple<Min, A>>;
|
|
12
|
+
export declare function boundedLengthTuple<A, Max extends StructuralPrefixLength>(min: number, max: Max, elementType: Type<A>, options?: Partial<Readonly<{
|
|
13
|
+
typeName: string;
|
|
14
|
+
defaultValue: MaxLengthTuple<Max, A>;
|
|
15
|
+
}>>): Type<MaxLengthTuple<Max, A>>;
|
|
16
|
+
export declare function boundedLengthTuple<A>(min: number, max: number, elementType: Type<A>, options?: Partial<Readonly<{
|
|
17
|
+
typeName: string;
|
|
18
|
+
defaultValue: readonly A[];
|
|
19
|
+
}>>): Type<readonly A[]>;
|
|
20
|
+
//# sourceMappingURL=bounded-length-tuple.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bounded-length-tuple.d.mts","sourceRoot":"","sources":["../../src/array/bounded-length-tuple.mts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,sBAAsB,EAC5B,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAUxC,YAAY,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAExD,wBAAgB,kBAAkB,CAChC,CAAC,EACD,GAAG,SAAS,sBAAsB,EAClC,GAAG,SAAS,sBAAsB,EAElC,GAAG,EAAE,GAAG,EACR,GAAG,EAAE,GAAG,EACR,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;CAC/C,CAAC,CACH,GACA,IAAI,CAAC,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;AAIzC,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,GAAG,SAAS,sBAAsB,EACtE,GAAG,EAAE,GAAG,EACR,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,cAAc,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;CACtC,CAAC,CACH,GACA,IAAI,CAAC,cAAc,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;AAIhC,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,GAAG,SAAS,sBAAsB,EACtE,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,GAAG,EACR,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,cAAc,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;CACtC,CAAC,CACH,GACA,IAAI,CAAC,cAAc,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;AAGhC,wBAAgB,kBAAkB,CAAC,CAAC,EAClC,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,SAAS,CAAC,EAAE,CAAC;CAC5B,CAAC,CACH,GACA,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC"}
|
|
@@ -4,8 +4,8 @@ import { createCastFn } from '../utils/create-cast-fn.mjs';
|
|
|
4
4
|
import { createIsFn } from '../utils/create-is-fn.mjs';
|
|
5
5
|
import { createPrimitiveValidationError, prependIndexToValidationErrors } from '../utils/validation-error.mjs';
|
|
6
6
|
|
|
7
|
-
function
|
|
8
|
-
const typeName = options?.typeName ?? `
|
|
7
|
+
function boundedLengthTuple(min, max, elementType, options) {
|
|
8
|
+
const typeName = options?.typeName ?? `BoundedLengthTuple<${min}, ${max}, ${elementType.typeName}>`;
|
|
9
9
|
const getDefaultValue = memoizeFunction(
|
|
10
10
|
() => options?.defaultValue ?? // An array of the minimum length is the shortest value within the range.
|
|
11
11
|
Arr.create(asUint32(min), elementType.defaultValue)
|
|
@@ -75,5 +75,5 @@ function arrayBoundedLength(min, max, elementType, options) {
|
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
export {
|
|
79
|
-
//# sourceMappingURL=
|
|
78
|
+
export { boundedLengthTuple };
|
|
79
|
+
//# sourceMappingURL=bounded-length-tuple.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bounded-length-tuple.mjs","sources":["../../src/array/bounded-length-tuple.mts"],"sourcesContent":["import { Arr, asUint32, memoizeFunction, Result } from 'ts-data-forge';\nimport {\n type BoundedLengthTuple,\n type MaxLengthTuple,\n type MinLengthTuple,\n type StructuralPrefixLength,\n} from 'ts-type-forge';\nimport { type Type } from '../type.mjs';\nimport {\n createAssertFn,\n createCastFn,\n createIsFn,\n createPrimitiveValidationError,\n prependIndexToValidationErrors,\n type ValidationError,\n} from '../utils/index.mjs';\n\nexport type { BoundedLengthTuple } from 'ts-type-forge';\n\nexport function boundedLengthTuple<\n A,\n Min extends StructuralPrefixLength,\n Max extends StructuralPrefixLength,\n>(\n min: Min,\n max: Max,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: BoundedLengthTuple<Min, Max, A>;\n }>\n >,\n): Type<BoundedLengthTuple<Min, Max, A>>;\n\n// Only the lower bound is in `StructuralPrefixLength` (`0..10`), so the upper bound is dropped from the\n// result type and only the \"at least `min`\" guarantee is kept.\nexport function boundedLengthTuple<A, Min extends StructuralPrefixLength>(\n min: Min,\n max: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: MinLengthTuple<Min, A>;\n }>\n >,\n): Type<MinLengthTuple<Min, A>>;\n\n// Only the upper bound is in `StructuralPrefixLength` (`0..10`), so the lower bound is dropped from the\n// result type and only the \"at most `max`\" guarantee is kept.\nexport function boundedLengthTuple<A, Max extends StructuralPrefixLength>(\n min: number,\n max: Max,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: MaxLengthTuple<Max, A>;\n }>\n >,\n): Type<MaxLengthTuple<Max, A>>;\n\n// Neither bound is in `StructuralPrefixLength` (`0..10`), so the result length is left unconstrained.\nexport function boundedLengthTuple<A>(\n min: number,\n max: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: readonly A[];\n }>\n >,\n): Type<readonly A[]>;\n\nexport function boundedLengthTuple<A>(\n min: number,\n max: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: readonly A[];\n }>\n >,\n): Type<readonly A[]> {\n type T = readonly A[];\n\n const typeName =\n options?.typeName ??\n `BoundedLengthTuple<${min}, ${max}, ${elementType.typeName}>`;\n\n const getDefaultValue = memoizeFunction(\n (): T =>\n options?.defaultValue ??\n // An array of the minimum length is the shortest value within the range.\n Arr.create(asUint32(min), elementType.defaultValue),\n );\n\n const validate: Type<T>['validate'] = (a) => {\n if (!Arr.isArray(a)) {\n return Result.err([\n createPrimitiveValidationError({\n actualValue: a,\n expectedType: 'array',\n typeName,\n details: undefined,\n }),\n ]);\n }\n\n if (a.length < min || max < a.length) {\n return Result.err([\n {\n path: [],\n actualValue: a,\n expectedType: typeName,\n typeName,\n details: {\n kind: 'array-range-length',\n minLength: min,\n maxLength: max,\n actualLength: a.length,\n },\n } satisfies ValidationError,\n ]);\n }\n\n const errors: readonly ValidationError[] = Arr.generate(function* () {\n for (const [index, el] of a.entries()) {\n const res = elementType.validate(el);\n\n if (Result.isErr(res)) {\n yield* prependIndexToValidationErrors(res.value, index);\n }\n }\n });\n\n if (Arr.isNonEmpty(errors)) {\n return Result.err(errors);\n }\n\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n return Result.ok(a as unknown as T);\n };\n\n const fill: Type<T>['fill'] = (a) => {\n if (!Arr.isArray(a)) {\n return getDefaultValue();\n }\n\n // Trim down to at most `max`, then pad up to at least `min`.\n const capped = Arr.take(a, asUint32(max));\n\n return capped.length >= min\n ? Arr.map(capped, (el) => elementType.fill(el) satisfies A)\n : Arr.map(\n Arr.seq(asUint32(min)),\n (i) => elementType.fill(capped[i]) satisfies A,\n );\n };\n\n const prune = (a: T): T => a.map((el) => elementType.prune(el));\n\n return {\n typeName,\n get defaultValue() {\n return getDefaultValue();\n },\n fill,\n prune,\n validate,\n is: createIsFn(validate),\n cast: createCastFn(validate),\n assertIs: createAssertFn(validate),\n };\n}\n"],"names":[],"mappings":";;;;;;AA4EO,SAAS,kBAAA,CACd,GAAA,EACA,GAAA,EACA,WAAA,EACA,OAAA,EAMoB;AAGpB,EAAA,MAAM,QAAA,GACJ,SAAS,QAAA,IACT,CAAA,mBAAA,EAAsB,GAAG,CAAA,EAAA,EAAK,GAAG,CAAA,EAAA,EAAK,WAAA,CAAY,QAAQ,CAAA,CAAA,CAAA;AAE5D,EAAA,MAAM,eAAA,GAAkB,eAAA;AAAA,IACtB,MACE,OAAA,EAAS,YAAA;AAAA,IAET,IAAI,MAAA,CAAO,QAAA,CAAS,GAAG,CAAA,EAAG,YAAY,YAAY;AAAA,GACtD;AAEA,EAAA,MAAM,QAAA,GAAgC,CAAC,CAAA,KAAM;AAC3C,IAAA,IAAI,CAAC,GAAA,CAAI,OAAA,CAAQ,CAAC,CAAA,EAAG;AACnB,MAAA,OAAO,OAAO,GAAA,CAAI;AAAA,QAChB,8BAAA,CAA+B;AAAA,UAC7B,WAAA,EAAa,CAAA;AAAA,UACb,YAAA,EAAc,OAAA;AAAA,UACd,QAAA;AAAA,UACA,OAAA,EAAS;AAAA,SACV;AAAA,OACF,CAAA;AAAA,IACH;AAEA,IAAA,IAAI,CAAA,C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AA,CAAO,MAAA,IAAU,GAAA,GACpB,GAAA,CAAI,GAAA,CAAI,MAAA,EAAQ,CAAC,EAAA,KAAO,WAAA,CAAY,IAAA,CAAK,EAAE,CAAa,IACxD,GAAA,CAAI,GAAA;AAAA,MACF,GAAA,CAAI,GAAA,CAAI,QAAA,CAAS,GAAG,CAAC,CAAA;AAAA,MACrB,CAAC,CAAA,KAAM,WAAA,CAAY,IAAA,CAAK,MAAA,CAAO,CAAC,CAAC;AAAA,KACnC;AAAA,EACN,CAAA;AAEA,EAAA,MAAM,KAAA,GAAQ,CAAC,CAAA,KAAY,CAAA,CAAE,GAAA,CAAI,CAAC,EAAA,KAAO,WAAA,CAAY,KAAA,CAAM,EAAE,CAAC,CAAA;AAE9D,EAAA,OAAO;AAAA,IACL,QAAA;AAAA,IACA,IAAI,YAAA,GAAe;AACjB,MAAA,OAAO,eAAA,EAAgB;AAAA,IACzB,CAAA;AAAA,IACA,IAAA;AAAA,IACA,KAAA;AAAA,IACA,QAAA;AAAA,IACA,EAAA,EAAI,WAAW,QAAQ,CAAA;AAAA,IACvB,IAAA,EAAM,aAAa,QAAQ,CAAA;AAAA,IAC3B,QAAA,EAAU,eAAe,QAAQ;AAAA,GACnC;AACF;;;;"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type FixedLengthTuple, type StructuralPrefixLength } from 'ts-type-forge';
|
|
2
|
+
import { type Type } from '../type.mjs';
|
|
3
|
+
export type { FixedLengthTuple } from 'ts-type-forge';
|
|
4
|
+
export declare const fixedLengthTuple: <A, N extends StructuralPrefixLength>(size: N, elementType: Type<A>, options?: Partial<Readonly<{
|
|
5
|
+
typeName: string;
|
|
6
|
+
defaultValue: FixedLengthTuple<N, A>;
|
|
7
|
+
}>>) => Type<FixedLengthTuple<N, A>>;
|
|
8
|
+
//# sourceMappingURL=fixed-length-tuple.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fixed-length-tuple.d.mts","sourceRoot":"","sources":["../../src/array/fixed-length-tuple.mts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,sBAAsB,EAC5B,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAUxC,YAAY,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEtD,eAAO,MAAM,gBAAgB,GAAI,CAAC,EAAE,CAAC,SAAS,sBAAsB,QAC5D,CAAC,eACM,IAAI,CAAC,CAAC,CAAC,YACV,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;CACtC,CAAC,CACH,KACA,IAAI,CAAC,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAoF7B,CAAC"}
|
|
@@ -4,8 +4,8 @@ import { createCastFn } from '../utils/create-cast-fn.mjs';
|
|
|
4
4
|
import { createIsFn } from '../utils/create-is-fn.mjs';
|
|
5
5
|
import { createPrimitiveValidationError, prependIndexToValidationErrors } from '../utils/validation-error.mjs';
|
|
6
6
|
|
|
7
|
-
const
|
|
8
|
-
const typeName = options?.typeName ?? `
|
|
7
|
+
const fixedLengthTuple = (size, elementType, options) => {
|
|
8
|
+
const typeName = options?.typeName ?? `FixedLengthTuple<${size}, ${elementType.typeName}>`;
|
|
9
9
|
const getDefaultValue = memoizeFunction(
|
|
10
10
|
() => options?.defaultValue ?? // eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
11
11
|
Arr.create(size, elementType.defaultValue)
|
|
@@ -21,7 +21,7 @@ const arrayOfLength = (size, elementType, options) => {
|
|
|
21
21
|
})
|
|
22
22
|
]);
|
|
23
23
|
}
|
|
24
|
-
if (!Arr.
|
|
24
|
+
if (!Arr.isFixedLengthTuple(a, size)) {
|
|
25
25
|
return Result.err([
|
|
26
26
|
{
|
|
27
27
|
path: [],
|
|
@@ -75,5 +75,5 @@ const arrayOfLength = (size, elementType, options) => {
|
|
|
75
75
|
};
|
|
76
76
|
};
|
|
77
77
|
|
|
78
|
-
export {
|
|
79
|
-
//# sourceMappingURL=
|
|
78
|
+
export { fixedLengthTuple };
|
|
79
|
+
//# sourceMappingURL=fixed-length-tuple.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fixed-length-tuple.mjs","sources":["../../src/array/fixed-length-tuple.mts"],"sourcesContent":["import { Arr, memoizeFunction, Result } from 'ts-data-forge';\nimport {\n type FixedLengthTuple,\n type StructuralPrefixLength,\n} from 'ts-type-forge';\nimport { type Type } from '../type.mjs';\nimport {\n createAssertFn,\n createCastFn,\n createIsFn,\n createPrimitiveValidationError,\n prependIndexToValidationErrors,\n type ValidationError,\n} from '../utils/index.mjs';\n\nexport type { FixedLengthTuple } from 'ts-type-forge';\n\nexport const fixedLengthTuple = <A, N extends StructuralPrefixLength>(\n size: N,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: FixedLengthTuple<N, A>;\n }>\n >,\n): Type<FixedLengthTuple<N, A>> => {\n type T = FixedLengthTuple<N, A>;\n\n const typeName =\n options?.typeName ?? `FixedLengthTuple<${size}, ${elementType.typeName}>`;\n\n const getDefaultValue = memoizeFunction(\n (): T =>\n options?.defaultValue ??\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n (Arr.create(size, elementType.defaultValue) as T),\n );\n\n const validate: Type<T>['validate'] = (a) => {\n if (!Arr.isArray(a)) {\n return Result.err([\n createPrimitiveValidationError({\n actualValue: a,\n expectedType: 'array',\n typeName,\n details: undefined,\n }),\n ]);\n }\n\n if (!Arr.isFixedLengthTuple(a, size)) {\n return Result.err([\n {\n path: [],\n actualValue: a,\n expectedType: typeName,\n typeName,\n details: {\n kind: 'array-length',\n expectedLength: size,\n actualLength: a.length,\n },\n } satisfies ValidationError,\n ]);\n }\n\n const errors: readonly ValidationError[] = Arr.generate(function* () {\n for (const [index, el] of a.entries()) {\n const res = elementType.validate(el);\n\n if (Result.isErr(res)) {\n yield* prependIndexToValidationErrors(res.value, index);\n }\n }\n });\n\n if (Arr.isNonEmpty(errors)) {\n return Result.err(errors);\n }\n\n return Result.ok(a);\n };\n\n const fill: Type<T>['fill'] = (a) =>\n !Arr.isArray(a)\n ? getDefaultValue()\n : // TODO: remove as\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n (Arr.map(\n Arr.seq(size),\n (i) => elementType.fill(a[i]) satisfies A,\n ) as T);\n\n const prune = (a: T): T =>\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Arr.map(a, (el) => elementType.prune(el)) as T;\n\n return {\n typeName,\n get defaultValue() {\n return getDefaultValue();\n },\n fill,\n prune,\n validate,\n is: createIsFn(validate),\n cast: createCastFn(validate),\n assertIs: createAssertFn(validate),\n };\n};\n"],"names":[],"mappings":";;;;;;AAiBO,MAAM,gBAAA,GAAmB,CAC9B,IAAA,EACA,WAAA,EACA,OAAA,KAMiC;AAGjC,EAAA,MAAM,WACJ,OAAA,EAAS,QAAA,IAAY,oBAAoB,IAAI,CAAA,EAAA,EAAK,YAAY,QAAQ,CAAA,CAAA,CAAA;AAExE,EAAA,MAAM,eAAA,GAAkB,eAAA;AAAA,IACtB,MACE,OAAA,EAAS,YAAA;AAAA,IAER,GAAA,CAAI,MAAA,CAAO,IAAA,EAAM,WAAA,CAAY,YAAY;AAAA,GAC9C;AAEA,EAAA,MAAM,QAAA,GAAgC,CAAC,CAAA,KAAM;AAC3C,IAAA,IAAI,CAAC,GAAA,CAAI,OAAA,CAAQ,CAAC,CAAA,EAAG;AACnB,MAAA,OAAO,OAAO,GAAA,CAAI;AAAA,QAChB,8BAAA,CAA+B;AAAA,UAC7B,WAAA,EAAa,CAAA;AAAA,UACb,YAAA,EAAc,OAAA;AAAA,UACd,QAAA;AAAA,UACA,OAAA,EAAS;AAAA,SACV;AAAA,OACF,CAAA;AAAA,IACH;AAEA,IAAA,IAAI,CAAC,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,CAAC,CAAA;AAAA;AAAA,IAEb,GAAA,CAAI,IAAI,CAAA,EAAG,CAAC,OAAO,WAAA,CAAY,KAAA,CAAM,EAAE,CAAC;AAAA,GAAA;AAE1C,EAAA,OAAO;AAAA,IACL,QAAA;AAAA,IACA,IAAI,YAAA,GAAe;AACjB,MAAA,OAAO,eAAA,EAAgB;AAAA,IACzB,CAAA;AAAA,IACA,IAAA;AAAA,IACA,KAAA;AAAA,IACA,QAAA;AAAA,IACA,EAAA,EAAI,WAAW,QAAQ,CAAA;AAAA,IACvB,IAAA,EAAM,aAAa,QAAQ,CAAA;AAAA,IAC3B,QAAA,EAAU,eAAe,QAAQ;AAAA,GACnC;AACF;;;;"}
|
package/dist/array/index.d.mts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export * from './array-at-least-length.mjs';
|
|
2
|
-
export * from './array-at-most-length.mjs';
|
|
3
|
-
export * from './array-bounded-length.mjs';
|
|
4
|
-
export * from './array-of-length.mjs';
|
|
5
1
|
export * from './array.mjs';
|
|
2
|
+
export * from './bounded-length-tuple.mjs';
|
|
3
|
+
export * from './fixed-length-tuple.mjs';
|
|
4
|
+
export * from './length-constrained-array.mjs';
|
|
5
|
+
export * from './max-length-tuple.mjs';
|
|
6
|
+
export * from './min-length-tuple.mjs';
|
|
6
7
|
export * from './non-empty-array.mjs';
|
|
7
8
|
export * from './tuple.mjs';
|
|
8
9
|
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/array/index.mts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/array/index.mts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC"}
|
package/dist/array/index.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export { arrayAtLeastLength } from './array-at-least-length.mjs';
|
|
2
|
-
export { arrayAtMostLength } from './array-at-most-length.mjs';
|
|
3
|
-
export { arrayBoundedLength } from './array-bounded-length.mjs';
|
|
4
|
-
export { arrayOfLength } from './array-of-length.mjs';
|
|
5
1
|
export { array } from './array.mjs';
|
|
2
|
+
export { boundedLengthTuple } from './bounded-length-tuple.mjs';
|
|
3
|
+
export { fixedLengthTuple } from './fixed-length-tuple.mjs';
|
|
4
|
+
export { boundedLengthArray, fixedLengthArray, maxLengthArray, minLengthArray } from './length-constrained-array.mjs';
|
|
5
|
+
export { maxLengthTuple } from './max-length-tuple.mjs';
|
|
6
|
+
export { minLengthTuple } from './min-length-tuple.mjs';
|
|
6
7
|
export { nonEmptyArray } from './non-empty-array.mjs';
|
|
7
8
|
export { tuple } from './tuple.mjs';
|
|
8
9
|
//# sourceMappingURL=index.mjs.map
|
package/dist/array/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type BoundedLengthArray, type FixedLengthArray, type MaxLengthArray, type MinLengthArray, type SupportedLength } from 'ts-type-forge';
|
|
2
|
+
import { type Type } from '../type.mjs';
|
|
3
|
+
export type { BoundedLengthArray, FixedLengthArray, MaxLengthArray, MinLengthArray, } from 'ts-type-forge';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a `Type` for a readonly array with exactly `size` elements, typed as
|
|
6
|
+
* the branded {@link FixedLengthArray} instead of the structural tuple type
|
|
7
|
+
* produced by `fixedLengthTuple`.
|
|
8
|
+
*
|
|
9
|
+
* Because the length constraint lives only in the brand, the element type is
|
|
10
|
+
* never expanded into tuple positions, which keeps type-checking cheap even
|
|
11
|
+
* when `A` is a large type. Prefer this over `fixedLengthTuple` when `A` is
|
|
12
|
+
* large; prefer `fixedLengthTuple` when positional element types or a literal
|
|
13
|
+
* `length` are needed.
|
|
14
|
+
*/
|
|
15
|
+
export declare function fixedLengthArray<A, N extends SupportedLength>(size: N, elementType: Type<A>, options?: Partial<Readonly<{
|
|
16
|
+
typeName: string;
|
|
17
|
+
defaultValue: FixedLengthArray<N, A>;
|
|
18
|
+
}>>): Type<FixedLengthArray<N, A>>;
|
|
19
|
+
export declare function fixedLengthArray<A>(size: number, elementType: Type<A>, options?: Partial<Readonly<{
|
|
20
|
+
typeName: string;
|
|
21
|
+
defaultValue: readonly A[];
|
|
22
|
+
}>>): Type<readonly A[]>;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a `Type` for a readonly array with at least `minLength` elements,
|
|
25
|
+
* typed as the branded {@link MinLengthArray} instead of the structural
|
|
26
|
+
* `readonly [A, ..., A, ...A[]]` type produced by `minLengthTuple`.
|
|
27
|
+
*
|
|
28
|
+
* Because the length constraint lives only in the brand, the element type is
|
|
29
|
+
* never expanded into tuple positions, which keeps type-checking cheap even
|
|
30
|
+
* when `A` is a large type.
|
|
31
|
+
*/
|
|
32
|
+
export declare function minLengthArray<A, N extends SupportedLength>(minLength: N, elementType: Type<A>, options?: Partial<Readonly<{
|
|
33
|
+
typeName: string;
|
|
34
|
+
defaultValue: MinLengthArray<N, A>;
|
|
35
|
+
}>>): Type<MinLengthArray<N, A>>;
|
|
36
|
+
export declare function minLengthArray<A>(minLength: number, elementType: Type<A>, options?: Partial<Readonly<{
|
|
37
|
+
typeName: string;
|
|
38
|
+
defaultValue: readonly A[];
|
|
39
|
+
}>>): Type<readonly A[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a `Type` for a readonly array with at most `maxLength` elements,
|
|
42
|
+
* typed as the branded {@link MaxLengthArray} instead of the union of tuple
|
|
43
|
+
* types produced by `maxLengthTuple`.
|
|
44
|
+
*
|
|
45
|
+
* Because the length constraint lives only in the brand, no union of tuples
|
|
46
|
+
* of the element type is ever constructed, which keeps type-checking cheap
|
|
47
|
+
* even when `A` is a large type.
|
|
48
|
+
*/
|
|
49
|
+
export declare function maxLengthArray<A, N extends SupportedLength>(maxLength: N, elementType: Type<A>, options?: Partial<Readonly<{
|
|
50
|
+
typeName: string;
|
|
51
|
+
defaultValue: MaxLengthArray<N, A>;
|
|
52
|
+
}>>): Type<MaxLengthArray<N, A>>;
|
|
53
|
+
export declare function maxLengthArray<A>(maxLength: number, elementType: Type<A>, options?: Partial<Readonly<{
|
|
54
|
+
typeName: string;
|
|
55
|
+
defaultValue: readonly A[];
|
|
56
|
+
}>>): Type<readonly A[]>;
|
|
57
|
+
/**
|
|
58
|
+
* Creates a `Type` for a readonly array whose length is within the inclusive
|
|
59
|
+
* range `[min, max]`, typed as the branded {@link BoundedLengthArray} instead
|
|
60
|
+
* of the union of tuple types produced by `boundedLengthTuple`.
|
|
61
|
+
*
|
|
62
|
+
* Because the length constraint lives only in the brand, no union of tuples
|
|
63
|
+
* of the element type is ever constructed, which keeps type-checking cheap
|
|
64
|
+
* even when `A` is a large type.
|
|
65
|
+
*/
|
|
66
|
+
export declare function boundedLengthArray<A, Min extends SupportedLength, Max extends SupportedLength>(min: Min, max: Max, elementType: Type<A>, options?: Partial<Readonly<{
|
|
67
|
+
typeName: string;
|
|
68
|
+
defaultValue: BoundedLengthArray<Min, Max, A>;
|
|
69
|
+
}>>): Type<BoundedLengthArray<Min, Max, A>>;
|
|
70
|
+
export declare function boundedLengthArray<A, Min extends SupportedLength>(min: Min, max: number, elementType: Type<A>, options?: Partial<Readonly<{
|
|
71
|
+
typeName: string;
|
|
72
|
+
defaultValue: MinLengthArray<Min, A>;
|
|
73
|
+
}>>): Type<MinLengthArray<Min, A>>;
|
|
74
|
+
export declare function boundedLengthArray<A, Max extends SupportedLength>(min: number, max: Max, elementType: Type<A>, options?: Partial<Readonly<{
|
|
75
|
+
typeName: string;
|
|
76
|
+
defaultValue: MaxLengthArray<Max, A>;
|
|
77
|
+
}>>): Type<MaxLengthArray<Max, A>>;
|
|
78
|
+
export declare function boundedLengthArray<A>(min: number, max: number, elementType: Type<A>, options?: Partial<Readonly<{
|
|
79
|
+
typeName: string;
|
|
80
|
+
defaultValue: readonly A[];
|
|
81
|
+
}>>): Type<readonly A[]>;
|
|
82
|
+
//# sourceMappingURL=length-constrained-array.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"length-constrained-array.d.mts","sourceRoot":"","sources":["../../src/array/length-constrained-array.mts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,eAAe,EACrB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAUxC,YAAY,EACV,kBAAkB,EAClB,gBAAgB,EAChB,cAAc,EACd,cAAc,GACf,MAAM,eAAe,CAAC;AAEvB;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,CAAC,SAAS,eAAe,EAC3D,IAAI,EAAE,CAAC,EACP,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;CACtC,CAAC,CACH,GACA,IAAI,CAAC,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAIhC,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,OAAO,CACf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IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,cAAc,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;CACtC,CAAC,CACH,GACA,IAAI,CAAC,cAAc,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;AAGhC,wBAAgB,kBAAkB,CAAC,CAAC,EAClC,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,SAAS,CAAC,EAAE,CAAC;CAC5B,CAAC,CACH,GACA,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC"}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { memoizeFunction, Arr, asUint32, Result } from 'ts-data-forge';
|
|
2
|
+
import { createAssertFn } from '../utils/create-assert-fn.mjs';
|
|
3
|
+
import { createCastFn } from '../utils/create-cast-fn.mjs';
|
|
4
|
+
import { createIsFn } from '../utils/create-is-fn.mjs';
|
|
5
|
+
import { createPrimitiveValidationError, prependIndexToValidationErrors } from '../utils/validation-error.mjs';
|
|
6
|
+
|
|
7
|
+
function fixedLengthArray(size, elementType, options) {
|
|
8
|
+
const typeName = options?.typeName ?? `FixedLengthArray<${size}, ${elementType.typeName}>`;
|
|
9
|
+
const getDefaultValue = memoizeFunction(
|
|
10
|
+
() => options?.defaultValue ?? Arr.create(asUint32(size), elementType.defaultValue)
|
|
11
|
+
);
|
|
12
|
+
const validate = (a) => {
|
|
13
|
+
if (!Arr.isArray(a)) {
|
|
14
|
+
return Result.err([
|
|
15
|
+
createPrimitiveValidationError({
|
|
16
|
+
actualValue: a,
|
|
17
|
+
expectedType: "array",
|
|
18
|
+
typeName,
|
|
19
|
+
details: void 0
|
|
20
|
+
})
|
|
21
|
+
]);
|
|
22
|
+
}
|
|
23
|
+
if (a.length !== size) {
|
|
24
|
+
return Result.err([
|
|
25
|
+
{
|
|
26
|
+
path: [],
|
|
27
|
+
actualValue: a,
|
|
28
|
+
expectedType: typeName,
|
|
29
|
+
typeName,
|
|
30
|
+
details: {
|
|
31
|
+
kind: "array-length",
|
|
32
|
+
expectedLength: size,
|
|
33
|
+
actualLength: a.length
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
]);
|
|
37
|
+
}
|
|
38
|
+
return validateElements(a, elementType);
|
|
39
|
+
};
|
|
40
|
+
const fill = (a) => Arr.isArray(a) ? Arr.map(
|
|
41
|
+
Arr.seq(asUint32(size)),
|
|
42
|
+
(i) => elementType.fill(a[i])
|
|
43
|
+
) : getDefaultValue();
|
|
44
|
+
return buildType({ typeName, getDefaultValue, validate, fill, elementType });
|
|
45
|
+
}
|
|
46
|
+
function minLengthArray(minLength, elementType, options) {
|
|
47
|
+
const typeName = options?.typeName ?? `MinLengthArray<${minLength}, ${elementType.typeName}>`;
|
|
48
|
+
const getDefaultValue = memoizeFunction(
|
|
49
|
+
() => options?.defaultValue ?? // An array of `minLength` elements is the shortest value satisfying the bound.
|
|
50
|
+
Arr.create(asUint32(minLength), elementType.defaultValue)
|
|
51
|
+
);
|
|
52
|
+
const validate = (a) => {
|
|
53
|
+
if (!Arr.isArray(a)) {
|
|
54
|
+
return Result.err([
|
|
55
|
+
createPrimitiveValidationError({
|
|
56
|
+
actualValue: a,
|
|
57
|
+
expectedType: "array",
|
|
58
|
+
typeName,
|
|
59
|
+
details: void 0
|
|
60
|
+
})
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
63
|
+
if (a.length < minLength) {
|
|
64
|
+
return Result.err([
|
|
65
|
+
{
|
|
66
|
+
path: [],
|
|
67
|
+
actualValue: a,
|
|
68
|
+
expectedType: typeName,
|
|
69
|
+
typeName,
|
|
70
|
+
details: {
|
|
71
|
+
kind: "array-min-length",
|
|
72
|
+
minLength,
|
|
73
|
+
actualLength: a.length
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
]);
|
|
77
|
+
}
|
|
78
|
+
return validateElements(a, elementType);
|
|
79
|
+
};
|
|
80
|
+
const fill = (a) => {
|
|
81
|
+
if (!Arr.isArray(a)) {
|
|
82
|
+
return getDefaultValue();
|
|
83
|
+
}
|
|
84
|
+
return a.length >= minLength ? Arr.map(a, (el) => elementType.fill(el)) : Arr.map(
|
|
85
|
+
Arr.seq(asUint32(minLength)),
|
|
86
|
+
(i) => elementType.fill(a[i])
|
|
87
|
+
);
|
|
88
|
+
};
|
|
89
|
+
return buildType({ typeName, getDefaultValue, validate, fill, elementType });
|
|
90
|
+
}
|
|
91
|
+
function maxLengthArray(maxLength, elementType, options) {
|
|
92
|
+
const typeName = options?.typeName ?? `MaxLengthArray<${maxLength}, ${elementType.typeName}>`;
|
|
93
|
+
const getDefaultValue = memoizeFunction(
|
|
94
|
+
() => options?.defaultValue ?? // The empty array is the shortest value satisfying `length <= maxLength`.
|
|
95
|
+
Arr.create(0, elementType.defaultValue)
|
|
96
|
+
);
|
|
97
|
+
const validate = (a) => {
|
|
98
|
+
if (!Arr.isArray(a)) {
|
|
99
|
+
return Result.err([
|
|
100
|
+
createPrimitiveValidationError({
|
|
101
|
+
actualValue: a,
|
|
102
|
+
expectedType: "array",
|
|
103
|
+
typeName,
|
|
104
|
+
details: void 0
|
|
105
|
+
})
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
108
|
+
if (a.length > maxLength) {
|
|
109
|
+
return Result.err([
|
|
110
|
+
{
|
|
111
|
+
path: [],
|
|
112
|
+
actualValue: a,
|
|
113
|
+
expectedType: typeName,
|
|
114
|
+
typeName,
|
|
115
|
+
details: {
|
|
116
|
+
kind: "array-max-length",
|
|
117
|
+
maxLength,
|
|
118
|
+
actualLength: a.length
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
]);
|
|
122
|
+
}
|
|
123
|
+
return validateElements(a, elementType);
|
|
124
|
+
};
|
|
125
|
+
const fill = (a) => Arr.isArray(a) ? (
|
|
126
|
+
// Keep the input but trim down to at most `maxLength` elements.
|
|
127
|
+
Arr.map(
|
|
128
|
+
Arr.take(a, asUint32(maxLength)),
|
|
129
|
+
(el) => elementType.fill(el)
|
|
130
|
+
)
|
|
131
|
+
) : getDefaultValue();
|
|
132
|
+
return buildType({ typeName, getDefaultValue, validate, fill, elementType });
|
|
133
|
+
}
|
|
134
|
+
function boundedLengthArray(min, max, elementType, options) {
|
|
135
|
+
const typeName = options?.typeName ?? `BoundedLengthArray<${min}, ${max}, ${elementType.typeName}>`;
|
|
136
|
+
const getDefaultValue = memoizeFunction(
|
|
137
|
+
() => options?.defaultValue ?? // An array of the minimum length is the shortest value within the range.
|
|
138
|
+
Arr.create(asUint32(min), elementType.defaultValue)
|
|
139
|
+
);
|
|
140
|
+
const validate = (a) => {
|
|
141
|
+
if (!Arr.isArray(a)) {
|
|
142
|
+
return Result.err([
|
|
143
|
+
createPrimitiveValidationError({
|
|
144
|
+
actualValue: a,
|
|
145
|
+
expectedType: "array",
|
|
146
|
+
typeName,
|
|
147
|
+
details: void 0
|
|
148
|
+
})
|
|
149
|
+
]);
|
|
150
|
+
}
|
|
151
|
+
if (a.length < min || max < a.length) {
|
|
152
|
+
return Result.err([
|
|
153
|
+
{
|
|
154
|
+
path: [],
|
|
155
|
+
actualValue: a,
|
|
156
|
+
expectedType: typeName,
|
|
157
|
+
typeName,
|
|
158
|
+
details: {
|
|
159
|
+
kind: "array-range-length",
|
|
160
|
+
minLength: min,
|
|
161
|
+
maxLength: max,
|
|
162
|
+
actualLength: a.length
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
]);
|
|
166
|
+
}
|
|
167
|
+
return validateElements(a, elementType);
|
|
168
|
+
};
|
|
169
|
+
const fill = (a) => {
|
|
170
|
+
if (!Arr.isArray(a)) {
|
|
171
|
+
return getDefaultValue();
|
|
172
|
+
}
|
|
173
|
+
const capped = Arr.take(a, asUint32(max));
|
|
174
|
+
return capped.length >= min ? Arr.map(capped, (el) => elementType.fill(el)) : Arr.map(
|
|
175
|
+
Arr.seq(asUint32(min)),
|
|
176
|
+
(i) => elementType.fill(capped[i])
|
|
177
|
+
);
|
|
178
|
+
};
|
|
179
|
+
return buildType({ typeName, getDefaultValue, validate, fill, elementType });
|
|
180
|
+
}
|
|
181
|
+
const validateElements = (a, elementType) => {
|
|
182
|
+
const errors = Arr.generate(function* () {
|
|
183
|
+
for (const [index, el] of a.entries()) {
|
|
184
|
+
const res = elementType.validate(el);
|
|
185
|
+
if (Result.isErr(res)) {
|
|
186
|
+
yield* prependIndexToValidationErrors(res.value, index);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
if (Arr.isNonEmpty(errors)) {
|
|
191
|
+
return Result.err(errors);
|
|
192
|
+
}
|
|
193
|
+
return Result.ok(a);
|
|
194
|
+
};
|
|
195
|
+
const buildType = ({
|
|
196
|
+
typeName,
|
|
197
|
+
getDefaultValue,
|
|
198
|
+
validate,
|
|
199
|
+
fill,
|
|
200
|
+
elementType
|
|
201
|
+
}) => ({
|
|
202
|
+
typeName,
|
|
203
|
+
get defaultValue() {
|
|
204
|
+
return getDefaultValue();
|
|
205
|
+
},
|
|
206
|
+
fill,
|
|
207
|
+
prune: (a) => a.map((el) => elementType.prune(el)),
|
|
208
|
+
validate,
|
|
209
|
+
is: createIsFn(validate),
|
|
210
|
+
cast: createCastFn(validate),
|
|
211
|
+
assertIs: createAssertFn(validate)
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
export { boundedLengthArray, fixedLengthArray, maxLengthArray, minLengthArray };
|
|
215
|
+
//# sourceMappingURL=length-constrained-array.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"length-constrained-array.mjs","sources":["../../src/array/length-constrained-array.mts"],"sourcesContent":["import { Arr, asUint32, memoizeFunction, Result } from 'ts-data-forge';\nimport {\n type BoundedLengthArray,\n type FixedLengthArray,\n type MaxLengthArray,\n type MinLengthArray,\n type SupportedLength,\n} from 'ts-type-forge';\nimport { type Type } from '../type.mjs';\nimport {\n createAssertFn,\n createCastFn,\n createIsFn,\n createPrimitiveValidationError,\n prependIndexToValidationErrors,\n type ValidationError,\n} from '../utils/index.mjs';\n\nexport type {\n BoundedLengthArray,\n FixedLengthArray,\n MaxLengthArray,\n MinLengthArray,\n} from 'ts-type-forge';\n\n/**\n * Creates a `Type` for a readonly array with exactly `size` elements, typed as\n * the branded {@link FixedLengthArray} instead of the structural tuple type\n * produced by `fixedLengthTuple`.\n *\n * Because the length constraint lives only in the brand, the element type is\n * never expanded into tuple positions, which keeps type-checking cheap even\n * when `A` is a large type. Prefer this over `fixedLengthTuple` when `A` is\n * large; prefer `fixedLengthTuple` when positional element types or a literal\n * `length` are needed.\n */\nexport function fixedLengthArray<A, N extends SupportedLength>(\n size: N,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: FixedLengthArray<N, A>;\n }>\n >,\n): Type<FixedLengthArray<N, A>>;\n\n// For sizes outside `SupportedLength` (`0..2048`) the length cannot be encoded in the brand,\n// so the result length is left unconstrained (`readonly A[]`).\nexport function fixedLengthArray<A>(\n size: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: readonly A[];\n }>\n >,\n): Type<readonly A[]>;\n\nexport function fixedLengthArray<A>(\n size: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: readonly A[];\n }>\n >,\n): Type<readonly A[]> {\n type T = readonly A[];\n\n const typeName =\n options?.typeName ?? `FixedLengthArray<${size}, ${elementType.typeName}>`;\n\n const getDefaultValue = memoizeFunction(\n (): T =>\n options?.defaultValue ??\n Arr.create(asUint32(size), elementType.defaultValue),\n );\n\n const validate: Type<T>['validate'] = (a) => {\n if (!Arr.isArray(a)) {\n return Result.err([\n createPrimitiveValidationError({\n actualValue: a,\n expectedType: 'array',\n typeName,\n details: undefined,\n }),\n ]);\n }\n\n if (a.length !== size) {\n return Result.err([\n {\n path: [],\n actualValue: a,\n expectedType: typeName,\n typeName,\n details: {\n kind: 'array-length',\n expectedLength: size,\n actualLength: a.length,\n },\n } satisfies ValidationError,\n ]);\n }\n\n return validateElements(a, elementType);\n };\n\n const fill: Type<T>['fill'] = (a) =>\n Arr.isArray(a)\n ? Arr.map(\n Arr.seq(asUint32(size)),\n (i) => elementType.fill(a[i]) satisfies A,\n )\n : getDefaultValue();\n\n return buildType({ typeName, getDefaultValue, validate, fill, elementType });\n}\n\n/**\n * Creates a `Type` for a readonly array with at least `minLength` elements,\n * typed as the branded {@link MinLengthArray} instead of the structural\n * `readonly [A, ..., A, ...A[]]` type produced by `minLengthTuple`.\n *\n * Because the length constraint lives only in the brand, the element type is\n * never expanded into tuple positions, which keeps type-checking cheap even\n * when `A` is a large type.\n */\nexport function minLengthArray<A, N extends SupportedLength>(\n minLength: N,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: MinLengthArray<N, A>;\n }>\n >,\n): Type<MinLengthArray<N, A>>;\n\n// For bounds outside `SupportedLength` (`0..2048`) the length cannot be encoded in the brand,\n// so the result length is left unconstrained (`readonly A[]`).\nexport function minLengthArray<A>(\n minLength: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: readonly A[];\n }>\n >,\n): Type<readonly A[]>;\n\nexport function minLengthArray<A>(\n minLength: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: readonly A[];\n }>\n >,\n): Type<readonly A[]> {\n type T = readonly A[];\n\n const typeName =\n options?.typeName ??\n `MinLengthArray<${minLength}, ${elementType.typeName}>`;\n\n const getDefaultValue = memoizeFunction(\n (): T =>\n options?.defaultValue ??\n // An array of `minLength` elements is the shortest value satisfying the bound.\n Arr.create(asUint32(minLength), elementType.defaultValue),\n );\n\n const validate: Type<T>['validate'] = (a) => {\n if (!Arr.isArray(a)) {\n return Result.err([\n createPrimitiveValidationError({\n actualValue: a,\n expectedType: 'array',\n typeName,\n details: undefined,\n }),\n ]);\n }\n\n if (a.length < minLength) {\n return Result.err([\n {\n path: [],\n actualValue: a,\n expectedType: typeName,\n typeName,\n details: {\n kind: 'array-min-length',\n minLength,\n actualLength: a.length,\n },\n } satisfies ValidationError,\n ]);\n }\n\n return validateElements(a, elementType);\n };\n\n const fill: Type<T>['fill'] = (a) => {\n if (!Arr.isArray(a)) {\n return getDefaultValue();\n }\n\n // Keep the input if it is long enough; otherwise pad up to `minLength`.\n return a.length >= minLength\n ? Arr.map(a, (el) => elementType.fill(el) satisfies A)\n : Arr.map(\n Arr.seq(asUint32(minLength)),\n (i) => elementType.fill(a[i]) satisfies A,\n );\n };\n\n return buildType({ typeName, getDefaultValue, validate, fill, elementType });\n}\n\n/**\n * Creates a `Type` for a readonly array with at most `maxLength` elements,\n * typed as the branded {@link MaxLengthArray} instead of the union of tuple\n * types produced by `maxLengthTuple`.\n *\n * Because the length constraint lives only in the brand, no union of tuples\n * of the element type is ever constructed, which keeps type-checking cheap\n * even when `A` is a large type.\n */\nexport function maxLengthArray<A, N extends SupportedLength>(\n maxLength: N,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: MaxLengthArray<N, A>;\n }>\n >,\n): Type<MaxLengthArray<N, A>>;\n\n// For bounds outside `SupportedLength` (`0..2048`) the length cannot be encoded in the brand,\n// so the result length is left unconstrained (`readonly A[]`).\nexport function maxLengthArray<A>(\n maxLength: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: readonly A[];\n }>\n >,\n): Type<readonly A[]>;\n\nexport function maxLengthArray<A>(\n maxLength: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: readonly A[];\n }>\n >,\n): Type<readonly A[]> {\n type T = readonly A[];\n\n const typeName =\n options?.typeName ??\n `MaxLengthArray<${maxLength}, ${elementType.typeName}>`;\n\n const getDefaultValue = memoizeFunction(\n (): T =>\n options?.defaultValue ??\n // The empty array is the shortest value satisfying `length <= maxLength`.\n Arr.create(0, elementType.defaultValue),\n );\n\n const validate: Type<T>['validate'] = (a) => {\n if (!Arr.isArray(a)) {\n return Result.err([\n createPrimitiveValidationError({\n actualValue: a,\n expectedType: 'array',\n typeName,\n details: undefined,\n }),\n ]);\n }\n\n if (a.length > maxLength) {\n return Result.err([\n {\n path: [],\n actualValue: a,\n expectedType: typeName,\n typeName,\n details: {\n kind: 'array-max-length',\n maxLength,\n actualLength: a.length,\n },\n } satisfies ValidationError,\n ]);\n }\n\n return validateElements(a, elementType);\n };\n\n const fill: Type<T>['fill'] = (a) =>\n Arr.isArray(a)\n ? // Keep the input but trim down to at most `maxLength` elements.\n Arr.map(\n Arr.take(a, asUint32(maxLength)),\n (el) => elementType.fill(el) satisfies A,\n )\n : getDefaultValue();\n\n return buildType({ typeName, getDefaultValue, validate, fill, elementType });\n}\n\n/**\n * Creates a `Type` for a readonly array whose length is within the inclusive\n * range `[min, max]`, typed as the branded {@link BoundedLengthArray} instead\n * of the union of tuple types produced by `boundedLengthTuple`.\n *\n * Because the length constraint lives only in the brand, no union of tuples\n * of the element type is ever constructed, which keeps type-checking cheap\n * even when `A` is a large type.\n */\nexport function boundedLengthArray<\n A,\n Min extends SupportedLength,\n Max extends SupportedLength,\n>(\n min: Min,\n max: Max,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: BoundedLengthArray<Min, Max, A>;\n }>\n >,\n): Type<BoundedLengthArray<Min, Max, A>>;\n\n// Only the lower bound is in `SupportedLength`, so the upper bound is dropped from\n// the result type and only the \"at least `min`\" guarantee is kept.\nexport function boundedLengthArray<A, Min extends SupportedLength>(\n min: Min,\n max: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: MinLengthArray<Min, A>;\n }>\n >,\n): Type<MinLengthArray<Min, A>>;\n\n// Only the upper bound is in `SupportedLength`, so the lower bound is dropped from\n// the result type and only the \"at most `max`\" guarantee is kept.\nexport function boundedLengthArray<A, Max extends SupportedLength>(\n min: number,\n max: Max,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: MaxLengthArray<Max, A>;\n }>\n >,\n): Type<MaxLengthArray<Max, A>>;\n\n// Neither bound is in `SupportedLength`, so the result length is left unconstrained.\nexport function boundedLengthArray<A>(\n min: number,\n max: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: readonly A[];\n }>\n >,\n): Type<readonly A[]>;\n\nexport function boundedLengthArray<A>(\n min: number,\n max: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: readonly A[];\n }>\n >,\n): Type<readonly A[]> {\n type T = readonly A[];\n\n const typeName =\n options?.typeName ??\n `BoundedLengthArray<${min}, ${max}, ${elementType.typeName}>`;\n\n const getDefaultValue = memoizeFunction(\n (): T =>\n options?.defaultValue ??\n // An array of the minimum length is the shortest value within the range.\n Arr.create(asUint32(min), elementType.defaultValue),\n );\n\n const validate: Type<T>['validate'] = (a) => {\n if (!Arr.isArray(a)) {\n return Result.err([\n createPrimitiveValidationError({\n actualValue: a,\n expectedType: 'array',\n typeName,\n details: undefined,\n }),\n ]);\n }\n\n if (a.length < min || max < a.length) {\n return Result.err([\n {\n path: [],\n actualValue: a,\n expectedType: typeName,\n typeName,\n details: {\n kind: 'array-range-length',\n minLength: min,\n maxLength: max,\n actualLength: a.length,\n },\n } satisfies ValidationError,\n ]);\n }\n\n return validateElements(a, elementType);\n };\n\n const fill: Type<T>['fill'] = (a) => {\n if (!Arr.isArray(a)) {\n return getDefaultValue();\n }\n\n // Trim down to at most `max`, then pad up to at least `min`.\n const capped = Arr.take(a, asUint32(max));\n\n return capped.length >= min\n ? Arr.map(capped, (el) => elementType.fill(el) satisfies A)\n : Arr.map(\n Arr.seq(asUint32(min)),\n (i) => elementType.fill(capped[i]) satisfies A,\n );\n };\n\n return buildType({ typeName, getDefaultValue, validate, fill, elementType });\n}\n\nconst validateElements = <A,>(\n a: readonly unknown[],\n elementType: Type<A>,\n): Result<readonly A[], readonly ValidationError[]> => {\n const errors: readonly ValidationError[] = Arr.generate(function* () {\n for (const [index, el] of a.entries()) {\n const res = elementType.validate(el);\n\n if (Result.isErr(res)) {\n yield* prependIndexToValidationErrors(res.value, index);\n }\n }\n });\n\n if (Arr.isNonEmpty(errors)) {\n return Result.err(errors);\n }\n\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n return Result.ok(a as readonly A[]);\n};\n\nconst buildType = <A,>({\n typeName,\n getDefaultValue,\n validate,\n fill,\n elementType,\n}: Readonly<{\n typeName: string;\n getDefaultValue: () => readonly A[];\n validate: (a: unknown) => Result<readonly A[], readonly ValidationError[]>;\n fill: (a: unknown) => readonly A[];\n elementType: Type<A>;\n}>): Type<readonly A[]> =>\n ({\n typeName,\n get defaultValue() {\n return getDefaultValue();\n },\n fill,\n prune: (a) => a.map((el) => elementType.prune(el)),\n validate,\n is: createIsFn(validate),\n cast: createCastFn(validate),\n assertIs: createAssertFn(validate),\n }) as const;\n"],"names":[],"mappings":";;;;;;AA4DO,SAAS,gBAAA,CACd,IAAA,EACA,WAAA,EACA,OAAA,EAMoB;AAGpB,EAAA,MAAM,WACJ,OAAA,EAAS,QAAA,IAAY,oBAAoB,IAAI,CAAA,EAAA,EAAK,YAAY,QAAQ,CAAA,CAAA,CAAA;AAExE,EAAA,MAAM,eAAA,GAAkB,eAAA;AAAA,IACtB,MACE,SAAS,YAAA,IACT,GAAA,CAAI,OAAO,QAAA,CAAS,IAAI,CAAA,EAAG,WAAA,CAAY,YAAY;AAAA,GACvD;AAEA,EAAA,MAAM,QAAA,GAAgC,CAAC,CAAA,KAAM;AAC3C,IAAA,IAAI,CAAC,GAAA,CAAI,OAAA,CAAQ,CAAC,CAAA,EAAG;AACnB,MAAA,OAAO,OAAO,GAAA,CAAI;AAAA,QAChB,8BAAA,CAA+B;AAAA,UAC7B,WAAA,EAAa,CAAA;AAA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+B,GAAA,CAAI,KAAA,EAAO,KAAK,CAAA;AAAA,MACxD;AAAA,IACF;AAAA,EACF,CAAC,CAAA;AAED,EAAA,IAAI,GAAA,CAAI,UAAA,CAAW,MAAM,CAAA,EAAG;AAC1B,IAAA,OAAO,MAAA,CAAO,IAAI,MAAM,CAAA;AAAA,EAC1B;AAGA,EAAA,OAAO,MAAA,CAAO,GAAG,CAAiB,CAAA;AACpC,CAAA;AAEA,MAAM,YAAY,CAAK;AAAA,EACrB,QAAA;AAAA,EACA,eAAA;AAAA,EACA,QAAA;AAAA,EACA,IAAA;AAAA,EACA;AACF,CAAA,MAOG;AAAA,EACC,QAAA;AAAA,EACA,IAAI,YAAA,GAAe;AACjB,IAAA,OAAO,eAAA,EAAgB;AAAA,EACzB,CAAA;AAAA,EACA,IAAA;AAAA,EACA,KAAA,EAAO,CAAC,CAAA,KAAM,CAAA,CAAE,GAAA,CAAI,CAAC,EAAA,KAAO,WAAA,CAAY,KAAA,CAAM,EAAE,CAAC,CAAA;AAAA,EACjD,QAAA;AAAA,EACA,EAAA,EAAI,WAAW,QAAQ,CAAA;AAAA,EACvB,IAAA,EAAM,aAAa,QAAQ,CAAA;AAAA,EAC3B,QAAA,EAAU,eAAe,QAAQ;AACnC,CAAA,CAAA;;;;"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type MaxLengthTuple, type StructuralPrefixLength } from 'ts-type-forge';
|
|
2
|
+
import { type Type } from '../type.mjs';
|
|
3
|
+
export type { MaxLengthTuple } from 'ts-type-forge';
|
|
4
|
+
export declare function maxLengthTuple<A, N extends StructuralPrefixLength>(size: N, elementType: Type<A>, options?: Partial<Readonly<{
|
|
5
|
+
typeName: string;
|
|
6
|
+
defaultValue: MaxLengthTuple<N, A>;
|
|
7
|
+
}>>): Type<MaxLengthTuple<N, A>>;
|
|
8
|
+
export declare function maxLengthTuple<A>(size: number, elementType: Type<A>, options?: Partial<Readonly<{
|
|
9
|
+
typeName: string;
|
|
10
|
+
defaultValue: readonly A[];
|
|
11
|
+
}>>): Type<readonly A[]>;
|
|
12
|
+
//# sourceMappingURL=max-length-tuple.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"max-length-tuple.d.mts","sourceRoot":"","sources":["../../src/array/max-length-tuple.mts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,sBAAsB,EAC5B,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAUxC,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpD,wBAAgB,cAAc,CAAC,CAAC,EAAE,CAAC,SAAS,sBAAsB,EAChE,IAAI,EAAE,CAAC,EACP,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;CACpC,CAAC,CACH,GACA,IAAI,CAAC,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAI9B,wBAAgB,cAAc,CAAC,CAAC,EAC9B,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,SAAS,CAAC,EAAE,CAAC;CAC5B,CAAC,CACH,GACA,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC"}
|
|
@@ -4,8 +4,8 @@ import { createCastFn } from '../utils/create-cast-fn.mjs';
|
|
|
4
4
|
import { createIsFn } from '../utils/create-is-fn.mjs';
|
|
5
5
|
import { createPrimitiveValidationError, prependIndexToValidationErrors } from '../utils/validation-error.mjs';
|
|
6
6
|
|
|
7
|
-
function
|
|
8
|
-
const typeName = options?.typeName ?? `
|
|
7
|
+
function maxLengthTuple(size, elementType, options) {
|
|
8
|
+
const typeName = options?.typeName ?? `MaxLengthTuple<${size}, ${elementType.typeName}>`;
|
|
9
9
|
const getDefaultValue = memoizeFunction(
|
|
10
10
|
() => options?.defaultValue ?? // The empty array is the shortest value satisfying `length <= size`.
|
|
11
11
|
Arr.create(0, elementType.defaultValue)
|
|
@@ -71,5 +71,5 @@ function arrayAtMostLength(size, elementType, options) {
|
|
|
71
71
|
};
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
export {
|
|
75
|
-
//# sourceMappingURL=
|
|
74
|
+
export { maxLengthTuple };
|
|
75
|
+
//# sourceMappingURL=max-length-tuple.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"max-length-tuple.mjs","sources":["../../src/array/max-length-tuple.mts"],"sourcesContent":["import { Arr, asUint32, memoizeFunction, Result } from 'ts-data-forge';\nimport {\n type MaxLengthTuple,\n type StructuralPrefixLength,\n} from 'ts-type-forge';\nimport { type Type } from '../type.mjs';\nimport {\n createAssertFn,\n createCastFn,\n createIsFn,\n createPrimitiveValidationError,\n prependIndexToValidationErrors,\n type ValidationError,\n} from '../utils/index.mjs';\n\nexport type { MaxLengthTuple } from 'ts-type-forge';\n\nexport function maxLengthTuple<A, N extends StructuralPrefixLength>(\n size: N,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: MaxLengthTuple<N, A>;\n }>\n >,\n): Type<MaxLengthTuple<N, A>>;\n\n// For sizes outside `StructuralPrefixLength` (`0..10`) the exact length cannot be encoded in the type,\n// so the result length is left unconstrained (`readonly A[]`).\nexport function maxLengthTuple<A>(\n size: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: readonly A[];\n }>\n >,\n): Type<readonly A[]>;\n\nexport function maxLengthTuple<A>(\n size: number,\n elementType: Type<A>,\n options?: Partial<\n Readonly<{\n typeName: string;\n defaultValue: readonly A[];\n }>\n >,\n): Type<readonly A[]> {\n type T = readonly A[];\n\n const typeName =\n options?.typeName ?? `MaxLengthTuple<${size}, ${elementType.typeName}>`;\n\n const getDefaultValue = memoizeFunction(\n (): T =>\n options?.defaultValue ??\n // The empty array is the shortest value satisfying `length <= size`.\n Arr.create(0, elementType.defaultValue),\n );\n\n const validate: Type<T>['validate'] = (a) => {\n if (!Arr.isArray(a)) {\n return Result.err([\n createPrimitiveValidationError({\n actualValue: a,\n expectedType: 'array',\n typeName,\n details: undefined,\n }),\n ]);\n }\n\n if (a.length > size) {\n return Result.err([\n {\n path: [],\n actualValue: a,\n expectedType: typeName,\n typeName,\n details: {\n kind: 'array-max-length',\n maxLength: size,\n actualLength: a.length,\n },\n } satisfies ValidationError,\n ]);\n }\n\n const errors: readonly ValidationError[] = Arr.generate(function* () {\n for (const [index, el] of a.entries()) {\n const res = elementType.validate(el);\n\n if (Result.isErr(res)) {\n yield* prependIndexToValidationErrors(res.value, index);\n }\n }\n });\n\n if (Arr.isNonEmpty(errors)) {\n return Result.err(errors);\n }\n\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n return Result.ok(a as unknown as T);\n };\n\n const fill: Type<T>['fill'] = (a) =>\n Arr.isArray(a)\n ? // Keep the input but trim down to at most `size` elements.\n Arr.map(\n Arr.take(a, asUint32(size)),\n (el) => elementType.fill(el) satisfies A,\n )\n : getDefaultValue();\n\n const prune = (a: T): T => a.map((el) => elementType.prune(el));\n\n return {\n typeName,\n get defaultValue() {\n return getDefaultValue();\n },\n fill,\n prune,\n validate,\n is: createIsFn(validate),\n cast: createCastFn(validate),\n assertIs: createAssertFn(validate),\n };\n}\n"],"names":[],"mappings":";;;;;;AAyCO,SAAS,cAAA,CACd,IAAA,EACA,WAAA,EACA,OAAA,EAMoB;AAGpB,EAAA,MAAM,WACJ,OAAA,EAAS,QAAA,IAAY,kBAAkB,IAAI,CAAA,EAAA,EAAK,YAAY,QAAQ,CAAA,CAAA,CAAA;AAEtE,EAAA,MAAM,e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A,EAAG;AAC1B,MAAA,OAAO,MAAA,CAAO,IAAI,MAAM,CAAA;AAAA,IAC1B;AAGA,IAAA,OAAO,MAAA,CAAO,GAAG,CAAiB,CAAA;AAAA,EACpC,CAAA;AAEA,EAAA,MAAM,IAAA,GAAwB,CAAC,CAAA,KAC7B,GAAA,CAAI,QAAQ,CAAC,CAAA;AAAA;AAAA,IAET,GAAA,CAAI,GAAA;AAAA,MACF,GAAA,CAAI,IAAA,CAAK,CAAA,EAAG,QAAA,CAAS,IAAI,CAAC,CAAA;AAAA,MAC1B,CAAC,EAAA,KAAO,WAAA,CAAY,IAAA,CAAK,EAAE;AAAA;AAC7B,MACA,eAAA,EAAgB;AAEtB,EAAA,MAAM,KAAA,GAAQ,CAAC,CAAA,KAAY,CAAA,CAAE,GAAA,CAAI,CAAC,EAAA,KAAO,WAAA,CAAY,KAAA,CAAM,EAAE,CAAC,CAAA;AAE9D,EAAA,OAAO;AAAA,IACL,QAAA;AAAA,IACA,IAAI,YAAA,GAAe;AACjB,MAAA,OAAO,eAAA,EAAgB;AAAA,IACzB,CAAA;AAAA,IACA,IAAA;AAAA,IACA,KAAA;AAAA,IACA,QAAA;AAAA,IACA,EAAA,EAAI,WAAW,QAAQ,CAAA;AAAA,IACvB,IAAA,EAAM,aAAa,QAAQ,CAAA;AAAA,IAC3B,QAAA,EAAU,eAAe,QAAQ;AAAA,GACnC;AACF;;;;"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type MinLengthTuple, type StructuralPrefixLength } from 'ts-type-forge';
|
|
2
|
+
import { type Type } from '../type.mjs';
|
|
3
|
+
export type { MinLengthTuple } from 'ts-type-forge';
|
|
4
|
+
export declare function minLengthTuple<A, N extends StructuralPrefixLength>(size: N, elementType: Type<A>, options?: Partial<Readonly<{
|
|
5
|
+
typeName: string;
|
|
6
|
+
defaultValue: MinLengthTuple<N, A>;
|
|
7
|
+
}>>): Type<MinLengthTuple<N, A>>;
|
|
8
|
+
export declare function minLengthTuple<A>(size: number, elementType: Type<A>, options?: Partial<Readonly<{
|
|
9
|
+
typeName: string;
|
|
10
|
+
defaultValue: readonly A[];
|
|
11
|
+
}>>): Type<readonly A[]>;
|
|
12
|
+
//# sourceMappingURL=min-length-tuple.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"min-length-tuple.d.mts","sourceRoot":"","sources":["../../src/array/min-length-tuple.mts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,sBAAsB,EAC5B,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAUxC,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpD,wBAAgB,cAAc,CAAC,CAAC,EAAE,CAAC,SAAS,sBAAsB,EAChE,IAAI,EAAE,CAAC,EACP,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;CACpC,CAAC,CACH,GACA,IAAI,CAAC,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAI9B,wBAAgB,cAAc,CAAC,CAAC,EAC9B,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,SAAS,CAAC,EAAE,CAAC;CAC5B,CAAC,CACH,GACA,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC"}
|
|
@@ -4,8 +4,8 @@ import { createCastFn } from '../utils/create-cast-fn.mjs';
|
|
|
4
4
|
import { createIsFn } from '../utils/create-is-fn.mjs';
|
|
5
5
|
import { createPrimitiveValidationError, prependIndexToValidationErrors } from '../utils/validation-error.mjs';
|
|
6
6
|
|
|
7
|
-
function
|
|
8
|
-
const typeName = options?.typeName ?? `
|
|
7
|
+
function minLengthTuple(size, elementType, options) {
|
|
8
|
+
const typeName = options?.typeName ?? `MinLengthTuple<${size}, ${elementType.typeName}>`;
|
|
9
9
|
const getDefaultValue = memoizeFunction(
|
|
10
10
|
() => options?.defaultValue ?? // An array of `size` elements is the shortest value satisfying the bound.
|
|
11
11
|
Arr.create(asUint32(size), elementType.defaultValue)
|
|
@@ -68,5 +68,5 @@ function arrayAtLeastLength(size, elementType, options) {
|
|
|
68
68
|
};
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
export {
|
|
72
|
-
//# sourceMappingURL=
|
|
71
|
+
export { minLengthTuple };
|
|
72
|
+
//# sourceMappingURL=min-length-tuple.mjs.map
|