@duplojs/utils 1.0.2 → 1.0.4

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.
Files changed (53) hide show
  1. package/dist/common/and.d.ts +31 -0
  2. package/dist/common/equal.cjs +14 -0
  3. package/dist/common/equal.d.ts +3 -0
  4. package/dist/common/equal.mjs +12 -0
  5. package/dist/common/index.d.ts +3 -0
  6. package/dist/common/isType.cjs +24 -0
  7. package/dist/common/isType.d.ts +20 -0
  8. package/dist/common/isType.mjs +22 -0
  9. package/dist/common/not.cjs +12 -0
  10. package/dist/common/not.d.ts +2 -0
  11. package/dist/common/not.mjs +10 -0
  12. package/dist/common/or.d.ts +26 -0
  13. package/dist/index.cjs +6 -0
  14. package/dist/index.mjs +3 -0
  15. package/dist/object/discriminate.cjs +15 -0
  16. package/dist/object/discriminate.d.ts +11 -0
  17. package/dist/object/discriminate.mjs +13 -0
  18. package/dist/object/getDeepProperty.cjs +18 -0
  19. package/dist/object/getDeepProperty.d.ts +6 -0
  20. package/dist/object/getDeepProperty.mjs +16 -0
  21. package/dist/object/index.cjs +4 -0
  22. package/dist/object/index.d.ts +2 -0
  23. package/dist/object/index.mjs +2 -0
  24. package/dist/object/types/getPropsWithValueExtends.d.ts +3 -0
  25. package/dist/object/types/index.d.ts +2 -0
  26. package/dist/object/types/unFlatObject.d.ts +21 -0
  27. package/dist/pattern/index.cjs +2 -2
  28. package/dist/pattern/index.d.ts +1 -1
  29. package/dist/pattern/index.mjs +1 -1
  30. package/dist/pattern/isMatch.cjs +6 -1
  31. package/dist/pattern/isMatch.d.ts +5 -2
  32. package/dist/pattern/isMatch.mjs +6 -1
  33. package/dist/pattern/match.cjs +1 -1
  34. package/dist/pattern/match.mjs +2 -2
  35. package/dist/pattern/result.cjs +1 -1
  36. package/dist/pattern/result.mjs +1 -1
  37. package/dist/pattern/types/complexMatchedValue/object.d.ts +2 -1
  38. package/dist/pattern/when.cjs +17 -0
  39. package/dist/pattern/when.d.ts +6 -0
  40. package/dist/pattern/when.mjs +15 -0
  41. package/dist/string/endsWith.d.ts +5 -2
  42. package/dist/string/includes.d.ts +5 -2
  43. package/dist/string/index.cjs +2 -0
  44. package/dist/string/index.d.ts +1 -0
  45. package/dist/string/index.mjs +1 -0
  46. package/dist/string/isIn.cjs +12 -0
  47. package/dist/string/isIn.d.ts +2 -0
  48. package/dist/string/isIn.mjs +10 -0
  49. package/dist/string/startsWith.d.ts +5 -2
  50. package/package.json +1 -1
  51. package/dist/pattern/matchPrimitive.cjs +0 -17
  52. package/dist/pattern/matchPrimitive.d.ts +0 -3
  53. package/dist/pattern/matchPrimitive.mjs +0 -15
@@ -0,0 +1,31 @@
1
+ import { type IsEqual, type AnyFunction, type UnionToIntersection } from "./types";
2
+ type ExtractIntersection<GenericInput extends unknown, GenericPredicatedInput extends AnyFunction<any[], boolean>[], GenericResult extends unknown = GenericInput> = GenericPredicatedInput extends [
3
+ (input: any) => input is infer InferredPredicate,
4
+ ...infer InferredRest extends AnyFunction<any[], boolean>[]
5
+ ] ? Extract<GenericResult, InferredPredicate> extends infer InferredResult extends GenericInput ? InferredRest extends readonly [] ? InferredResult : ExtractIntersection<GenericInput, InferredRest, InferredResult> : never : never;
6
+ type ExtractPredicate<GenericInput extends unknown, GenericPredicatedInput extends AnyFunction<any[], boolean>[]> = GenericPredicatedInput extends [
7
+ (input: any) => input is infer InferredPredicate,
8
+ ...infer InferredRest extends AnyFunction<any[], boolean>[]
9
+ ] ? InferredRest extends readonly [] ? InferredPredicate : InferredPredicate | ExtractPredicate<GenericInput, InferredRest> : GenericInput;
10
+ type ComputeResult<GenericInput extends unknown, GenericPredicatedInput extends AnyFunction<any[], boolean>[]> = ExtractIntersection<GenericInput, GenericPredicatedInput> extends infer InferredResult extends GenericInput ? IsEqual<InferredResult, never> extends true ? GenericInput & UnionToIntersection<ExtractPredicate<GenericInput, GenericPredicatedInput>> : InferredResult : never;
11
+ export declare function and<GenericInput extends unknown, GenericArrayPredicatedInput extends [
12
+ (input: GenericInput) => input is any,
13
+ (input: GenericInput) => input is any,
14
+ ...((input: GenericInput) => input is any)[]
15
+ ]>(predicatedList: GenericArrayPredicatedInput): (input: GenericInput) => input is ComputeResult<GenericInput, GenericArrayPredicatedInput>;
16
+ export declare function and<GenericInput extends unknown>(predicatedList: [
17
+ (input: GenericInput) => boolean,
18
+ (input: GenericInput) => boolean,
19
+ ...((input: GenericInput) => boolean)[]
20
+ ]): (input: GenericInput) => boolean;
21
+ export declare function and<GenericInput extends unknown, GenericArrayPredicatedInput extends [
22
+ (input: GenericInput) => input is any,
23
+ (input: GenericInput) => input is any,
24
+ ...((input: GenericInput) => input is any)[]
25
+ ]>(input: GenericInput, predicatedList: GenericArrayPredicatedInput): input is ComputeResult<GenericInput, GenericArrayPredicatedInput>;
26
+ export declare function and<GenericInput extends unknown>(input: GenericInput, predicatedList: [
27
+ (input: GenericInput) => boolean,
28
+ (input: GenericInput) => boolean,
29
+ ...((input: GenericInput) => boolean)[]
30
+ ]): boolean;
31
+ export {};
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ function equal(...args) {
4
+ if (args.length === 1) {
5
+ const [value] = args;
6
+ return (input) => equal(input, value);
7
+ }
8
+ const [input, value] = args;
9
+ return value instanceof Array
10
+ ? value.includes(input)
11
+ : input === value;
12
+ }
13
+
14
+ exports.equal = equal;
@@ -0,0 +1,3 @@
1
+ export type EligibleEqual = string | null | number | undefined | bigint | boolean | symbol;
2
+ export declare function equal<GenericInput extends EligibleEqual, GenericValue extends GenericInput>(value: GenericValue | GenericValue[]): (input: GenericInput) => input is NoInfer<GenericValue>;
3
+ export declare function equal<GenericInput extends EligibleEqual, GenericValue extends GenericInput>(input: GenericInput, value: GenericValue | GenericValue[]): input is GenericValue;
@@ -0,0 +1,12 @@
1
+ function equal(...args) {
2
+ if (args.length === 1) {
3
+ const [value] = args;
4
+ return (input) => equal(input, value);
5
+ }
6
+ const [input, value] = args;
7
+ return value instanceof Array
8
+ ? value.includes(input)
9
+ : input === value;
10
+ }
11
+
12
+ export { equal };
@@ -27,3 +27,6 @@ export * from "./loop";
27
27
  export * from "./forward";
