@drunkcod/argis 0.0.4 → 0.0.6

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
@@ -5,12 +5,15 @@ export type WithRequired<T, K extends keyof T> = Required<NoNullMembers<Pick<T,
5
5
  export type WithKey<K extends PropertyKey, V = unknown> = {
6
6
  [p in K]: V;
7
7
  };
8
+ export type OmitIndex<T> = {
9
+ [K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K];
10
+ };
8
11
  export declare class ArgumentError extends Error {
9
12
  constructor(message: string);
10
- static null(message: string): void;
13
+ static null(message: string): never;
11
14
  static missing({ key }: {
12
15
  key: PropertyKey;
13
- }): void;
16
+ }): never;
14
17
  }
15
18
  export declare function isNil(x: any): x is null | undefined;
16
19
  export declare function isNil<T extends object>(x: T | null, key: keyof T): x is T & WithKey<typeof key, null | undefined>;
@@ -23,7 +26,9 @@ export declare function argNotNil<T, K extends keyof T>(x: T, key: K): asserts x
23
26
  [P in K]-?: NonNullable<T[K]>;
24
27
  };
25
28
  export declare function hasOwn<T extends object, K extends PropertyKey>(x: T, key: K): x is T & WithKey<K>;
29
+ 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>;
26
30
  export declare function assertOwn<T extends object, K extends PropertyKey>(x: T, key: K): asserts x is T & WithKey<K>;
31
+ 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>;
27
32
  export declare function nullIfEmpty<T extends {
28
33
  length: number;
29
34
  }>(x: T | null): T | null;
package/lib/index.js CHANGED
@@ -1,15 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ArgumentError = void 0;
4
- exports.isNil = isNil;
5
- exports.isNotNil = isNotNil;
6
- exports.assertNotNil = assertNotNil;
7
- exports.argNotNil = argNotNil;
8
- exports.hasOwn = hasOwn;
9
- exports.assertOwn = assertOwn;
10
- exports.nullIfEmpty = nullIfEmpty;
11
- exports.intOrUndefined = intOrUndefined;
12
- class ArgumentError extends Error {
1
+ export class ArgumentError extends Error {
13
2
  constructor(message) {
14
3
  super(message);
15
4
  }
@@ -24,30 +13,32 @@ class ArgumentError extends Error {
24
13
  throw e;
25
14
  }
26
15
  }
27
- exports.ArgumentError = ArgumentError;
28
- function isNil(x, key) {
16
+ export function isNil(x, key) {
29
17
  return x == null || (key !== undefined && x[key] == null);
30
18
  }
31
- function isNotNil(x, key) {
19
+ export function isNotNil(x, key) {
32
20
  return !isNil(x) && (key === undefined || !isNil(x[key]));
33
21
  }
34
- function assertNotNil(x) {
22
+ export function assertNotNil(x) {
35
23
  if (x == null)
36
24
  ArgumentError.null('Unexpected null');
37
25
  }
38
- function argNotNil(x, key) {
26
+ export function argNotNil(x, key) {
39
27
  isNotNil(x, key) || ArgumentError.null(`'${String(key)}' was null or undefined`);
40
28
  }
41
- function hasOwn(x, key) {
29
+ function _hasOwn(x, key) {
42
30
  return Object.hasOwn(x, key);
43
31
  }
44
- function assertOwn(x, key) {
45
- hasOwn(x, key) || ArgumentError.missing({ key });
32
+ export function hasOwn(x, key, ofType) {
33
+ return _hasOwn(x, key) && (ofType == undefined || ofType(x[key]));
46
34
  }
47
- function nullIfEmpty(x) {
35
+ export function assertOwn(x, key, ofType) {
36
+ (ofType ? hasOwn(x, key, ofType) : hasOwn(x, key)) || ArgumentError.missing({ key });
37
+ }
38
+ export function nullIfEmpty(x) {
48
39
  return x == null || x.length == 0 ? null : x;
49
40
  }
50
- function intOrUndefined(x) {
41
+ export function intOrUndefined(x) {
51
42
  if (isNil(x))
52
43
  return undefined;
53
44
  const v = Number.parseInt(x);
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@drunkcod/argis",
3
- "version": "0.0.4",
3
+ "type": "module",
4
+ "version": "0.0.6",
4
5
  "description": "Tiny method arg checking with strong types.",
5
6
  "scripts": {
6
- "build": "rimraf lib && tsc",
7
+ "clean": "rimraf lib",
8
+ "compile": "tsc",
9
+ "build": "npm-run-all clean compile --silent",
7
10
  "test": "jest"
8
11
  },
9
12
  "author": "Tobbe Gyllebring",
@@ -17,6 +20,7 @@
17
20
  "devDependencies": {
18
21
  "@jest/globals": "^29.7.0",
19
22
  "jest": "^29.7.0",
23
+ "npm-run-all": "^4.1.5",
20
24
  "rimraf": "^6.0.1",
21
25
  "ts-jest": "^29.2.5",
22
26
  "typescript": "^5.5.4"