@drunkcod/argis 0.0.10 → 0.0.12

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.
@@ -28,14 +28,15 @@ export declare function isNotNil<T>(x?: T): x is T;
28
28
  export declare function isNotNil<T, K extends keyof T>(x: T, key: K): x is T & {
29
29
  [P in K]-?: NonNullable<T[K]>;
30
30
  };
31
+ export declare function isThenable(x: unknown): boolean;
31
32
  export declare function assertNotNil<T>(x: T | null): asserts x is T;
32
33
  export declare function argNotNil<T, K extends keyof T>(x: T, key: K): asserts x is T & {
33
34
  [P in K]-?: NonNullable<T[K]>;
34
35
  };
35
- export declare function hasOwn<T extends object, K extends PropertyKey>(x: T, key: K): x is T & WithKey<K>;
36
- export declare function hasOwn<T extends object, K extends PropertyKey, V>(x: T, key: K, ofType: (found: unknown) => found is V): x is T & WithKey<K, V>;
37
- export declare function hasKey<T extends object, K extends PropertyKey>(x: T, key: K): x is T & WithKey<K>;
38
- export declare function hasKey<T extends object, K extends PropertyKey, V>(x: T, key: K, ofType: (found: unknown) => found is V): x is T & WithKey<K, V>;
36
+ export declare function hasOwn<T extends object, K extends PropertyKey>(x: T | unknown, key: K): x is T & WithKey<K>;
37
+ export declare function hasOwn<T extends object, K extends PropertyKey, V>(x: T | unknown, key: K, ofType: (found: unknown) => found is V): x is T & WithKey<K, V>;
38
+ export declare function hasKey<T extends object, K extends PropertyKey>(x: T | unknown, key: K): x is T & WithKey<K>;
39
+ export declare function hasKey<T extends object, K extends PropertyKey, V>(x: T | unknown, key: K, ofType: (found: unknown) => found is V): x is T & WithKey<K, V>;
39
40
  export declare function assertOwn<T extends object, K extends PropertyKey>(x: T, key: K): asserts x is T & WithKey<K>;
40
41
  export declare function assertOwn<T extends object, K extends PropertyKey, V>(x: T, key: K, ofType: (found: unknown) => found is V): asserts x is T & WithKey<K, V>;
41
42
  export declare function assertKey<T extends object, K extends PropertyKey>(x: T, key: K): asserts x is T & WithKey<K>;
package/lib/cjs/index.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ArgumentError = void 0;
4
4
  exports.isNil = isNil;
5
5
  exports.isNotNil = isNotNil;
6
+ exports.isThenable = isThenable;
6
7
  exports.assertNotNil = assertNotNil;
7
8
  exports.argNotNil = argNotNil;
8
9
  exports.hasOwn = hasOwn;