28
28
  export * from "./when";
29
29
  export * from "./whenNot";
30
+ export * from "./equal";
31
+ export * from "./not";
32
+ export * from "./isType";
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ const testTypeWrapper = {
4
+ string: (input) => typeof input === "string",
5
+ number: (input) => typeof input === "number",
6
+ bigint: (input) => typeof input === "bigint",
7
+ boolean: (input) => typeof input === "boolean",
8
+ function: (input) => typeof input === "function",
9
+ symbol: (input) => typeof input === "symbol",
10
+ undefined: (input) => typeof input === "undefined",
11
+ null: (input) => input === null,
12
+ array: (input) => input instanceof Array,
13
+ object: (input) => !!input && typeof input === "object" && !(input instanceof Array),
14
+ };
15
+ function isType(...args) {
16
+ if (args.length === 1) {
17
+ const [type] = args;
18
+ return (input) => isType(input, type);
19
+ }
20
+ const [input, type] = args;
21
+ return testTypeWrapper[type](input);
22
+ }
23
+
24
+ exports.isType = isType;
@@ -0,0 +1,20 @@
1
+ import { type IsEqual, type AnyFunction } from "./types";
2
+ interface Type {
3
+ string: [string, never];
4
+ number: [number, never];
5
+ boolean: [boolean, never];
6
+ function: [AnyFunction, never];
7
+ bigint: [bigint, never];
8
+ undefined: [undefined, never];
9
+ null: [null, never];
10
+ symbol: [symbol, never];
11
+ object: [object, any[] | AnyFunction];
12
+ array: [any[], never];
13
+ }
14
+ type ComputeResult<GenericInput extends unknown, GenericTypeEntry extends Type[keyof Type]> = Exclude<Extract<GenericInput, GenericTypeEntry[0]>, GenericTypeEntry[1]>;
15
+ type EligibleType<GenericInput extends unknown> = {
16
+ [Prop in keyof Type]: IsEqual<ComputeResult<GenericInput, Type[Prop]>, never> extends true ? never : Prop;
17
+ }[keyof Type];
18
+ export declare function isType<GenericInput extends unknown, GenericType extends EligibleType<GenericInput>>(type: GenericType): (input: GenericInput) => input is ComputeResult<GenericInput, Type[GenericType]>;
19
+ export declare function isType<GenericInput extends unknown, GenericType extends EligibleType<GenericInput>>(input: GenericInput, type: GenericType): input is ComputeResult<GenericInput, Type[GenericType]>;
20
+ export {};
@@ -0,0 +1,22 @@
1
+ const testTypeWrapper = {
2
+ string: (input) => typeof input === "string",
3
+ number: (input) => typeof input === "number",
4
+ bigint: (input) => typeof input === "bigint",
5
+ boolean: (input) => typeof input === "boolean",
6
+ function: (input) => typeof input === "function",
7
+ symbol: (input) => typeof input === "symbol",
8
+ undefined: (input) => typeof input === "undefined",
9
+ null: (input) => input === null,
10
+ array: (input) => input instanceof Array,
11
+ object: (input) => !!input && typeof input === "object" && !(input instanceof Array),
12
+ };
13
+ function isType(...args) {
14
+ if (args.length === 1) {
15
+ const [type] = args;
16
+ return (input) => isType(input, type);
17
+ }
18
+ const [input, type] = args;
19
+ return testTypeWrapper[type](input);
20
+ }
21
+
22
+ export { isType };
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ function not(...args) {
4
+ if (args.length === 1) {
5
+ const [predicate] = args;
6
+ return (input) => not(input, predicate);
7
+ }
8
+ const [input, predicate] = args;
9
+ return !predicate(input);
10
+ }
11
+
12
+ exports.not = not;
@@ -0,0 +1,2 @@
1
+ export declare function not<GenericInput extends unknown, GenericPredicatedInput extends GenericInput>(predicate: (input: GenericInput) => input is GenericPredicatedInput): (input: GenericInput) => input is Exclude<GenericInput, GenericPredicatedInput>;
2
+ export declare function not<GenericInput extends unknown, GenericPredicatedInput extends GenericInput>(input: GenericInput, predicate: (input: GenericInput) => input is GenericPredicatedInput): input is Exclude<GenericInput, GenericPredicatedInput>;
@@ -0,0 +1,10 @@
1
+ function not(...args) {
2
+ if (args.length === 1) {
3
+ const [predicate] = args;
4
+ return (input) => not(input, predicate);
5
+ }
6
+ const [input, predicate] = args;
7
+ return !predicate(input);
8
+ }
9
+
10
+ export { not };
@@ -0,0 +1,26 @@
1
+ import { type AnyFunction } from "./types";
2
+ type ExtractPredicate<GenericInput extends unknown, GenericPredicatedInput extends AnyFunction<any[], boolean>[]> = GenericPredicatedInput extends [
3
+ (input: any) => input is infer InferredPredicate,
4
+ ...infer InferredRest extends AnyFunction<any[], boolean>[]
5
+ ] ? InferredRest extends readonly [] ? InferredPredicate : InferredPredicate | ExtractPredicate<GenericInput, InferredRest> : GenericInput;
6
+ export declare function or<GenericInput extends unknown, GenericArrayPredicatedInput extends [
7
+ (input: GenericInput) => input is any,
8
+ (input: GenericInput) => input is any,
9
+ ...((input: GenericInput) => input is any)[]
10
+ ]>(predicatedList: GenericArrayPredicatedInput): (input: GenericInput) => input is Extract<GenericInput, ExtractPredicate<GenericInput, GenericArrayPredicatedInput>>;
11
+ export declare function or<GenericInput extends unknown>(predicatedList: [
12
+ (input: GenericInput) => boolean,
13
+ (input: GenericInput) => boolean,
14
+ ...((input: GenericInput) => boolean)[]
15
+ ]): (input: GenericInput) => boolean;
16
+ export declare function or<GenericInput extends unknown, GenericArrayPredicatedInput extends [
17
+ (input: GenericInput) => input is any,
18
+ (input: GenericInput) => input is any,
19
+ ...((input: GenericInput) => input is any)[]
20
+ ]>(input: GenericInput, predicatedList: GenericArrayPredicatedInput): input is Extract<GenericInput, ExtractPredicate<GenericInput, GenericArrayPredicatedInput>>;
21
+ export declare function or<GenericInput extends unknown>(input: GenericInput, predicatedList: [
22
+ (input: GenericInput) => boolean,
23
+ (input: GenericInput) => boolean,
24
+ ...((input: GenericInput) => boolean)[]
25
+ ]): boolean;
26
+ export {};
package/dist/index.cjs CHANGED
@@ -28,6 +28,9 @@ var loop = require('./common/loop.cjs');
28
28
  var forward = require('./common/forward.cjs');
