type-fns 0.9.1 → 0.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.
@@ -1,3 +1,9 @@
1
+ type AssessMethod<T extends {
2
+ [index: string | number | symbol]: string | number;
3
+ }> = (token: string | number | symbol) => token is T[keyof T] & ((token: string | number | symbol) => boolean);
4
+ type AssureMethod<T extends {
5
+ [index: string | number | symbol]: string | number;
6
+ }> = <I extends string | number | symbol>(input: I) => I & T[keyof T];
1
7
  /**
2
8
  * a generic fn that allows us to create type checks for enums. for example:
3
9
  * ```ts
@@ -11,10 +17,13 @@
11
17
  * }
12
18
  *
13
19
  * // define a type check for your enum
14
- * const isPlanet = createIsOfEnum(Planet);
20
+ * const isPlanet = createIsOfEnum.assess(Planet);
15
21
  *
16
- * // use your new type check for a type guard
17
- * if (!isPlanet(potentialPlanet)) throw new Error('is not a planet');
22
+ * // use it to assess type for type guarded blocks
23
+ * if (!isPlanet.assess(potentialPlanet)) throw new Error('is not a planet');
24
+ *
25
+ * // use it to assure type for type guarded type casts
26
+ * const planet: Planet = isPlanet.assure(potentialPlanet)
18
27
  * ```
19
28
  *
20
29
  * ref: https://stackoverflow.com/a/58278753/3068233
@@ -23,4 +32,8 @@ export declare const createIsOfEnum: <T extends {
23
32
  [index: string]: string | number;
24
33
  [index: number]: string | number;
25
34
  [index: symbol]: string | number;
26
- }>(e: T) => (token: string | number | symbol) => token is T[keyof T];
35
+ }>(e: T) => {
36
+ assess: AssessMethod<T>;
37
+ assure: AssureMethod<T>;
38
+ } & AssessMethod<T>;
39
+ export {};
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createIsOfEnum = void 0;
4
+ const UnexpectedCodePathError_1 = require("../utils/errors/UnexpectedCodePathError");
4
5
  /**
5
6
  * a generic fn that allows us to create type checks for enums. for example:
6
7
  * ```ts
@@ -14,14 +15,30 @@ exports.createIsOfEnum = void 0;
14
15
  * }
15
16
  *
16
17
  * // define a type check for your enum
17
- * const isPlanet = createIsOfEnum(Planet);
18
+ * const isPlanet = createIsOfEnum.assess(Planet);
18
19
  *
19
- * // use your new type check for a type guard
20
- * if (!isPlanet(potentialPlanet)) throw new Error('is not a planet');
20
+ * // use it to assess type for type guarded blocks
21
+ * if (!isPlanet.assess(potentialPlanet)) throw new Error('is not a planet');
22
+ *
23
+ * // use it to assure type for type guarded type casts
24
+ * const planet: Planet = isPlanet.assure(potentialPlanet)
21
25
  * ```
22
26
  *
23
27
  * ref: https://stackoverflow.com/a/58278753/3068233
24
28
  */
25
- const createIsOfEnum = (e) => (token) => Object.values(e).includes(token);
29
+ const createIsOfEnum = (e) => {
30
+ const assess = (token) => Object.values(e).includes(token);
31
+ const assure = (input) => assess(input)
32
+ ? input
33
+ : (() => {
34
+ throw new UnexpectedCodePathError_1.UnexpectedCodePathError('is not of DeclaredGoogleAdsCriterionUserInterestTaxonomy');
35
+ })();
36
+ const result = function (...input) {
37
+ return assess(...input);
38
+ };
39
+ result.assess = assess;
40
+ result.assure = assure;
41
+ return result;
42
+ };
26
43
  exports.createIsOfEnum = createIsOfEnum;
27
44
  //# sourceMappingURL=isOfEnum.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"isOfEnum.js","sourceRoot":"","sources":["../../src/checks/isOfEnum.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACI,MAAM,cAAc,GACzB,CAAmE,CAAI,EAAE,EAAE,CAC3E,CAAC,KAA+B,EAAuB,EAAE,CACvD,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAmB,CAAC,CAAC;AAHtC,QAAA,cAAc,kBAGwB"}