@@ -35,6 +36,9 @@ function isNil(x, key) {
35
36
  function isNotNil(x, key) {
36
37
  return !isNil(x) && (key === undefined || !isNil(x[key]));
37
38
  }
39
+ function isThenable(x) {
40
+ return x != null && typeof x === 'object' && 'then' in x && typeof x.then === 'function';
41
+ }
38
42
  function assertNotNil(x) {
39
43
  if (x == null)
40
44
  ArgumentError.null('Unexpected null');
@@ -49,7 +53,7 @@ function hasOwn(x, key, ofType) {
49
53
  return _hasOwn(x, key) && (ofType == undefined || ofType(x[key]));
50
54
  }
51
55
  function _hasKey(x, key) {
52
- return key in x;
56
+ return typeof x === 'object' && key in x;
53
57
  }
54
58
  function hasKey(x, key, ofType) {
55
59
  return _hasKey(x, key) && (ofType == undefined || ofType(x[key]));
@@ -69,18 +73,14 @@ function intOrUndefined(x) {
69
73
  const v = Number.parseInt(x);
70
74
  return Number.isNaN(v) ? undefined : v;
71
75
  }
72
- function filterEntries(x, p) {
73
- if (x == null)
74
- return null;
75
- if (typeof x !== 'object')
76
- return x;
77
- return Object.fromEntries(Object.entries(x).filter(p));
76
+ function* excludeEntries(x, p) {
77
+ for (const k in x)
78
+ if (!p(k))
79
+ yield [k, Reflect.get(x, k)];
78
80
  }
79
81
  function omit(x, ...keys) {
80
- const ks = keys;
81
- return filterEntries(x, ([key]) => !ks.includes(key));
82
+ return Object.fromEntries(excludeEntries(x, Array.prototype.includes.bind(keys)));
82
83
  }
83
84
  function pick(x, ...keys) {
84
- const ks = keys;
85
- return filterEntries(x, ([key]) => ks.includes(key));
85
+ return Object.fromEntries(keys.map((k) => [keys, x[k]]));
86
86
  }
package/lib/index.d.ts CHANGED
@@ -28,14 +28,15 @@ export declare function isNotNil<T>(x?: T): x is T;
28
28
  export declare function isNotNil<T, K extends keyof T>(x: T, key: K): x is T & {
29
29
  [P in K]-?: NonNullable<T[K]>;
30
30
  };
31
+ export declare function isThenable(x: unknown): boolean;
31
32
  export declare function assertNotNil<T>(x: T | null): asserts x is T;
32
33
  export declare function argNotNil<T, K extends keyof T>(x: T, key: K): asserts x is T & {
33
34
  [P in K]-?: NonNullable<T[K]>;
34
35
  };
35
- export declare function hasOwn<T extends object, K extends PropertyKey>(x: T, key: K): x is T & WithKey<K>;
36
- export declare function hasOwn<T extends object, K extends PropertyKey, V>(x: T, key: K, ofType: (found: unknown) => found is V): x is T & WithKey<K, V>;
37
- export declare function hasKey<T extends object, K extends PropertyKey>(x: T, key: K): x is T & WithKey<K>;
38
- export declare function hasKey<T extends object, K extends PropertyKey, V>(x: T, key: K, ofType: (found: unknown) => found is V): x is T & WithKey<K, V>;
36
+ export declare function hasOwn<T extends object, K extends PropertyKey>(x: T | unknown, key: K): x is T & WithKey<K>;
37
+ export declare function hasOwn<T extends object, K extends PropertyKey, V>(x: T | unknown, key: K, ofType: (found: unknown) => found is V): x is T & WithKey<K, V>;
38
+ export declare function hasKey<T extends object, K extends PropertyKey>(x: T | unknown, key: K): x is T & WithKey<K>;
39
+ export declare function hasKey<T extends object, K extends PropertyKey, V>(x: T | unknown, key: K, ofType: (found: unknown) => found is V): x is T & WithKey<K, V>;
39
40
  export declare function assertOwn<T extends object, K extends PropertyKey>(x: T, key: K): asserts x is T & WithKey<K>;
40
41
  export declare function assertOwn<T extends object, K extends PropertyKey, V>(x: T, key: K, ofType: (found: unknown) => found is V): asserts x is T & WithKey<K, V>;
41
42
  export declare function assertKey<T extends object, K extends PropertyKey>(x: T, key: K): asserts x is T & WithKey<K>;
package/lib/index.js CHANGED
@@ -19,6 +19,9 @@ export function isNil(x, key) {
19
19
  export function isNotNil(x, key) {
20
20
  return !isNil(x) && (key === undefined || !isNil(x[key]));
21
21
  }
22
+ export function isThenable(x) {
23
+ return x != null && typeof x === 'object' && 'then' in x && typeof x.then === 'function';
24
+ }
22
25
  export function assertNotNil(x) {
23
26
  if (x == null)
24
27
  ArgumentError.null('Unexpected null');
@@ -33,7 +36,7 @@ export function hasOwn(x, key, ofType) {
33
36
  return _hasOwn(x, key) && (ofType == undefined || ofType(x[key]));
34
37
  }
35
38
  function _hasKey(x, key) {
36
- return key in x;
39
+ return typeof x === 'object' && key in x;
37
40
  }
38
41
  export function hasKey(x, key, ofType) {
39
42
  return _hasKey(x, key) && (ofType == undefined || ofType(x[key]));
@@ -53,18 +56,14 @@ export function intOrUndefined(x) {
53
56
  const v = Number.parseInt(x);
54
57
  return Number.isNaN(v) ? undefined : v;
55
58
  }
56
- function filterEntries(x, p) {
57
- if (x == null)
58
- return null;
59
- if (typeof x !== 'object')
60
- return x;
61
- return Object.fromEntries(Object.entries(x).filter(p));
59
+ function* excludeEntries(x, p) {
60
+ for (const k in x)
61
+ if (!p(k))
62
+ yield [k, Reflect.get(x, k)];
62
63
  }
63
64
  export function omit(x, ...keys) {
64
- const ks = keys;
65
- return filterEntries(x, ([key]) => !ks.includes(key));
65
+ return Object.fromEntries(excludeEntries(x, Array.prototype.includes.bind(keys)));
66
66
  }
67
67
  export function pick(x, ...keys) {
68
- const ks = keys;
69
- return filterEntries(x, ([key]) => ks.includes(key));
68
+ return Object.fromEntries(keys.map((k) => [keys, x[k]]));
70
69
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@drunkcod/argis",
3
3
  "type": "module",
4
- "version": "0.0.10",
4
+ "version": "0.0.12",
5
5
  "description": "Tiny method arg checking with strong types.",
6
6
  "scripts": {
7
7
  "clean": "rimraf lib",
@@ -9,7 +9,7 @@
9
9
  "cjs:compile": "tsc --module commonjs --outdir lib/cjs",
10
10
  "cjs:fixup": "echo '{\"type\": \"commonjs\"}' > lib/cjs/package.json",
11
11
  "build": "npm-run-all clean -p compile cjs:compile -s cjs:fixup --silent",
12
- "test": "jest"
12
+ "test": "node --experimental-vm-modules --disable-warning=ExperimentalWarning node_modules/jest/bin/jest.js"
13
13
  },
14
14
  "author": "Tobbe Gyllebring",
15
15
  "license": "MIT",
@@ -32,11 +32,11 @@
32
32
  }
33
33
  },
34
34
  "devDependencies": {
35
- "@jest/globals": "^29.7.0",
36
- "jest": "^29.7.0",
35
+ "@jest/globals": "^30.0.4",
36
+ "jest": "^30.0.4",
37
37
  "npm-run-all": "^4.1.5",
38
38
  "rimraf": "^6.0.1",
39
- "ts-jest": "^29.2.5",
40
- "typescript": "^5.7.3"
39
+ "ts-jest": "^29.4.0",
40
+ "typescript": "^5.8.3"
41
41
  }
42
42
  }