@cabloy/utils 2.0.3 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +45 -5
- package/dist/utils.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -116,6 +116,9 @@ function setProperty(obj, name, value) {
|
|
|
116
116
|
obj[names[names.length - 1]] = value;
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
|
+
function hasProperty(obj, name, sep) {
|
|
120
|
+
return _hasProperty(obj, name, sep);
|
|
121
|
+
}
|
|
119
122
|
function getProperty(obj, name, sep) {
|
|
120
123
|
return _getProperty(obj, name, sep, false);
|
|
121
124
|
}
|
|
@@ -123,6 +126,24 @@ function getPropertyObject(obj, name, sep) {
|
|
|
123
126
|
return _getProperty(obj, name, sep, true);
|
|
124
127
|
}
|
|
125
128
|
const __keysIgnore = ['constructor', 'prototype', '__proto__'];
|
|
129
|
+
function _hasProperty(_obj, name, sep) {
|
|
130
|
+
if (!_obj) return false;
|
|
131
|
+
let obj = _obj;
|
|
132
|
+
const names = name.split(sep || '.');
|
|
133
|
+
// loop
|
|
134
|
+
for (const _name of names) {
|
|
135
|
+
const [name, index] = _parsePropertyKey(_name);
|
|
136
|
+
if (__keysIgnore.includes(name)) throw new Error(`invalid prop: ${name}`);
|
|
137
|
+
if (obj === undefined || !Object.hasOwnProperty.call(obj, name)) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
obj = obj[name];
|
|
141
|
+
if (index !== undefined) {
|
|
142
|
+
obj = obj[index];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
126
147
|
function _getProperty(_obj, name, sep, forceObject) {
|
|
127
148
|
if (!_obj) return undefined;
|
|
128
149
|
let obj = _obj;
|
|
@@ -226,6 +247,8 @@ const celEnvBase = new Environment({
|
|
|
226
247
|
enableOptionalTypes: true,
|
|
227
248
|
homogeneousAggregateLiterals: false
|
|
228
249
|
});
|
|
250
|
+
|
|
251
|
+
// array
|
|
229
252
|
const params = [];
|
|
230
253
|
for (let i = 0; i < 10; i++) {
|
|
231
254
|
params.push('dyn');
|
|
@@ -237,16 +260,33 @@ celEnvBase.registerFunction('join(list):string', list => {
|
|
|
237
260
|
celEnvBase.registerFunction('join(list,string):string', (list, sep) => {
|
|
238
261
|
return _join(list, sep);
|
|
239
262
|
});
|
|
263
|
+
|
|
264
|
+
// string
|
|
265
|
+
celEnvBase.registerFunction('string(null):string', value => {
|
|
266
|
+
return String(value);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
// operator: +
|
|
270
|
+
celEnvBase.registerOperator('string + int', (str, n) => str + String(n));
|
|
271
|
+
celEnvBase.registerOperator('int + string', (n, str) => String(n) + str);
|
|
272
|
+
|
|
273
|
+
// operator: ==
|
|
274
|
+
celEnvBase.registerOperator('string == null', (str, n) => str === n);
|
|
275
|
+
celEnvBase.registerOperator('int == null', (num, n) => num === n);
|
|
276
|
+
celEnvBase.registerOperator('bool == null', (b, n) => b === n);
|
|
277
|
+
|
|
278
|
+
// get
|
|
240
279
|
celEnvBase.registerFunction('get(map,string):dyn', (obj, name) => {
|
|
241
280
|
return getProperty(obj, name);
|
|
242
281
|
});
|
|
243
282
|
celEnvBase.registerFunction('get(map,string,string):dyn', (obj, name, sep) => {
|
|
244
283
|
return getProperty(obj, name, sep);
|
|
245
284
|
});
|
|
246
|
-
celEnvBase.
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
285
|
+
celEnvBase.registerFunction('exists(null,string):bool', (obj, name) => {
|
|
286
|
+
return hasProperty(obj, name);
|
|
287
|
+
});
|
|
288
|
+
celEnvBase.registerFunction('exists(map,string):bool', (obj, name) => {
|
|
289
|
+
return hasProperty(obj, name);
|
|
250
290
|
});
|
|
251
291
|
function _concat(...args) {
|
|
252
292
|
return [].concat(...args);
|
|
@@ -428,4 +468,4 @@ function zodCustomError(path, message) {
|
|
|
428
468
|
return error;
|
|
429
469
|
}
|
|
430
470
|
|
|
431
|
-
export { StringPrefixCel, StringPrefixRaw, StringPrefixRegexp, addLeadingSlash, catchError, catchErrorSync, cel, celEnvBase, checkMeta, combineApiPath, combineApiPathControllerAndAction, combineApiPathControllerAndActionRaw, combineParamsAndQuery, combineQueries, combineResourceName, combineResourceNameParts, createFunction, defaultPathSerializer, deprecated, ensureArray, evaluate, evaluateExpressions, evaluateSimple, getProperty, getPropertyObject, getRandomInt, hashkey, isClass, isClassStrict, isConstructor, isEmpty, isEmptyObject, isFunction, isNil, isNilOrEmptyString, isNumber, isObject, isPlainObject, isPromise, isString, isSymbol, isUndefined, matchSelector, normalizePath, parse, raw, regexp, replaceTemplate, safeBoolean, setProperty, sleep, stringLazy, stripEndSlash, zodCustomError };
|
|
471
|
+
export { StringPrefixCel, StringPrefixRaw, StringPrefixRegexp, addLeadingSlash, catchError, catchErrorSync, cel, celEnvBase, checkMeta, combineApiPath, combineApiPathControllerAndAction, combineApiPathControllerAndActionRaw, combineParamsAndQuery, combineQueries, combineResourceName, combineResourceNameParts, createFunction, defaultPathSerializer, deprecated, ensureArray, evaluate, evaluateExpressions, evaluateSimple, getProperty, getPropertyObject, getRandomInt, hasProperty, hashkey, isClass, isClassStrict, isConstructor, isEmpty, isEmptyObject, isFunction, isNil, isNilOrEmptyString, isNumber, isObject, isPlainObject, isPromise, isString, isSymbol, isUndefined, matchSelector, normalizePath, parse, raw, regexp, replaceTemplate, safeBoolean, setProperty, sleep, stringLazy, stripEndSlash, zodCustomError };
|
package/dist/utils.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export declare function catchErrorSync<T>(fnMethod: (...args: any[]) => T): [T,
|
|
|
4
4
|
export declare function sleep(ms: any): Promise<unknown>;
|
|
5
5
|
export declare function replaceTemplate(content: string, scope: object): string;
|
|
6
6
|
export declare function setProperty<T>(obj: object, name: string, value: T): void;
|
|
7
|
+
export declare function hasProperty(obj: object | undefined, name: string, sep?: string): boolean;
|
|
7
8
|
export declare function getProperty<T>(obj: object | undefined, name: string, sep?: string): T | undefined;
|
|
8
9
|
export declare function getPropertyObject<T>(obj: object | undefined, name: string, sep?: string): T | undefined;
|
|
9
10
|
export declare function getRandomInt(size: number, start?: number): number;
|