@drunkcod/argis 0.0.2 → 0.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.
package/lib/index.d.ts CHANGED
@@ -2,24 +2,30 @@ type NoNullMembers<T> = {
2
2
  [P in keyof T]: NonNullable<T[P]>;
3
3
  };
4
4
  export type WithRequired<T, K extends keyof T> = Required<NoNullMembers<Pick<T, K>>> & Omit<T, K>;
5
- export type WithKey<K extends string | symbol, V = unknown> = {
5
+ export type WithKey<K extends PropertyKey, V = unknown> = {
6
6
  [p in K]: V;
7
7
  };
8
8
  export declare class ArgumentError extends Error {
9
9
  constructor(message: string);
10
10
  static null(message: string): void;
11
11
  static missing({ key }: {
12
- key: string;
12
+ key: PropertyKey;
13
13
  }): void;
14
14
  }
15
15
  export declare function isNil(x: any): x is null | undefined;
16
+ export declare function isNil<T extends object>(x: T | null, key: keyof T): x is T & WithKey<typeof key, null | undefined>;
16
17
  export declare function isNotNil<T>(x?: T): x is T;
17
18
  export declare function isNotNil<T, K extends keyof T>(x: T, key: K): x is T & {
18
19
  [P in K]-?: NonNullable<T[K]>;
19
20
  };
21
+ export declare function assertNotNil<T>(x: T | null): asserts x is T;
20
22
  export declare function argNotNil<T, K extends keyof T>(x: T, key: K): asserts x is T & {
21
23
  [P in K]-?: NonNullable<T[K]>;
22
24
  };
23
- export declare function hasOwn<T extends object, K extends string>(x: T, key: K): x is T & WithKey<K>;
24
- export declare function assertOwn<T extends object, K extends string>(x: T, key: K): asserts x is T & WithKey<K>;
25
+ export declare function hasOwn<T extends object, K extends PropertyKey>(x: T, key: K): x is T & WithKey<K>;
26
+ export declare function assertOwn<T extends object, K extends PropertyKey>(x: T, key: K): asserts x is T & WithKey<K>;
27
+ export declare function nullIfEmpty<T extends {
28
+ length: number;
29
+ }>(x: T | null): T | null;
30
+ export declare function intOrUndefined(x?: string | null): number | undefined;
25
31
  export {};
package/lib/index.js CHANGED
@@ -3,9 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ArgumentError = void 0;
4
4
  exports.isNil = isNil;
5
5
  exports.isNotNil = isNotNil;
6
+ exports.assertNotNil = assertNotNil;
6
7
  exports.argNotNil = argNotNil;
7
8
  exports.hasOwn = hasOwn;
8
9
  exports.assertOwn = assertOwn;
10
+ exports.nullIfEmpty = nullIfEmpty;
11
+ exports.intOrUndefined = intOrUndefined;
9
12
  class ArgumentError extends Error {
10
13
  constructor(message) {
11
14
  super(message);
@@ -16,18 +19,22 @@ class ArgumentError extends Error {
16
19
  throw e;
17
20
  }
18
21
  static missing({ key }) {
19
- const e = new ArgumentError(`Missing property ${key}.`);
22
+ const e = new ArgumentError(`Missing property ${String(key)}.`);
20
23
  e.name = 'MissingPropertyError';
21
24
  throw e;
22
25
  }
23
26
  }
24
27
  exports.ArgumentError = ArgumentError;
25
- function isNil(x) {
26
- return x == null;
28
+ function isNil(x, key) {
29
+ return x == null || (key !== undefined && x[key] == null);
27
30
  }
28
31
  function isNotNil(x, key) {
29
32
  return !isNil(x) && (key === undefined || !isNil(x[key]));
30
33
  }
34
+ function assertNotNil(x) {
35
+ if (x == null)
36
+ ArgumentError.null('Unexpected null');
37
+ }
31
38
  function argNotNil(x, key) {
32
39
  isNotNil(x, key) || ArgumentError.null(`'${String(key)}' was null or undefined`);
33
40
  }
@@ -37,3 +44,12 @@ function hasOwn(x, key) {
37
44
  function assertOwn(x, key) {
38
45
  hasOwn(x, key) || ArgumentError.missing({ key });
39
46
  }
47
+ function nullIfEmpty(x) {
48
+ return x == null || x.length == 0 ? null : x;
49
+ }
50
+ function intOrUndefined(x) {
51
+ if (isNil(x))
52
+ return undefined;
53
+ const v = Number.parseInt(x);
54
+ return Number.isNaN(v) ? undefined : v;
55
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drunkcod/argis",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Tiny method arg checking with strong types.",
5
5
  "scripts": {
6
6
  "build": "rimraf lib && tsc",
@@ -8,8 +8,8 @@
8
8
  },
9
9
  "author": "Tobbe Gyllebring",
10
10
  "license": "MIT",
11
- "main": "./lib/index.js",
12
- "types": "./lib/index.d.ts",
11
+ "main": "lib/index.js",
12
+ "types": "lib/index.d.ts",
13
13
  "files": [
14
14
  "lib/**/*",
15
15
  "!lib/**/*.spec.*"