@salesforce/ts-types 1.5.20 → 1.7.0
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.txt +1 -1
- package/README.md +1 -1
- package/lib/experimental/index.d.ts +1 -45
- package/lib/experimental/index.js +5 -75
- package/lib/index.js +5 -1
- package/lib/narrowing/as.js +11 -11
- package/lib/narrowing/assert.js +14 -14
- package/lib/narrowing/coerce.d.ts +2 -2
- package/lib/narrowing/coerce.js +3 -3
- package/lib/narrowing/ensure.js +13 -13
- package/lib/narrowing/get.js +14 -14
- package/lib/narrowing/has.d.ts +11 -11
- package/lib/narrowing/has.js +12 -12
- package/lib/narrowing/index.js +5 -1
- package/lib/narrowing/object.js +3 -3
- package/lib/narrowing/to.js +2 -2
- package/lib/types/index.js +5 -1
- package/package.json +20 -18
package/LICENSE.txt
CHANGED
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ This is a simple TypeScript-oriented library developed for use in Salesforce Typ
|
|
|
7
7
|
1. A collection of commonly desired types.
|
|
8
8
|
1. A collection of type-narrowing convenience functions for writing concise type-guards.
|
|
9
9
|
|
|
10
|
-
See the [API documentation](https://forcedotcom.github.io/
|
|
10
|
+
See the [API documentation](https://forcedotcom.github.io/ts-types) for more details on each of the utilities that `ts-types` provides.
|
|
11
11
|
|
|
12
12
|
## Why did we create it?
|
|
13
13
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AnyConstructor, Dictionary, Nullable, Optional, View } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* @ignore
|
|
4
4
|
*/
|
|
@@ -26,51 +26,7 @@ export declare function is<T extends object>(obj: Nullable<object>, shape: Objec
|
|
|
26
26
|
* @ignore
|
|
27
27
|
*/
|
|
28
28
|
export declare function as<T extends object>(obj: Nullable<object>, shape: ObjectShape): Optional<T>;
|
|
29
|
-
/**
|
|
30
|
-
* A view over an `object` with constrainable, optional properties.
|
|
31
|
-
*
|
|
32
|
-
* @ignore
|
|
33
|
-
*/
|
|
34
|
-
export declare type ViewOptional<K extends string, V = unknown> = {
|
|
35
|
-
[_ in K]?: V;
|
|
36
|
-
};
|
|
37
29
|
/**
|
|
38
30
|
* @ignore
|
|
39
31
|
*/
|
|
40
32
|
export declare function hasNull<T, K extends string>(value: T, key: K): value is T & View<K, string>;
|
|
41
|
-
/**
|
|
42
|
-
* @ignore
|
|
43
|
-
*/
|
|
44
|
-
export declare function view<T, K extends string>(value: Nullable<T>, keys: Many<K>): Optional<T & ViewOptional<K>>;
|
|
45
|
-
/**
|
|
46
|
-
* @ignore
|
|
47
|
-
*/
|
|
48
|
-
export declare function viewString<T, K extends string>(value: Nullable<T>, key: K): Optional<T & ViewOptional<K, string>>;
|
|
49
|
-
/**
|
|
50
|
-
* @ignore
|
|
51
|
-
*/
|
|
52
|
-
export declare function viewNumber<T, K extends string>(value: Nullable<T>, key: K): Optional<T & ViewOptional<K, number>>;
|
|
53
|
-
/**
|
|
54
|
-
* @ignore
|
|
55
|
-
*/
|
|
56
|
-
export declare function viewBoolean<T, K extends string>(value: Nullable<T>, key: K): Optional<T & ViewOptional<K, boolean>>;
|
|
57
|
-
/**
|
|
58
|
-
* @ignore
|
|
59
|
-
*/
|
|
60
|
-
export declare function viewObject<T, K extends string>(value: Nullable<T>, key: K): Optional<T & ViewOptional<K, object>>;
|
|
61
|
-
/**
|
|
62
|
-
* @ignore
|
|
63
|
-
*/
|
|
64
|
-
export declare function viewPlainObject<T, K extends string>(value: Nullable<T>, key: K): Optional<T & ViewOptional<K, object>>;
|
|
65
|
-
/**
|
|
66
|
-
* @ignore
|
|
67
|
-
*/
|
|
68
|
-
export declare function viewInstance<T, K extends string, C extends AnyConstructor>(value: Nullable<T>, key: K, ctor: C): Optional<T & ViewOptional<K, InstanceType<C>>>;
|
|
69
|
-
/**
|
|
70
|
-
* @ignore
|
|
71
|
-
*/
|
|
72
|
-
export declare function viewArray<T, K extends string>(value: Nullable<T>, key: K): Optional<T & ViewOptional<K, AnyArray>>;
|
|
73
|
-
/**
|
|
74
|
-
* @ignore
|
|
75
|
-
*/
|
|
76
|
-
export declare function viewFunction<T, K extends string>(value: Nullable<T>, key: K): Optional<T & ViewOptional<K, AnyFunction>>;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.
|
|
9
|
+
exports.hasNull = exports.as = exports.is = void 0;
|
|
10
10
|
/**
|
|
11
11
|
* A staging area for either introducing or removing type and functions, incrementally.
|
|
12
12
|
*/
|
|
@@ -15,15 +15,15 @@ const narrowing_1 = require("../narrowing");
|
|
|
15
15
|
* @ignore
|
|
16
16
|
*/
|
|
17
17
|
function is(obj, shape) {
|
|
18
|
-
const isVerifiable = (v) => narrowing_1.isString(v) || narrowing_1.isFunction(v);
|
|
18
|
+
const isVerifiable = (v) => (0, narrowing_1.isString)(v) || (0, narrowing_1.isFunction)(v);
|
|
19
19
|
return (!obj ||
|
|
20
|
-
narrowing_1.definiteEntriesOf(shape)
|
|
20
|
+
(0, narrowing_1.definiteEntriesOf)(shape)
|
|
21
21
|
.map(([k, v]) => ({
|
|
22
22
|
key: k,
|
|
23
23
|
...(isVerifiable(v) ? { type: v, optional: false } : v),
|
|
24
24
|
}))
|
|
25
25
|
.every(({ key, type, optional }) => (optional && !(key in obj)) ||
|
|
26
|
-
(narrowing_1.isString(type) ? typeof narrowing_1.get(obj, key) === type : narrowing_1.get(obj, key) instanceof type)));
|
|
26
|
+
((0, narrowing_1.isString)(type) ? typeof (0, narrowing_1.get)(obj, key) === type : (0, narrowing_1.get)(obj, key) instanceof type)));
|
|
27
27
|
}
|
|
28
28
|
exports.is = is;
|
|
29
29
|
/**
|
|
@@ -41,77 +41,7 @@ exports.as = as;
|
|
|
41
41
|
* @ignore
|
|
42
42
|
*/
|
|
43
43
|
function hasNull(value, key) {
|
|
44
|
-
return narrowing_1.has(value, key) && value[key] == null;
|
|
44
|
+
return (0, narrowing_1.has)(value, key) && value[key] == null;
|
|
45
45
|
}
|
|
46
46
|
exports.hasNull = hasNull;
|
|
47
|
-
/**
|
|
48
|
-
* @ignore
|
|
49
|
-
*/
|
|
50
|
-
function view(value, keys) {
|
|
51
|
-
return narrowing_1.has(value, keys) ? value : undefined;
|
|
52
|
-
}
|
|
53
|
-
exports.view = view;
|
|
54
|
-
/**
|
|
55
|
-
* @ignore
|
|
56
|
-
*/
|
|
57
|
-
function viewOptional(value, key, hasType) {
|
|
58
|
-
return hasType(value, key) ? value : hasNull(value, key) ? value : undefined;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* @ignore
|
|
62
|
-
*/
|
|
63
|
-
function viewString(value, key) {
|
|
64
|
-
return viewOptional(value, key, narrowing_1.hasString);
|
|
65
|
-
}
|
|
66
|
-
exports.viewString = viewString;
|
|
67
|
-
/**
|
|
68
|
-
* @ignore
|
|
69
|
-
*/
|
|
70
|
-
function viewNumber(value, key) {
|
|
71
|
-
return viewOptional(value, key, narrowing_1.hasNumber);
|
|
72
|
-
}
|
|
73
|
-
exports.viewNumber = viewNumber;
|
|
74
|
-
/**
|
|
75
|
-
* @ignore
|
|
76
|
-
*/
|
|
77
|
-
function viewBoolean(value, key) {
|
|
78
|
-
return viewOptional(value, key, narrowing_1.hasBoolean);
|
|
79
|
-
}
|
|
80
|
-
exports.viewBoolean = viewBoolean;
|
|
81
|
-
/**
|
|
82
|
-
* @ignore
|
|
83
|
-
*/
|
|
84
|
-
function viewObject(value, key) {
|
|
85
|
-
return viewOptional(value, key, narrowing_1.hasObject);
|
|
86
|
-
}
|
|
87
|
-
exports.viewObject = viewObject;
|
|
88
|
-
/**
|
|
89
|
-
* @ignore
|
|
90
|
-
*/
|
|
91
|
-
function viewPlainObject(value, key) {
|
|
92
|
-
return viewOptional(value, key, narrowing_1.hasPlainObject);
|
|
93
|
-
}
|
|
94
|
-
exports.viewPlainObject = viewPlainObject;
|
|
95
|
-
/**
|
|
96
|
-
* @ignore
|
|
97
|
-
*/
|
|
98
|
-
function viewInstance(value, key, ctor) {
|
|
99
|
-
const hasType = (v, k) => narrowing_1.hasInstance(v, k, ctor);
|
|
100
|
-
return viewOptional(value, key, hasType);
|
|
101
|
-
}
|
|
102
|
-
exports.viewInstance = viewInstance;
|
|
103
|
-
/**
|
|
104
|
-
* @ignore
|
|
105
|
-
*/
|
|
106
|
-
function viewArray(value, key) {
|
|
107
|
-
return viewOptional(value, key, narrowing_1.hasArray);
|
|
108
|
-
}
|
|
109
|
-
exports.viewArray = viewArray;
|
|
110
|
-
/**
|
|
111
|
-
* @ignore
|
|
112
|
-
*/
|
|
113
|
-
function viewFunction(value, key) {
|
|
114
|
-
return viewOptional(value, key, narrowing_1.hasFunction);
|
|
115
|
-
}
|
|
116
|
-
exports.viewFunction = viewFunction;
|
|
117
47
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js
CHANGED
|
@@ -7,7 +7,11 @@
|
|
|
7
7
|
*/
|
|
8
8
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
9
|
if (k2 === undefined) k2 = k;
|
|
10
|
-
Object.
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
11
15
|
}) : (function(o, m, k, k2) {
|
|
12
16
|
if (k2 === undefined) k2 = k;
|
|
13
17
|
o[k2] = m[k];
|
package/lib/narrowing/as.js
CHANGED
|
@@ -10,57 +10,57 @@ exports.asJsonArray = exports.asJsonMap = exports.asFunction = exports.asArray =
|
|
|
10
10
|
const is_1 = require("./is");
|
|
11
11
|
// underlying function
|
|
12
12
|
function asString(value, defaultValue) {
|
|
13
|
-
return is_1.isString(value) ? value : defaultValue;
|
|
13
|
+
return (0, is_1.isString)(value) ? value : defaultValue;
|
|
14
14
|
}
|
|
15
15
|
exports.asString = asString;
|
|
16
16
|
// underlying function
|
|
17
17
|
function asNumber(value, defaultValue) {
|
|
18
|
-
return is_1.isNumber(value) ? value : defaultValue;
|
|
18
|
+
return (0, is_1.isNumber)(value) ? value : defaultValue;
|
|
19
19
|
}
|
|
20
20
|
exports.asNumber = asNumber;
|
|
21
21
|
// underlying function
|
|
22
22
|
function asBoolean(value, defaultValue) {
|
|
23
|
-
return is_1.isBoolean(value) ? value : defaultValue;
|
|
23
|
+
return (0, is_1.isBoolean)(value) ? value : defaultValue;
|
|
24
24
|
}
|
|
25
25
|
exports.asBoolean = asBoolean;
|
|
26
26
|
// underlying function
|
|
27
27
|
function asObject(value, defaultValue) {
|
|
28
|
-
return is_1.isObject(value) ? value : defaultValue;
|
|
28
|
+
return (0, is_1.isObject)(value) ? value : defaultValue;
|
|
29
29
|
}
|
|
30
30
|
exports.asObject = asObject;
|
|
31
31
|
// underlying function
|
|
32
32
|
function asPlainObject(value, defaultValue) {
|
|
33
|
-
return is_1.isPlainObject(value) ? value : defaultValue;
|
|
33
|
+
return (0, is_1.isPlainObject)(value) ? value : defaultValue;
|
|
34
34
|
}
|
|
35
35
|
exports.asPlainObject = asPlainObject;
|
|
36
36
|
// underlying function
|
|
37
37
|
function asDictionary(value, defaultValue) {
|
|
38
|
-
return is_1.isDictionary(value) ? value : defaultValue;
|
|
38
|
+
return (0, is_1.isDictionary)(value) ? value : defaultValue;
|
|
39
39
|
}
|
|
40
40
|
exports.asDictionary = asDictionary;
|
|
41
41
|
// underlying function
|
|
42
42
|
function asInstance(value, ctor, defaultValue) {
|
|
43
|
-
return is_1.isInstance(value, ctor) ? value : defaultValue;
|
|
43
|
+
return (0, is_1.isInstance)(value, ctor) ? value : defaultValue;
|
|
44
44
|
}
|
|
45
45
|
exports.asInstance = asInstance;
|
|
46
46
|
// underlying function
|
|
47
47
|
function asArray(value, defaultValue) {
|
|
48
|
-
return is_1.isArray(value) ? value : defaultValue;
|
|
48
|
+
return (0, is_1.isArray)(value) ? value : defaultValue;
|
|
49
49
|
}
|
|
50
50
|
exports.asArray = asArray;
|
|
51
51
|
// underlying function
|
|
52
52
|
function asFunction(value, defaultValue) {
|
|
53
|
-
return is_1.isFunction(value) ? value : defaultValue;
|
|
53
|
+
return (0, is_1.isFunction)(value) ? value : defaultValue;
|
|
54
54
|
}
|
|
55
55
|
exports.asFunction = asFunction;
|
|
56
56
|
// underlying function
|
|
57
57
|
function asJsonMap(value, defaultValue) {
|
|
58
|
-
return is_1.isJsonMap(value) ? value : defaultValue;
|
|
58
|
+
return (0, is_1.isJsonMap)(value) ? value : defaultValue;
|
|
59
59
|
}
|
|
60
60
|
exports.asJsonMap = asJsonMap;
|
|
61
61
|
// underlying function
|
|
62
62
|
function asJsonArray(value, defaultValue) {
|
|
63
|
-
return is_1.isJsonArray(value) ? value : defaultValue;
|
|
63
|
+
return (0, is_1.isJsonArray)(value) ? value : defaultValue;
|
|
64
64
|
}
|
|
65
65
|
exports.asJsonArray = asJsonArray;
|
|
66
66
|
//# sourceMappingURL=as.js.map
|
package/lib/narrowing/assert.js
CHANGED
|
@@ -19,7 +19,7 @@ const to_1 = require("./to");
|
|
|
19
19
|
*/
|
|
20
20
|
function assert(condition, message) {
|
|
21
21
|
if (!condition) {
|
|
22
|
-
throw new errors_1.AssertionFailedError(message
|
|
22
|
+
throw new errors_1.AssertionFailedError(message ?? 'Assertion condition was false');
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
exports.assert = assert;
|
|
@@ -39,7 +39,7 @@ exports.assert = assert;
|
|
|
39
39
|
* @throws {@link AssertionFailedError} If the value was undefined.
|
|
40
40
|
*/
|
|
41
41
|
function assertNonNull(value, message) {
|
|
42
|
-
assert(value != null, message
|
|
42
|
+
assert(value != null, message ?? 'Value is not defined');
|
|
43
43
|
}
|
|
44
44
|
exports.assertNonNull = assertNonNull;
|
|
45
45
|
/**
|
|
@@ -50,7 +50,7 @@ exports.assertNonNull = assertNonNull;
|
|
|
50
50
|
* @throws {@link AssertionFailedError} If the value was undefined.
|
|
51
51
|
*/
|
|
52
52
|
function assertString(value, message) {
|
|
53
|
-
assertNonNull(as_1.asString(value), message
|
|
53
|
+
assertNonNull((0, as_1.asString)(value), message ?? 'Value is not a string');
|
|
54
54
|
}
|
|
55
55
|
exports.assertString = assertString;
|
|
56
56
|
/**
|
|
@@ -61,7 +61,7 @@ exports.assertString = assertString;
|
|
|
61
61
|
* @throws {@link AssertionFailedError} If the value was undefined.
|
|
62
62
|
*/
|
|
63
63
|
function assertNumber(value, message) {
|
|
64
|
-
assertNonNull(as_1.asNumber(value), message
|
|
64
|
+
assertNonNull((0, as_1.asNumber)(value), message ?? 'Value is not a number');
|
|
65
65
|
}
|
|
66
66
|
exports.assertNumber = assertNumber;
|
|
67
67
|
/**
|
|
@@ -72,7 +72,7 @@ exports.assertNumber = assertNumber;
|
|
|
72
72
|
* @throws {@link AssertionFailedError} If the value was undefined.
|
|
73
73
|
*/
|
|
74
74
|
function assertBoolean(value, message) {
|
|
75
|
-
assertNonNull(as_1.asBoolean(value), message
|
|
75
|
+
assertNonNull((0, as_1.asBoolean)(value), message ?? 'Value is not a boolean');
|
|
76
76
|
}
|
|
77
77
|
exports.assertBoolean = assertBoolean;
|
|
78
78
|
/**
|
|
@@ -83,7 +83,7 @@ exports.assertBoolean = assertBoolean;
|
|
|
83
83
|
* @throws {@link AssertionFailedError} If the value was undefined.
|
|
84
84
|
*/
|
|
85
85
|
function assertObject(value, message) {
|
|
86
|
-
assertNonNull(as_1.asObject(value), message
|
|
86
|
+
assertNonNull((0, as_1.asObject)(value), message ?? 'Value is not an object');
|
|
87
87
|
}
|
|
88
88
|
exports.assertObject = assertObject;
|
|
89
89
|
/**
|
|
@@ -95,7 +95,7 @@ exports.assertObject = assertObject;
|
|
|
95
95
|
* @throws {@link AssertionFailedError} If the value was undefined.
|
|
96
96
|
*/
|
|
97
97
|
function assertPlainObject(value, message) {
|
|
98
|
-
assertNonNull(as_1.asPlainObject(value), message
|
|
98
|
+
assertNonNull((0, as_1.asPlainObject)(value), message ?? 'Value is not a plain object');
|
|
99
99
|
}
|
|
100
100
|
exports.assertPlainObject = assertPlainObject;
|
|
101
101
|
/**
|
|
@@ -107,7 +107,7 @@ exports.assertPlainObject = assertPlainObject;
|
|
|
107
107
|
* @throws {@link AssertionFailedError} If the value was undefined.
|
|
108
108
|
*/
|
|
109
109
|
function assertDictionary(value, message) {
|
|
110
|
-
assertNonNull(as_1.asDictionary(value), message
|
|
110
|
+
assertNonNull((0, as_1.asDictionary)(value), message ?? 'Value is not a dictionary object');
|
|
111
111
|
}
|
|
112
112
|
exports.assertDictionary = assertDictionary;
|
|
113
113
|
/**
|
|
@@ -119,7 +119,7 @@ exports.assertDictionary = assertDictionary;
|
|
|
119
119
|
* @throws {@link AssertionFailedError} If the value was undefined.
|
|
120
120
|
*/
|
|
121
121
|
function assertInstance(value, ctor, message) {
|
|
122
|
-
assertNonNull(as_1.asInstance(value, ctor), message
|
|
122
|
+
assertNonNull((0, as_1.asInstance)(value, ctor), message ?? `Value is not an instance of ${ctor.name}`);
|
|
123
123
|
}
|
|
124
124
|
exports.assertInstance = assertInstance;
|
|
125
125
|
/**
|
|
@@ -130,7 +130,7 @@ exports.assertInstance = assertInstance;
|
|
|
130
130
|
* @throws {@link AssertionFailedError} If the value was undefined.
|
|
131
131
|
*/
|
|
132
132
|
function assertArray(value, message) {
|
|
133
|
-
assertNonNull(as_1.asArray(value), message
|
|
133
|
+
assertNonNull((0, as_1.asArray)(value), message ?? 'Value is not an array');
|
|
134
134
|
}
|
|
135
135
|
exports.assertArray = assertArray;
|
|
136
136
|
/**
|
|
@@ -141,7 +141,7 @@ exports.assertArray = assertArray;
|
|
|
141
141
|
* @throws {@link AssertionFailedError} If the value was undefined.
|
|
142
142
|
*/
|
|
143
143
|
function assertFunction(value, message) {
|
|
144
|
-
assertNonNull(as_1.asFunction(value), message
|
|
144
|
+
assertNonNull((0, as_1.asFunction)(value), message ?? 'Value is not a function');
|
|
145
145
|
}
|
|
146
146
|
exports.assertFunction = assertFunction;
|
|
147
147
|
/**
|
|
@@ -154,7 +154,7 @@ exports.assertFunction = assertFunction;
|
|
|
154
154
|
* @throws {@link AssertionFailedError} If the value was not a JSON value type.
|
|
155
155
|
*/
|
|
156
156
|
function assertAnyJson(value, message) {
|
|
157
|
-
assertNonNull(to_1.toAnyJson(value), message
|
|
157
|
+
assertNonNull((0, to_1.toAnyJson)(value), message ?? 'Value is not a JSON-compatible value type');
|
|
158
158
|
}
|
|
159
159
|
exports.assertAnyJson = assertAnyJson;
|
|
160
160
|
/**
|
|
@@ -165,7 +165,7 @@ exports.assertAnyJson = assertAnyJson;
|
|
|
165
165
|
* @throws {@link AssertionFailedError} If the value was undefined.
|
|
166
166
|
*/
|
|
167
167
|
function assertJsonMap(value, message) {
|
|
168
|
-
assertNonNull(as_1.asJsonMap(value), message
|
|
168
|
+
assertNonNull((0, as_1.asJsonMap)(value), message ?? 'Value is not a JsonMap');
|
|
169
169
|
}
|
|
170
170
|
exports.assertJsonMap = assertJsonMap;
|
|
171
171
|
/**
|
|
@@ -176,7 +176,7 @@ exports.assertJsonMap = assertJsonMap;
|
|
|
176
176
|
* @throws {@link AssertionFailedError} If the value was undefined.
|
|
177
177
|
*/
|
|
178
178
|
function assertJsonArray(value, message) {
|
|
179
|
-
assertNonNull(as_1.asJsonArray(value), message
|
|
179
|
+
assertNonNull((0, as_1.asJsonArray)(value), message ?? 'Value is not a JsonArray');
|
|
180
180
|
}
|
|
181
181
|
exports.assertJsonArray = assertJsonArray;
|
|
182
182
|
//# sourceMappingURL=assert.js.map
|
|
@@ -30,7 +30,7 @@ export declare function coerceJsonMap<T extends object>(value: Nullable<T>): Opt
|
|
|
30
30
|
* Narrows an object of type `T` to a `JsonMap` using a shallow type-compatibility check. Use this when the source of
|
|
31
31
|
* the object is known to be JSON-compatible and you want simple type coercion to a `JsonMap`. Use {@link toJsonMap}
|
|
32
32
|
* instead when the `value` object _cannot_ be guaranteed to be JSON-compatible and you want an assurance of runtime
|
|
33
|
-
* type safety. This is a shortcut for writing `asJsonMap(coerceAnyJson(value))
|
|
33
|
+
* type safety. This is a shortcut for writing `asJsonMap(coerceAnyJson(value)) ?? defaultValue`.
|
|
34
34
|
*
|
|
35
35
|
* @param value The object to coerce.
|
|
36
36
|
* @param defaultValue The default to return if `value` was not defined.
|
|
@@ -49,7 +49,7 @@ export declare function coerceJsonArray<T>(value: Nullable<T[]>): Optional<JsonA
|
|
|
49
49
|
* Narrows an array of type `T` to a `JsonArray` using a shallow type-compatibility check. Use this when the source of
|
|
50
50
|
* the array is known to be JSON-compatible and you want simple type coercion to a `JsonArray`. Use {@link toJsonArray}
|
|
51
51
|
* instead when the `value` array _cannot_ be guaranteed to be JSON-compatible and you want an assurance of runtime
|
|
52
|
-
* type safety. This is a shortcut for writing `asJsonArray(coerceAnyJson(value))
|
|
52
|
+
* type safety. This is a shortcut for writing `asJsonArray(coerceAnyJson(value)) ?? defaultValue`.
|
|
53
53
|
*
|
|
54
54
|
* @param value The array to coerce.
|
|
55
55
|
* @param defaultValue The default to return if `value` was not defined.
|
package/lib/narrowing/coerce.js
CHANGED
|
@@ -11,17 +11,17 @@ const as_1 = require("./as");
|
|
|
11
11
|
const is_1 = require("./is");
|
|
12
12
|
// underlying function
|
|
13
13
|
function coerceAnyJson(value, defaultValue) {
|
|
14
|
-
return is_1.isAnyJson(value) ? value : defaultValue;
|
|
14
|
+
return (0, is_1.isAnyJson)(value) ? value : defaultValue;
|
|
15
15
|
}
|
|
16
16
|
exports.coerceAnyJson = coerceAnyJson;
|
|
17
17
|
// underlying function
|
|
18
18
|
function coerceJsonMap(value, defaultValue) {
|
|
19
|
-
return as_1.asJsonMap(coerceAnyJson(value))
|
|
19
|
+
return (0, as_1.asJsonMap)(coerceAnyJson(value)) ?? defaultValue;
|
|
20
20
|
}
|
|
21
21
|
exports.coerceJsonMap = coerceJsonMap;
|
|
22
22
|
// underlying method
|
|
23
23
|
function coerceJsonArray(value, defaultValue) {
|
|
24
|
-
return as_1.asJsonArray(coerceAnyJson(value))
|
|
24
|
+
return (0, as_1.asJsonArray)(coerceAnyJson(value)) ?? defaultValue;
|
|
25
25
|
}
|
|
26
26
|
exports.coerceJsonArray = coerceJsonArray;
|
|
27
27
|
//# sourceMappingURL=coerce.js.map
|
package/lib/narrowing/ensure.js
CHANGED
|
@@ -27,7 +27,7 @@ const to_1 = require("./to");
|
|
|
27
27
|
*/
|
|
28
28
|
function ensure(value, message) {
|
|
29
29
|
if (value == null) {
|
|
30
|
-
throw new errors_1.UnexpectedValueTypeError(message
|
|
30
|
+
throw new errors_1.UnexpectedValueTypeError(message ?? 'Value is not defined');
|
|
31
31
|
}
|
|
32
32
|
return value;
|
|
33
33
|
}
|
|
@@ -40,7 +40,7 @@ exports.ensure = ensure;
|
|
|
40
40
|
* @throws {@link UnexpectedValueTypeError} If the value was undefined.
|
|
41
41
|
*/
|
|
42
42
|
function ensureString(value, message) {
|
|
43
|
-
return ensure(as_1.asString(value), message
|
|
43
|
+
return ensure((0, as_1.asString)(value), message ?? 'Value is not a string');
|
|
44
44
|
}
|
|
45
45
|
exports.ensureString = ensureString;
|
|
46
46
|
/**
|
|
@@ -51,7 +51,7 @@ exports.ensureString = ensureString;
|
|
|
51
51
|
* @throws {@link UnexpectedValueTypeError} If the value was undefined.
|
|
52
52
|
*/
|
|
53
53
|
function ensureNumber(value, message) {
|
|
54
|
-
return ensure(as_1.asNumber(value), message
|
|
54
|
+
return ensure((0, as_1.asNumber)(value), message ?? 'Value is not a number');
|
|
55
55
|
}
|
|
56
56
|
exports.ensureNumber = ensureNumber;
|
|
57
57
|
/**
|
|
@@ -62,7 +62,7 @@ exports.ensureNumber = ensureNumber;
|
|
|
62
62
|
* @throws {@link UnexpectedValueTypeError} If the value was undefined.
|
|
63
63
|
*/
|
|
64
64
|
function ensureBoolean(value, message) {
|
|
65
|
-
return ensure(as_1.asBoolean(value), message
|
|
65
|
+
return ensure((0, as_1.asBoolean)(value), message ?? 'Value is not a boolean');
|
|
66
66
|
}
|
|
67
67
|
exports.ensureBoolean = ensureBoolean;
|
|
68
68
|
/**
|
|
@@ -73,7 +73,7 @@ exports.ensureBoolean = ensureBoolean;
|
|
|
73
73
|
* @throws {@link UnexpectedValueTypeError} If the value was undefined.
|
|
74
74
|
*/
|
|
75
75
|
function ensureObject(value, message) {
|
|
76
|
-
return ensure(as_1.asObject(value), message
|
|
76
|
+
return ensure((0, as_1.asObject)(value), message ?? 'Value is not an object');
|
|
77
77
|
}
|
|
78
78
|
exports.ensureObject = ensureObject;
|
|
79
79
|
/**
|
|
@@ -85,7 +85,7 @@ exports.ensureObject = ensureObject;
|
|
|
85
85
|
* @throws {@link UnexpectedValueTypeError} If the value was undefined.
|
|
86
86
|
*/
|
|
87
87
|
function ensurePlainObject(value, message) {
|
|
88
|
-
return ensure(as_1.asPlainObject(value), message
|
|
88
|
+
return ensure((0, as_1.asPlainObject)(value), message ?? 'Value is not a plain object');
|
|
89
89
|
}
|
|
90
90
|
exports.ensurePlainObject = ensurePlainObject;
|
|
91
91
|
/**
|
|
@@ -97,7 +97,7 @@ exports.ensurePlainObject = ensurePlainObject;
|
|
|
97
97
|
* @throws {@link UnexpectedValueTypeError} If the value was undefined.
|
|
98
98
|
*/
|
|
99
99
|
function ensureDictionary(value, message) {
|
|
100
|
-
return ensure(as_1.asDictionary(value), message
|
|
100
|
+
return ensure((0, as_1.asDictionary)(value), message ?? 'Value is not a dictionary object');
|
|
101
101
|
}
|
|
102
102
|
exports.ensureDictionary = ensureDictionary;
|
|
103
103
|
/**
|
|
@@ -109,7 +109,7 @@ exports.ensureDictionary = ensureDictionary;
|
|
|
109
109
|
* @throws {@link UnexpectedValueTypeError} If the value was undefined.
|
|
110
110
|
*/
|
|
111
111
|
function ensureInstance(value, ctor, message) {
|
|
112
|
-
return ensure(as_1.asInstance(value, ctor), message
|
|
112
|
+
return ensure((0, as_1.asInstance)(value, ctor), message ?? `Value is not an instance of ${ctor.name}`);
|
|
113
113
|
}
|
|
114
114
|
exports.ensureInstance = ensureInstance;
|
|
115
115
|
/**
|
|
@@ -120,7 +120,7 @@ exports.ensureInstance = ensureInstance;
|
|
|
120
120
|
* @throws {@link UnexpectedValueTypeError} If the value was undefined.
|
|
121
121
|
*/
|
|
122
122
|
function ensureArray(value, message) {
|
|
123
|
-
return ensure(as_1.asArray(value), message
|
|
123
|
+
return ensure((0, as_1.asArray)(value), message ?? 'Value is not an array');
|
|
124
124
|
}
|
|
125
125
|
exports.ensureArray = ensureArray;
|
|
126
126
|
/**
|
|
@@ -131,7 +131,7 @@ exports.ensureArray = ensureArray;
|
|
|
131
131
|
* @throws {@link UnexpectedValueTypeError} If the value was undefined.
|
|
132
132
|
*/
|
|
133
133
|
function ensureFunction(value, message) {
|
|
134
|
-
return ensure(as_1.asFunction(value), message
|
|
134
|
+
return ensure((0, as_1.asFunction)(value), message ?? 'Value is not a function');
|
|
135
135
|
}
|
|
136
136
|
exports.ensureFunction = ensureFunction;
|
|
137
137
|
/**
|
|
@@ -144,7 +144,7 @@ exports.ensureFunction = ensureFunction;
|
|
|
144
144
|
* @throws {@link UnexpectedValueTypeError} If the value was not a JSON value type.
|
|
145
145
|
*/
|
|
146
146
|
function ensureAnyJson(value, message) {
|
|
147
|
-
return ensure(to_1.toAnyJson(value), message
|
|
147
|
+
return ensure((0, to_1.toAnyJson)(value), message ?? 'Value is not a JSON-compatible value type');
|
|
148
148
|
}
|
|
149
149
|
exports.ensureAnyJson = ensureAnyJson;
|
|
150
150
|
/**
|
|
@@ -155,7 +155,7 @@ exports.ensureAnyJson = ensureAnyJson;
|
|
|
155
155
|
* @throws {@link UnexpectedValueTypeError} If the value was undefined.
|
|
156
156
|
*/
|
|
157
157
|
function ensureJsonMap(value, message) {
|
|
158
|
-
return ensure(as_1.asJsonMap(value), message
|
|
158
|
+
return ensure((0, as_1.asJsonMap)(value), message ?? 'Value is not a JsonMap');
|
|
159
159
|
}
|
|
160
160
|
exports.ensureJsonMap = ensureJsonMap;
|
|
161
161
|
/**
|
|
@@ -166,7 +166,7 @@ exports.ensureJsonMap = ensureJsonMap;
|
|
|
166
166
|
* @throws {@link UnexpectedValueTypeError} If the value was undefined.
|
|
167
167
|
*/
|
|
168
168
|
function ensureJsonArray(value, message) {
|
|
169
|
-
return ensure(as_1.asJsonArray(value), message
|
|
169
|
+
return ensure((0, as_1.asJsonArray)(value), message ?? 'Value is not a JsonArray');
|
|
170
170
|
}
|
|
171
171
|
exports.ensureJsonArray = ensureJsonArray;
|
|
172
172
|
//# sourceMappingURL=ensure.js.map
|
package/lib/narrowing/get.js
CHANGED
|
@@ -35,73 +35,73 @@ const internal_1 = require("./internal");
|
|
|
35
35
|
* @param defaultValue The default to return if the query result was not defined.
|
|
36
36
|
*/
|
|
37
37
|
function get(from, path, defaultValue) {
|
|
38
|
-
return internal_1.valueOrDefault(path
|
|
38
|
+
return (0, internal_1.valueOrDefault)(path
|
|
39
39
|
// keep values in quotes together
|
|
40
40
|
.split(/['"]/)
|
|
41
41
|
// values in quotes will always be odd indexes, split the non-quotes values as normal
|
|
42
42
|
.reduce((r, p, index) => (index % 2 === 1 ? [...r, p] : [...r, ...p.split(/[.[\]]/)]), [])
|
|
43
43
|
.filter((p) => !!p)
|
|
44
|
-
.reduce((r, p) => (has_1.has(r, p) ? r[p] : undefined), from), defaultValue);
|
|
44
|
+
.reduce((r, p) => ((0, has_1.has)(r, p) ? r[p] : undefined), from), defaultValue);
|
|
45
45
|
}
|
|
46
46
|
exports.get = get;
|
|
47
47
|
// underlying function
|
|
48
48
|
function getString(from, path, defaultValue) {
|
|
49
|
-
return internal_1.valueOrDefault(as_1.asString(get(from, path)), defaultValue);
|
|
49
|
+
return (0, internal_1.valueOrDefault)((0, as_1.asString)(get(from, path)), defaultValue);
|
|
50
50
|
}
|
|
51
51
|
exports.getString = getString;
|
|
52
52
|
// underlying function
|
|
53
53
|
function getNumber(from, path, defaultValue) {
|
|
54
|
-
return internal_1.valueOrDefault(as_1.asNumber(get(from, path)), defaultValue);
|
|
54
|
+
return (0, internal_1.valueOrDefault)((0, as_1.asNumber)(get(from, path)), defaultValue);
|
|
55
55
|
}
|
|
56
56
|
exports.getNumber = getNumber;
|
|
57
57
|
// underlying function
|
|
58
58
|
function getBoolean(from, path, defaultValue) {
|
|
59
|
-
return internal_1.valueOrDefault(as_1.asBoolean(get(from, path)), defaultValue);
|
|
59
|
+
return (0, internal_1.valueOrDefault)((0, as_1.asBoolean)(get(from, path)), defaultValue);
|
|
60
60
|
}
|
|
61
61
|
exports.getBoolean = getBoolean;
|
|
62
62
|
// underlying function
|
|
63
63
|
function getObject(from, path, defaultValue) {
|
|
64
|
-
return internal_1.valueOrDefault(as_1.asObject(get(from, path)), defaultValue);
|
|
64
|
+
return (0, internal_1.valueOrDefault)((0, as_1.asObject)(get(from, path)), defaultValue);
|
|
65
65
|
}
|
|
66
66
|
exports.getObject = getObject;
|
|
67
67
|
// underlying function
|
|
68
68
|
function getPlainObject(from, path, defaultValue) {
|
|
69
|
-
return internal_1.valueOrDefault(as_1.asPlainObject(get(from, path)), defaultValue);
|
|
69
|
+
return (0, internal_1.valueOrDefault)((0, as_1.asPlainObject)(get(from, path)), defaultValue);
|
|
70
70
|
}
|
|
71
71
|
exports.getPlainObject = getPlainObject;
|
|
72
72
|
// underlying function
|
|
73
73
|
function getDictionary(from, path, defaultValue) {
|
|
74
|
-
return internal_1.valueOrDefault(as_1.asDictionary(get(from, path)), defaultValue);
|
|
74
|
+
return (0, internal_1.valueOrDefault)((0, as_1.asDictionary)(get(from, path)), defaultValue);
|
|
75
75
|
}
|
|
76
76
|
exports.getDictionary = getDictionary;
|
|
77
77
|
// underlying function
|
|
78
78
|
function getInstance(from, path, ctor, defaultValue) {
|
|
79
|
-
return internal_1.valueOrDefault(as_1.asInstance(get(from, path), ctor), defaultValue);
|
|
79
|
+
return (0, internal_1.valueOrDefault)((0, as_1.asInstance)(get(from, path), ctor), defaultValue);
|
|
80
80
|
}
|
|
81
81
|
exports.getInstance = getInstance;
|
|
82
82
|
// underlying function
|
|
83
83
|
function getArray(from, path, defaultValue) {
|
|
84
|
-
return internal_1.valueOrDefault(as_1.asArray(get(from, path)), defaultValue);
|
|
84
|
+
return (0, internal_1.valueOrDefault)((0, as_1.asArray)(get(from, path)), defaultValue);
|
|
85
85
|
}
|
|
86
86
|
exports.getArray = getArray;
|
|
87
87
|
// underlying function
|
|
88
88
|
function getFunction(from, path, defaultValue) {
|
|
89
|
-
return internal_1.valueOrDefault(as_1.asFunction(get(from, path)), defaultValue);
|
|
89
|
+
return (0, internal_1.valueOrDefault)((0, as_1.asFunction)(get(from, path)), defaultValue);
|
|
90
90
|
}
|
|
91
91
|
exports.getFunction = getFunction;
|
|
92
92
|
// underlying function
|
|
93
93
|
function getAnyJson(from, path, defaultValue) {
|
|
94
|
-
return internal_1.valueOrDefault(coerce_1.coerceAnyJson(get(from, path)), defaultValue);
|
|
94
|
+
return (0, internal_1.valueOrDefault)((0, coerce_1.coerceAnyJson)(get(from, path)), defaultValue);
|
|
95
95
|
}
|
|
96
96
|
exports.getAnyJson = getAnyJson;
|
|
97
97
|
// underlying function
|
|
98
98
|
function getJsonMap(from, path, defaultValue) {
|
|
99
|
-
return internal_1.valueOrDefault(as_1.asJsonMap(getAnyJson(from, path)), defaultValue);
|
|
99
|
+
return (0, internal_1.valueOrDefault)((0, as_1.asJsonMap)(getAnyJson(from, path)), defaultValue);
|
|
100
100
|
}
|
|
101
101
|
exports.getJsonMap = getJsonMap;
|
|
102
102
|
// underlying function
|
|
103
103
|
function getJsonArray(from, path, defaultValue) {
|
|
104
|
-
return internal_1.valueOrDefault(as_1.asJsonArray(getAnyJson(from, path)), defaultValue);
|
|
104
|
+
return (0, internal_1.valueOrDefault)((0, as_1.asJsonArray)(getAnyJson(from, path)), defaultValue);
|
|
105
105
|
}
|
|
106
106
|
exports.getJsonArray = getJsonArray;
|
|
107
107
|
//# sourceMappingURL=get.js.map
|
package/lib/narrowing/has.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ import { AnyArray, AnyConstructor, AnyFunction, AnyJson, JsonArray, JsonMap, Man
|
|
|
20
20
|
* @param value The value to test.
|
|
21
21
|
* @param keys One or more `string` keys to check for existence.
|
|
22
22
|
*/
|
|
23
|
-
export declare function has<T
|
|
23
|
+
export declare function has<T, K extends string>(value: T, keys: Many<K>): value is T & object & View<K>;
|
|
24
24
|
/**
|
|
25
25
|
* Tests whether a value of type `T` contains a property `key` of type `string`. If so, the type of the tested value is
|
|
26
26
|
* narrowed to reflect the existence of that key for convenient access in the same scope. Returns `false` if the
|
|
@@ -39,7 +39,7 @@ export declare function has<T extends unknown, K extends string>(value: T, keys:
|
|
|
39
39
|
* @param value The value to test.
|
|
40
40
|
* @param keys A `string` key to check for existence.
|
|
41
41
|
*/
|
|
42
|
-
export declare function hasString<T
|
|
42
|
+
export declare function hasString<T, K extends string>(value: T, key: K): value is T & object & View<K, string>;
|
|
43
43
|
/**
|
|
44
44
|
* Tests whether a value of type `T` contains a property `key` of type `number`. If so, the type of the tested value is
|
|
45
45
|
* narrowed to reflect the existence of that key for convenient access in the same scope. Returns `false` if the
|
|
@@ -58,7 +58,7 @@ export declare function hasString<T extends unknown, K extends string>(value: T,
|
|
|
58
58
|
* @param value The value to test.
|
|
59
59
|
* @param keys A `number` key to check for existence.
|
|
60
60
|
*/
|
|
61
|
-
export declare function hasNumber<T
|
|
61
|
+
export declare function hasNumber<T, K extends string>(value: T, key: K): value is T & object & View<K, number>;
|
|
62
62
|
/**
|
|
63
63
|
* Tests whether a value of type `T` contains a property `key` of type `boolean`. If so, the type of the tested value is
|
|
64
64
|
* narrowed to reflect the existence of that key for convenient access in the same scope. Returns `false` if the
|
|
@@ -77,7 +77,7 @@ export declare function hasNumber<T extends unknown, K extends string>(value: T,
|
|
|
77
77
|
* @param value The value to test.
|
|
78
78
|
* @param keys A `boolean` key to check for existence.
|
|
79
79
|
*/
|
|
80
|
-
export declare function hasBoolean<T
|
|
80
|
+
export declare function hasBoolean<T, K extends string>(value: T, key: K): value is T & object & View<K, boolean>;
|
|
81
81
|
/**
|
|
82
82
|
* Tests whether a value of type `T` contains a property `key` of type `object`. If so, the type of the tested value is
|
|
83
83
|
* narrowed to reflect the existence of that key for convenient access in the same scope. Returns `false` if the
|
|
@@ -98,7 +98,7 @@ export declare function hasBoolean<T extends unknown, K extends string>(value: T
|
|
|
98
98
|
* @param value The value to test.
|
|
99
99
|
* @param keys An `object` key to check for existence.
|
|
100
100
|
*/
|
|
101
|
-
export declare function hasObject<V extends object = object, T
|
|
101
|
+
export declare function hasObject<V extends object = object, T = unknown, K extends string = string>(value: T, key: K): value is T & object & View<K, V>;
|
|
102
102
|
/**
|
|
103
103
|
* Tests whether a value of type `T` contains a property `key` whose type tests positively when tested with
|
|
104
104
|
* {@link isPlainObject}. If so, the type of the tested value is narrowed to reflect the existence of that key for
|
|
@@ -120,7 +120,7 @@ export declare function hasObject<V extends object = object, T extends unknown =
|
|
|
120
120
|
* @param value The value to test.
|
|
121
121
|
* @param keys A "plain" `object` key to check for existence.
|
|
122
122
|
*/
|
|
123
|
-
export declare function hasPlainObject<V extends object = object, T
|
|
123
|
+
export declare function hasPlainObject<V extends object = object, T = unknown, K extends string = string>(value: T, key: K): value is T & object & View<K, V>;
|
|
124
124
|
/**
|
|
125
125
|
* Tests whether a value of type `T` contains a property `key` whose type tests positively when tested with
|
|
126
126
|
* {@link isDictionary}. If so, the type of the tested value is narrowed to reflect the existence of that key for
|
|
@@ -142,7 +142,7 @@ export declare function hasPlainObject<V extends object = object, T extends unkn
|
|
|
142
142
|
* @param value The value to test.
|
|
143
143
|
* @param keys A "dictionary" `object` key to check for existence.
|
|
144
144
|
*/
|
|
145
|
-
export declare function hasDictionary<V = unknown, T
|
|
145
|
+
export declare function hasDictionary<V = unknown, T = unknown, K extends string = string>(value: T, key: K): value is T & object & View<K, Dictionary<V>>;
|
|
146
146
|
/**
|
|
147
147
|
* Tests whether a value of type `T` contains a property `key` whose type tests positively when tested with
|
|
148
148
|
* {@link isInstance} when compared with the given constructor type `C`. If so, the type of the tested value is
|
|
@@ -165,7 +165,7 @@ export declare function hasDictionary<V = unknown, T extends unknown = unknown,
|
|
|
165
165
|
* @param value The value to test.
|
|
166
166
|
* @param keys An instance of type `C` key to check for existence.
|
|
167
167
|
*/
|
|
168
|
-
export declare function hasInstance<T
|
|
168
|
+
export declare function hasInstance<T, K extends string, C extends AnyConstructor>(value: Optional<T>, key: K, ctor: C): value is T & View<K, InstanceType<C>>;
|
|
169
169
|
/**
|
|
170
170
|
* Tests whether a value of type `T` contains a property `key` of type {@link AnyArray}. If so, the type of the tested
|
|
171
171
|
* value is narrowed to reflect the existence of that key for convenient access in the same scope. Returns `false` if
|
|
@@ -184,7 +184,7 @@ export declare function hasInstance<T extends unknown, K extends string, C exten
|
|
|
184
184
|
* @param value The value to test.
|
|
185
185
|
* @param keys An `AnyArray` key to check for existence.
|
|
186
186
|
*/
|
|
187
|
-
export declare function hasArray<T
|
|
187
|
+
export declare function hasArray<T, K extends string>(value: Optional<T>, key: K): value is T & object & View<K, AnyArray>;
|
|
188
188
|
/**
|
|
189
189
|
* Tests whether a value of type `T` contains a property `key` of type {@link AnyFunction}. If so, the type of the
|
|
190
190
|
* tested value is narrowed to reflect the existence of that key for convenient access in the same scope. Returns
|
|
@@ -202,7 +202,7 @@ export declare function hasArray<T extends unknown, K extends string>(value: Opt
|
|
|
202
202
|
* @param value The value to test.
|
|
203
203
|
* @param keys An `AnyFunction` key to check for existence.
|
|
204
204
|
*/
|
|
205
|
-
export declare function hasFunction<T
|
|
205
|
+
export declare function hasFunction<T, K extends string>(value: Optional<T>, key: K): value is T & object & View<K, AnyFunction>;
|
|
206
206
|
/**
|
|
207
207
|
* Tests whether a value of type `T` contains a property `key` of type {@link AnyJson}, _using a shallow test for
|
|
208
208
|
* `AnyJson` compatibility_ (see {@link isAnyJson} for more information). If so, the type of the
|
|
@@ -220,7 +220,7 @@ export declare function hasFunction<T extends unknown, K extends string>(value:
|
|
|
220
220
|
* @param value The value to test.
|
|
221
221
|
* @param keys An `AnyJson` key to check for existence.
|
|
222
222
|
*/
|
|
223
|
-
export declare function hasAnyJson<T
|
|
223
|
+
export declare function hasAnyJson<T, K extends string>(value: Optional<T>, key: K): value is T & object & View<K, AnyJson>;
|
|
224
224
|
/**
|
|
225
225
|
* Tests whether a value of type `T extends AnyJson` contains a property `key` of type {@link JsonMap}. If so, the type
|
|
226
226
|
* of the tested value is narrowed to reflect the existence of that key for convenient access in the same scope. Returns
|
package/lib/narrowing/has.js
CHANGED
|
@@ -30,7 +30,7 @@ const is_1 = require("./is");
|
|
|
30
30
|
* @param keys One or more `string` keys to check for existence.
|
|
31
31
|
*/
|
|
32
32
|
function has(value, keys) {
|
|
33
|
-
return is_1.isObject(value) && (is_1.isArray(keys) ? keys.every((k) => k in value) : keys in value);
|
|
33
|
+
return (0, is_1.isObject)(value) && ((0, is_1.isArray)(keys) ? keys.every((k) => k in value) : keys in value);
|
|
34
34
|
}
|
|
35
35
|
exports.has = has;
|
|
36
36
|
/**
|
|
@@ -52,7 +52,7 @@ exports.has = has;
|
|
|
52
52
|
* @param keys A `string` key to check for existence.
|
|
53
53
|
*/
|
|
54
54
|
function hasString(value, key) {
|
|
55
|
-
return has(value, key) && is_1.isString(value[key]);
|
|
55
|
+
return has(value, key) && (0, is_1.isString)(value[key]);
|
|
56
56
|
}
|
|
57
57
|
exports.hasString = hasString;
|
|
58
58
|
/**
|
|
@@ -74,7 +74,7 @@ exports.hasString = hasString;
|
|
|
74
74
|
* @param keys A `number` key to check for existence.
|
|
75
75
|
*/
|
|
76
76
|
function hasNumber(value, key) {
|
|
77
|
-
return has(value, key) && is_1.isNumber(value[key]);
|
|
77
|
+
return has(value, key) && (0, is_1.isNumber)(value[key]);
|
|
78
78
|
}
|
|
79
79
|
exports.hasNumber = hasNumber;
|
|
80
80
|
/**
|
|
@@ -96,7 +96,7 @@ exports.hasNumber = hasNumber;
|
|
|
96
96
|
* @param keys A `boolean` key to check for existence.
|
|
97
97
|
*/
|
|
98
98
|
function hasBoolean(value, key) {
|
|
99
|
-
return has(value, key) && is_1.isBoolean(value[key]);
|
|
99
|
+
return has(value, key) && (0, is_1.isBoolean)(value[key]);
|
|
100
100
|
}
|
|
101
101
|
exports.hasBoolean = hasBoolean;
|
|
102
102
|
/**
|
|
@@ -120,7 +120,7 @@ exports.hasBoolean = hasBoolean;
|
|
|
120
120
|
* @param keys An `object` key to check for existence.
|
|
121
121
|
*/
|
|
122
122
|
function hasObject(value, key) {
|
|
123
|
-
return has(value, key) && is_1.isObject(value[key]);
|
|
123
|
+
return has(value, key) && (0, is_1.isObject)(value[key]);
|
|
124
124
|
}
|
|
125
125
|
exports.hasObject = hasObject;
|
|
126
126
|
/**
|
|
@@ -145,7 +145,7 @@ exports.hasObject = hasObject;
|
|
|
145
145
|
* @param keys A "plain" `object` key to check for existence.
|
|
146
146
|
*/
|
|
147
147
|
function hasPlainObject(value, key) {
|
|
148
|
-
return has(value, key) && is_1.isPlainObject(value[key]);
|
|
148
|
+
return has(value, key) && (0, is_1.isPlainObject)(value[key]);
|
|
149
149
|
}
|
|
150
150
|
exports.hasPlainObject = hasPlainObject;
|
|
151
151
|
/**
|
|
@@ -170,7 +170,7 @@ exports.hasPlainObject = hasPlainObject;
|
|
|
170
170
|
* @param keys A "dictionary" `object` key to check for existence.
|
|
171
171
|
*/
|
|
172
172
|
function hasDictionary(value, key) {
|
|
173
|
-
return has(value, key) && is_1.isDictionary(value[key]);
|
|
173
|
+
return has(value, key) && (0, is_1.isDictionary)(value[key]);
|
|
174
174
|
}
|
|
175
175
|
exports.hasDictionary = hasDictionary;
|
|
176
176
|
/**
|
|
@@ -218,7 +218,7 @@ exports.hasInstance = hasInstance;
|
|
|
218
218
|
* @param keys An `AnyArray` key to check for existence.
|
|
219
219
|
*/
|
|
220
220
|
function hasArray(value, key) {
|
|
221
|
-
return has(value, key) && is_1.isArray(value[key]);
|
|
221
|
+
return has(value, key) && (0, is_1.isArray)(value[key]);
|
|
222
222
|
}
|
|
223
223
|
exports.hasArray = hasArray;
|
|
224
224
|
/**
|
|
@@ -239,7 +239,7 @@ exports.hasArray = hasArray;
|
|
|
239
239
|
* @param keys An `AnyFunction` key to check for existence.
|
|
240
240
|
*/
|
|
241
241
|
function hasFunction(value, key) {
|
|
242
|
-
return has(value, key) && is_1.isFunction(value[key]);
|
|
242
|
+
return has(value, key) && (0, is_1.isFunction)(value[key]);
|
|
243
243
|
}
|
|
244
244
|
exports.hasFunction = hasFunction;
|
|
245
245
|
/**
|
|
@@ -260,7 +260,7 @@ exports.hasFunction = hasFunction;
|
|
|
260
260
|
* @param keys An `AnyJson` key to check for existence.
|
|
261
261
|
*/
|
|
262
262
|
function hasAnyJson(value, key) {
|
|
263
|
-
return has(value, key) && is_1.isAnyJson(value[key]);
|
|
263
|
+
return has(value, key) && (0, is_1.isAnyJson)(value[key]);
|
|
264
264
|
}
|
|
265
265
|
exports.hasAnyJson = hasAnyJson;
|
|
266
266
|
/**
|
|
@@ -280,7 +280,7 @@ exports.hasAnyJson = hasAnyJson;
|
|
|
280
280
|
* @param keys A `JsonMap` key to check for existence.
|
|
281
281
|
*/
|
|
282
282
|
function hasJsonMap(value, key) {
|
|
283
|
-
return hasAnyJson(value, key) && is_1.isJsonMap(value[key]);
|
|
283
|
+
return hasAnyJson(value, key) && (0, is_1.isJsonMap)(value[key]);
|
|
284
284
|
}
|
|
285
285
|
exports.hasJsonMap = hasJsonMap;
|
|
286
286
|
/**
|
|
@@ -300,7 +300,7 @@ exports.hasJsonMap = hasJsonMap;
|
|
|
300
300
|
* @param keys A `JsonArray` key to check for existence.
|
|
301
301
|
*/
|
|
302
302
|
function hasJsonArray(value, key) {
|
|
303
|
-
return hasAnyJson(value, key) && is_1.isJsonArray(value[key]);
|
|
303
|
+
return hasAnyJson(value, key) && (0, is_1.isJsonArray)(value[key]);
|
|
304
304
|
}
|
|
305
305
|
exports.hasJsonArray = hasJsonArray;
|
|
306
306
|
//# sourceMappingURL=has.js.map
|
package/lib/narrowing/index.js
CHANGED
|
@@ -7,7 +7,11 @@
|
|
|
7
7
|
*/
|
|
8
8
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
9
|
if (k2 === undefined) k2 = k;
|
|
10
|
-
Object.
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
11
15
|
}) : (function(o, m, k, k2) {
|
|
12
16
|
if (k2 === undefined) k2 = k;
|
|
13
17
|
o[k2] = m[k];
|
package/lib/narrowing/object.js
CHANGED
|
@@ -36,7 +36,7 @@ exports.definiteValuesOf = exports.definiteKeysOf = exports.definiteEntriesOf =
|
|
|
36
36
|
* @param obj The object of interest.
|
|
37
37
|
*/
|
|
38
38
|
function keysOf(obj) {
|
|
39
|
-
return Object.keys(obj
|
|
39
|
+
return Object.keys(obj ?? {});
|
|
40
40
|
}
|
|
41
41
|
exports.keysOf = keysOf;
|
|
42
42
|
/**
|
|
@@ -68,7 +68,7 @@ exports.keysOf = keysOf;
|
|
|
68
68
|
* @param obj The object of interest.
|
|
69
69
|
*/
|
|
70
70
|
function entriesOf(obj) {
|
|
71
|
-
return Object.entries(obj
|
|
71
|
+
return Object.entries(obj ?? {});
|
|
72
72
|
}
|
|
73
73
|
exports.entriesOf = entriesOf;
|
|
74
74
|
/**
|
|
@@ -100,7 +100,7 @@ exports.entriesOf = entriesOf;
|
|
|
100
100
|
* @param obj The object of interest.
|
|
101
101
|
*/
|
|
102
102
|
function valuesOf(obj) {
|
|
103
|
-
return Object.values(obj
|
|
103
|
+
return Object.values(obj ?? {});
|
|
104
104
|
}
|
|
105
105
|
exports.valuesOf = valuesOf;
|
|
106
106
|
/**
|
package/lib/narrowing/to.js
CHANGED
|
@@ -21,12 +21,12 @@ function toAnyJson(value, defaultValue) {
|
|
|
21
21
|
exports.toAnyJson = toAnyJson;
|
|
22
22
|
// underlying function
|
|
23
23
|
function toJsonMap(value, defaultValue) {
|
|
24
|
-
return as_1.asJsonMap(toAnyJson(value))
|
|
24
|
+
return (0, as_1.asJsonMap)(toAnyJson(value)) ?? defaultValue;
|
|
25
25
|
}
|
|
26
26
|
exports.toJsonMap = toJsonMap;
|
|
27
27
|
// underlying method
|
|
28
28
|
function toJsonArray(value, defaultValue) {
|
|
29
|
-
return as_1.asJsonArray(toAnyJson(value))
|
|
29
|
+
return (0, as_1.asJsonArray)(toAnyJson(value)) ?? defaultValue;
|
|
30
30
|
}
|
|
31
31
|
exports.toJsonArray = toJsonArray;
|
|
32
32
|
//# sourceMappingURL=to.js.map
|
package/lib/types/index.js
CHANGED
|
@@ -7,7 +7,11 @@
|
|
|
7
7
|
*/
|
|
8
8
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
9
|
if (k2 === undefined) k2 = k;
|
|
10
|
-
Object.
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
11
15
|
}) : (function(o, m, k, k2) {
|
|
12
16
|
if (k2 === undefined) k2 = k;
|
|
13
17
|
o[k2] = m[k];
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@salesforce/ts-types",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Types and related utilities for TypeScript",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
-
"repository": "
|
|
6
|
+
"repository": "forcedotcom/ts-types",
|
|
7
7
|
"author": "Salesforce",
|
|
8
8
|
"license": "BSD-3-Clause",
|
|
9
9
|
"types": "lib/index.d.ts",
|
|
@@ -13,13 +13,15 @@
|
|
|
13
13
|
],
|
|
14
14
|
"scripts": {
|
|
15
15
|
"build": "sf-build",
|
|
16
|
+
"ci-docs": "yarn sf-ci-docs",
|
|
16
17
|
"clean": "sf-clean",
|
|
17
18
|
"clean-all": "sf-clean all",
|
|
18
19
|
"compile": "sf-compile",
|
|
19
20
|
"docs": "sf-docs",
|
|
20
21
|
"format": "sf-format",
|
|
21
22
|
"lint": "sf-lint",
|
|
22
|
-
"prepack": "sf-
|
|
23
|
+
"prepack": "sf-prepack",
|
|
24
|
+
"prepare": "sf-install",
|
|
23
25
|
"pretest": "sf-compile-test",
|
|
24
26
|
"test": "sf-test"
|
|
25
27
|
},
|
|
@@ -27,28 +29,28 @@
|
|
|
27
29
|
"tslib": "^2.2.0"
|
|
28
30
|
},
|
|
29
31
|
"devDependencies": {
|
|
30
|
-
"@salesforce/dev-config": "^
|
|
31
|
-
"@salesforce/dev-scripts": "^
|
|
32
|
+
"@salesforce/dev-config": "^3.0.0",
|
|
33
|
+
"@salesforce/dev-scripts": "^3.1.0",
|
|
32
34
|
"@salesforce/prettier-config": "^0.0.2",
|
|
33
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
34
|
-
"@typescript-eslint/parser": "^
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "^5.40.0",
|
|
36
|
+
"@typescript-eslint/parser": "^5.40.1",
|
|
35
37
|
"chai": "^4.2.0",
|
|
36
|
-
"eslint": "^
|
|
37
|
-
"eslint-config-prettier": "^
|
|
38
|
-
"eslint-config-salesforce": "^
|
|
38
|
+
"eslint": "^8.25.0",
|
|
39
|
+
"eslint-config-prettier": "^8.5.0",
|
|
40
|
+
"eslint-config-salesforce": "^1.1.0",
|
|
39
41
|
"eslint-config-salesforce-license": "^0.1.6",
|
|
40
|
-
"eslint-config-salesforce-typescript": "^
|
|
42
|
+
"eslint-config-salesforce-typescript": "^1.1.1",
|
|
41
43
|
"eslint-plugin-header": "^3.0.0",
|
|
42
|
-
"eslint-plugin-import": "2.
|
|
43
|
-
"eslint-plugin-jsdoc": "^
|
|
44
|
+
"eslint-plugin-import": "2.26.0",
|
|
45
|
+
"eslint-plugin-jsdoc": "^39.3.6",
|
|
44
46
|
"eslint-plugin-prefer-arrow": "^1.2.1",
|
|
45
|
-
"
|
|
46
|
-
"mocha": "^
|
|
47
|
+
"husky": "^7.0.4",
|
|
48
|
+
"mocha": "^9.1.3",
|
|
47
49
|
"nyc": "^15.1.0",
|
|
48
|
-
"prettier": "^2.
|
|
50
|
+
"prettier": "^2.7.1",
|
|
49
51
|
"pretty-quick": "^3.1.0",
|
|
50
52
|
"sinon": "10.0.0",
|
|
51
53
|
"ts-node": "^10.0.0",
|
|
52
|
-
"typescript": "^4.
|
|
54
|
+
"typescript": "^4.8.4"
|
|
53
55
|
}
|
|
54
|
-
}
|
|
56
|
+
}
|