29
29
  var when = require('./common/when.cjs');
30
30
  var whenNot = require('./common/whenNot.cjs');
31
+ var equal = require('./common/equal.cjs');
32
+ var not = require('./common/not.cjs');
33
+ var isType = require('./common/isType.cjs');
31
34
  var index = require('./array/index.cjs');
32
35
  var index$1 = require('./number/index.cjs');
33
36
  var index$2 = require('./either/index.cjs');
@@ -72,6 +75,9 @@ exports.loop = loop.loop;
72
75
  exports.forward = forward.forward;
73
76
  exports.when = when.when;
74
77
  exports.whenNot = whenNot.whenNot;
78
+ exports.equal = equal.equal;
79
+ exports.not = not.not;
80
+ exports.isType = isType.isType;
75
81
  exports.A = index;
76
82
  exports.DArray = index;
77
83
  exports.DNumber = index$1;
package/dist/index.mjs CHANGED
@@ -26,6 +26,9 @@ export { loop } from './common/loop.mjs';
26
26
  export { forward } from './common/forward.mjs';
27
27
  export { when } from './common/when.mjs';
28
28
  export { whenNot } from './common/whenNot.mjs';
29
+ export { equal } from './common/equal.mjs';
30
+ export { not } from './common/not.mjs';
31
+ export { isType } from './common/isType.mjs';
29
32
  import * as index from './array/index.mjs';
30
33
  export { index as A };
