@cabloy/utils 2.0.4 → 2.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/index.js +38 -9
- 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;
|
|
@@ -240,14 +261,6 @@ celEnvBase.registerFunction('join(list,string):string', (list, sep) => {
|
|
|
240
261
|
return _join(list, sep);
|
|
241
262
|
});
|
|
242
263
|
|
|
243
|
-
// get
|
|
244
|
-
celEnvBase.registerFunction('get(map,string):dyn', (obj, name) => {
|
|
245
|
-
return getProperty(obj, name);
|
|
246
|
-
});
|
|
247
|
-
celEnvBase.registerFunction('get(map,string,string):dyn', (obj, name, sep) => {
|
|
248
|
-
return getProperty(obj, name, sep);
|
|
249
|
-
});
|
|
250
|
-
|
|
251
264
|
// string
|
|
252
265
|
celEnvBase.registerFunction('string(null):string', value => {
|
|
253
266
|
return String(value);
|
|
@@ -256,11 +269,27 @@ celEnvBase.registerFunction('string(null):string', value => {
|
|
|
256
269
|
// operator: +
|
|
257
270
|
celEnvBase.registerOperator('string + int', (str, n) => str + String(n));
|
|
258
271
|
celEnvBase.registerOperator('int + string', (n, str) => String(n) + str);
|
|
272
|
+
celEnvBase.registerOperator('string + null', (str, _n) => str);
|
|
273
|
+
celEnvBase.registerOperator('null + string', (_n, str) => str);
|
|
259
274
|
|
|
260
275
|
// operator: ==
|
|
261
276
|
celEnvBase.registerOperator('string == null', (str, n) => str === n);
|
|
262
277
|
celEnvBase.registerOperator('int == null', (num, n) => num === n);
|
|
263
278
|
celEnvBase.registerOperator('bool == null', (b, n) => b === n);
|
|
279
|
+
|
|
280
|
+
// get
|
|
281
|
+
celEnvBase.registerFunction('get(map,string):dyn', (obj, name) => {
|
|
282
|
+
return getProperty(obj, name);
|
|
283
|
+
});
|
|
284
|
+
celEnvBase.registerFunction('get(map,string,string):dyn', (obj, name, sep) => {
|
|
285
|
+
return getProperty(obj, name, sep);
|
|
286
|
+
});
|
|
287
|
+
celEnvBase.registerFunction('exists(null,string):bool', (obj, name) => {
|
|
288
|
+
return hasProperty(obj, name);
|
|
289
|
+
});
|
|
290
|
+
celEnvBase.registerFunction('exists(map,string):bool', (obj, name) => {
|
|
291
|
+
return hasProperty(obj, name);
|
|
292
|
+
});
|
|
264
293
|
function _concat(...args) {
|
|
265
294
|
return [].concat(...args);
|
|
266
295
|
}
|
|
@@ -441,4 +470,4 @@ function zodCustomError(path, message) {
|
|
|
441
470
|
return error;
|
|
442
471
|
}
|
|
443
472
|
|
|
444
|
-
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 };
|
|
473
|
+
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;
|