@drunkcod/argis 0.0.6 → 0.0.8

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.
@@ -0,0 +1,39 @@
1
+ type NoNullMembers<T> = {
2
+ [P in keyof T]: NonNullable<T[P]>;
3
+ };
4
+ export type WithRequired<T, K extends keyof T> = Required<NoNullMembers<Pick<T, K>>> & Omit<T, K>;
5
+ export type WithKey<K extends PropertyKey, V = unknown> = {
6
+ [p in K]: V;
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
+ };
11
+ export type OfType<K, T> = K extends T ? K : never;
12
+ export declare class ArgumentError extends Error {
13
+ constructor(message: string);
14
+ static null(message: string): never;
15
+ static missing({ key }: {
16
+ key: PropertyKey;
17
+ }): never;
18
+ }
19
+ export declare function isNil(x: any): x is null | undefined;
20
+ export declare function isNil<T extends object>(x: T | null, key: keyof T): x is T & WithKey<typeof key, null | undefined>;
21
+ export declare function isNotNil<T>(x?: T): x is T;
22
+ export declare function isNotNil<T, K extends keyof T>(x: T, key: K): x is T & {
23
+ [P in K]-?: NonNullable<T[K]>;
24
+ };
25
+ export declare function assertNotNil<T>(x: T | null): asserts x is T;
26
+ export declare function argNotNil<T, K extends keyof T>(x: T, key: K): asserts x is T & {
27
+ [P in K]-?: NonNullable<T[K]>;
28
+ };
29
+ export declare function hasOwn<T extends object, K extends PropertyKey>(x: T, key: K): x is T & WithKey<K>;
30
+ 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>;
31
+ export declare function assertOwn<T extends object, K extends PropertyKey>(x: T, key: K): asserts x is T & WithKey<K>;
32
+ 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>;
33
+ export declare function nullIfEmpty<T extends {
34
+ length: number;
35
+ }>(x: T | null): T | null;
36
+ export declare function intOrUndefined(x?: string | null): number | undefined;
37
+ export declare function omit<T extends object, K extends OfType<keyof T, string>>(x: T, ...keys: readonly K[]): Omit<T, K>;
38
+ export declare function pick<T extends object, K extends OfType<keyof T, string>>(x: T, ...keys: readonly K[]): Pick<T, K>;
39
+ export {};
@@ -0,0 +1,75 @@
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
+ exports.omit = omit;
13
+ exports.pick = pick;
14
+ class ArgumentError extends Error {
15
+ constructor(message) {
16
+ super(message);
17
+ }
18
+ static null(message) {
19
+ const e = new ArgumentError(message);
20
+ e.name = 'ArgumentNullError';
21
+ throw e;
22
+ }
23
+ static missing({ key }) {
24
+ const e = new ArgumentError(`Missing property ${String(key)}.`);
25
+ e.name = 'MissingPropertyError';
26
+ throw e;
27
+ }
28
+ }
29
+ exports.ArgumentError = ArgumentError;
30
+ function isNil(x, key) {
31
+ return x == null || (key !== undefined && x[key] == null);
32
+ }
33
+ function isNotNil(x, key) {
34
+ return !isNil(x) && (key === undefined || !isNil(x[key]));
35
+ }
36
+ function assertNotNil(x) {
37
+ if (x == null)
38
+ ArgumentError.null('Unexpected null');
39
+ }
40
+ function argNotNil(x, key) {
41
+ isNotNil(x, key) || ArgumentError.null(`'${String(key)}' was null or undefined`);
42
+ }
43
+ function _hasOwn(x, key) {
44
+ return Object.hasOwn(x, key);
45
+ }
46
+ function hasOwn(x, key, ofType) {
47
+ return _hasOwn(x, key) && (ofType == undefined || ofType(x[key]));
48
+ }
49
+ function assertOwn(x, key, ofType) {
50
+ (ofType ? hasOwn(x, key, ofType) : hasOwn(x, key)) || ArgumentError.missing({ key });
51
+ }
52
+ function nullIfEmpty(x) {
53
+ return x == null || x.length == 0 ? null : x;
54
+ }
55
+ function intOrUndefined(x) {
56
+ if (isNil(x))
57
+ return undefined;
58
+ const v = Number.parseInt(x);
59
+ return Number.isNaN(v) ? undefined : v;
60
+ }
61
+ function filterEntries(x, p) {
62
+ if (x == null)
63
+ return null;
64
+ if (typeof x !== 'object')
65
+ return x;
66
+ return Object.fromEntries(Object.entries(x).filter(p));
67
+ }
68
+ function omit(x, ...keys) {
69
+ const ks = keys;
70
+ return filterEntries(x, ([key]) => !ks.includes(key));
71
+ }
72
+ function pick(x, ...keys) {
73
+ const ks = keys;
74
+ return filterEntries(x, ([key]) => ks.includes(key));
75
+ }
@@ -0,0 +1 @@
1
+ {"type": "commonjs"}
package/lib/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export type WithKey<K extends PropertyKey, V = unknown> = {
8
8
  export type OmitIndex<T> = {
9
9
  [K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K];
10
10
  };
11
+ export type OfType<K, T> = K extends T ? K : never;
11
12
  export declare class ArgumentError extends Error {
12
13
  constructor(message: string);
13
14
  static null(message: string): never;
@@ -33,4 +34,6 @@ export declare function nullIfEmpty<T extends {
33
34
  length: number;
34
35
  }>(x: T | null): T | null;
35
36
  export declare function intOrUndefined(x?: string | null): number | undefined;
37
+ export declare function omit<T extends object, K extends OfType<keyof T, string>>(x: T, ...keys: readonly K[]): Omit<T, K>;
38
+ export declare function pick<T extends object, K extends OfType<keyof T, string>>(x: T, ...keys: readonly K[]): Pick<T, K>;
36
39
  export {};
package/lib/index.js CHANGED
@@ -44,3 +44,18 @@ export function intOrUndefined(x) {
44
44
  const v = Number.parseInt(x);
45
45
  return Number.isNaN(v) ? undefined : v;
46
46
  }
47
+ function filterEntries(x, p) {
48
+ if (x == null)
49
+ return null;
50
+ if (typeof x !== 'object')
51
+ return x;
52
+ return Object.fromEntries(Object.entries(x).filter(p));
53
+ }
54
+ export function omit(x, ...keys) {
55
+ const ks = keys;
56
+ return filterEntries(x, ([key]) => !ks.includes(key));
57
+ }
58
+ export function pick(x, ...keys) {
59
+ const ks = keys;
60
+ return filterEntries(x, ([key]) => ks.includes(key));
61
+ }
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "@drunkcod/argis",
3
3
  "type": "module",
4
- "version": "0.0.6",
4
+ "version": "0.0.8",
5
5
  "description": "Tiny method arg checking with strong types.",
6
6
  "scripts": {
7
7
  "clean": "rimraf lib",
8
8
  "compile": "tsc",
9
- "build": "npm-run-all clean compile --silent",
9
+ "cjs:compile": "tsc --module commonjs --outdir lib/cjs",
10
+ "cjs:fixup": "echo '{\"type\": \"commonjs\"}' > lib/cjs/package.json",
11
+ "build": "npm-run-all clean -p compile cjs:compile -s cjs:fixup --silent",
10
12
  "test": "jest"
11
13
  },
12
14
  "author": "Tobbe Gyllebring",
@@ -17,12 +19,24 @@
17
19
  "lib/**/*",
18
20
  "!lib/**/*.spec.*"
19
21
  ],
22
+ "exports": {
23
+ ".": {
24
+ "import": {
25
+ "default": "./lib/index.js",
26
+ "types": "./lib/index.d.ts"
27
+ },
28
+ "require": {
29
+ "default": "./lib/cjs/index.js",
30
+ "types": "./lib/cjs/index.d.ts"
31
+ }
32
+ }
33
+ },
20
34
  "devDependencies": {
21
35
  "@jest/globals": "^29.7.0",
22
36
  "jest": "^29.7.0",
23
37
  "npm-run-all": "^4.1.5",
24
38
  "rimraf": "^6.0.1",
25
39
  "ts-jest": "^29.2.5",
26
- "typescript": "^5.5.4"
40
+ "typescript": "^5.7.2"
27
41
  }
28
42
  }