31
34
  export { index as DArray };
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ var equal = require('../common/equal.cjs');
4
+ var getDeepProperty = require('./getDeepProperty.cjs');
5
+
6
+ function discriminate(...args) {
7
+ if (args.length === 2) {
8
+ const [path, value] = args;
9
+ return (input) => discriminate(input, path, value);
10
+ }
11
+ const [input, path, value] = args;
12
+ return equal.equal(getDeepProperty.getDeepProperty(input, path), value);
13
+ }
14
+
15
+ exports.discriminate = discriminate;
@@ -0,0 +1,11 @@
1
+ import { type EligibleEqual, type MaybeArray } from "../common";
2
+ import { type UnFlatObject, type FlatObject } from "./types";
3
+ import { type GetPropsWithValueExtends } from "./types/getPropsWithValueExtends";
4
+ type ObjectProjection<GenericInput extends object> = FlatObject<GenericInput> extends infer InferredResult extends object ? Omit<Pick<InferredResult, GetPropsWithValueExtends<InferredResult, EligibleEqual>>, `${string}[${string}]${string}`> : never;
5
+ export declare function discriminate<GenericInput extends object, GenericObjectProjection extends ObjectProjection<GenericInput>, GenericPath extends keyof GenericObjectProjection, GenericValue extends EligibleEqual>(path: GenericPath, value: (MaybeArray<(GenericValue & GenericObjectProjection[GenericPath])> | MaybeArray<GenericObjectProjection[GenericPath]>)): (input: GenericInput) => input is Extract<GenericInput, UnFlatObject<{
6
+ [Prop in GenericPath]: GenericValue;
7
+ }>>;
8
+ export declare function discriminate<GenericInput extends object, GenericObjectProjection extends ObjectProjection<GenericInput>, GenericPath extends keyof GenericObjectProjection, GenericValue extends EligibleEqual>(input: GenericInput, path: GenericPath, value: (MaybeArray<(GenericValue & GenericObjectProjection[GenericPath])> | MaybeArray<GenericObjectProjection[GenericPath]>)): input is Extract<GenericInput, UnFlatObject<{
9
+ [Prop in GenericPath]: GenericValue;
10
+ }>>;
11
+ export {};
@@ -0,0 +1,13 @@
1
+ import { equal } from '../common/equal.mjs';
2
+ import { getDeepProperty } from './getDeepProperty.mjs';
3
+
4
+ function discriminate(...args) {
5
+ if (args.length === 2) {
6
+ const [path, value] = args;
7
+ return (input) => discriminate(input, path, value);
8
+ }
9
+ const [input, path, value] = args;
10
+ return equal(getDeepProperty(input, path), value);
11
+ }
12
+
13
+ export { discriminate };
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ const regexExtractProperty = /([^.]*)(?:\.([^]*))?/;
4
+ function getDeepProperty(...args) {
5
+ if (args.length === 1) {
6
+ const [path] = args;
7
+ return (input) => getDeepProperty(input, path);
8
+ }
9
+ const [input, path] = args;
10
+ const [_match, first, rest] = path.match(regexExtractProperty);
11
+ const currentValue = input[first];
12
+ if (rest) {
13
+ return getDeepProperty(currentValue, rest);
14
+ }
15
+ return currentValue;
16
+ }
17
+
18
+ exports.getDeepProperty = getDeepProperty;
@@ -0,0 +1,6 @@
1
+ import { type EligibleEqual } from "../common";
2
+ import { type FlatObject, type GetPropsWithValueExtends } from "./types";
3
+ type ObjectProjection<GenericInput extends object> = FlatObject<GenericInput> extends infer InferredResult extends object ? Omit<Pick<InferredResult, GetPropsWithValueExtends<InferredResult, EligibleEqual>>, `${string}[${string}]${string}`> : never;
4
+ export declare function getDeepProperty<GenericInput extends object, GenericObjectProjection extends ObjectProjection<GenericInput>, GenericPath extends keyof GenericObjectProjection>(path: GenericPath): (input: GenericInput) => GenericObjectProjection[GenericPath];
5
+ export declare function getDeepProperty<GenericInput extends object, GenericObjectProjection extends ObjectProjection<GenericInput>, GenericPath extends keyof GenericObjectProjection>(input: GenericInput, path: GenericPath): GenericObjectProjection[GenericPath];
6
+ export {};
@@ -0,0 +1,16 @@
1
+ const regexExtractProperty = /([^.]*)(?:\.([^]*))?/;
2
+ function getDeepProperty(...args) {
3
+ if (args.length === 1) {
4
+ const [path] = args;
5
+ return (input) => getDeepProperty(input, path);
6
+ }
7
+ const [input, path] = args;
8
+ const [_match, first, rest] = path.match(regexExtractProperty);
9
+ const currentValue = input[first];
10
+ if (rest) {
11
+ return getDeepProperty(currentValue, rest);
12
+ }
13
+ return currentValue;
14
+ }
15
+
16
+ export { getDeepProperty };
@@ -13,6 +13,8 @@ var assign = require('./assign.cjs');
13
13
  var override = require('./override.cjs');
14
14
  var omit = require('./omit.cjs');
15
15
  var pick = require('./pick.cjs');
16
+ var discriminate = require('./discriminate.cjs');
17
+ var getDeepProperty = require('./getDeepProperty.cjs');
16
18
 
17
19
 
18
20
 
@@ -29,3 +31,5 @@ exports.assign = assign.assign;
29
31
  exports.override = override.override;
30
32
  exports.omit = omit.omit;
31
33
  exports.pick = pick.pick;
34
+ exports.discriminate = discriminate.discriminate;
35
+ exports.getDeepProperty = getDeepProperty.getDeepProperty;
@@ -12,3 +12,5 @@ export * from "./assign";
12
12
  export * from "./override";
13
13
  export * from "./omit";
14
14
  export * from "./pick";
15
+ export * from "./discriminate";
16
+ export * from "./getDeepProperty";
@@ -11,3 +11,5 @@ export { assign } from './assign.mjs';
11
11
  export { override } from './override.mjs';
12
12
  export { omit } from './omit.mjs';
13
13
  export { pick } from './pick.mjs';
14
+ export { discriminate } from './discriminate.mjs';
15
+ export { getDeepProperty } from './getDeepProperty.mjs';
@@ -0,0 +1,3 @@
1
+ export type GetPropsWithValueExtends<GenericObject extends object, GenericValue extends unknown> = {
2
+ [Prop in keyof GenericObject]: GenericObject[Prop] extends GenericValue ? Prop : never;
3
+ }[keyof GenericObject];
@@ -4,3 +4,5 @@ export * from "./requiredKeys";
4
4
  export * from "./UnionObjectToIntersection";
5
5
  export * from "./getPropsWithValue";
6
6
  export * from "./flatObject";
