@drunkcod/argis 0.0.3 → 0.0.5
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 +4 -0
- package/lib/index.js +16 -17
- package/package.json +6 -2
package/lib/index.d.ts
CHANGED
|
@@ -24,4 +24,8 @@ export declare function argNotNil<T, K extends keyof T>(x: T, key: K): asserts x
|
|
|
24
24
|
};
|
|
25
25
|
export declare function hasOwn<T extends object, K extends PropertyKey>(x: T, key: K): x is T & WithKey<K>;
|
|
26
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;
|
|
27
31
|
export {};
|
package/lib/index.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
|
|
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
|
-
class ArgumentError extends Error {
|
|
1
|
+
export class ArgumentError extends Error {
|
|
11
2
|
constructor(message) {
|
|
12
3
|
super(message);
|
|
13
4
|
}
|
|
@@ -22,23 +13,31 @@ class ArgumentError extends Error {
|
|
|
22
13
|
throw e;
|
|
23
14
|
}
|
|
24
15
|
}
|
|
25
|
-
|
|
26
|
-
function isNil(x, key) {
|
|
16
|
+
export function isNil(x, key) {
|
|
27
17
|
return x == null || (key !== undefined && x[key] == null);
|
|
28
18
|
}
|
|
29
|
-
function isNotNil(x, key) {
|
|
19
|
+
export function isNotNil(x, key) {
|
|
30
20
|
return !isNil(x) && (key === undefined || !isNil(x[key]));
|
|
31
21
|
}
|
|
32
|
-
function assertNotNil(x) {
|
|
22
|
+
export function assertNotNil(x) {
|
|
33
23
|
if (x == null)
|
|
34
24
|
ArgumentError.null('Unexpected null');
|
|
35
25
|
}
|
|
36
|
-
function argNotNil(x, key) {
|
|
26
|
+
export function argNotNil(x, key) {
|
|
37
27
|
isNotNil(x, key) || ArgumentError.null(`'${String(key)}' was null or undefined`);
|
|
38
28
|
}
|
|
39
|
-
function hasOwn(x, key) {
|
|
29
|
+
export function hasOwn(x, key) {
|
|
40
30
|
return Object.hasOwn(x, key);
|
|
41
31
|
}
|
|
42
|
-
function assertOwn(x, key) {
|
|
32
|
+
export function assertOwn(x, key) {
|
|
43
33
|
hasOwn(x, key) || ArgumentError.missing({ key });
|
|
44
34
|
}
|
|
35
|
+
export function nullIfEmpty(x) {
|
|
36
|
+
return x == null || x.length == 0 ? null : x;
|
|
37
|
+
}
|
|
38
|
+
export function intOrUndefined(x) {
|
|
39
|
+
if (isNil(x))
|
|
40
|
+
return undefined;
|
|
41
|
+
const v = Number.parseInt(x);
|
|
42
|
+
return Number.isNaN(v) ? undefined : v;
|
|
43
|
+
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drunkcod/argis",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.5",
|
|
4
5
|
"description": "Tiny method arg checking with strong types.",
|
|
5
6
|
"scripts": {
|
|
6
|
-
"
|
|
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"
|