@leyyo/common 1.0.4 → 1.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/dist/assertion/common-assertion.d.ts +54 -0
- package/dist/assertion/common-assertion.js +180 -0
- package/dist/assertion/common-assertion.js.map +1 -0
- package/dist/assertion/index-types.d.ts +154 -159
- package/dist/assertion/index-types.js +1 -0
- package/dist/assertion/index-types.js.map +1 -0
- package/dist/assertion/index.d.ts +0 -1
- package/dist/assertion/index.js +1 -1
- package/dist/assertion/index.js.map +1 -0
- package/dist/descriptor/common-descriptor.d.ts +19 -0
- package/dist/descriptor/common-descriptor.js +92 -0
- package/dist/descriptor/common-descriptor.js.map +1 -0
- package/dist/descriptor/index-types.d.ts +21 -0
- package/dist/{literal → descriptor}/index-types.js +1 -0
- package/dist/descriptor/index-types.js.map +1 -0
- package/dist/descriptor/index.d.ts +1 -0
- package/dist/{storage → descriptor}/index.js +1 -1
- package/dist/descriptor/index.js.map +1 -0
- package/dist/developer/common-developer.d.ts +29 -0
- package/dist/developer/common-developer.js +266 -0
- package/dist/developer/common-developer.js.map +1 -0
- package/dist/developer/index-types.d.ts +77 -0
- package/dist/{storage → developer}/index-types.js +1 -0
- package/dist/developer/index-types.js.map +1 -0
- package/dist/developer/index.d.ts +1 -0
- package/dist/developer/index.js +18 -0
- package/dist/developer/index.js.map +1 -0
- package/dist/error/{common-error-impl.d.ts → common-error.d.ts} +5 -5
- package/dist/error/common-error.js +104 -0
- package/dist/error/common-error.js.map +1 -0
- package/dist/error/index-types.d.ts +21 -2
- package/dist/error/index-types.js +1 -0
- package/dist/error/index-types.js.map +1 -0
- package/dist/error/index.d.ts +0 -1
- package/dist/error/index.js +1 -1
- package/dist/error/index.js.map +1 -0
- package/dist/exception/assertion-exception.d.ts +2 -2
- package/dist/exception/assertion-exception.js +3 -2
- package/dist/exception/assertion-exception.js.map +1 -0
- package/dist/exception/caused-exception.d.ts +2 -2
- package/dist/exception/caused-exception.js +3 -2
- package/dist/exception/caused-exception.js.map +1 -0
- package/dist/exception/developer-exception.d.ts +2 -2
- package/dist/exception/developer-exception.js +3 -2
- package/dist/exception/developer-exception.js.map +1 -0
- package/dist/exception/exception.d.ts +7 -5
- package/dist/exception/exception.js +14 -4
- package/dist/exception/exception.js.map +1 -0
- package/dist/exception/index-types.d.ts +1 -1
- package/dist/exception/index-types.js +1 -0
- package/dist/exception/index-types.js.map +1 -0
- package/dist/exception/index.d.ts +1 -0
- package/dist/exception/index.js +2 -0
- package/dist/exception/index.js.map +1 -0
- package/dist/exception/invalid-value-exception.d.ts +5 -0
- package/dist/exception/invalid-value-exception.js +12 -0
- package/dist/exception/invalid-value-exception.js.map +1 -0
- package/dist/exception/multiple-exception.js +1 -0
- package/dist/exception/multiple-exception.js.map +1 -0
- package/dist/fqn/common-fqn.d.ts +20 -0
- package/dist/fqn/common-fqn.js +116 -0
- package/dist/fqn/common-fqn.js.map +1 -0
- package/dist/fqn/index-types.d.ts +14 -5
- package/dist/fqn/index-types.js +1 -0
- package/dist/fqn/index-types.js.map +1 -0
- package/dist/fqn/index.d.ts +0 -1
- package/dist/fqn/index.js +1 -1
- package/dist/fqn/index.js.map +1 -0
- package/dist/hook/{common-hook-impl.d.ts → common-hook.d.ts} +12 -10
- package/dist/hook/{common-hook-impl.js → common-hook.js} +27 -21
- package/dist/hook/common-hook.js.map +1 -0
- package/dist/hook/index-types.d.ts +29 -14
- package/dist/hook/index-types.js +1 -0
- package/dist/hook/index-types.js.map +1 -0
- package/dist/hook/index.d.ts +0 -1
- package/dist/hook/index.js +1 -1
- package/dist/hook/index.js.map +1 -0
- package/dist/index.d.ts +27 -11
- package/dist/index.js +30 -20
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +2 -0
- package/dist/internal.js +4 -1
- package/dist/internal.js.map +1 -0
- package/dist/is/{common-is-impl.d.ts → common-is.d.ts} +19 -5
- package/dist/is/common-is.js +136 -0
- package/dist/is/common-is.js.map +1 -0
- package/dist/is/index-types.d.ts +48 -3
- package/dist/is/index-types.js +1 -0
- package/dist/is/index-types.js.map +1 -0
- package/dist/is/index.d.ts +0 -1
- package/dist/is/index.js +1 -1
- package/dist/is/index.js.map +1 -0
- package/dist/leyyo/index-types.d.ts +30 -17
- package/dist/leyyo/index-types.js +1 -0
- package/dist/leyyo/index-types.js.map +1 -0
- package/dist/leyyo/index.d.ts +1 -1
- package/dist/leyyo/index.js +2 -1
- package/dist/leyyo/index.js.map +1 -0
- package/dist/leyyo/leyyo-common-hook.d.ts +10 -0
- package/dist/leyyo/leyyo-common-hook.js +18 -0
- package/dist/leyyo/leyyo-common-hook.js.map +1 -0
- package/dist/leyyo/leyyo.d.ts +35 -0
- package/dist/leyyo/leyyo.js +77 -0
- package/dist/leyyo/leyyo.js.map +1 -0
- package/dist/log/common-log.d.ts +14 -0
- package/dist/log/common-log.js +92 -0
- package/dist/log/common-log.js.map +1 -0
- package/dist/log/index-types.d.ts +57 -3
- package/dist/log/index-types.js +1 -0
- package/dist/log/index-types.js.map +1 -0
- package/dist/log/index.d.ts +2 -2
- package/dist/log/index.js +3 -2
- package/dist/log/index.js.map +1 -0
- package/dist/log/logger-instance.d.ts +31 -0
- package/dist/log/logger-instance.js +114 -0
- package/dist/log/logger-instance.js.map +1 -0
- package/dist/{literal → log}/severity.js +1 -0
- package/dist/log/severity.js.map +1 -0
- package/dist/repo/common-repo.d.ts +44 -0
- package/dist/repo/common-repo.js +180 -0
- package/dist/repo/common-repo.js.map +1 -0
- package/dist/repo/index-types.d.ts +104 -0
- package/dist/{shared/index-aliases.js → repo/index-types.js} +1 -2
- package/dist/repo/index-types.js.map +1 -0
- package/dist/repo/index.d.ts +2 -0
- package/dist/repo/index.js +19 -0
- package/dist/repo/index.js.map +1 -0
- package/dist/repo/repo-type.d.ts +8 -0
- package/dist/repo/repo-type.js +8 -0
- package/dist/repo/repo-type.js.map +1 -0
- package/dist/server.d.ts +1 -0
- package/dist/server.js +5 -0
- package/dist/server.js.map +1 -0
- package/dist/shared/index-types.d.ts +162 -132
- package/dist/shared/index-types.js +2 -1
- package/dist/shared/index-types.js.map +1 -0
- package/dist/shared/index.d.ts +0 -4
- package/dist/shared/index.js +1 -4
- package/dist/shared/index.js.map +1 -0
- package/dist/system/common-system.d.ts +10 -0
- package/dist/system/common-system.js +65 -0
- package/dist/system/common-system.js.map +1 -0
- package/dist/{literal → system}/country-code.js +1 -0
- package/dist/system/country-code.js.map +1 -0
- package/dist/{literal → system}/environment.js +1 -0
- package/dist/system/environment.js.map +1 -0
- package/dist/{literal → system}/http-method.js +1 -0
- package/dist/system/http-method.js.map +1 -0
- package/dist/{literal → system}/http-place.js +1 -0
- package/dist/system/http-place.js.map +1 -0
- package/dist/system/index-types.d.ts +6 -0
- package/dist/system/index-types.js +3 -0
- package/dist/system/index-types.js.map +1 -0
- package/dist/{literal → system}/index.d.ts +1 -8
- package/dist/{literal → system}/index.js +2 -8
- package/dist/system/index.js.map +1 -0
- package/dist/{literal → system}/language-code.js +1 -0
- package/dist/system/language-code.js.map +1 -0
- package/dist/{literal → system}/locale-code.js +1 -0
- package/dist/system/locale-code.js.map +1 -0
- package/dist/{literal → system}/sys-class.js +1 -0
- package/dist/system/sys-class.js.map +1 -0
- package/dist/{literal → system}/sys-function.js +1 -0
- package/dist/system/sys-function.js.map +1 -0
- package/dist/to/common-to.d.ts +45 -0
- package/dist/to/common-to.js +868 -0
- package/dist/to/common-to.js.map +1 -0
- package/dist/to/index-types.d.ts +38 -18
- package/dist/to/index-types.js +1 -0
- package/dist/to/index-types.js.map +1 -0
- package/dist/to/index.d.ts +6 -1
- package/dist/to/index.js +7 -1
- package/dist/to/index.js.map +1 -0
- package/dist/{literal → to}/key-value.js +1 -0
- package/dist/to/key-value.js.map +1 -0
- package/dist/{shared → to}/list.d.ts +1 -0
- package/dist/{shared → to}/list.js +4 -0
- package/dist/to/list.js.map +1 -0
- package/dist/{literal → to}/primitive.js +1 -0
- package/dist/to/primitive.js.map +1 -0
- package/dist/{literal → to}/real-value.js +1 -0
- package/dist/to/real-value.js.map +1 -0
- package/dist/{literal → to}/weak-false.js +1 -0
- package/dist/to/weak-false.js.map +1 -0
- package/dist/{literal → to}/weak-true.js +1 -0
- package/dist/to/weak-true.js.map +1 -0
- package/dist/wrapper/common-wrapper.d.ts +26 -0
- package/dist/wrapper/common-wrapper.js +81 -0
- package/dist/wrapper/common-wrapper.js.map +1 -0
- package/dist/wrapper/index-types.d.ts +29 -0
- package/dist/wrapper/index-types.js +3 -0
- package/dist/wrapper/index-types.js.map +1 -0
- package/dist/wrapper/index.d.ts +2 -0
- package/dist/wrapper/index.js +19 -0
- package/dist/wrapper/index.js.map +1 -0
- package/dist/wrapper/wrap.d.ts +7 -0
- package/dist/wrapper/wrap.js +11 -0
- package/dist/wrapper/wrap.js.map +1 -0
- package/package.json +13 -15
- package/dist/assertion/common-assertion-impl.d.ts +0 -65
- package/dist/assertion/common-assertion-impl.js +0 -248
- package/dist/error/common-error-impl.js +0 -92
- package/dist/fqn/common-fqn-impl.d.ts +0 -18
- package/dist/fqn/common-fqn-impl.js +0 -121
- package/dist/is/common-is-impl.js +0 -91
- package/dist/leyyo/leyyo-impl.d.ts +0 -26
- package/dist/leyyo/leyyo-impl.js +0 -94
- package/dist/literal/index-types.d.ts +0 -6
- package/dist/literal/storage-type.d.ts +0 -8
- package/dist/literal/storage-type.js +0 -7
- package/dist/log/common-log-impl.d.ts +0 -14
- package/dist/log/common-log-impl.js +0 -59
- package/dist/log/logger-impl.d.ts +0 -23
- package/dist/log/logger-impl.js +0 -77
- package/dist/shared/assert.d.ts +0 -6
- package/dist/shared/assert.js +0 -14
- package/dist/shared/index-aliases.d.ts +0 -167
- package/dist/shared/index-constants.d.ts +0 -8
- package/dist/shared/index-constants.js +0 -11
- package/dist/storage/common-storage-impl.d.ts +0 -47
- package/dist/storage/common-storage-impl.js +0 -150
- package/dist/storage/index-types.d.ts +0 -87
- package/dist/storage/index.d.ts +0 -2
- package/dist/to/common-to-impl.d.ts +0 -38
- package/dist/to/common-to-impl.js +0 -553
- /package/dist/{literal → log}/severity.d.ts +0 -0
- /package/dist/{literal → system}/country-code.d.ts +0 -0
- /package/dist/{literal → system}/environment.d.ts +0 -0
- /package/dist/{literal → system}/http-method.d.ts +0 -0
- /package/dist/{literal → system}/http-place.d.ts +0 -0
- /package/dist/{literal → system}/language-code.d.ts +0 -0
- /package/dist/{literal → system}/locale-code.d.ts +0 -0
- /package/dist/{literal → system}/sys-class.d.ts +0 -0
- /package/dist/{literal → system}/sys-function.d.ts +0 -0
- /package/dist/{literal → to}/key-value.d.ts +0 -0
- /package/dist/{literal → to}/primitive.d.ts +0 -0
- /package/dist/{literal → to}/real-value.d.ts +0 -0
- /package/dist/{literal → to}/weak-false.d.ts +0 -0
- /package/dist/{literal → to}/weak-true.d.ts +0 -0
|
@@ -0,0 +1,868 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CommonTo = void 0;
|
|
4
|
+
const leyyo_1 = require("../leyyo");
|
|
5
|
+
const internal_1 = require("../internal");
|
|
6
|
+
const primitive_1 = require("./primitive");
|
|
7
|
+
const real_value_1 = require("./real-value");
|
|
8
|
+
const key_value_1 = require("./key-value");
|
|
9
|
+
const weak_true_1 = require("./weak-true");
|
|
10
|
+
const weak_false_1 = require("./weak-false");
|
|
11
|
+
const list_1 = require("./list");
|
|
12
|
+
// noinspection JSUnusedGlobalSymbols
|
|
13
|
+
class CommonTo {
|
|
14
|
+
constructor() {
|
|
15
|
+
// region properties
|
|
16
|
+
this._EMPTY = [null, undefined];
|
|
17
|
+
this._EXPECTED_ANY = ['string', 'boolean', 'bigint', 'object', 'number', 'array'];
|
|
18
|
+
this._EXPECTED_ARRAY = ['array', 'Set'];
|
|
19
|
+
this._EXPECTED_BOOL = ['boolean', 'string', 'number'];
|
|
20
|
+
this._EXPECTED_DATE = ['string', 'number', 'date', 'moment'];
|
|
21
|
+
this._EXPECTED_ENUM = ['string', 'number'];
|
|
22
|
+
this._EXPECTED_NUMBER = ['string', 'number', 'bigint'];
|
|
23
|
+
this._EXPECTED_STRING = ['boolean', 'string', 'number'];
|
|
24
|
+
this._EXPECTED_OBJECT = ['object', 'Map'];
|
|
25
|
+
// endregion types
|
|
26
|
+
}
|
|
27
|
+
// endregion properties
|
|
28
|
+
// region private
|
|
29
|
+
_enumInMap(value, map) {
|
|
30
|
+
// regular, in values
|
|
31
|
+
if (Object.values(map).includes(value)) {
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
// ir-regular, in keys
|
|
35
|
+
if (Object.keys(map).includes(value)) {
|
|
36
|
+
return map[value];
|
|
37
|
+
}
|
|
38
|
+
if (typeof value === 'string') {
|
|
39
|
+
// regular, in lower-case
|
|
40
|
+
let str = value.toLowerCase();
|
|
41
|
+
let v = this._enumInMap(str, map);
|
|
42
|
+
if (!this._EMPTY.includes(v)) {
|
|
43
|
+
return v;
|
|
44
|
+
}
|
|
45
|
+
// regular, in upper-case
|
|
46
|
+
str = str.toUpperCase();
|
|
47
|
+
v = this._enumInMap(str, map);
|
|
48
|
+
if (!this._EMPTY.includes(v)) {
|
|
49
|
+
return v;
|
|
50
|
+
}
|
|
51
|
+
if (/^[0-9]+$/.test(value)) {
|
|
52
|
+
try {
|
|
53
|
+
return this._enumInMap(parseInt(value, 10), map);
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
_enumInArray(value, arr) {
|
|
63
|
+
// regular, in values
|
|
64
|
+
if (arr.includes(value)) {
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
if (typeof value === 'string') {
|
|
68
|
+
// regular, in lower-case
|
|
69
|
+
let str = value.toLowerCase();
|
|
70
|
+
let v = this._enumInArray(str, arr);
|
|
71
|
+
if (!this._EMPTY.includes(v)) {
|
|
72
|
+
return v;
|
|
73
|
+
}
|
|
74
|
+
// regular, in upper-case
|
|
75
|
+
str = str.toUpperCase();
|
|
76
|
+
v = this._enumInArray(str, arr);
|
|
77
|
+
if (!this._EMPTY.includes(v)) {
|
|
78
|
+
return v;
|
|
79
|
+
}
|
|
80
|
+
if (/^[0-9]+$/.test(value)) {
|
|
81
|
+
try {
|
|
82
|
+
return this._enumInArray(parseInt(value, 10), arr);
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
// noinspection JSMethodCanBeStatic
|
|
92
|
+
_enumInAlteration(value, alt) {
|
|
93
|
+
// ir-regular, in keys
|
|
94
|
+
if (Object.keys(alt).includes(value)) {
|
|
95
|
+
return alt[value];
|
|
96
|
+
}
|
|
97
|
+
if (typeof value === 'string') {
|
|
98
|
+
// regular, in lower-case
|
|
99
|
+
let str = value.toLowerCase();
|
|
100
|
+
let v = this._enumInAlteration(str, alt);
|
|
101
|
+
if (!this._EMPTY.includes(v)) {
|
|
102
|
+
return v;
|
|
103
|
+
}
|
|
104
|
+
// regular, in upper-case
|
|
105
|
+
str = str.toUpperCase();
|
|
106
|
+
v = this._enumInAlteration(str, alt);
|
|
107
|
+
if (!this._EMPTY.includes(v)) {
|
|
108
|
+
return v;
|
|
109
|
+
}
|
|
110
|
+
if (/^[0-9]+$/.test(value)) {
|
|
111
|
+
try {
|
|
112
|
+
return this._enumInAlteration(parseInt(value, 10), alt);
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
// endregion private
|
|
122
|
+
// region secure
|
|
123
|
+
$init(leyyo) {
|
|
124
|
+
this.is = leyyo.is;
|
|
125
|
+
this.dev = leyyo.dev;
|
|
126
|
+
this.wrapper = leyyo.wrapper;
|
|
127
|
+
leyyo.$secure.$lazyRun(() => {
|
|
128
|
+
const enumMap = {
|
|
129
|
+
Primitive: primitive_1.PrimitiveItems,
|
|
130
|
+
RealValue: real_value_1.RealValueItems,
|
|
131
|
+
KeyValue: key_value_1.KeyValueItems,
|
|
132
|
+
WeakTrue: weak_true_1.WeakTrueItems,
|
|
133
|
+
WeakFalse: weak_false_1.WeakFalseItems,
|
|
134
|
+
};
|
|
135
|
+
for (const [name, value] of Object.entries(enumMap)) {
|
|
136
|
+
leyyo.fqn.register(name, value, 'enum', internal_1.FQN_PCK);
|
|
137
|
+
leyyo.hook.queueForCallback(leyyo_1.LeyyoCommonHook.enumPendingRegister, value);
|
|
138
|
+
}
|
|
139
|
+
}).$lazyRun(() => {
|
|
140
|
+
leyyo.fqn.register(null, CommonTo, 'class', internal_1.FQN_PCK);
|
|
141
|
+
leyyo.fqn.register(null, list_1.List, 'class', internal_1.FQN_PCK);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
get $back() {
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
get $secure() {
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
$runOpt(opt) {
|
|
151
|
+
if (typeof opt === 'function') {
|
|
152
|
+
try {
|
|
153
|
+
return opt();
|
|
154
|
+
}
|
|
155
|
+
catch (e) {
|
|
156
|
+
const key = `<error>[${Date.now()}]`;
|
|
157
|
+
return {
|
|
158
|
+
[key]: [e.message, e.name],
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else if (!this._EMPTY.includes(opt) || typeof opt !== 'object' || opt.constructor !== Object) {
|
|
163
|
+
const key = `<invalid-opt>[${Date.now()}]`;
|
|
164
|
+
return {
|
|
165
|
+
[key]: this.dev.secureJson(opt),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
return opt;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
$runFn(fn, value, opt) {
|
|
173
|
+
try {
|
|
174
|
+
return fn(value());
|
|
175
|
+
}
|
|
176
|
+
catch (e) {
|
|
177
|
+
return this.$errorOrLog(opt, undefined, e);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
$errorOrLog(opt, extra, e) {
|
|
181
|
+
var _a;
|
|
182
|
+
const newOpt = (_a = this.$runOpt(opt)) !== null && _a !== void 0 ? _a : {};
|
|
183
|
+
if (!newOpt.silent) {
|
|
184
|
+
delete newOpt.silent;
|
|
185
|
+
if (e instanceof Error) {
|
|
186
|
+
this.dev.native(e, newOpt, extra);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
this.dev.raise(newOpt, extra);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
if (e instanceof Error) {
|
|
194
|
+
this.dev.log(e, newOpt, 'warn');
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
this.dev.log(newOpt, extra, 'warn');
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
$unexpectedError(value, expected, opt) {
|
|
203
|
+
return this.$errorOrLog(opt, { expected, type: typeof value, value });
|
|
204
|
+
}
|
|
205
|
+
// endregion secure
|
|
206
|
+
// region types
|
|
207
|
+
realNumber(value, opt) {
|
|
208
|
+
if (isNaN(value) || !isFinite(value)) {
|
|
209
|
+
return this.$errorOrLog(opt, { issue: 'invalid.real.number', value, type: typeof value });
|
|
210
|
+
}
|
|
211
|
+
return value;
|
|
212
|
+
}
|
|
213
|
+
any(value, opt) {
|
|
214
|
+
if (this._EMPTY.includes(value)) {
|
|
215
|
+
return value;
|
|
216
|
+
}
|
|
217
|
+
switch (typeof value) {
|
|
218
|
+
case 'string':
|
|
219
|
+
case 'boolean':
|
|
220
|
+
case 'bigint':
|
|
221
|
+
case 'object':
|
|
222
|
+
return value;
|
|
223
|
+
case 'number':
|
|
224
|
+
return this.realNumber(value, opt);
|
|
225
|
+
case 'function':
|
|
226
|
+
return this.$runFn(v => this.any(v, opt), value, opt);
|
|
227
|
+
}
|
|
228
|
+
return this.$unexpectedError(value, this._EXPECTED_ANY, opt);
|
|
229
|
+
}
|
|
230
|
+
list(value, opt, itemFn) {
|
|
231
|
+
if (this._EMPTY.includes(value)) {
|
|
232
|
+
return value;
|
|
233
|
+
}
|
|
234
|
+
switch (typeof value) {
|
|
235
|
+
case "string":
|
|
236
|
+
case "boolean":
|
|
237
|
+
case "number":
|
|
238
|
+
case "bigint":
|
|
239
|
+
return this.list([value], opt, itemFn);
|
|
240
|
+
case 'object':
|
|
241
|
+
if (value instanceof Set) {
|
|
242
|
+
const newList = new list_1.List();
|
|
243
|
+
if (value.size < 1) {
|
|
244
|
+
return newList;
|
|
245
|
+
}
|
|
246
|
+
if (typeof itemFn !== "function") {
|
|
247
|
+
newList.push(...Array.from(value.values()));
|
|
248
|
+
return newList;
|
|
249
|
+
}
|
|
250
|
+
const clonedArr = this.array(Array.from(value.values()), opt, itemFn);
|
|
251
|
+
if (clonedArr) {
|
|
252
|
+
newList.push(...clonedArr);
|
|
253
|
+
}
|
|
254
|
+
return newList;
|
|
255
|
+
}
|
|
256
|
+
else if (value instanceof list_1.List) {
|
|
257
|
+
if (typeof itemFn !== "function" || value.length < 1) {
|
|
258
|
+
return value;
|
|
259
|
+
}
|
|
260
|
+
const clonedArr = this.array([...value], opt, itemFn);
|
|
261
|
+
value.clear();
|
|
262
|
+
if (clonedArr) {
|
|
263
|
+
value.push(...clonedArr);
|
|
264
|
+
}
|
|
265
|
+
return value;
|
|
266
|
+
}
|
|
267
|
+
if (Array.isArray(value)) {
|
|
268
|
+
const newList = new list_1.List();
|
|
269
|
+
if (value.length < 1) {
|
|
270
|
+
return newList;
|
|
271
|
+
}
|
|
272
|
+
if (typeof itemFn !== "function") {
|
|
273
|
+
newList.push(...value);
|
|
274
|
+
return newList;
|
|
275
|
+
}
|
|
276
|
+
const clonedArr = this.array(value, opt, itemFn);
|
|
277
|
+
if (clonedArr) {
|
|
278
|
+
newList.push(...clonedArr);
|
|
279
|
+
}
|
|
280
|
+
return newList;
|
|
281
|
+
}
|
|
282
|
+
break;
|
|
283
|
+
case 'function':
|
|
284
|
+
return this.$runFn(v => this.set(v, opt, itemFn), value, opt);
|
|
285
|
+
}
|
|
286
|
+
return this.$unexpectedError(value, this._EXPECTED_ARRAY, opt);
|
|
287
|
+
}
|
|
288
|
+
set(value, opt, itemFn) {
|
|
289
|
+
if (this._EMPTY.includes(value)) {
|
|
290
|
+
return value;
|
|
291
|
+
}
|
|
292
|
+
switch (typeof value) {
|
|
293
|
+
case "string":
|
|
294
|
+
case "boolean":
|
|
295
|
+
case "number":
|
|
296
|
+
case "bigint":
|
|
297
|
+
return this.set([value], opt, itemFn);
|
|
298
|
+
case 'object':
|
|
299
|
+
if (value instanceof Set) {
|
|
300
|
+
if (typeof itemFn !== "function" || value.size < 1) {
|
|
301
|
+
return value;
|
|
302
|
+
}
|
|
303
|
+
const clonedArr = this.array(Array.from(value.values()), opt, itemFn);
|
|
304
|
+
value.clear();
|
|
305
|
+
if (clonedArr) {
|
|
306
|
+
clonedArr.forEach(item => value.add(item));
|
|
307
|
+
}
|
|
308
|
+
return value;
|
|
309
|
+
}
|
|
310
|
+
else if (value instanceof list_1.List) {
|
|
311
|
+
if (value.length < 1) {
|
|
312
|
+
return new Set();
|
|
313
|
+
}
|
|
314
|
+
if (typeof itemFn !== "function") {
|
|
315
|
+
return new Set([...value]);
|
|
316
|
+
}
|
|
317
|
+
return new Set(this.array([...value], opt, itemFn));
|
|
318
|
+
}
|
|
319
|
+
if (Array.isArray(value)) {
|
|
320
|
+
if (value.length < 1) {
|
|
321
|
+
return new Set();
|
|
322
|
+
}
|
|
323
|
+
if (typeof itemFn !== "function") {
|
|
324
|
+
return new Set(value);
|
|
325
|
+
}
|
|
326
|
+
return new Set(this.array(value, opt, itemFn));
|
|
327
|
+
}
|
|
328
|
+
break;
|
|
329
|
+
case 'function':
|
|
330
|
+
return this.$runFn(v => this.set(v, opt, itemFn), value, opt);
|
|
331
|
+
}
|
|
332
|
+
return this.$unexpectedError(value, this._EXPECTED_ARRAY, opt);
|
|
333
|
+
}
|
|
334
|
+
array(value, opt, itemFn) {
|
|
335
|
+
if (this._EMPTY.includes(value)) {
|
|
336
|
+
return value;
|
|
337
|
+
}
|
|
338
|
+
switch (typeof value) {
|
|
339
|
+
case "string":
|
|
340
|
+
case "boolean":
|
|
341
|
+
case "number":
|
|
342
|
+
case "bigint":
|
|
343
|
+
return this.array([value], opt, itemFn);
|
|
344
|
+
case 'object':
|
|
345
|
+
if (value instanceof Set) {
|
|
346
|
+
if (value.size < 1) {
|
|
347
|
+
return [];
|
|
348
|
+
}
|
|
349
|
+
return this.array(Array.from(value.values()), opt, itemFn);
|
|
350
|
+
}
|
|
351
|
+
else if (value instanceof list_1.List) {
|
|
352
|
+
if (value.length < 1) {
|
|
353
|
+
return [];
|
|
354
|
+
}
|
|
355
|
+
return this.array([...value], opt, itemFn);
|
|
356
|
+
}
|
|
357
|
+
if (Array.isArray(value)) {
|
|
358
|
+
if (typeof itemFn !== "function" || value.length < 1) {
|
|
359
|
+
return value;
|
|
360
|
+
}
|
|
361
|
+
const newOpt = this.$runOpt(opt);
|
|
362
|
+
const field = (typeof newOpt.field === 'string') ? newOpt.field : undefined;
|
|
363
|
+
const clonedOpt = Object.assign({}, newOpt);
|
|
364
|
+
return value.map((v, index) => {
|
|
365
|
+
clonedOpt.field = field ? `${field}#${index}` : `#${index}`;
|
|
366
|
+
try {
|
|
367
|
+
return itemFn(v, index, clonedOpt);
|
|
368
|
+
}
|
|
369
|
+
catch (e) {
|
|
370
|
+
if (newOpt.silent) {
|
|
371
|
+
this.dev.log(e, clonedOpt, 'warn');
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
this.dev.raise(e, clonedOpt);
|
|
375
|
+
}
|
|
376
|
+
return v;
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
return this.array([value], opt, itemFn);
|
|
381
|
+
case 'function':
|
|
382
|
+
return this.$runFn(v => this.array(v, opt, itemFn), value, opt);
|
|
383
|
+
}
|
|
384
|
+
return this.$unexpectedError(value, this._EXPECTED_ARRAY, opt);
|
|
385
|
+
}
|
|
386
|
+
boolean(value, opt) {
|
|
387
|
+
if (this._EMPTY.includes(value)) {
|
|
388
|
+
return value;
|
|
389
|
+
}
|
|
390
|
+
switch (typeof value) {
|
|
391
|
+
case 'boolean':
|
|
392
|
+
return value;
|
|
393
|
+
case 'string':
|
|
394
|
+
value = value.trim().toLowerCase();
|
|
395
|
+
if (value === '') {
|
|
396
|
+
return undefined;
|
|
397
|
+
}
|
|
398
|
+
if (weak_true_1.WeakTrueItems.includes(value)) {
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
if (weak_false_1.WeakFalseItems.includes(value)) {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
return this.$errorOrLog(opt, { issue: 'invalid.bool.text', value });
|
|
405
|
+
case 'number':
|
|
406
|
+
return value > 0;
|
|
407
|
+
case 'object':
|
|
408
|
+
if (Array.isArray(value) && value.length === 1) {
|
|
409
|
+
return this.boolean(value[0], opt);
|
|
410
|
+
}
|
|
411
|
+
break;
|
|
412
|
+
case 'function':
|
|
413
|
+
return this.$runFn(v => this.boolean(v, opt), value, opt);
|
|
414
|
+
}
|
|
415
|
+
return this.$unexpectedError(value, this._EXPECTED_BOOL, opt);
|
|
416
|
+
}
|
|
417
|
+
date(value, opt) {
|
|
418
|
+
if (this._EMPTY.includes(value)) {
|
|
419
|
+
return value;
|
|
420
|
+
}
|
|
421
|
+
let fn;
|
|
422
|
+
switch (typeof value) {
|
|
423
|
+
case 'object':
|
|
424
|
+
if (value instanceof Date) {
|
|
425
|
+
return value;
|
|
426
|
+
}
|
|
427
|
+
else if (typeof value['toDate'] === 'function') {
|
|
428
|
+
fn = () => value['toDate']();
|
|
429
|
+
return this.$runFn(v => this.date(v, opt), fn, opt);
|
|
430
|
+
}
|
|
431
|
+
if (Array.isArray(value)) {
|
|
432
|
+
const arr = value;
|
|
433
|
+
if (arr.length === 1) {
|
|
434
|
+
return this.date(arr[0], opt);
|
|
435
|
+
}
|
|
436
|
+
if (arr.length > 1 && arr.length < 8) {
|
|
437
|
+
const [year, monthIndex, date, hours, minutes, seconds, ms] = arr;
|
|
438
|
+
fn = () => new Date(year, monthIndex, date, hours, minutes, seconds, ms);
|
|
439
|
+
return this.$runFn(v => this.date(v, opt), fn, opt);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
else if (value.id !== undefined) {
|
|
443
|
+
return this.date(value.id, opt);
|
|
444
|
+
}
|
|
445
|
+
break;
|
|
446
|
+
case 'string':
|
|
447
|
+
if (value.trim() === '') {
|
|
448
|
+
return undefined;
|
|
449
|
+
}
|
|
450
|
+
fn = () => new Date(value.trim());
|
|
451
|
+
return this.$runFn(v => this.date(v, opt), fn, opt);
|
|
452
|
+
case 'bigint':
|
|
453
|
+
fn = () => new Date(value.toString(10));
|
|
454
|
+
return this.$runFn(v => this.date(v, opt), fn, opt);
|
|
455
|
+
case 'number':
|
|
456
|
+
fn = () => new Date(value);
|
|
457
|
+
return this.$runFn(v => this.date(v, opt), fn, opt);
|
|
458
|
+
case 'function':
|
|
459
|
+
return this.$runFn(v => this.date(v, opt), value, opt);
|
|
460
|
+
}
|
|
461
|
+
return this.$unexpectedError(value, this._EXPECTED_DATE, opt);
|
|
462
|
+
}
|
|
463
|
+
enumeration(value, map, opt, alt) {
|
|
464
|
+
if (this._EMPTY.includes(value)) {
|
|
465
|
+
return value;
|
|
466
|
+
}
|
|
467
|
+
if (!this.is.bareObject(map)) {
|
|
468
|
+
return this.$errorOrLog(opt, { issue: 'invalid.enum.map', type: typeof map, map });
|
|
469
|
+
}
|
|
470
|
+
let v;
|
|
471
|
+
switch (typeof value) {
|
|
472
|
+
case 'string':
|
|
473
|
+
value = value.trim();
|
|
474
|
+
if (value === '') {
|
|
475
|
+
return undefined;
|
|
476
|
+
}
|
|
477
|
+
v = this._enumInMap(value, map);
|
|
478
|
+
if (!this._EMPTY.includes(v)) {
|
|
479
|
+
return v;
|
|
480
|
+
}
|
|
481
|
+
if (this.is.bareObject(alt)) {
|
|
482
|
+
v = this._enumInAlteration(value, alt);
|
|
483
|
+
if (!this._EMPTY.includes(v)) {
|
|
484
|
+
return v;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
return this.$unexpectedError(value, this._EXPECTED_ENUM, opt);
|
|
488
|
+
case 'number':
|
|
489
|
+
const num = this.realNumber(value, opt);
|
|
490
|
+
if (!this._EMPTY.includes(num)) {
|
|
491
|
+
return null;
|
|
492
|
+
}
|
|
493
|
+
v = this._enumInMap(value, map);
|
|
494
|
+
if (!this._EMPTY.includes(v)) {
|
|
495
|
+
return v;
|
|
496
|
+
}
|
|
497
|
+
if (this.is.bareObject(alt)) {
|
|
498
|
+
v = this._enumInAlteration(value, alt);
|
|
499
|
+
if (!this._EMPTY.includes(v)) {
|
|
500
|
+
return v;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return this.$unexpectedError(value, this._EXPECTED_ENUM, opt);
|
|
504
|
+
case 'object':
|
|
505
|
+
if (Array.isArray(value) && value.length === 1) {
|
|
506
|
+
return this.enumeration(value[0], map, opt, alt);
|
|
507
|
+
}
|
|
508
|
+
return this.enumeration(value.id, map, opt, alt);
|
|
509
|
+
case 'function':
|
|
510
|
+
return this.$runFn(v => this.enumeration(v, map, opt, alt), value, opt);
|
|
511
|
+
}
|
|
512
|
+
return this.$unexpectedError(value, this._EXPECTED_ENUM, opt);
|
|
513
|
+
}
|
|
514
|
+
literal(value, items, opt, alt) {
|
|
515
|
+
if (this._EMPTY.includes(value)) {
|
|
516
|
+
return value;
|
|
517
|
+
}
|
|
518
|
+
if (!Array.isArray(items)) {
|
|
519
|
+
return this.$errorOrLog(opt, { issue: 'invalid.enum.literal', type: typeof items, items });
|
|
520
|
+
}
|
|
521
|
+
let v;
|
|
522
|
+
switch (typeof value) {
|
|
523
|
+
case 'string':
|
|
524
|
+
value = value.trim();
|
|
525
|
+
if (value === '') {
|
|
526
|
+
return undefined;
|
|
527
|
+
}
|
|
528
|
+
v = this._enumInArray(value, items);
|
|
529
|
+
if (!this._EMPTY.includes(v)) {
|
|
530
|
+
return v;
|
|
531
|
+
}
|
|
532
|
+
if (this.is.bareObject(alt)) {
|
|
533
|
+
v = this._enumInAlteration(value, alt);
|
|
534
|
+
if (!this._EMPTY.includes(v)) {
|
|
535
|
+
return v;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return this.$unexpectedError(value, this._EXPECTED_ENUM, opt);
|
|
539
|
+
case 'number':
|
|
540
|
+
const num = this.realNumber(value, opt);
|
|
541
|
+
if (!this._EMPTY.includes(num)) {
|
|
542
|
+
return null;
|
|
543
|
+
}
|
|
544
|
+
v = this._enumInArray(value, items);
|
|
545
|
+
if (!this._EMPTY.includes(v)) {
|
|
546
|
+
return v;
|
|
547
|
+
}
|
|
548
|
+
if (this.is.bareObject(alt)) {
|
|
549
|
+
v = this._enumInAlteration(value, alt);
|
|
550
|
+
if (!this._EMPTY.includes(v)) {
|
|
551
|
+
return v;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
return this.$unexpectedError(value, this._EXPECTED_ENUM, opt);
|
|
555
|
+
case 'object':
|
|
556
|
+
if (Array.isArray(value) && value.length === 1) {
|
|
557
|
+
return this.literal(value[0], items, opt, alt);
|
|
558
|
+
}
|
|
559
|
+
return this.literal(value.id, items, opt, alt);
|
|
560
|
+
case 'function':
|
|
561
|
+
return this.$runFn(v => this.literal(v, items, opt, alt), value, opt);
|
|
562
|
+
}
|
|
563
|
+
return this.$unexpectedError(value, this._EXPECTED_ENUM, opt);
|
|
564
|
+
}
|
|
565
|
+
float(value, opt) {
|
|
566
|
+
if (this._EMPTY.includes(value)) {
|
|
567
|
+
return value;
|
|
568
|
+
}
|
|
569
|
+
let fn;
|
|
570
|
+
switch (typeof value) {
|
|
571
|
+
case 'string':
|
|
572
|
+
fn = () => parseFloat(value);
|
|
573
|
+
return this.$runFn(v => this.float(v, opt), fn, opt);
|
|
574
|
+
case 'number':
|
|
575
|
+
return this.realNumber(value, opt);
|
|
576
|
+
case 'bigint':
|
|
577
|
+
fn = () => Number(value);
|
|
578
|
+
return this.$runFn(v => this.float(v, opt), fn, opt);
|
|
579
|
+
case 'boolean':
|
|
580
|
+
return value ? 1 : 0;
|
|
581
|
+
case 'object':
|
|
582
|
+
if (Array.isArray(value) && value.length === 1) {
|
|
583
|
+
return this.float(value[0], opt);
|
|
584
|
+
}
|
|
585
|
+
return this.float(value.id, opt);
|
|
586
|
+
case 'function':
|
|
587
|
+
return this.$runFn(v => this.float(v, opt), value, opt);
|
|
588
|
+
}
|
|
589
|
+
return this.$unexpectedError(value, this._EXPECTED_NUMBER, opt);
|
|
590
|
+
}
|
|
591
|
+
func(value, opt) {
|
|
592
|
+
if (this._EMPTY.includes(value)) {
|
|
593
|
+
return value;
|
|
594
|
+
}
|
|
595
|
+
switch (typeof value) {
|
|
596
|
+
case "function":
|
|
597
|
+
return value;
|
|
598
|
+
case 'object':
|
|
599
|
+
if (this.wrapper.isFunction(value)) {
|
|
600
|
+
return this.wrapper.asFunction(value);
|
|
601
|
+
}
|
|
602
|
+
if (Array.isArray(value) && value.length === 1) {
|
|
603
|
+
return this.func(value[0], opt);
|
|
604
|
+
}
|
|
605
|
+
break;
|
|
606
|
+
}
|
|
607
|
+
return this.$unexpectedError(value, ['function'], opt);
|
|
608
|
+
}
|
|
609
|
+
integer(value, opt) {
|
|
610
|
+
if (this._EMPTY.includes(value)) {
|
|
611
|
+
return value;
|
|
612
|
+
}
|
|
613
|
+
let fn;
|
|
614
|
+
switch (typeof value) {
|
|
615
|
+
case 'string':
|
|
616
|
+
fn = () => parseInt(value);
|
|
617
|
+
return this.$runFn(v => this.integer(v, opt), fn, opt);
|
|
618
|
+
case 'number':
|
|
619
|
+
let num = this.realNumber(value, opt);
|
|
620
|
+
if (num !== null && !Number.isSafeInteger(num)) {
|
|
621
|
+
num = Math.floor(num);
|
|
622
|
+
}
|
|
623
|
+
return num;
|
|
624
|
+
case 'bigint':
|
|
625
|
+
fn = () => Number(value);
|
|
626
|
+
return this.$runFn(v => this.integer(v, opt), fn, opt);
|
|
627
|
+
case 'boolean':
|
|
628
|
+
return value ? 1 : 0;
|
|
629
|
+
case 'object':
|
|
630
|
+
if (Array.isArray(value) && value.length === 1) {
|
|
631
|
+
return this.integer(value[0], opt);
|
|
632
|
+
}
|
|
633
|
+
return this.integer(value.id, opt);
|
|
634
|
+
case 'function':
|
|
635
|
+
return this.$runFn(v => this.integer(v, opt), value, opt);
|
|
636
|
+
}
|
|
637
|
+
return this.$unexpectedError(value, this._EXPECTED_NUMBER, opt);
|
|
638
|
+
}
|
|
639
|
+
object(value, opt) {
|
|
640
|
+
return this.dict(value, opt);
|
|
641
|
+
}
|
|
642
|
+
map(value, opt, valueFn, keyFn) {
|
|
643
|
+
if (this._EMPTY.includes(value)) {
|
|
644
|
+
return value;
|
|
645
|
+
}
|
|
646
|
+
switch (typeof value) {
|
|
647
|
+
case 'object':
|
|
648
|
+
if (value instanceof Set) {
|
|
649
|
+
if (value.size === 1) {
|
|
650
|
+
return this.map(Array.from(value.values())[0], opt, valueFn, keyFn);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
else if (value instanceof list_1.List) {
|
|
654
|
+
if (value.length === 1) {
|
|
655
|
+
return this.map(value[0], opt, valueFn, keyFn);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
else if (value instanceof Map) {
|
|
659
|
+
if (typeof valueFn !== "function" && typeof keyFn !== "function") {
|
|
660
|
+
return value;
|
|
661
|
+
}
|
|
662
|
+
if (value.size < 1) {
|
|
663
|
+
return value;
|
|
664
|
+
}
|
|
665
|
+
const newOpt = this.$runOpt(opt);
|
|
666
|
+
const field = (typeof newOpt.field === 'string') ? newOpt.field : undefined;
|
|
667
|
+
const clonedOpt = Object.assign({}, newOpt);
|
|
668
|
+
const cloned = Object.fromEntries(value.entries());
|
|
669
|
+
let index = -1;
|
|
670
|
+
value.clear();
|
|
671
|
+
for (const [k, v] of Object.entries(cloned)) {
|
|
672
|
+
if (typeof k === 'symbol') {
|
|
673
|
+
continue;
|
|
674
|
+
}
|
|
675
|
+
index++;
|
|
676
|
+
let newKey = k;
|
|
677
|
+
let newValue = v;
|
|
678
|
+
if (typeof keyFn === 'function') {
|
|
679
|
+
clonedOpt.field = field ? `${field}#${index}` : `#${index}`;
|
|
680
|
+
try {
|
|
681
|
+
newKey = keyFn(k, index, clonedOpt);
|
|
682
|
+
}
|
|
683
|
+
catch (e) {
|
|
684
|
+
this.$errorOrLog(clonedOpt, undefined, e);
|
|
685
|
+
}
|
|
686
|
+
if (this._EMPTY.includes(newKey)) {
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
if (typeof valueFn === 'function') {
|
|
691
|
+
clonedOpt.field = field ? `${field}.${k}` : k;
|
|
692
|
+
try {
|
|
693
|
+
newValue = valueFn(v, k, clonedOpt);
|
|
694
|
+
}
|
|
695
|
+
catch (e) {
|
|
696
|
+
this.$errorOrLog(clonedOpt, undefined, e);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
value.set(newKey, newValue);
|
|
700
|
+
}
|
|
701
|
+
return value;
|
|
702
|
+
}
|
|
703
|
+
else if (Array.isArray(value)) {
|
|
704
|
+
if (value.length === 1) {
|
|
705
|
+
return this.map(value[0], opt, valueFn, keyFn);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
else if (this.is.bareObject(value)) {
|
|
709
|
+
const newMap = new Map();
|
|
710
|
+
const keys = Object.keys(value);
|
|
711
|
+
if (keys.length < 1) {
|
|
712
|
+
return newMap;
|
|
713
|
+
}
|
|
714
|
+
for (const [k, v] of Object.entries(value)) {
|
|
715
|
+
newMap.set(k, v);
|
|
716
|
+
}
|
|
717
|
+
return this.map(newMap, opt, valueFn, keyFn);
|
|
718
|
+
}
|
|
719
|
+
break;
|
|
720
|
+
case 'function':
|
|
721
|
+
return this.$runFn(v => this.map(v, opt, valueFn, keyFn), value, opt);
|
|
722
|
+
}
|
|
723
|
+
return this.$unexpectedError(value, this._EXPECTED_OBJECT, opt);
|
|
724
|
+
}
|
|
725
|
+
dict(value, opt, valueFn, keyFn) {
|
|
726
|
+
if (this._EMPTY.includes(value)) {
|
|
727
|
+
return value;
|
|
728
|
+
}
|
|
729
|
+
switch (typeof value) {
|
|
730
|
+
case 'object':
|
|
731
|
+
if (value instanceof Set) {
|
|
732
|
+
if (value.size === 1) {
|
|
733
|
+
return this.dict(Array.from(value.values())[0], opt, valueFn, keyFn);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
else if (value instanceof list_1.List) {
|
|
737
|
+
if (value.length === 1) {
|
|
738
|
+
return this.dict(value[0], opt, valueFn, keyFn);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
else if (value instanceof Map) {
|
|
742
|
+
if (value.size < 1) {
|
|
743
|
+
return {};
|
|
744
|
+
}
|
|
745
|
+
return this.dict(Object.fromEntries(value.entries()), opt, valueFn, keyFn);
|
|
746
|
+
}
|
|
747
|
+
else if (Array.isArray(value)) {
|
|
748
|
+
if (value.length === 1) {
|
|
749
|
+
return this.dict(value[0], opt, valueFn, keyFn);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
else if (this.is.bareObject(value)) {
|
|
753
|
+
if (typeof valueFn !== "function" && typeof keyFn !== "function") {
|
|
754
|
+
return value;
|
|
755
|
+
}
|
|
756
|
+
const keys = Object.keys(value);
|
|
757
|
+
if (keys.length < 1) {
|
|
758
|
+
return value;
|
|
759
|
+
}
|
|
760
|
+
const newOpt = this.$runOpt(opt);
|
|
761
|
+
const field = (typeof newOpt.field === 'string') ? newOpt.field : undefined;
|
|
762
|
+
const clonedOpt = Object.assign({}, newOpt);
|
|
763
|
+
const cloned = Object.assign({}, value);
|
|
764
|
+
keys
|
|
765
|
+
.filter(k => typeof k !== 'symbol')
|
|
766
|
+
.forEach(k => {
|
|
767
|
+
try {
|
|
768
|
+
delete value[k];
|
|
769
|
+
}
|
|
770
|
+
catch (e) {
|
|
771
|
+
}
|
|
772
|
+
});
|
|
773
|
+
let index = -1;
|
|
774
|
+
for (const [k, v] of Object.entries(cloned)) {
|
|
775
|
+
if (typeof k === 'symbol') {
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
index++;
|
|
779
|
+
let newKey = k;
|
|
780
|
+
let newValue = v;
|
|
781
|
+
if (typeof keyFn === 'function') {
|
|
782
|
+
clonedOpt.field = field ? `${field}#${index}` : `#${index}`;
|
|
783
|
+
try {
|
|
784
|
+
newKey = keyFn(k, index, clonedOpt);
|
|
785
|
+
}
|
|
786
|
+
catch (e) {
|
|
787
|
+
this.$errorOrLog(clonedOpt, undefined, e);
|
|
788
|
+
}
|
|
789
|
+
if (this._EMPTY.includes(newKey)) {
|
|
790
|
+
continue;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
if (typeof valueFn === 'function') {
|
|
794
|
+
clonedOpt.field = field ? `${field}.${k}` : k;
|
|
795
|
+
try {
|
|
796
|
+
newValue = valueFn(v, k, clonedOpt);
|
|
797
|
+
}
|
|
798
|
+
catch (e) {
|
|
799
|
+
this.$errorOrLog(clonedOpt, undefined, e);
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
try {
|
|
803
|
+
value[newKey] = newValue;
|
|
804
|
+
}
|
|
805
|
+
catch (e) {
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
return value;
|
|
809
|
+
}
|
|
810
|
+
// other objects, don't toch
|
|
811
|
+
return value;
|
|
812
|
+
case 'function':
|
|
813
|
+
return this.$runFn(v => this.dict(v, opt, valueFn, keyFn), value, opt);
|
|
814
|
+
}
|
|
815
|
+
return this.$unexpectedError(value, this._EXPECTED_OBJECT, opt);
|
|
816
|
+
}
|
|
817
|
+
string(value, opt) {
|
|
818
|
+
if (this._EMPTY.includes(value)) {
|
|
819
|
+
return value;
|
|
820
|
+
}
|
|
821
|
+
switch (typeof value) {
|
|
822
|
+
case 'string':
|
|
823
|
+
return value;
|
|
824
|
+
case 'number':
|
|
825
|
+
const num = this.realNumber(value, opt);
|
|
826
|
+
return this._EMPTY.includes(num) ? num : num.toString(10);
|
|
827
|
+
case 'bigint':
|
|
828
|
+
return value.toString();
|
|
829
|
+
case 'boolean':
|
|
830
|
+
return value ? 'true' : 'false';
|
|
831
|
+
case 'object':
|
|
832
|
+
if (Array.isArray(value) && value.length === 1) {
|
|
833
|
+
return this.string(value[0], opt);
|
|
834
|
+
}
|
|
835
|
+
return this.string(value.id, opt);
|
|
836
|
+
case 'function':
|
|
837
|
+
return this.$runFn(v => this.string(v, opt), value, opt);
|
|
838
|
+
}
|
|
839
|
+
return this.$unexpectedError(value, this._EXPECTED_STRING, opt);
|
|
840
|
+
}
|
|
841
|
+
text(value, opt) {
|
|
842
|
+
if (this._EMPTY.includes(value)) {
|
|
843
|
+
return value;
|
|
844
|
+
}
|
|
845
|
+
switch (typeof value) {
|
|
846
|
+
case 'string':
|
|
847
|
+
const str = value.trim();
|
|
848
|
+
return str ? str : undefined;
|
|
849
|
+
case 'number':
|
|
850
|
+
const num = this.realNumber(value, opt);
|
|
851
|
+
return this._EMPTY.includes(num) ? num : num.toString(10);
|
|
852
|
+
case 'bigint':
|
|
853
|
+
return value.toString();
|
|
854
|
+
case 'boolean':
|
|
855
|
+
return value ? 'true' : 'false';
|
|
856
|
+
case 'object':
|
|
857
|
+
if (Array.isArray(value) && value.length === 1) {
|
|
858
|
+
return this.text(value[0], opt);
|
|
859
|
+
}
|
|
860
|
+
return this.text(value.id, opt);
|
|
861
|
+
case 'function':
|
|
862
|
+
return this.$runFn(v => this.text(v, opt), value, opt);
|
|
863
|
+
}
|
|
864
|
+
return this.$unexpectedError(value, this._EXPECTED_STRING, opt);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
exports.CommonTo = CommonTo;
|
|
868
|
+
//# sourceMappingURL=common-to.js.map
|