7
+ export * from "./getPropsWithValueExtends";
8
+ export * from "./unFlatObject";
@@ -0,0 +1,21 @@
1
+ import { type IsEqual, type Adaptor, type LastUnionElement, type UnionToIntersection, type SimplifyTopLevel, type IsExtends } from "../../common";
2
+ type GroupPath<GenericPath extends string> = IsEqual<GenericPath, never> extends true ? never : LastUnionElement<GenericPath> extends infer InferredLast ? InferredLast extends `${infer InferredFirst}.${string}` ? InferredFirst extends `[${number}]` ? Extract<GenericPath, `[${number}].${string}`> extends infer InferredResult ? ["[tuple]", InferredResult] | GroupPath<Exclude<GenericPath, InferredResult>> : never : Extract<GenericPath, `${InferredFirst}.${string}`> extends infer InferredResult ? [InferredFirst, InferredResult] | GroupPath<Exclude<GenericPath, InferredResult>> : never : InferredLast extends `[${number}]` ? ["[tuple]", Extract<GenericPath, `[${number}]`>] | GroupPath<Exclude<GenericPath, `[${number}]`>> : [InferredLast, InferredLast] | GroupPath<Exclude<GenericPath, InferredLast>> : never;
3
+ type RemoveFirstFromKey<GenericObject extends object, GenericFirst extends string> = {
4
+ [Prop in keyof GenericObject as Prop extends `${GenericFirst}.${infer InferredRst}` ? InferredRst : never]: GenericObject[Prop];
5
+ };
6
+ type UnFlatTuple<GenericObject extends object, GenericGroupPath extends [string, string], GenericLastTuple extends unknown[] = []> = Extract<GenericGroupPath[1], `[${GenericLastTuple["length"]}]${string}`> extends infer InferredElementPath ? IsEqual<InferredElementPath, never> extends true ? GenericLastTuple : (IsEqual<InferredElementPath, `[${GenericLastTuple["length"]}]`> extends true ? [
7
+ ...GenericLastTuple,
8
+ GenericObject[Adaptor<InferredElementPath, keyof GenericObject>]
9
+ ] : [
10
+ ...GenericLastTuple,
11
+ UnFlatObject<RemoveFirstFromKey<Pick<GenericObject, Adaptor<InferredElementPath, keyof GenericObject>>, `[${GenericLastTuple["length"]}]`>>
12
+ ]) extends infer InferredResult extends any[] ? UnFlatTuple<GenericObject, GenericGroupPath, InferredResult> : never : never;
13
+ export type UnFlatObject<GenericObject extends object> = GroupPath<Adaptor<keyof GenericObject, string>> extends infer InferredGroupedPath ? (InferredGroupedPath extends [
14
+ infer InferredFirst extends string,
15
+ infer InferredPath extends string
16
+ ] ? IsExtends<InferredFirst, "[tuple]"> extends true ? UnFlatTuple<GenericObject, InferredGroupedPath> : IsEqual<InferredFirst, "[number]"> extends true ? IsEqual<InferredFirst, InferredPath> extends true ? GenericObject[Adaptor<InferredFirst, keyof GenericObject>][] : UnFlatObject<RemoveFirstFromKey<Pick<GenericObject, Adaptor<InferredPath, keyof GenericObject>>, InferredFirst>>[] : IsEqual<InferredFirst, InferredPath> extends true ? {
17
+ [Prop in InferredFirst]: GenericObject[Adaptor<Prop, keyof GenericObject>];
18
+ } : {
19
+ [Prop in InferredFirst]: UnFlatObject<RemoveFirstFromKey<Pick<GenericObject, Adaptor<InferredPath, keyof GenericObject>>, InferredFirst>>;
20
+ } : never) extends infer InferredResult ? SimplifyTopLevel<UnionToIntersection<InferredResult>> : never : never;
21
+ export {};
@@ -3,11 +3,11 @@
3
3
  var result = require('./result.cjs');
4
4
  var exhaustive = require('./exhaustive.cjs');
5
5
  var otherwise = require('./otherwise.cjs');
6
- var matchPrimitive = require('./matchPrimitive.cjs');
7
6
  var match = require('./match.cjs');
8
7
  var isMatch = require('./isMatch.cjs');
9
8
  var pattern = require('./types/pattern.cjs');
10
9
  var union = require('./union.cjs');
10
+ var when = require('./when.cjs');
11
11
 
12
12
 
13
13
 
@@ -15,8 +15,8 @@ exports.isResult = result.isResult;
15
15
  exports.result = result.result;
16
16
  exports.exhaustive = exhaustive.exhaustive;
17
17
  exports.otherwise = otherwise.otherwise;
18
- exports.matchPrimitive = matchPrimitive.matchPrimitive;
19
18
  exports.match = match.match;
20
19
  exports.isMatch = isMatch.isMatch;
21
20
  exports.SymbolToolPatternFunctionLabel = pattern.SymbolToolPatternFunctionLabel;
22
21
  exports.union = union.union;
22
+ exports.when = when.when;
@@ -1,8 +1,8 @@
1
1
  export * from "./result";
2
2
  export * from "./exhaustive";
3
3
  export * from "./otherwise";
4
- export * from "./matchPrimitive";
5
4
  export * from "./match";
6
5
  export * from "./isMatch";
7
6
  export * from "./types";
8
7
  export * from "./union";
8
+ export * from "./when";
@@ -1,8 +1,8 @@
1
1
  export { isResult, result } from './result.mjs';
2
2
  export { exhaustive } from './exhaustive.mjs';
3
3
  export { otherwise } from './otherwise.mjs';
4
- export { matchPrimitive } from './matchPrimitive.mjs';
5
4
  export { match } from './match.mjs';
6
5
  export { isMatch } from './isMatch.mjs';
7
6
  export { SymbolToolPatternFunctionLabel } from './types/pattern.mjs';
8
7
  export { union } from './union.mjs';
8
+ export { when } from './when.mjs';
@@ -3,7 +3,12 @@
3
3
  var pattern = require('./types/pattern.cjs');
