ts-fortress 8.1.0 → 8.3.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/dist/enum/enum.d.mts +8 -2
- package/dist/enum/enum.d.mts.map +1 -1
- package/dist/enum/enum.mjs +12 -4
- package/dist/enum/enum.mjs.map +1 -1
- package/dist/predefined/brand/string/non-empty-string.d.mts +10 -1
- package/dist/predefined/brand/string/non-empty-string.d.mts.map +1 -1
- package/dist/predefined/brand/string/non-empty-string.mjs +7 -9
- package/dist/predefined/brand/string/non-empty-string.mjs.map +1 -1
- package/dist/predefined/int8.mjs +2 -0
- package/dist/predefined/int8.mjs.map +1 -1
- package/dist/predefined/uint8.mjs +2 -0
- package/dist/predefined/uint8.mjs.map +1 -1
- package/dist/primitives/string.d.mts +10 -0
- package/dist/primitives/string.d.mts.map +1 -1
- package/dist/primitives/string.mjs.map +1 -1
- package/package.json +1 -1
- package/src/enum/enum.mts +40 -6
- package/src/enum/enum.test.mts +70 -0
- package/src/predefined/brand/string/non-empty-string.mts +26 -11
- package/src/predefined/brand/string/non-empty-string.test.mts +34 -0
- package/src/primitives/string.mts +13 -0
package/dist/enum/enum.d.mts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { type ArrayElement, type NonEmptyArray, type Primitive } from 'ts-type-forge';
|
|
2
2
|
import { type Type } from '../type.mjs';
|
|
3
|
-
export declare
|
|
3
|
+
export declare function enumType<const Values extends NonEmptyArray<Primitive>>(values: Values, options?: Partial<Readonly<{
|
|
4
4
|
typeName: string;
|
|
5
5
|
defaultValue: ArrayElement<Values>;
|
|
6
|
-
|
|
6
|
+
allowAnyString: false;
|
|
7
|
+
}>>): Type<ArrayElement<Values>>;
|
|
8
|
+
export declare function enumType<const Values extends NonEmptyArray<string>>(values: Values, options: Readonly<{
|
|
9
|
+
allowAnyString: true;
|
|
10
|
+
typeName?: string;
|
|
11
|
+
defaultValue?: ArrayElement<Values> | (string & {});
|
|
12
|
+
}>): Type<ArrayElement<Values> | (string & {})>;
|
|
7
13
|
//# sourceMappingURL=enum.d.mts.map
|
package/dist/enum/enum.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.d.mts","sourceRoot":"","sources":["../../src/enum/enum.mts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,SAAS,EACf,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"enum.d.mts","sourceRoot":"","sources":["../../src/enum/enum.mts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,SAAS,EACf,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAQxC,wBAAgB,QAAQ,CAAC,KAAK,CAAC,MAAM,SAAS,aAAa,CAAC,SAAS,CAAC,EACpE,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC;IACnC,cAAc,EAAE,KAAK,CAAC;CACvB,CAAC,CACH,GACA,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC;AAM9B,wBAAgB,QAAQ,CAAC,KAAK,CAAC,MAAM,SAAS,aAAa,CAAC,MAAM,CAAC,EACjE,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,QAAQ,CAAC;IAChB,cAAc,EAAE,IAAI,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;CACrD,CAAC,GACD,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC"}
|
package/dist/enum/enum.mjs
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import { ISet, memoizeFunction, Result } from 'ts-data-forge';
|
|
1
|
+
import { isString, ISet, memoizeFunction, Result } from 'ts-data-forge';
|
|
2
2
|
import { createAssertFn } from '../utils/create-assert-fn.mjs';
|
|
3
3
|
import { createCastFn } from '../utils/create-cast-fn.mjs';
|
|
4
4
|
import { createIsFn } from '../utils/create-is-fn.mjs';
|
|
5
|
+
import '@sindresorhus/is';
|
|
6
|
+
import '../other-types/unknown.mjs';
|
|
7
|
+
import { string } from '../primitives/string.mjs';
|
|
5
8
|
|
|
6
|
-
|
|
9
|
+
function enumType(values, options) {
|
|
10
|
+
if (options?.allowAnyString === true &&
|
|
11
|
+
values.every(isString) &&
|
|
12
|
+
isString(options.defaultValue)) {
|
|
13
|
+
return string(options.defaultValue);
|
|
14
|
+
}
|
|
7
15
|
const valueSet = ISet.create(values);
|
|
8
16
|
const typeName = options?.typeName ?? 'enum';
|
|
9
17
|
const getDefaultValue = memoizeFunction(
|
|
@@ -29,7 +37,7 @@ const enumType = (values, options) => {
|
|
|
29
37
|
const is = createIsFn(validate);
|
|
30
38
|
const fill = (a) => (is(a) ? a : getDefaultValue());
|
|
31
39
|
return {
|
|
32
|
-
typeName
|
|
40
|
+
typeName,
|
|
33
41
|
get defaultValue() {
|
|
34
42
|
return getDefaultValue();
|
|
35
43
|
},
|
|
@@ -39,7 +47,7 @@ const enumType = (values, options) => {
|
|
|
39
47
|
assertIs: createAssertFn(validate),
|
|
40
48
|
cast: createCastFn(validate),
|
|
41
49
|
};
|
|
42
|
-
}
|
|
50
|
+
}
|
|
43
51
|
|
|
44
52
|
export { enumType };
|
|
45
53
|
//# sourceMappingURL=enum.mjs.map
|
package/dist/enum/enum.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.mjs","sources":["../../src/enum/enum.mts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"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ACR,EAAE;AACF,QAAA,QAAQ,EAAE,cAAc,CAAC,QAAQ,CAAC;AAClC,QAAA,IAAI,EAAE,YAAY,CAAC,QAAQ,CAAC;KAC7B;AACH;;;;"}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { type NonEmptyString } from 'ts-type-forge';
|
|
2
|
+
import { type StringConstraintsResultType, type StringTypeConstraints } from '../../../primitives/index.mjs';
|
|
2
3
|
import { type Type } from '../../../type.mjs';
|
|
3
4
|
export type { NonEmptyString } from 'ts-type-forge';
|
|
4
|
-
|
|
5
|
+
/**
|
|
6
|
+
* The constraints accepted by {@link nonEmptyString}: every {@link string}
|
|
7
|
+
* constraint except `nonempty`, which is always implied.
|
|
8
|
+
*/
|
|
9
|
+
export type NonEmptyStringConstraints = Omit<StringTypeConstraints, 'nonempty'>;
|
|
10
|
+
export declare function nonEmptyString(defaultValue?: string): Type<NonEmptyString>;
|
|
11
|
+
export declare function nonEmptyString<const C extends NonEmptyStringConstraints>(defaultValue: string, constraints: C): Type<StringConstraintsResultType<C & Readonly<{
|
|
12
|
+
nonempty: true;
|
|
13
|
+
}>>>;
|
|
5
14
|
//# sourceMappingURL=non-empty-string.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"non-empty-string.d.mts","sourceRoot":"","sources":["../../../../src/predefined/brand/string/non-empty-string.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"non-empty-string.d.mts","sourceRoot":"","sources":["../../../../src/predefined/brand/string/non-empty-string.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAEL,KAAK,2BAA2B,EAChC,KAAK,qBAAqB,EAC3B,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAE9C,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpD;;;GAGG;AACH,MAAM,MAAM,yBAAyB,GAAG,IAAI,CAAC,qBAAqB,EAAE,UAAU,CAAC,CAAC;AAEhF,wBAAgB,cAAc,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC,cAAc,CAAC,CAAC;AAE5E,wBAAgB,cAAc,CAAC,KAAK,CAAC,CAAC,SAAS,yBAAyB,EACtE,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,CAAC,GACb,IAAI,CAAC,2BAA2B,CAAC,CAAC,GAAG,QAAQ,CAAC;IAAE,QAAQ,EAAE,IAAI,CAAA;CAAE,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { brand } from '../../../brand/brand.mjs';
|
|
1
|
+
import 'ts-data-forge';
|
|
3
2
|
import '@sindresorhus/is';
|
|
4
3
|
import '../../../other-types/unknown.mjs';
|
|
5
4
|
import { string } from '../../../primitives/string.mjs';
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
});
|
|
6
|
+
function nonEmptyString(defaultValue = ' ', constraints) {
|
|
7
|
+
// `nonempty` is always implied; the remaining constraints (if any) are
|
|
8
|
+
// forwarded to `string`, which validates them at runtime and refines the
|
|
9
|
+
// result type the same way it does for a plain string type.
|
|
10
|
+
return string(defaultValue, { nonempty: true, ...constraints });
|
|
11
|
+
}
|
|
14
12
|
|
|
15
13
|
export { nonEmptyString };
|
|
16
14
|
//# sourceMappingURL=non-empty-string.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"non-empty-string.mjs","sources":["../../../../src/predefined/brand/string/non-empty-string.mts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"non-empty-string.mjs","sources":["../../../../src/predefined/brand/string/non-empty-string.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;SAwBgB,cAAc,CAC5B,YAAA,GAAuB,GAAG,EAC1B,WAAuC,EAAA;;;;AAKvC,IAAA,OAAO,MAAM,CAAC,YAAY,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,WAAW,EAAE,CAAC;AACjE;;;;"}
|
package/dist/predefined/int8.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"int8.mjs","sources":["../../src/predefined/int8.mts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"int8.mjs","sources":["../../src/predefined/int8.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;AAIO,MAAM,IAAI,GAAG,CAAC,YAAA,GAAqB,CAAC,KACzC,QAAQ,CAAC;IACP,KAAK,EAAE,IAAI;AACX,IAAA,GAAG,EAAE,GAAG;IACR,YAAY;AACb,CAAA;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uint8.mjs","sources":["../../src/predefined/uint8.mts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"uint8.mjs","sources":["../../src/predefined/uint8.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;AAIO,MAAM,KAAK,GAAG,CAAC,YAAA,GAAsB,CAAC,KAC3C,SAAS,CAAC;AACR,IAAA,KAAK,EAAE,CAAC;AACR,IAAA,GAAG,EAAE,GAAG;IACR,YAAY;AACb,CAAA;;;;"}
|
|
@@ -13,6 +13,16 @@ type Constraints = Partial<Readonly<{
|
|
|
13
13
|
lowercase: true;
|
|
14
14
|
regex: RegExp;
|
|
15
15
|
}>>;
|
|
16
|
+
/**
|
|
17
|
+
* The set of constraints accepted by {@link string}.
|
|
18
|
+
*/
|
|
19
|
+
export type StringTypeConstraints = Constraints;
|
|
20
|
+
/**
|
|
21
|
+
* The result type produced by {@link string} for a given set of constraints
|
|
22
|
+
* `C` (e.g. `nonempty` yields {@link NonEmptyString}, `startsWith` narrows to a
|
|
23
|
+
* template literal type, etc.).
|
|
24
|
+
*/
|
|
25
|
+
export type StringConstraintsResultType<C extends StringTypeConstraints> = ConstraintsResultType<C>;
|
|
16
26
|
type DefaultValueType<S extends string, C extends Constraints> = DefaultValueWhenNonemptyIsOn<S, C> & DefaultValueWhenMinLengthIsOn<S, C> & DefaultValueWhenMaxLengthIsOn<S, C> & DefaultValueWhenStartsWithIsOn<C> & DefaultValueWhenEndsWithIsOn<C> & DefaultValueWhenIncludesIsOn<C> & DefaultValueWhenUppercaseIsOn<S, C> & DefaultValueWhenLowercaseIsOn<S, C>;
|
|
17
27
|
type ConstraintsResultType<C extends Constraints> = ConstraintsResultTypeWhenNonemptyIsOn<C> & DefaultValueWhenStartsWithIsOn<C> & DefaultValueWhenEndsWithIsOn<C> & DefaultValueWhenIncludesIsOn<C>;
|
|
18
28
|
type DefaultValueWhenStartsWithIsOn<C extends Constraints> = C extends Readonly<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.d.mts","sourceRoot":"","sources":["../../src/primitives/string.mts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpD,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,wBAAgB,MAAM,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;AAE5D,wBAAgB,MAAM,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,CAAC,CAAC,SAAS,WAAW,EAClE,YAAY,EAAE,CAAC,GAAG,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,EACxC,WAAW,EAAE,CAAC,GACb,IAAI,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,CAAC;AAmClC,KAAK,WAAW,GAAG,OAAO,CACxB,QAAQ,CAAC;IACP,QAAQ,EAAE,IAAI,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM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OAAO,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC;;IAG9D,IAAI,SAAS,KAAK,IAAI,IAAI,KAAK,KAAK,KAAK,CAAC,WAAW,EAAE,EAAE;AACvD,QAAA,OAAO,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,CAAC,CAAC;;IAG3D,IAAI,SAAS,KAAK,IAAI,IAAI,KAAK,KAAK,KAAK,CAAC,WAAW,EAAE,EAAE;AACvD,QAAA,OAAO,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,CAAC,CAAC;;AAG3D,IAAA,IAAI,KAAK,KAAK,SAAS,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;AAC7C,QAAA,OAAO,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;;AAGxD,IAAA,OAAO,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC;AACxB,CAAC;AAEH,MAAM,YAAY,GAAG,CACnB,KAAa,EACb,cAAsB,EACtB,eAAmD,KAEnD,CAAA,cAAA,EAAiB,KAAK,CAAA,6CAAA,EAAgD,cAAc,CAAA,GAAA,EAAM,QAAQ,CAAC,eAAe,CAAC,GAAG,eAAe,CAAC,MAAM,GAAG,eAAe,EAAW;;;;"}
|
package/package.json
CHANGED
package/src/enum/enum.mts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { ISet, memoizeFunction, Result } from 'ts-data-forge';
|
|
1
|
+
import { ISet, isString, memoizeFunction, Result } from 'ts-data-forge';
|
|
2
2
|
import {
|
|
3
3
|
type ArrayElement,
|
|
4
4
|
type NonEmptyArray,
|
|
5
5
|
type Primitive,
|
|
6
6
|
} from 'ts-type-forge';
|
|
7
|
+
import { string } from '../primitives/index.mjs';
|
|
7
8
|
import { type Type } from '../type.mjs';
|
|
8
9
|
import {
|
|
9
10
|
createAssertFn,
|
|
@@ -12,16 +13,49 @@ import {
|
|
|
12
13
|
type ValidationError,
|
|
13
14
|
} from '../utils/index.mjs';
|
|
14
15
|
|
|
15
|
-
export
|
|
16
|
+
export function enumType<const Values extends NonEmptyArray<Primitive>>(
|
|
16
17
|
values: Values,
|
|
17
18
|
options?: Partial<
|
|
18
19
|
Readonly<{
|
|
19
20
|
typeName: string;
|
|
20
21
|
defaultValue: ArrayElement<Values>;
|
|
22
|
+
allowAnyString: false;
|
|
21
23
|
}>
|
|
22
24
|
>,
|
|
23
|
-
): Type<ArrayElement<Values
|
|
24
|
-
|
|
25
|
+
): Type<ArrayElement<Values>>;
|
|
26
|
+
|
|
27
|
+
// When `allowAnyString` is on, the result type keeps autocomplete for the
|
|
28
|
+
// listed members via the `T | (string & {})` idiom while still accepting any
|
|
29
|
+
// other string, and at runtime the value is validated as a plain string
|
|
30
|
+
// (i.e. it behaves like `string()`).
|
|
31
|
+
export function enumType<const Values extends NonEmptyArray<string>>(
|
|
32
|
+
values: Values,
|
|
33
|
+
options: Readonly<{
|
|
34
|
+
allowAnyString: true;
|
|
35
|
+
typeName?: string;
|
|
36
|
+
defaultValue?: ArrayElement<Values> | (string & {});
|
|
37
|
+
}>,
|
|
38
|
+
): Type<ArrayElement<Values> | (string & {})>;
|
|
39
|
+
|
|
40
|
+
export function enumType<const Values extends NonEmptyArray<Primitive>>(
|
|
41
|
+
values: Values,
|
|
42
|
+
options?: Partial<
|
|
43
|
+
Readonly<{
|
|
44
|
+
typeName: string;
|
|
45
|
+
defaultValue: ArrayElement<Values> | (string & {});
|
|
46
|
+
allowAnyString: boolean;
|
|
47
|
+
}>
|
|
48
|
+
>,
|
|
49
|
+
): Type<ArrayElement<Values> | (string & {})> {
|
|
50
|
+
if (
|
|
51
|
+
options?.allowAnyString === true &&
|
|
52
|
+
values.every(isString) &&
|
|
53
|
+
isString(options.defaultValue)
|
|
54
|
+
) {
|
|
55
|
+
return string(options.defaultValue);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type T = ArrayElement<Values> | (string & {});
|
|
25
59
|
|
|
26
60
|
const valueSet = ISet.create(values);
|
|
27
61
|
|
|
@@ -55,7 +89,7 @@ export const enumType = <const Values extends NonEmptyArray<Primitive>>(
|
|
|
55
89
|
const fill: Type<T>['fill'] = (a) => (is(a) ? a : getDefaultValue());
|
|
56
90
|
|
|
57
91
|
return {
|
|
58
|
-
typeName
|
|
92
|
+
typeName,
|
|
59
93
|
get defaultValue() {
|
|
60
94
|
return getDefaultValue();
|
|
61
95
|
},
|
|
@@ -65,4 +99,4 @@ export const enumType = <const Values extends NonEmptyArray<Primitive>>(
|
|
|
65
99
|
assertIs: createAssertFn(validate),
|
|
66
100
|
cast: createCastFn(validate),
|
|
67
101
|
};
|
|
68
|
-
}
|
|
102
|
+
}
|
package/src/enum/enum.test.mts
CHANGED
|
@@ -108,3 +108,73 @@ describe(enumType, () => {
|
|
|
108
108
|
});
|
|
109
109
|
});
|
|
110
110
|
});
|
|
111
|
+
|
|
112
|
+
describe('enumType with allowAnyString', () => {
|
|
113
|
+
const color = enumType(['red', 'green', 'blue'], {
|
|
114
|
+
allowAnyString: true,
|
|
115
|
+
defaultValue: '',
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
type Color = TypeOf<typeof color>;
|
|
119
|
+
|
|
120
|
+
// Autocomplete is preserved for the listed members via the open-union idiom,
|
|
121
|
+
// while any other string is still part of the type.
|
|
122
|
+
expectType<Color, 'red' | 'green' | 'blue' | (string & {})>('=');
|
|
123
|
+
|
|
124
|
+
expectType<typeof color.defaultValue, Color>('=');
|
|
125
|
+
|
|
126
|
+
describe('is', () => {
|
|
127
|
+
test('accepts a listed member', () => {
|
|
128
|
+
assert.isTrue(color.is('red'));
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('accepts any other string at runtime (behaves like string())', () => {
|
|
132
|
+
assert.isTrue(color.is('purple'));
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('rejects non-string values', () => {
|
|
136
|
+
assert.isFalse(color.is(42));
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe('validate', () => {
|
|
141
|
+
test('returns an unlisted string as-is', () => {
|
|
142
|
+
const result = color.validate('purple');
|
|
143
|
+
|
|
144
|
+
assert.isTrue(Result.isOk(result));
|
|
145
|
+
|
|
146
|
+
expect(Result.unwrapThrow(result)).toBe('purple');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('rejects non-string values with a string-type error', () => {
|
|
150
|
+
const result = color.validate(42);
|
|
151
|
+
|
|
152
|
+
assert.isTrue(Result.isErr(result));
|
|
153
|
+
|
|
154
|
+
assert.deepStrictEqual(
|
|
155
|
+
validationErrorsToMessages(Result.unwrapErrThrow(result)),
|
|
156
|
+
[
|
|
157
|
+
'Error: expected <string> type but <number> type value `42` was passed.',
|
|
158
|
+
],
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe('default value and fill', () => {
|
|
164
|
+
test('uses the configured default value', () => {
|
|
165
|
+
expect(color.defaultValue).toBe('');
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('keeps any string', () => {
|
|
169
|
+
const x: unknown = 'anything';
|
|
170
|
+
|
|
171
|
+
expect(color.fill(x)).toBe('anything');
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test('non-string falls back to the default value', () => {
|
|
175
|
+
const x: unknown = 123;
|
|
176
|
+
|
|
177
|
+
expect(color.fill(x)).toBe('');
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
});
|
|
@@ -1,21 +1,36 @@
|
|
|
1
1
|
import { isNonEmptyString } from 'ts-data-forge';
|
|
2
2
|
import { type NonEmptyString } from 'ts-type-forge';
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
string,
|
|
5
|
+
type StringConstraintsResultType,
|
|
6
|
+
type StringTypeConstraints,
|
|
7
|
+
} from '../../../primitives/index.mjs';
|
|
5
8
|
import { type Type } from '../../../type.mjs';
|
|
6
9
|
|
|
7
10
|
export type { NonEmptyString } from 'ts-type-forge';
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The constraints accepted by {@link nonEmptyString}: every {@link string}
|
|
14
|
+
* constraint except `nonempty`, which is always implied.
|
|
15
|
+
*/
|
|
16
|
+
export type NonEmptyStringConstraints = Omit<StringTypeConstraints, 'nonempty'>;
|
|
17
|
+
|
|
18
|
+
export function nonEmptyString(defaultValue?: string): Type<NonEmptyString>;
|
|
19
|
+
|
|
20
|
+
export function nonEmptyString<const C extends NonEmptyStringConstraints>(
|
|
21
|
+
defaultValue: string,
|
|
22
|
+
constraints: C,
|
|
23
|
+
): Type<StringConstraintsResultType<C & Readonly<{ nonempty: true }>>>;
|
|
24
|
+
|
|
25
|
+
export function nonEmptyString(
|
|
10
26
|
defaultValue: string = ' ',
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
});
|
|
27
|
+
constraints?: NonEmptyStringConstraints,
|
|
28
|
+
): Type<NonEmptyString> {
|
|
29
|
+
// `nonempty` is always implied; the remaining constraints (if any) are
|
|
30
|
+
// forwarded to `string`, which validates them at runtime and refines the
|
|
31
|
+
// result type the same way it does for a plain string type.
|
|
32
|
+
return string(defaultValue, { nonempty: true, ...constraints });
|
|
33
|
+
}
|
|
19
34
|
|
|
20
35
|
if (import.meta.vitest !== undefined) {
|
|
21
36
|
test('defaultValue', () => {
|
|
@@ -52,3 +52,37 @@ describe(nonEmptyString, () => {
|
|
|
52
52
|
expect(t.defaultValue).toBe('default');
|
|
53
53
|
});
|
|
54
54
|
});
|
|
55
|
+
|
|
56
|
+
describe('nonEmptyString with extra constraints', () => {
|
|
57
|
+
test('forwards constraints to string and still enforces non-emptiness', () => {
|
|
58
|
+
const t = nonEmptyString('abcd', { minLength: 3 });
|
|
59
|
+
|
|
60
|
+
// Non-refining constraints (e.g. `minLength`) keep a plain `NonEmptyString`.
|
|
61
|
+
expectType<TypeOf<typeof t>, NonEmptyString>('=');
|
|
62
|
+
|
|
63
|
+
assert.isTrue(t.is('abcd'));
|
|
64
|
+
|
|
65
|
+
assert.isTrue(t.is('abc'));
|
|
66
|
+
|
|
67
|
+
assert.isFalse(t.is('ab')); // shorter than minLength
|
|
68
|
+
|
|
69
|
+
assert.isFalse(t.is('')); // empty string is still rejected
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('throws when the default value violates a forwarded constraint', () => {
|
|
73
|
+
expect(() => nonEmptyString('ab', { minLength: 3 })).toThrow(
|
|
74
|
+
'minLength = 3',
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('refines the result type for startsWith', () => {
|
|
79
|
+
const t = nonEmptyString('a-thing', { startsWith: 'a' });
|
|
80
|
+
|
|
81
|
+
// `startsWith` refines the result type, just like `string` does.
|
|
82
|
+
expectType<TypeOf<typeof t>, NonEmptyString & `a${string}`>('=');
|
|
83
|
+
|
|
84
|
+
assert.isTrue(t.is('a-thing'));
|
|
85
|
+
|
|
86
|
+
assert.isFalse(t.is('b-thing'));
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -59,6 +59,19 @@ type Constraints = Partial<
|
|
|
59
59
|
}>
|
|
60
60
|
>;
|
|
61
61
|
|
|
62
|
+
/**
|
|
63
|
+
* The set of constraints accepted by {@link string}.
|
|
64
|
+
*/
|
|
65
|
+
export type StringTypeConstraints = Constraints;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The result type produced by {@link string} for a given set of constraints
|
|
69
|
+
* `C` (e.g. `nonempty` yields {@link NonEmptyString}, `startsWith` narrows to a
|
|
70
|
+
* template literal type, etc.).
|
|
71
|
+
*/
|
|
72
|
+
export type StringConstraintsResultType<C extends StringTypeConstraints> =
|
|
73
|
+
ConstraintsResultType<C>;
|
|
74
|
+
|
|
62
75
|
type DefaultValueType<
|
|
63
76
|
S extends string,
|
|
64
77
|
C extends Constraints,
|