ts-fortress 2.9.0 → 2.10.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.
@@ -30,6 +30,7 @@ export { enumType } from './enum/enum.mjs';
30
30
  export { intRange } from './enum/int-range.mjs';
31
31
  export { uintRange } from './enum/uint-range.mjs';
32
32
  export { literal } from './other-types/literal.mjs';
33
+ export { refine } from './other-types/refine.mjs';
33
34
  export { unknown } from './other-types/unknown.mjs';
34
35
  export { int8 } from './predefined/int8.mjs';
35
36
  export { JsonObject, JsonPrimitive, JsonValue } from './predefined/json.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"entry-point.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"entry-point.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/index.mjs CHANGED
@@ -29,6 +29,7 @@ export { enumType } from './enum/enum.mjs';
29
29
  export { intRange } from './enum/int-range.mjs';
30
30
  export { uintRange } from './enum/uint-range.mjs';
31
31
  export { literal } from './other-types/literal.mjs';
32
+ export { refine } from './other-types/refine.mjs';
32
33
  export { unknown } from './other-types/unknown.mjs';
33
34
  export { int8 } from './predefined/int8.mjs';
34
35
  export { JsonObject, JsonPrimitive, JsonValue } from './predefined/json.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,3 +1,4 @@
1
1
  export * from './literal.mjs';
2
+ export * from './refine.mjs';
2
3
  export * from './unknown.mjs';
3
4
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/other-types/index.mts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/other-types/index.mts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC"}
@@ -1,3 +1,4 @@
1
1
  export { literal } from './literal.mjs';
2
+ export { refine } from './refine.mjs';
2
3
  export { unknown } from './unknown.mjs';
3
4
  //# sourceMappingURL=index.mjs.map
@@ -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,8 @@
1
+ import { type Type } from '../type.mjs';
2
+ export declare const refine: <Base extends Extract<Primitive, string | boolean | bigint | number>, R extends Base>({ baseType, is, defaultValue, typeName, }: Readonly<{
3
+ baseType: Type<Base>;
4
+ is: (a: Base) => a is R;
5
+ defaultValue: R;
6
+ typeName?: string;
7
+ }>) => Type<R>;
8
+ //# sourceMappingURL=refine.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refine.d.mts","sourceRoot":"","sources":["../../src/other-types/refine.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAOxC,eAAO,MAAM,MAAM,GACjB,IAAI,SAAS,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,CAAC,EACnE,CAAC,SAAS,IAAI,EACd,2CAKC,QAAQ,CAAC;IACV,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;IACrB,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC;IACxB,YAAY,EAAE,CAAC,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC,KAAG,IAAI,CAAC,CAAC,CAwBV,CAAC"}
@@ -0,0 +1,27 @@
1
+ import { pipe, Result } from 'ts-data-forge';
2
+ import { createPrimitiveValidationError } from '../utils/validation-error.mjs';
3
+ import { createType } from '../utils/create-type.mjs';
4
+
5
+ const refine = ({ baseType, is, defaultValue, typeName = `${baseType.typeName} refined`, }) => {
6
+ const validate = (a) => pipe(a)
7
+ .map(baseType.validate)
8
+ .map((res) => Result.isErr(res)
9
+ ? res
10
+ : is(res.value)
11
+ ? Result.ok(res.value)
12
+ : Result.err([
13
+ createPrimitiveValidationError({
14
+ actualValue: a,
15
+ expectedType: typeName,
16
+ typeName,
17
+ }),
18
+ ])).value;
19
+ return createType({
20
+ typeName,
21
+ defaultValue,
22
+ validate,
23
+ });
24
+ };
25
+
26
+ export { refine };
27
+ //# sourceMappingURL=refine.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refine.mjs","sources":["../../src/other-types/refine.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;MAQa,MAAM,GAAG,CAGpB,EACA,QAAQ,EACR,EAAE,EACF,YAAY,EACZ,QAAQ,GAAG,CAAA,EAAG,QAAQ,CAAC,QAAQ,CAAA,QAAA,CAAU,GAMzC,KAAa;IACb,MAAM,QAAQ,GAAwB,CAAC,CAAC,KACtC,IAAI,CAAC,CAAC;AACH,SAAA,GAAG,CAAC,QAAQ,CAAC,QAAQ;SACrB,GAAG,CACF,CAAC,GAAG,KACF,MAAM,CAAC,KAAK,CAAC,GAAG;AACd,UAAE;AACF,UAAE,EAAE,CAAC,GAAG,CAAC,KAAK;cACV,MAAM,CAAC,EAAE,CAAC,GAAG,CAAC,KAAK;AACrB,cAAE,MAAM,CAAC,GAAG,CAAC;AACT,gBAAA,8BAA8B,CAAC;AAC7B,oBAAA,WAAW,EAAE,CAAC;AACd,oBAAA,YAAY,EAAE,QAAQ;oBACtB,QAAQ;iBACT,CAAC;aACH,CAAC,CACX,CAAC,KAAK;AAEX,IAAA,OAAO,UAAU,CAAC;QAChB,QAAQ;QACR,YAAY;QACZ,QAAQ;AACT,KAAA,CAAC;AACJ;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-fortress",
3
- "version": "2.9.0",
3
+ "version": "2.10.0",
4
4
  "private": false,
5
5
  "keywords": [
6
6
  "typescript"
@@ -1,2 +1,3 @@
1
1
  export * from './literal.mjs';
2
+ export * from './refine.mjs';
2
3
  export * from './unknown.mjs';
@@ -0,0 +1,46 @@
1
+ import { pipe, Result } from 'ts-data-forge';
2
+ import { type Type } from '../type.mjs';
3
+ import {
4
+ createPrimitiveValidationError,
5
+ createType,
6
+ type ValidationError,
7
+ } from '../utils/index.mjs';
8
+
9
+ export const refine = <
10
+ Base extends Extract<Primitive, string | boolean | bigint | number>,
11
+ R extends Base,
12
+ >({
13
+ baseType,
14
+ is,
15
+ defaultValue,
16
+ typeName = `${baseType.typeName} refined`,
17
+ }: Readonly<{
18
+ baseType: Type<Base>;
19
+ is: (a: Base) => a is R;
20
+ defaultValue: R;
21
+ typeName?: string;
22
+ }>): Type<R> => {
23
+ const validate: Type<R>['validate'] = (a) =>
24
+ pipe(a)
25
+ .map(baseType.validate)
26
+ .map(
27
+ (res): Result<R, readonly ValidationError[]> =>
28
+ Result.isErr(res)
29
+ ? res
30
+ : is(res.value)
31
+ ? Result.ok(res.value)
32
+ : Result.err([
33
+ createPrimitiveValidationError({
34
+ actualValue: a,
35
+ expectedType: typeName,
36
+ typeName,
37
+ }),
38
+ ]),
39
+ ).value;
40
+
41
+ return createType({
42
+ typeName,
43
+ defaultValue,
44
+ validate,
45
+ });
46
+ };