@drunkcod/argis 0.0.1 → 0.0.3
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/LICENSE +21 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +23 -3
- package/package.json +5 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Tobbe Gyllebring
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/index.d.ts
CHANGED
|
@@ -2,14 +2,26 @@ 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 PropertyKey, V = unknown> = {
|
|
6
|
+
[p in K]: V;
|
|
7
|
+
};
|
|
5
8
|
export declare class ArgumentError extends Error {
|
|
6
9
|
constructor(message: string);
|
|
7
10
|
static null(message: string): void;
|
|
11
|
+
static missing({ key }: {
|
|
12
|
+
key: PropertyKey;
|
|
13
|
+
}): void;
|
|
8
14
|
}
|
|
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>;
|
|
17
|
+
export declare function isNotNil<T>(x?: T): x is T;
|
|
9
18
|
export declare function isNotNil<T, K extends keyof T>(x: T, key: K): x is T & {
|
|
10
19
|
[P in K]-?: NonNullable<T[K]>;
|
|
11
20
|
};
|
|
21
|
+
export declare function assertNotNil<T>(x: T | null): asserts x is T;
|
|
12
22
|
export declare function argNotNil<T, K extends keyof T>(x: T, key: K): asserts x is T & {
|
|
13
23
|
[P in K]-?: NonNullable<T[K]>;
|
|
14
24
|
};
|
|
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>;
|
|
15
27
|
export {};
|
package/lib/index.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ArgumentError = void 0;
|
|
4
|
+
exports.isNil = isNil;
|
|
4
5
|
exports.isNotNil = isNotNil;
|
|
6
|
+
exports.assertNotNil = assertNotNil;
|
|
5
7
|
exports.argNotNil = argNotNil;
|
|
8
|
+
exports.hasOwn = hasOwn;
|
|
9
|
+
exports.assertOwn = assertOwn;
|
|
6
10
|
class ArgumentError extends Error {
|
|
7
11
|
constructor(message) {
|
|
8
12
|
super(message);
|
|
@@ -12,13 +16,29 @@ class ArgumentError extends Error {
|
|
|
12
16
|
e.name = 'ArgumentNullError';
|
|
13
17
|
throw e;
|
|
14
18
|
}
|
|
19
|
+
static missing({ key }) {
|
|
20
|
+
const e = new ArgumentError(`Missing property ${String(key)}.`);
|
|
21
|
+
e.name = 'MissingPropertyError';
|
|
22
|
+
throw e;
|
|
23
|
+
}
|
|
15
24
|
}
|
|
16
25
|
exports.ArgumentError = ArgumentError;
|
|
26
|
+
function isNil(x, key) {
|
|
27
|
+
return x == null || (key !== undefined && x[key] == null);
|
|
28
|
+
}
|
|
17
29
|
function isNotNil(x, key) {
|
|
18
|
-
|
|
19
|
-
|
|
30
|
+
return !isNil(x) && (key === undefined || !isNil(x[key]));
|
|
31
|
+
}
|
|
32
|
+
function assertNotNil(x) {
|
|
33
|
+
if (x == null)
|
|
34
|
+
ArgumentError.null('Unexpected null');
|
|
20
35
|
}
|
|
21
|
-
;
|
|
22
36
|
function argNotNil(x, key) {
|
|
23
37
|
isNotNil(x, key) || ArgumentError.null(`'${String(key)}' was null or undefined`);
|
|
24
38
|
}
|
|
39
|
+
function hasOwn(x, key) {
|
|
40
|
+
return Object.hasOwn(x, key);
|
|
41
|
+
}
|
|
42
|
+
function assertOwn(x, key) {
|
|
43
|
+
hasOwn(x, key) || ArgumentError.missing({ key });
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drunkcod/argis",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Tiny method arg checking with strong types.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "rimraf lib && tsc",
|
|
@@ -8,10 +8,11 @@
|
|
|
8
8
|
},
|
|
9
9
|
"author": "Tobbe Gyllebring",
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"main": "
|
|
12
|
-
"types": "
|
|
11
|
+
"main": "lib/index.js",
|
|
12
|
+
"types": "lib/index.d.ts",
|
|
13
13
|
"files": [
|
|
14
|
-
"lib/**/*"
|
|
14
|
+
"lib/**/*",
|
|
15
|
+
"!lib/**/*.spec.*"
|
|
15
16
|
],
|
|
16
17
|
"devDependencies": {
|
|
17
18
|
"@jest/globals": "^29.7.0",
|