4
4
 
5
5
  const SymbolToolPatternFunction = Symbol.for(pattern.SymbolToolPatternFunctionLabel);
6
- function isMatch(input, pattern) {
6
+ function isMatch(...args) {
7
+ if (args.length === 1) {
8
+ const [pattern] = args;
9
+ return (input) => isMatch(input, pattern);
10
+ }
11
+ const [input, pattern] = args;
7
12
  if (typeof pattern === "string"
8
13
  || typeof pattern === "number"
9
14
  || typeof pattern === "boolean"
@@ -1,2 +1,5 @@
1
- import { type Pattern } from "./types/pattern";
2
- export declare function isMatch(input: unknown, pattern: Pattern): boolean;
1
+ import { type ForcePredicate, type AnyValue, type FixDeepFunctionInfer } from "../common";
2
+ import { type Pattern, type PatternValue } from "./types/pattern";
3
+ import { type ComplexMatchedValue } from "./types";
4
+ export declare function isMatch<GenericInput extends AnyValue, const GenericPattern extends Pattern<GenericInput>>(pattern: FixDeepFunctionInfer<Pattern<GenericInput>, GenericPattern>): (input: GenericInput) => input is ForcePredicate<GenericInput, ComplexMatchedValue<GenericInput, PatternValue<GenericPattern>>>;
5
+ export declare function isMatch<GenericInput extends AnyValue, const GenericPattern extends Pattern<GenericInput>>(input: GenericInput, pattern: FixDeepFunctionInfer<Pattern<GenericInput>, GenericPattern>): input is ForcePredicate<GenericInput, ComplexMatchedValue<GenericInput, PatternValue<GenericPattern>>>;
@@ -1,7 +1,12 @@
1
1
  import { SymbolToolPatternFunctionLabel } from './types/pattern.mjs';
2
2
 
3
3
  const SymbolToolPatternFunction = Symbol.for(SymbolToolPatternFunctionLabel);
4
- function isMatch(input, pattern) {
4
+ function isMatch(...args) {
5
+ if (args.length === 1) {
6
+ const [pattern] = args;
7
+ return (input) => isMatch(input, pattern);
8
+ }
9
+ const [input, pattern] = args;
5
10
  if (typeof pattern === "string"
6
11
  || typeof pattern === "number"
7
12
  || typeof pattern === "boolean"
@@ -9,7 +9,7 @@ function match(...args) {
9
9
  return (input) => match(input, pattern, theFunction);
10
10
  }
11
11
  const [input, pattern, theFunction] = args;
12
- if (isMatch.isMatch(input, pattern)) {
12
+ if (!result.isResult(input) && isMatch.isMatch(input, pattern)) {
13
13
  return result.result(theFunction(input));
14
14
  }
15
15
  return input;
@@ -1,4 +1,4 @@
1
- import { result } from './result.mjs';
1
+ import { isResult, result } from './result.mjs';
2
2
  import { isMatch } from './isMatch.mjs';
3
3
 
4
4
  function match(...args) {
@@ -7,7 +7,7 @@ function match(...args) {
7
7
  return (input) => match(input, pattern, theFunction);
8
8
  }
9
9
  const [input, pattern, theFunction] = args;
10
- if (isMatch(input, pattern)) {
10
+ if (!isResult(input) && isMatch(input, pattern)) {
11
11
  return result(theFunction(input));
12
12
  }
13
13
  return input;
@@ -5,7 +5,7 @@ var wrapValue = require('../common/wrapValue.cjs');
5
5
 
6
6
  const patternResultKind = kind.createKind("pattern-result");
7
7
  function result(value) {
8
- return patternResultKind.addTo(wrapValue.wrapValue(value), null);
8
+ return patternResultKind.addTo(wrapValue.wrapValue(value));
9
9
  }
10
10
  const isResult = patternResultKind.has;
11
11
 
@@ -3,7 +3,7 @@ import { wrapValue } from '../common/wrapValue.mjs';
3
3
 
4
4
  const patternResultKind = createKind("pattern-result");
5
5
  function result(value) {
6
- return patternResultKind.addTo(wrapValue(value), null);
6
+ return patternResultKind.addTo(wrapValue(value));
7
7
  }
8
8
  const isResult = patternResultKind.has;
9
9
 
@@ -1,5 +1,6 @@
1
1
  import { type Adaptor, type SimplifyTopLevel, type IsEqual } from "../../../common";
2
2
  import { type ComplexMatchedValue } from ".";
3
+ import { type GetPropsWithValue } from "../../../object";
3
4
  export type ComplexMatchedObject<GenericInput extends unknown, GenericPatternValue extends unknown> = ([
4
5
  Exclude<Extract<GenericInput, object>, readonly any[]>,
5
6
  Exclude<Extract<GenericPatternValue, object>, readonly any[]>
@@ -8,4 +9,4 @@ export type ComplexMatchedObject<GenericInput extends unknown, GenericPatternVal
8
9
  infer InferredPatternValue
9
10
  ] ? InferredInput extends any ? InferredPatternValue extends any ? Extract<InferredInput, InferredPatternValue> extends infer InferredObviousMatchedValue ? IsEqual<InferredObviousMatchedValue, never> extends false ? InferredObviousMatchedValue : IsEqual<Extract<keyof InferredInput, keyof InferredPatternValue>, keyof InferredPatternValue> extends false ? never : SimplifyTopLevel<Omit<InferredInput, keyof InferredPatternValue> & {
10
11
  -readonly [Prop in keyof InferredPatternValue]: Extract<ComplexMatchedValue<InferredInput[Adaptor<Prop, keyof InferredInput>], InferredPatternValue[Prop]>, any>;
11
- }> extends infer InferredResult ? Extract<InferredResult, any> extends InferredInput ? IsEqual<InferredResult, InferredInput> extends true ? InferredInput : InferredResult : never : never : never : never : never : never);
12
+ }> extends infer InferredResult extends object ? Extract<InferredResult, any> extends InferredInput ? IsEqual<InferredResult, InferredInput> extends true ? InferredInput : IsEqual<GetPropsWithValue<Pick<InferredResult, Adaptor<keyof InferredPatternValue, keyof InferredResult>>, never>, never> extends true ? InferredResult : never : never : never : never : never : never : never);
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ var result = require('./result.cjs');
4
+
5
+ function when(...args) {
6
+ if (args.length === 2) {
7
+ const [predicate, theFunction] = args;
8
+ return (input) => when(input, predicate, theFunction);
9
+ }
10
+ const [input, predicate, theFunction] = args;
11
+ if (!result.isResult(input) && predicate(input)) {
12
+ return result.result(theFunction(input));
13
+ }
14
+ return input;
15
+ }
16
+
17
+ exports.when = when;
@@ -0,0 +1,6 @@
1
+ import { type AnyValue } from "../common";
2
+ import { type PatternResult } from "./result";
3
+ export declare function when<GenericInput extends AnyValue, GenericInputValue extends Exclude<GenericInput, PatternResult>, GenericInputPatternResult extends Extract<GenericInput, PatternResult>, GenericPredicatedInput extends GenericInputValue, GenericOutput extends AnyValue>(predicate: (input: GenericInputValue) => input is GenericPredicatedInput, theFunction: (predicatedInput: GenericPredicatedInput) => GenericOutput): (input: (GenericInput | GenericInputPatternResult | GenericInputValue)) => (GenericInputPatternResult | Exclude<GenericInputValue, GenericPredicatedInput> | PatternResult<GenericOutput>);
4
+ export declare function when<GenericInput extends AnyValue, GenericInputValue extends Exclude<GenericInput, PatternResult>, GenericInputPatternResult extends Extract<GenericInput, PatternResult>, GenericOutput extends AnyValue>(predicate: (input: GenericInputValue) => boolean, theFunction: (predicatedInput: GenericInputValue) => GenericOutput): (input: (GenericInput | GenericInputPatternResult | GenericInputValue)) => (GenericInputPatternResult | GenericInputValue | PatternResult<GenericOutput>);
5
+ export declare function when<GenericInput extends AnyValue, GenericInputValue extends Exclude<GenericInput, PatternResult>, GenericInputPatternResult extends Extract<GenericInput, PatternResult>, GenericPredicatedInput extends GenericInputValue, GenericOutput extends AnyValue>(input: (GenericInput | GenericInputPatternResult | GenericInputValue), predicate: (input: GenericInputValue) => input is GenericPredicatedInput, theFunction: (predicatedInput: GenericPredicatedInput) => GenericOutput): (GenericInputPatternResult | Exclude<GenericInputValue, GenericPredicatedInput> | PatternResult<GenericOutput>);
6
+ export declare function when<GenericInput extends AnyValue, GenericInputValue extends Exclude<GenericInput, PatternResult>, GenericInputPatternResult extends Extract<GenericInput, PatternResult>, GenericOutput extends AnyValue>(input: (GenericInput | GenericInputPatternResult | GenericInputValue), predicate: (input: GenericInputValue) => boolean, theFunction: (predicatedInput: GenericInputValue) => GenericOutput): (GenericInputPatternResult | GenericInputValue | PatternResult<GenericOutput>);
@@ -0,0 +1,15 @@
1
+ import { isResult, result } from './result.mjs';
2
+
3
+ function when(...args) {
4
+ if (args.length === 2) {
5
+ const [predicate, theFunction] = args;
6
+ return (input) => when(input, predicate, theFunction);
7
+ }
8
+ const [input, predicate, theFunction] = args;
9
+ if (!isResult(input) && predicate(input)) {
10
+ return result(theFunction(input));
11
+ }
12
+ return input;
13
+ }
14
+
15
+ export { when };
@@ -1,2 +1,5 @@
1
- export declare function endsWith<GenericSearchString extends string, GenericString extends string>(searchString: GenericSearchString): (input: GenericString) => input is Extract<GenericString, `${string}${GenericSearchString}`>;
2
- export declare function endsWith<GenericString extends string, GenericSearchString extends string>(input: GenericString, searchString: GenericSearchString): input is Extract<GenericString, `${string}${GenericSearchString}`>;
1
+ import { type IsEqual } from "../common";
2
+ type ComputeResult<GenericString extends string, GenericSearchString extends string> = Extract<GenericString, `${string}${GenericSearchString}`> extends infer InferredResult extends GenericString ? IsEqual<InferredResult, never> extends true ? GenericString & `${string}${GenericSearchString}` : InferredResult : never;
3
+ export declare function endsWith<GenericString extends string, GenericSearchString extends string>(searchString: GenericSearchString): (input: GenericString) => input is ComputeResult<GenericString, GenericSearchString>;
4
+ export declare function endsWith<GenericString extends string, GenericSearchString extends string>(input: GenericString, searchString: GenericSearchString): input is ComputeResult<GenericString, GenericSearchString>;
5
+ export {};
@@ -1,2 +1,5 @@
1
- export declare function includes<GenericSearchString extends string, GenericString extends string>(searchString: GenericSearchString): (input: GenericString) => input is Extract<GenericString, `${string}${GenericSearchString}${string}`>;
2
- export declare function includes<GenericString extends string, GenericSearchString extends string>(input: GenericString, searchString: GenericSearchString): input is Extract<GenericString, `${string}${GenericSearchString}${string}`>;
1
+ import { type IsEqual } from "../common";
2
+ type ComputeResult<GenericString extends string, GenericSearchString extends string> = Extract<GenericString, `${string}${GenericSearchString}${string}`> extends infer InferredResult extends GenericString ? IsEqual<InferredResult, never> extends true ? GenericString & `${string}${GenericSearchString}${string}` : InferredResult : never;
3
+ export declare function includes<GenericString extends string, GenericSearchString extends string>(searchString: GenericSearchString): (input: GenericString) => input is ComputeResult<GenericString, GenericSearchString>;
4
+ export declare function includes<GenericString extends string, GenericSearchString extends string>(input: GenericString, searchString: GenericSearchString): input is ComputeResult<GenericString, GenericSearchString>;
5
+ export {};
@@ -31,6 +31,7 @@ var lastIndexOf = require('./lastIndexOf.cjs');
31
31
  var repeat = require('./repeat.cjs');
32
32
  var normalize = require('./normalize.cjs');
33
33
  var test = require('./test.cjs');
34
+ var isIn = require('./isIn.cjs');
34
35
 
35
36
 
36
37
 
@@ -65,3 +66,4 @@ exports.lastIndexOf = lastIndexOf.lastIndexOf;
65
66
  exports.repeat = repeat.repeat;
66
67
  exports.normalize = normalize.normalize;
67
68
  exports.test = test.test;
69
+ exports.isIn = isIn.isIn;
@@ -26,3 +26,4 @@ export * from "./lastIndexOf";
26
26
  export * from "./repeat";
27
27
  export * from "./normalize";
28
28
  export * from "./test";
29
+ export * from "./isIn";
@@ -29,3 +29,4 @@ export { lastIndexOf } from './lastIndexOf.mjs';
29
29
  export { repeat } from './repeat.mjs';
30
30
  export { normalize } from './normalize.mjs';
31
31
  export { test } from './test.mjs';
32
+ export { isIn } from './isIn.mjs';
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ function isIn(...args) {
4
+ if (args.length === 1) {
5
+ const [array] = args;
6
+ return (input) => isIn(input, array);
7
+ }
8
+ const [input, array] = args;
9
+ return array.includes(input);
10
+ }
11
+
12
+ exports.isIn = isIn;
@@ -0,0 +1,2 @@
1
+ export declare function isIn<GenericInput extends string, GenericValue extends GenericInput>(array: readonly GenericValue[]): (input: GenericInput) => input is Extract<GenericInput, GenericValue>;
2
+ export declare function isIn<GenericInput extends string, GenericValue extends GenericInput>(input: GenericInput, array: readonly GenericValue[]): input is Extract<GenericInput, GenericValue>;
@@ -0,0 +1,10 @@
1
+ function isIn(...args) {
2
+ if (args.length === 1) {
3
+ const [array] = args;
4
+ return (input) => isIn(input, array);
5
+ }
6
+ const [input, array] = args;
7
+ return array.includes(input);
8
+ }
9
+
10
+ export { isIn };
@@ -1,2 +1,5 @@
1
- export declare function startsWith<GenericSearchString extends string, GenericString extends string>(searchString: GenericSearchString): (input: GenericString) => input is Extract<GenericString, `${GenericSearchString}${string}`>;
2
- export declare function startsWith<GenericString extends string, GenericSearchString extends string>(input: GenericString, searchString: GenericSearchString): input is Extract<GenericString, `${GenericSearchString}${string}`>;
1
+ import { type IsEqual } from "../common";
2
+ type ComputeResult<GenericString extends string, GenericSearchString extends string> = Extract<GenericString, `${GenericSearchString}${string}`> extends infer InferredResult extends GenericString ? IsEqual<InferredResult, never> extends true ? GenericString & `${GenericSearchString}${string}` : InferredResult : never;
3
+ export declare function startsWith<GenericString extends string, GenericSearchString extends string>(searchString: GenericSearchString): (input: GenericString) => input is ComputeResult<GenericString, GenericSearchString>;
4
+ export declare function startsWith<GenericString extends string, GenericSearchString extends string>(input: GenericString, searchString: GenericSearchString): input is ComputeResult<GenericString, GenericSearchString>;
5
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duplojs/utils",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "author": "mathcovax",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,17 +0,0 @@
1
- 'use strict';
2
-
3
- var result = require('./result.cjs');
4
-
5
- function matchPrimitive(primitive, theFunction) {
6
- const formattedPrimitive = primitive instanceof Array
7
- ? primitive
8
- : [primitive];
9
- return (input) => {
10
- if (!result.isResult(input) && formattedPrimitive.includes(input)) {
11
- return result.result(theFunction(input));
12
- }
13
- return input;
14
- };
15
- }
16
-
17
- exports.matchPrimitive = matchPrimitive;
@@ -1,3 +0,0 @@
1
- import { type AnyValue } from "..";
2
- import { type PatternResult } from "./result";
3
- export declare function matchPrimitive<GenericInput extends AnyValue, GenericMatch extends Exclude<GenericInput, PatternResult>, GenericOutput extends AnyValue>(primitive: GenericMatch | GenericMatch[], theFunction: (predicatedInput: Extract<GenericInput, GenericMatch>) => GenericOutput): (input: GenericInput) => PatternResult<GenericOutput> | Exclude<GenericInput, GenericMatch>;
@@ -1,15 +0,0 @@
1
- import { isResult, result } from './result.mjs';
2
-
3
- function matchPrimitive(primitive, theFunction) {
4
- const formattedPrimitive = primitive instanceof Array
5
- ? primitive
6
- : [primitive];
7
- return (input) => {
8
- if (!isResult(input) && formattedPrimitive.includes(input)) {
9
- return result(theFunction(input));
10
- }
11
- return input;
12
- };
13
- }
14
-
15
- export { matchPrimitive };