@resultsafe/core-fp-result 0.1.9 → 0.2.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 +98 -83
- package/README.ru.md +304 -302
- package/docs/assets/logo.svg +0 -0
- package/package.json +1 -1
- package/types/index.d.ts +1 -0
- package/types/index.d.ts.map +1 -1
- package/types/refiners/types/Handler.d.ts +9 -1
- package/types/refiners/types/Handler.d.ts.map +1 -1
- package/types/refiners/types/MatchBuilder.d.ts +20 -1
- package/types/refiners/types/MatchBuilder.d.ts.map +1 -1
- package/types/refiners/types/Matcher.d.ts +23 -1
- package/types/refiners/types/Matcher.d.ts.map +1 -1
- package/types/refiners/types/SyncRefinedResult.d.ts +23 -1
- package/types/refiners/types/SyncRefinedResult.d.ts.map +1 -1
- package/types/refiners/types/SyncRefinedResultUnion.d.ts +24 -1
- package/types/refiners/types/SyncRefinedResultUnion.d.ts.map +1 -1
- package/types/refiners/types/SyncValidatorMap.d.ts +23 -1
- package/types/refiners/types/SyncValidatorMap.d.ts.map +1 -1
- package/types/refiners/types/UniversalAsyncRefinedResult.d.ts +27 -1
- package/types/refiners/types/UniversalAsyncRefinedResult.d.ts.map +1 -1
- package/types/refiners/types/UniversalRefinedResult.d.ts +27 -1
- package/types/refiners/types/UniversalRefinedResult.d.ts.map +1 -1
- package/types/refiners/types/VariantOf.d.ts +20 -1
- package/types/refiners/types/VariantOf.d.ts.map +1 -1
- package/types/shared-types.d.ts +142 -0
- package/types/shared-types.d.ts.map +1 -1
package/docs/assets/logo.svg
CHANGED
|
Binary file
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
package/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC"}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { VariantOf } from './VariantOf.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes a handler entry for variant matching in internal matcher mechanisms.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam K - The variant key type.
|
|
6
|
+
* @typeParam T - The variant union type.
|
|
7
|
+
* @typeParam R - The return type of the handler function.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
3
11
|
export type Handler<K extends string, T extends VariantOf<K>, R> = {
|
|
4
12
|
readonly variant: K;
|
|
5
13
|
readonly fn: (value: Extract<T, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Handler.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/Handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD
|
|
1
|
+
{"version":3,"file":"Handler.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/Handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD;;;;;;;;GAQG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI;IACjE,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;IACpB,QAAQ,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,EAAE;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,CAAC,KAAK,CAAC,CAAC;CACpD,CAAC"}
|
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
import type { VariantOf } from './VariantOf.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes the shape of a strict builder for variant matching.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam T - The variant union type.
|
|
6
|
+
* @typeParam R - The return type of the match operation.
|
|
7
|
+
* @typeParam Handled - The union of already handled variant types.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { MatchBuilder } from '@resultsafe/core-fp-result';
|
|
12
|
+
*
|
|
13
|
+
* const builder: MatchBuilder<MyVariant, string> = {
|
|
14
|
+
* with: (variant, fn) => builder,
|
|
15
|
+
* run: () => 'result'
|
|
16
|
+
* };
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @since 0.1.8
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
3
22
|
export type MatchBuilder<T extends VariantOf, R, Handled extends T['type'] = never> = {
|
|
4
23
|
readonly with: <K extends Exclude<T['type'], Handled>>(variant: K, fn: (value: Extract<T, {
|
|
5
24
|
type: K;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MatchBuilder.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/MatchBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD
|
|
1
|
+
{"version":3,"file":"MatchBuilder.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/MatchBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,YAAY,CACtB,CAAC,SAAS,SAAS,EACnB,CAAC,EACD,OAAO,SAAS,CAAC,CAAC,MAAM,CAAC,GAAG,KAAK,IAC/B;IACF,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,EACnD,OAAO,EAAE,CAAC,EACV,EAAE,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,EAAE;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,CAAC,KAAK,CAAC,KACtC,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC;IACrC,QAAQ,CAAC,GAAG,EAAE,OAAO,SAAS,CAAC,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,KAAK,CAAC;CAC3D,CAAC"}
|
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
import type { VariantOf } from './VariantOf.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes the shape of a non-strict builder for variant matching.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam T - The variant union type.
|
|
6
|
+
* @typeParam R - The return type of the match operation.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* Unlike {@link MatchBuilder}, this builder allows matching any variant
|
|
10
|
+
* and provides an `otherwise` fallback handler for unmatched cases.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { Matcher } from '@resultsafe/core-fp-result';
|
|
15
|
+
*
|
|
16
|
+
* const matcher: Matcher<MyVariant, string> = {
|
|
17
|
+
* with: (variant, fn) => matcher,
|
|
18
|
+
* otherwise: (fn) => ({ run: () => 'default' })
|
|
19
|
+
* };
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @since 0.1.8
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
3
25
|
export type Matcher<T extends VariantOf, R> = {
|
|
4
26
|
readonly with: <K extends T['type']>(variant: K, fn: (value: Extract<T, {
|
|
5
27
|
type: K;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Matcher.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/Matcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD
|
|
1
|
+
{"version":3,"file":"Matcher.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/Matcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,SAAS,EAAE,CAAC,IAAI;IAC5C,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,EACjC,OAAO,EAAE,CAAC,EACV,EAAE,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,EAAE;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,CAAC,KAAK,CAAC,KACtC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACnB,QAAQ,CAAC,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,KAAK;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;KAAE,CAAC;CACxE,CAAC"}
|
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
import type { PayloadKeys, ValidatorFn, VariantConfig } from '../../shared-types.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes a synchronously refined specific variant value.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam K - The variant key type.
|
|
6
|
+
* @typeParam TMap - The variant configuration map.
|
|
7
|
+
* @typeParam _TValidators - The validator map for the variant.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* This type represents a refined variant with its payload fields
|
|
11
|
+
* validated synchronously. The `type` field discriminates the variant,
|
|
12
|
+
* and the remaining fields are the validated payload.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { SyncRefinedResult, VariantConfig } from '@resultsafe/core-fp-result';
|
|
17
|
+
*
|
|
18
|
+
* type MyVariant = SyncRefinedResult<'user', { user: { payload: 'name' } }, {}>;
|
|
19
|
+
* // { type: 'user'; name: unknown }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @since 0.1.8
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
3
25
|
export type SyncRefinedResult<K extends keyof TMap & string, TMap extends Record<string, VariantConfig>, _TValidators extends Partial<Record<PayloadKeys<TMap[K]>, ValidatorFn>>> = {
|
|
4
26
|
type: K;
|
|
5
27
|
} & Record<PayloadKeys<TMap[K]>, unknown>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SyncRefinedResult.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/SyncRefinedResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,aAAa,EACd,MAAM,uBAAuB,CAAC;AAE/B
|
|
1
|
+
{"version":3,"file":"SyncRefinedResult.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/SyncRefinedResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,aAAa,EACd,MAAM,uBAAuB,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,iBAAiB,CAC3B,CAAC,SAAS,MAAM,IAAI,GAAG,MAAM,EAC7B,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EAC1C,YAAY,SAAS,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC,IACrE;IACF,IAAI,EAAE,CAAC,CAAC;CACT,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC"}
|
|
@@ -1,7 +1,30 @@
|
|
|
1
1
|
import type { VariantConfig } from '../../shared-types.js';
|
|
2
2
|
import type { SyncRefinedResult } from './SyncRefinedResult.js';
|
|
3
3
|
import type { SyncValidatorMap } from './SyncValidatorMap.js';
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Describes a union of synchronously refined variants.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam TMap - The variant configuration map.
|
|
8
|
+
* @typeParam TValidators - The validator map for all variants.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* This type represents the union of all refined variants in the map.
|
|
12
|
+
* Each variant is refined using {@link SyncRefinedResult}.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { SyncRefinedResultUnion, VariantConfig } from '@resultsafe/core-fp-result';
|
|
17
|
+
*
|
|
18
|
+
* type Variants = SyncRefinedResultUnion<
|
|
19
|
+
* { user: { payload: 'name' }; error: { payload: 'code' } },
|
|
20
|
+
* {}
|
|
21
|
+
* >;
|
|
22
|
+
* // { type: 'user'; name: unknown } | { type: 'error'; code: unknown }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @since 0.1.8
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
5
28
|
export type SyncRefinedResultUnion<TMap extends Record<string, VariantConfig>, TValidators extends SyncValidatorMap<TMap>> = {
|
|
6
29
|
[K in keyof TMap & string]: SyncRefinedResult<K, TMap, NonNullable<TValidators[K]>>;
|
|
7
30
|
}[keyof TMap & string];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SyncRefinedResultUnion.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/SyncRefinedResultUnion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D
|
|
1
|
+
{"version":3,"file":"SyncRefinedResultUnion.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/SyncRefinedResultUnion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,MAAM,sBAAsB,CAChC,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EAC1C,WAAW,SAAS,gBAAgB,CAAC,IAAI,CAAC,IACxC;KACD,CAAC,IAAI,MAAM,IAAI,GAAG,MAAM,GAAG,iBAAiB,CAC3C,CAAC,EACD,IAAI,EACJ,WAAW,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAC5B;CACF,CAAC,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC"}
|
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
import type { PayloadKeys, ValidatorFn, VariantConfig } from '../../shared-types.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes validator sets grouped by variant key.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam TMap - The variant configuration map.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* This type maps each variant to its optional validators for payload fields.
|
|
9
|
+
* Validators are used to refine and check variant data at runtime.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { SyncValidatorMap, VariantConfig } from '@resultsafe/core-fp-result';
|
|
14
|
+
*
|
|
15
|
+
* type Validators = SyncValidatorMap<{
|
|
16
|
+
* user: { payload: 'name' | 'age' };
|
|
17
|
+
* error: { payload: 'code' };
|
|
18
|
+
* }>;
|
|
19
|
+
* // { user?: { name?: ValidatorFn; age?: ValidatorFn }; error?: { code?: ValidatorFn } }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @since 0.1.8
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
3
25
|
export type SyncValidatorMap<TMap extends Record<string, VariantConfig>> = {
|
|
4
26
|
[K in keyof TMap]?: Partial<Record<PayloadKeys<TMap[K]>, ValidatorFn>>;
|
|
5
27
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SyncValidatorMap.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/SyncValidatorMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,aAAa,EACd,MAAM,uBAAuB,CAAC;AAE/B
|
|
1
|
+
{"version":3,"file":"SyncValidatorMap.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/SyncValidatorMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,aAAa,EACd,MAAM,uBAAuB,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,gBAAgB,CAAC,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,IAAI;KACxE,CAAC,IAAI,MAAM,IAAI,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC;CACvE,CAAC"}
|
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
import type { VariantConfig } from '../../shared-types.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes a generalized asynchronously refined variant value.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam K - The variant key type.
|
|
6
|
+
* @typeParam TMap - The variant configuration map.
|
|
7
|
+
* @typeParam _TValidators - The validator map for the variant.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* This type represents a variant refined through async validation.
|
|
11
|
+
* The `type` field discriminates the variant, and additional fields
|
|
12
|
+
* are validated asynchronously.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { UniversalAsyncRefinedResult, VariantConfig } from '@resultsafe/core-fp-result';
|
|
17
|
+
*
|
|
18
|
+
* type AsyncVariant = UniversalAsyncRefinedResult<
|
|
19
|
+
* 'user',
|
|
20
|
+
* { user: { payload: 'name' } },
|
|
21
|
+
* {}
|
|
22
|
+
* >;
|
|
23
|
+
* // { type: 'user'; [key: string]: unknown }
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @since 0.1.8
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
3
29
|
export type UniversalAsyncRefinedResult<K extends keyof TMap & string, TMap extends Record<string, VariantConfig>, _TValidators extends Record<string, unknown>> = {
|
|
4
30
|
type: K;
|
|
5
31
|
} & Record<string, unknown>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UniversalAsyncRefinedResult.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/UniversalAsyncRefinedResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D
|
|
1
|
+
{"version":3,"file":"UniversalAsyncRefinedResult.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/UniversalAsyncRefinedResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,MAAM,2BAA2B,CACrC,CAAC,SAAS,MAAM,IAAI,GAAG,MAAM,EAC7B,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EAC1C,YAAY,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAC1C;IACF,IAAI,EAAE,CAAC,CAAC;CACT,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC"}
|
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
import type { VariantConfig } from '../../shared-types.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes a generalized synchronously refined variant value.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam K - The variant key type.
|
|
6
|
+
* @typeParam TMap - The variant configuration map.
|
|
7
|
+
* @typeParam _TValidators - The validator map for the variant.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* This type represents a variant refined through sync validation.
|
|
11
|
+
* The `type` field discriminates the variant, and additional fields
|
|
12
|
+
* are validated synchronously.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { UniversalRefinedResult, VariantConfig } from '@resultsafe/core-fp-result';
|
|
17
|
+
*
|
|
18
|
+
* type SyncVariant = UniversalRefinedResult<
|
|
19
|
+
* 'user',
|
|
20
|
+
* { user: { payload: 'name' } },
|
|
21
|
+
* {}
|
|
22
|
+
* >;
|
|
23
|
+
* // { type: 'user'; [key: string]: unknown }
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @since 0.1.8
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
3
29
|
export type UniversalRefinedResult<K extends keyof TMap & string, TMap extends Record<string, VariantConfig>, _TValidators extends Record<string, unknown>> = {
|
|
4
30
|
type: K;
|
|
5
31
|
} & Record<string, unknown>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UniversalRefinedResult.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/UniversalRefinedResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D
|
|
1
|
+
{"version":3,"file":"UniversalRefinedResult.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/UniversalRefinedResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,MAAM,sBAAsB,CAChC,CAAC,SAAS,MAAM,IAAI,GAAG,MAAM,EAC7B,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EAC1C,YAAY,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAC1C;IACF,IAAI,EAAE,CAAC,CAAC;CACT,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC"}
|
|
@@ -1,4 +1,23 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Describes the minimal contract for a discriminated union variant.
|
|
3
|
+
*
|
|
4
|
+
* @typeParam K - The variant key type. Defaults to `string`.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* This type is used as a constraint for variant unions. Each variant
|
|
8
|
+
* must have a `type` field that serves as the discriminator.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { VariantOf } from '@resultsafe/core-fp-result';
|
|
13
|
+
*
|
|
14
|
+
* type MyVariant = VariantOf<'user' | 'error'>;
|
|
15
|
+
* // { type: 'user' } | { type: 'error' }
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @since 0.1.8
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
2
21
|
export type VariantOf<K extends string = string> = {
|
|
3
22
|
type: K;
|
|
4
23
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VariantOf.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/VariantOf.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"VariantOf.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/VariantOf.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI;IAAE,IAAI,EAAE,CAAC,CAAA;CAAE,CAAC"}
|
package/types/shared-types.d.ts
CHANGED
|
@@ -1,3 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the result of an operation that can either succeed with a
|
|
3
|
+
* value of type `T` or fail with an error of type `E`.
|
|
4
|
+
*
|
|
5
|
+
* @summary
|
|
6
|
+
* Represents the result of an operation that can either succeed with a
|
|
7
|
+
* value of type `T` or fail with an error of type `E`.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* `Result` is the core type of this library. Pattern match on the `ok`
|
|
11
|
+
* field to narrow the union, then access `value` or `error` safely.
|
|
12
|
+
*
|
|
13
|
+
* Unlike exceptions, `Result` makes the failure path explicit in the
|
|
14
|
+
* type signature, forcing the caller to handle both outcomes.
|
|
15
|
+
*
|
|
16
|
+
* @typeParam T - The type of the success value.
|
|
17
|
+
* @typeParam E - The type of the error.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { Result, Ok, Err } from '@resultsafe/core-fp-result';
|
|
22
|
+
*
|
|
23
|
+
* function divide(a: number, b: number): Result<number, string> {
|
|
24
|
+
* if (b === 0) return Err("Division by zero");
|
|
25
|
+
* return Ok(a / b);
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* const result = divide(10, 2);
|
|
29
|
+
*
|
|
30
|
+
* if (result.ok) {
|
|
31
|
+
* console.log(result.value); // 5
|
|
32
|
+
* } else {
|
|
33
|
+
* console.error(result.error);
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @since 0.1.8
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
1
40
|
export type Result<T, E> = {
|
|
2
41
|
readonly ok: true;
|
|
3
42
|
readonly value: T;
|
|
@@ -5,18 +44,121 @@ export type Result<T, E> = {
|
|
|
5
44
|
readonly ok: false;
|
|
6
45
|
readonly error: E;
|
|
7
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* Represents an optional value that can either contain a value of type `T`
|
|
49
|
+
* or be empty.
|
|
50
|
+
*
|
|
51
|
+
* @summary
|
|
52
|
+
* Represents an optional value that can either contain a value of type `T`
|
|
53
|
+
* or be empty.
|
|
54
|
+
*
|
|
55
|
+
* @remarks
|
|
56
|
+
* `Option` is used to represent the presence or absence of a value. It is
|
|
57
|
+
* an alternative to `null` or `undefined` that is type-safe and explicit.
|
|
58
|
+
*
|
|
59
|
+
* @typeParam T - The type of the contained value.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { Option } from '@resultsafe/core-fp-result';
|
|
64
|
+
*
|
|
65
|
+
* const someValue: Option<number> = { some: true, value: 42 };
|
|
66
|
+
* const noValue: Option<number> = { some: false };
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @since 0.1.8
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
8
72
|
export type Option<T> = {
|
|
9
73
|
readonly some: true;
|
|
10
74
|
readonly value: T;
|
|
11
75
|
} | {
|
|
12
76
|
readonly some: false;
|
|
13
77
|
};
|
|
78
|
+
/**
|
|
79
|
+
* Configuration options for variant validation.
|
|
80
|
+
*
|
|
81
|
+
* @remarks
|
|
82
|
+
* Used to configure how variants are validated, including payload keys
|
|
83
|
+
* and field strictness.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* import { VariantConfig } from '@resultsafe/core-fp-result';
|
|
88
|
+
*
|
|
89
|
+
* const config: VariantConfig = {
|
|
90
|
+
* payload: ['name', 'age'],
|
|
91
|
+
* forbidden: 'id',
|
|
92
|
+
* strictFields: true
|
|
93
|
+
* };
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* @since 0.1.8
|
|
97
|
+
* @public
|
|
98
|
+
*/
|
|
14
99
|
export interface VariantConfig {
|
|
15
100
|
readonly payload: 'never' | string | readonly string[];
|
|
16
101
|
readonly forbidden?: string | undefined;
|
|
17
102
|
readonly strictFields?: boolean | undefined;
|
|
18
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Extracts the payload keys from a `VariantConfig`.
|
|
106
|
+
*
|
|
107
|
+
* @typeParam T - The `VariantConfig` type to extract keys from.
|
|
108
|
+
* @returns The union of payload keys, or `never` if payload is `'never'`.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* import { PayloadKeys, VariantConfig } from '@resultsafe/core-fp-result';
|
|
113
|
+
*
|
|
114
|
+
* type Config = { payload: 'name' | 'age'; forbidden?: string };
|
|
115
|
+
* type Keys = PayloadKeys<Config>; // 'name' | 'age'
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* @since 0.1.8
|
|
119
|
+
* @public
|
|
120
|
+
*/
|
|
19
121
|
export type PayloadKeys<T extends VariantConfig> = T['payload'] extends 'never' ? never : T['payload'] extends string ? T['payload'] : T['payload'] extends readonly string[] ? T['payload'][number] : never;
|
|
122
|
+
/**
|
|
123
|
+
* A synchronous validator function that checks if a value is of a specific type.
|
|
124
|
+
*
|
|
125
|
+
* @typeParam T - The type the validator checks for.
|
|
126
|
+
* @param x - The value to validate.
|
|
127
|
+
* @returns `true` if `x` is of type `T`, `false` otherwise.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* import { ValidatorFn } from '@resultsafe/core-fp-result';
|
|
132
|
+
*
|
|
133
|
+
* const isNumber: ValidatorFn<number> = (x): x is number =>
|
|
134
|
+
* typeof x === 'number';
|
|
135
|
+
*
|
|
136
|
+
* console.log(isNumber(42)); // true
|
|
137
|
+
* console.log(isNumber('42')); // false
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @since 0.1.8
|
|
141
|
+
* @public
|
|
142
|
+
*/
|
|
20
143
|
export type ValidatorFn<T = unknown> = (x: unknown) => x is T;
|
|
144
|
+
/**
|
|
145
|
+
* An asynchronous validator function that checks if a value is valid.
|
|
146
|
+
*
|
|
147
|
+
* @param value - The value to validate.
|
|
148
|
+
* @returns A promise that resolves to `true` if valid, `false` otherwise.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* import { AsyncValidatorFn } from '@resultsafe/core-fp-result';
|
|
153
|
+
*
|
|
154
|
+
* const isValidId: AsyncValidatorFn = async (value) => {
|
|
155
|
+
* // Simulate async database check
|
|
156
|
+
* return typeof value === 'string' && value.length > 0;
|
|
157
|
+
* };
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* @since 0.1.8
|
|
161
|
+
* @public
|
|
162
|
+
*/
|
|
21
163
|
export type AsyncValidatorFn = (value: unknown) => Promise<boolean>;
|
|
22
164
|
//# sourceMappingURL=shared-types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-types.d.ts","sourceRoot":"","sources":["../../src/shared-types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,IACnB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GACxC;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,CAAC;AAE9C,MAAM,MAAM,MAAM,CAAC,CAAC,IAChB;IAAE,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GAC1C;IAAE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAA;CAAE,CAAC;AAE7B,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC;IACvD,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC,QAAQ,CAAC,YAAY,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAC7C;AAED,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,aAAa,IAAI,CAAC,CAAC,SAAS,CAAC,SAAS,OAAO,GAC3E,KAAK,GACL,CAAC,CAAC,SAAS,CAAC,SAAS,MAAM,GACzB,CAAC,CAAC,SAAS,CAAC,GACZ,CAAC,CAAC,SAAS,CAAC,SAAS,SAAS,MAAM,EAAE,GACpC,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,GACpB,KAAK,CAAC;AAEd,MAAM,MAAM,WAAW,CAAC,CAAC,GAAG,OAAO,IAAI,CAAC,CAAC,EAAE,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC;AAE9D,MAAM,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC"}
|
|
1
|
+
{"version":3,"file":"shared-types.d.ts","sourceRoot":"","sources":["../../src/shared-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,IACnB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GACxC;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IAChB;IAAE,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GAC1C;IAAE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAA;CAAE,CAAC;AAE7B;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC;IACvD,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC,QAAQ,CAAC,YAAY,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAC7C;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,aAAa,IAAI,CAAC,CAAC,SAAS,CAAC,SAAS,OAAO,GAC3E,KAAK,GACL,CAAC,CAAC,SAAS,CAAC,SAAS,MAAM,GACzB,CAAC,CAAC,SAAS,CAAC,GACZ,CAAC,CAAC,SAAS,CAAC,SAAS,SAAS,MAAM,EAAE,GACpC,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,GACpB,KAAK,CAAC;AAEd;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,GAAG,OAAO,IAAI,CAAC,CAAC,EAAE,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC;AAE9D;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC"}
|