1
+ {"version":3,"file":"isOfEnum.js","sourceRoot":"","sources":["../../src/checks/isOfEnum.ts"],"names":[],"mappings":";;;AAAA,qFAAkF;AAUlF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACI,MAAM,cAAc,GAAG,CAG5B,CAAI,EAIc,EAAE;IACpB,MAAM,MAAM,GAAG,CAAC,KAA+B,EAAuB,EAAE,CACtE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAmB,CAAC,CAAC;IACjD,MAAM,MAAM,GAAG,CAAqC,KAAQ,EAAE,EAAE,CAC9D,MAAM,CAAC,KAAK,CAAC;QACX,CAAC,CAAC,KAAK;QACP,CAAC,CAAC,CAAC,GAAG,EAAE;YACJ,MAAM,IAAI,iDAAuB,CAC/B,0DAA0D,CAC3D,CAAC;QACJ,CAAC,CAAC,EAAE,CAAC;IACX,MAAM,MAAM,GAAG,UACb,GAAG,KAAgC;QAEnC,OAAO,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC;IAC1B,CAGC,CAAC;IACF,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AA7BW,QAAA,cAAc,kBA6BzB"}
@@ -2,18 +2,37 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const isOfEnum_1 = require("./isOfEnum");
4
4
  describe('isOfEnum', () => {
5
- it('can be used for custom enums correctly', () => {
6
- let Planet;
7
- (function (Planet) {
8
- Planet["VENUS"] = "VENUS";
9
- Planet["EARTH"] = "EARTH";
10
- Planet["MARS"] = "MARS";
11
- })(Planet || (Planet = {}));
5
+ let Planet;
6
+ (function (Planet) {
7
+ Planet["VENUS"] = "VENUS";
8
+ Planet["EARTH"] = "EARTH";
9
+ Planet["MARS"] = "MARS";
10
+ })(Planet || (Planet = {}));
11
+ it('can be used to assess correctly, default call', () => {
12
12
  const isPlanet = (0, isOfEnum_1.createIsOfEnum)(Planet);
13
13
  const earthIsPlanet = isPlanet('EARTH');
14
14
  const plutoIsPlanet = isPlanet('PLUTO');
15
15
  expect(earthIsPlanet).toEqual(true);
16
16
  expect(plutoIsPlanet).toEqual(false);
17
17
  });
18
+ it('can be used to assess correctly, named call', () => {
19
+ const isPlanet = (0, isOfEnum_1.createIsOfEnum)(Planet);
20
+ const earthIsPlanet = isPlanet.assess('EARTH');
21
+ const plutoIsPlanet = isPlanet.assess('PLUTO');
22
+ expect(earthIsPlanet).toEqual(true);
23
+ expect(plutoIsPlanet).toEqual(false);
24
+ });
25
+ it('can be used to create type guarded branches', () => {
26
+ const isPlanet = (0, isOfEnum_1.createIsOfEnum)(Planet);
27
+ const earth = 'EARTH';
28
+ if (isPlanet.assess(earth)) {
29
+ const home = earth; // no type error
30
+ }
31
+ });
32
+ it('can be used to create type asserted casts', () => {
33
+ const isPlanet = (0, isOfEnum_1.createIsOfEnum)(Planet);
34
+ const earth = 'EARTH';
35
+ const home = isPlanet.assure(earth);
36
+ });
18
37
  });
19
38
  //# sourceMappingURL=isOfEnum.test.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"isOfEnum.test.js","sourceRoot":"","sources":["../../src/checks/isOfEnum.test.ts"],"names":[],"mappings":";;AAAA,yCAA4C;AAE5C,QAAQ,CAAC,UAAU,EAAE,GAAG,EAAE;IACxB,EAAE,CAAC,wCAAwC,EAAE,GAAG,EAAE;QAChD,IAAK,MAIJ;QAJD,WAAK,MAAM;YACT,yBAAe,CAAA;YACf,yBAAe,CAAA;YACf,uBAAa,CAAA;QACf,CAAC,EAJI,MAAM,KAAN,MAAM,QAIV;QACD,MAAM,QAAQ,GAAG,IAAA,yBAAc,EAAC,MAAM,CAAC,CAAC;QACxC,MAAM,aAAa,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;QACxC,MAAM,aAAa,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;QACxC,MAAM,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACpC,MAAM,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
1
+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AC,EAAE;YAC1B,MAAM,IAAI,GAAW,KAAK,CAAC,CAAC,gBAAgB;SAC7C;IACH,CAAC,CAAC,CAAC;IACH,EAAE,CAAC,2CAA2C,EAAE,GAAG,EAAE;QACnD,MAAM,QAAQ,GAAG,IAAA,yBAAc,EAAC,MAAM,CAAC,CAAC;QACxC,MAAM,KAAK,GAAG,OAAO,CAAC;QACtB,MAAM,IAAI,GAAW,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC9C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * a companion to the `Omit` type, returns a new object without the omitted attributes
3
3
  */
4
- export declare const omit: <T extends Record<string, any>>(obj: T, keys: (keyof T)[]) => Omit<T, keyof T>;
4
+ export declare const omit: <T extends Record<string, any>, K extends readonly (keyof T)[]>(obj: T, keys: K) => Omit<T, K[number]>;
@@ -1 +1 @@
1
- {"version":3,"file":"omit.js","sourceRoot":"","sources":["../../src/companions/omit.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACI,MAAM,IAAI,GAAG,CAClB,GAAM,EACN,IAAiB,EACa,EAAE,CAChC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;IACnD,iEAAiE;IACjE,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,OAAO,OAAO,CAAC;IAEvC,6CAA6C;IAC7C,uCAAY,OAAO,KAAE,CAAC,GAAG,CAAC,EAAE,KAAK,IAAG;AACtC,CAAC,EAAE,EAAyB,CAAQ,CAAC;AAV1B,QAAA,IAAI,QAUsB"}
1
+ {"version":3,"file":"omit.js","sourceRoot":"","sources":["../../src/companions/omit.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACI,MAAM,IAAI,GAAG,CAIlB,GAAM,EACN,IAAO,EACa,EAAE,CACtB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;IACnD,iEAAiE;IACjE,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,OAAO,OAAO,CAAC;IAEvC,6CAA6C;IAC7C,uCAAY,OAAO,KAAE,CAAC,GAAG,CAAC,EAAE,KAAK,IAAG;AACtC,CAAC,EAAE,EAAyB,CAAQ,CAAC;AAb1B,QAAA,IAAI,QAasB"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const omit_1 = require("./omit");
4
+ describe('omit', () => {
5
+ it('should satisfy the standard Omit type', () => {
6
+ const withKey = { a: 1, b: 2 };
7
+ const omitKeyTypeFound = (0, omit_1.omit)(withKey, ['a']);
8
+ const omitKeyTypeCheck = omitKeyTypeFound;
9
+ });
10
+ });
11
+ //# sourceMappingURL=omit.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"omit.test.js","sourceRoot":"","sources":["../../src/companions/omit.test.ts"],"names":[],"mappings":";;AAAA,iCAA8B;AAE9B,QAAQ,CAAC,MAAM,EAAE,GAAG,EAAE;IACpB,EAAE,CAAC,uCAAuC,EAAE,GAAG,EAAE;QAC/C,MAAM,OAAO,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QAE/B,MAAM,gBAAgB,GAAG,IAAA,WAAI,EAAC,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QAC9C,MAAM,gBAAgB,GAA8B,gBAAgB,CAAC;IACvE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * a companion to the `Pick` type, returns a new object with only the picked attributes
3
3
  */
4
- export declare const pick: <T extends Record<string, any>>(obj: T, keys: (keyof T)[]) => Pick<T, keyof T>;
4
+ export declare const pick: <T extends Record<string, any>, K extends readonly (keyof T)[]>(obj: T, keys: K) => Pick<T, K[number]>;
@@ -1 +1 @@
1
- {"version":3,"file":"pick.js","sourceRoot":"","sources":["../../src/companions/pick.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACI,MAAM,IAAI,GAAG,CAClB,GAAM,EACN,IAAiB,EACa,EAAE,CAChC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;IACnD,qEAAqE;IACrE,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,OAAO,OAAO,CAAC;IAExC,yCAAyC;IACzC,uCAAY,OAAO,KAAE,CAAC,GAAG,CAAC,EAAE,KAAK,IAAG;AACtC,CAAC,EAAE,EAAyB,CAAQ,CAAC;AAV1B,QAAA,IAAI,QAUsB"}
1
+ {"version":3,"file":"pick.js","sourceRoot":"","sources":["../../src/companions/pick.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACI,MAAM,IAAI,GAAG,CAIlB,GAAM,EACN,IAAO,EACa,EAAE,CACtB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;IACnD,qEAAqE;IACrE,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,OAAO,OAAO,CAAC;IAExC,yCAAyC;IACzC,uCAAY,OAAO,KAAE,CAAC,GAAG,CAAC,EAAE,KAAK,IAAG;AACtC,CAAC,EAAE,EAAyB,CAAQ,CAAC;AAb1B,QAAA,IAAI,QAasB"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const pick_1 = require("./pick");
4
+ describe('pick', () => {
5
+ it('should satisfy the standard Pick type', () => {
6
+ const withAll = { a: 1, b: 2 };
7
+ const pickKeyTypeFound = (0, pick_1.pick)(withAll, ['a']);
8
+ const pickKeyTypeCheck = pickKeyTypeFound;
9
+ });
10
+ });
11
+ //# sourceMappingURL=pick.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pick.test.js","sourceRoot":"","sources":["../../src/companions/pick.test.ts"],"names":[],"mappings":";;AAAA,iCAA8B;AAE9B,QAAQ,CAAC,MAAM,EAAE,GAAG,EAAE;IACpB,EAAE,CAAC,uCAAuC,EAAE,GAAG,EAAE;QAC/C,MAAM,OAAO,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;QAE/B,MAAM,gBAAgB,GAAG,IAAA,WAAI,EAAC,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QAC9C,MAAM,gBAAgB,GAA8B,gBAAgB,CAAC;IACvE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "type-fns",
3
3
  "author": "ehmpathy",
4
4
  "description": "A set of types, type checks, and type guards for simpler, safer, and easier to read code.",
5
- "version": "0.9.1",
5
+ "version": "0.10.0",
6
6
  "repository": "ehmpathy/type-fns",
7
7
  "homepage": "https://github.com/ehmpathy/type-fns",
8
8
  "keywords": [