@cabloy/utils 1.0.0 → 1.0.1

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.
@@ -0,0 +1,16 @@
1
+ export declare const isUndefined: (obj: any) => obj is undefined;
2
+ export declare const isObject: (fn: any) => fn is object;
3
+ export declare const isPlainObject: (fn: any) => fn is object;
4
+ export declare const addLeadingSlash: (path?: string) => string;
5
+ export declare const normalizePath: (path?: string) => string;
6
+ export declare const stripEndSlash: (path: string) => string;
7
+ export declare const isFunction: (val: any) => val is Function;
8
+ export declare const isString: (val: any) => val is string;
9
+ export declare const isNumber: (val: any) => val is number;
10
+ export declare const isConstructor: (val: any) => boolean;
11
+ export declare const isNil: (val: any) => val is null | undefined;
12
+ export declare const isEmpty: (array: any) => boolean;
13
+ export declare const isSymbol: (val: any) => val is symbol;
14
+ export declare function isPromise(obj: any): obj is Promise<any>;
15
+ export declare function isNilOrEmptyString(str?: string | undefined | null): str is null | undefined | '';
16
+ //# sourceMappingURL=check.d.ts.map
package/dist/check.js ADDED
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isNilOrEmptyString = exports.isPromise = exports.isSymbol = exports.isEmpty = exports.isNil = exports.isConstructor = exports.isNumber = exports.isString = exports.isFunction = exports.stripEndSlash = exports.normalizePath = exports.addLeadingSlash = exports.isPlainObject = exports.isObject = exports.isUndefined = void 0;
4
+ /* eslint-disable @typescript-eslint/no-use-before-define */
5
+ const isUndefined = (obj) => typeof obj === 'undefined';
6
+ exports.isUndefined = isUndefined;
7
+ const isObject = (fn) => !(0, exports.isNil)(fn) && typeof fn === 'object';
8
+ exports.isObject = isObject;
9
+ const isPlainObject = (fn) => {
10
+ if (!(0, exports.isObject)(fn)) {
11
+ return false;
12
+ }
13
+ const proto = Object.getPrototypeOf(fn);
14
+ if (proto === null) {
15
+ return true;
16
+ }
17
+ const ctor = Object.prototype.hasOwnProperty.call(proto, 'constructor') && proto.constructor;
18
+ return (typeof ctor === 'function' &&
19
+ ctor instanceof ctor &&
20
+ Function.prototype.toString.call(ctor) === Function.prototype.toString.call(Object));
21
+ };
22
+ exports.isPlainObject = isPlainObject;
23
+ const addLeadingSlash = (path) => path && typeof path === 'string' ? (path.charAt(0) !== '/' ? '/' + path : path) : '';
24
+ exports.addLeadingSlash = addLeadingSlash;
25
+ const normalizePath = (path) => path
26
+ ? path.startsWith('/')
27
+ ? ('/' + path.replace(/\/+$/, '')).replace(/\/+/g, '/')
28
+ : '/' + path.replace(/\/+$/, '')
29
+ : '/';
30
+ exports.normalizePath = normalizePath;
31
+ const stripEndSlash = (path) => (path[path.length - 1] === '/' ? path.slice(0, path.length - 1) : path);
32
+ exports.stripEndSlash = stripEndSlash;
33
+ const isFunction = (val) => typeof val === 'function';
34
+ exports.isFunction = isFunction;
35
+ const isString = (val) => typeof val === 'string';
36
+ exports.isString = isString;
37
+ const isNumber = (val) => typeof val === 'number';
38
+ exports.isNumber = isNumber;
39
+ const isConstructor = (val) => val === 'constructor';
40
+ exports.isConstructor = isConstructor;
41
+ const isNil = (val) => (0, exports.isUndefined)(val) || val === null;
42
+ exports.isNil = isNil;
43
+ const isEmpty = (array) => !(array && array.length > 0);
44
+ exports.isEmpty = isEmpty;
45
+ const isSymbol = (val) => typeof val === 'symbol';
46
+ exports.isSymbol = isSymbol;
47
+ function isPromise(obj) {
48
+ return obj instanceof Promise || (obj && typeof obj.then === 'function');
49
+ }
50
+ exports.isPromise = isPromise;
51
+ function isNilOrEmptyString(str) {
52
+ return str === undefined || str === null || str === '';
53
+ }
54
+ exports.isNilOrEmptyString = isNilOrEmptyString;
55
+ //# sourceMappingURL=check.js.map
package/dist/index.d.ts CHANGED
@@ -1,9 +1,3 @@
1
- export declare function isNilOrEmptyString(str?: string | undefined | null): str is null | undefined | '';
2
- export declare function deprecated(oldUsage: any, newUsage: any): void;
3
- export declare function catchError<T>(fnMethod: (...args: any[]) => Promise<T>): Promise<[T, undefined] | [undefined, Error]>;
4
- export declare function sleep(ms: any): Promise<unknown>;
5
- export declare function replaceTemplate(content: string, scope: object): string;
6
- export declare function setProperty<T>(obj: object, name: string, value: T): void;
7
- export declare function getProperty<T>(obj: object, name: string, sep?: string): T | undefined;
8
- export declare function getPropertyObject<T>(obj: object, name: string, sep?: string): T | undefined;
1
+ export * from './check.js';
2
+ export * from './utils.js';
9
3
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,89 +1,19 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPropertyObject = exports.getProperty = exports.setProperty = exports.replaceTemplate = exports.sleep = exports.catchError = exports.deprecated = exports.isNilOrEmptyString = void 0;
4
- function isNilOrEmptyString(str) {
5
- return str === undefined || str === null || str === '';
6
- }
7
- exports.isNilOrEmptyString = isNilOrEmptyString;
8
- function deprecated(oldUsage, newUsage) {
9
- const message = '`'
10
- .concat(oldUsage, '` is deprecated and will be removed in a later version. Use `')
11
- .concat(newUsage, '` instead');
12
- return console.warn(message);
13
- }
14
- exports.deprecated = deprecated;
15
- async function catchError(fnMethod) {
16
- let error;
17
- let data;
18
- try {
19
- data = await fnMethod();
20
- }
21
- catch (err) {
22
- error = err;
23
- }
24
- return error ? [undefined, error] : [data, undefined];
25
- }
26
- exports.catchError = catchError;
27
- async function sleep(ms) {
28
- return new Promise(resolve => setTimeout(resolve, ms));
29
- }
30
- exports.sleep = sleep;
31
- function replaceTemplate(content, scope) {
32
- if (!content)
33
- return content;
34
- return content.toString().replace(/(\\)?{{ *([\w\.]+) *}}/g, (block, skip, key) => {
35
- if (skip) {
36
- return block.substring(skip.length);
37
- }
38
- const value = getProperty(scope, key);
39
- return value !== undefined ? value : '';
40
- });
41
- }
42
- exports.replaceTemplate = replaceTemplate;
43
- function setProperty(obj, name, value) {
44
- const names = name.split('.');
45
- if (names.length === 1) {
46
- obj[name] = value;
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
47
7
  }
48
- else {
49
- for (let i = 0; i < names.length - 1; i++) {
50
- const _obj = obj[names[i]];
51
- if (_obj) {
52
- obj = _obj;
53
- }
54
- else {
55
- obj = obj[names[i]] = {};
56
- }
57
- }
58
- obj[names[names.length - 1]] = value;
59
- }
60
- }
61
- exports.setProperty = setProperty;
62
- function getProperty(obj, name, sep) {
63
- return _getProperty(obj, name, sep, false);
64
- }
65
- exports.getProperty = getProperty;
66
- function getPropertyObject(obj, name, sep) {
67
- return _getProperty(obj, name, sep, true);
68
- }
69
- exports.getPropertyObject = getPropertyObject;
70
- function _getProperty(obj, name, sep, forceObject) {
71
- if (!obj)
72
- return undefined;
73
- const names = name.split(sep || '.');
74
- // loop
75
- for (const name of names) {
76
- if (obj[name] === undefined || obj[name] === null) {
77
- if (forceObject) {
78
- obj[name] = {};
79
- }
80
- else {
81
- obj = obj[name];
82
- break;
83
- }
84
- }
85
- obj = obj[name];
86
- }
87
- return obj;
88
- }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./check.js"), exports);
18
+ __exportStar(require("./utils.js"), exports);
89
19
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,8 @@
1
+ export declare function deprecated(oldUsage: any, newUsage: any): void;
2
+ export declare function catchError<T>(fnMethod: (...args: any[]) => Promise<T>): Promise<[T, undefined] | [undefined, Error]>;
3
+ export declare function sleep(ms: any): Promise<unknown>;
4
+ export declare function replaceTemplate(content: string, scope: object): string;
5
+ export declare function setProperty<T>(obj: object, name: string, value: T): void;
6
+ export declare function getProperty<T>(obj: object, name: string, sep?: string): T | undefined;
7
+ export declare function getPropertyObject<T>(obj: object, name: string, sep?: string): T | undefined;
8
+ //# sourceMappingURL=utils.d.ts.map
package/dist/utils.js ADDED
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPropertyObject = exports.getProperty = exports.setProperty = exports.replaceTemplate = exports.sleep = exports.catchError = exports.deprecated = void 0;
4
+ function deprecated(oldUsage, newUsage) {
5
+ const message = '`'
6
+ .concat(oldUsage, '` is deprecated and will be removed in a later version. Use `')
7
+ .concat(newUsage, '` instead');
8
+ return console.warn(message);
9
+ }
10
+ exports.deprecated = deprecated;
11
+ async function catchError(fnMethod) {
12
+ let error;
13
+ let data;
14
+ try {
15
+ data = await fnMethod();
16
+ }
17
+ catch (err) {
18
+ error = err;
19
+ }
20
+ return error ? [undefined, error] : [data, undefined];
21
+ }
22
+ exports.catchError = catchError;
23
+ async function sleep(ms) {
24
+ return new Promise(resolve => setTimeout(resolve, ms));
25
+ }
26
+ exports.sleep = sleep;
27
+ function replaceTemplate(content, scope) {
28
+ if (!content)
29
+ return content;
30
+ return content.toString().replace(/(\\)?{{ *([\w\.]+) *}}/g, (block, skip, key) => {
31
+ if (skip) {
32
+ return block.substring(skip.length);
33
+ }
34
+ const value = getProperty(scope, key);
35
+ return value !== undefined ? value : '';
36
+ });
37
+ }
38
+ exports.replaceTemplate = replaceTemplate;
39
+ function setProperty(obj, name, value) {
40
+ const names = name.split('.');
41
+ if (names.length === 1) {
42
+ obj[name] = value;
43
+ }
44
+ else {
45
+ for (let i = 0; i < names.length - 1; i++) {
46
+ const _obj = obj[names[i]];
47
+ if (_obj) {
48
+ obj = _obj;
49
+ }
50
+ else {
51
+ obj = obj[names[i]] = {};
52
+ }
53
+ }
54
+ obj[names[names.length - 1]] = value;
55
+ }
56
+ }
57
+ exports.setProperty = setProperty;
58
+ function getProperty(obj, name, sep) {
59
+ return _getProperty(obj, name, sep, false);
60
+ }
61
+ exports.getProperty = getProperty;
62
+ function getPropertyObject(obj, name, sep) {
63
+ return _getProperty(obj, name, sep, true);
64
+ }
65
+ exports.getPropertyObject = getPropertyObject;
66
+ function _getProperty(obj, name, sep, forceObject) {
67
+ if (!obj)
68
+ return undefined;
69
+ const names = name.split(sep || '.');
70
+ // loop
71
+ for (const name of names) {
72
+ if (obj[name] === undefined || obj[name] === null) {
73
+ if (forceObject) {
74
+ obj[name] = {};
75
+ }
76
+ else {
77
+ obj = obj[name];
78
+ break;
79
+ }
80
+ }
81
+ obj = obj[name];
82
+ }
83
+ return obj;
84
+ }
85
+ //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cabloy/utils",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "cabloy utils",
5
5
  "publishConfig": {
6
6
  "access": "public"