@cabloy/utils 1.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016-present Vona
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @cabloy/utils
@@ -0,0 +1,9 @@
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;
9
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,89 @@
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;
47
+ }
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
+ }
89
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@cabloy/utils",
3
+ "version": "1.0.0",
4
+ "description": "cabloy utils",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "exports": {
9
+ ".": {
10
+ "types": [
11
+ "./dist/index.d.ts",
12
+ "./src/index.ts"
13
+ ],
14
+ "import": "./dist/index.js",
15
+ "require": "./dist/index.js",
16
+ "default": "./src/index.ts"
17
+ },
18
+ "./package.json": "./package.json"
19
+ },
20
+ "files": [
21
+ "dist/**/*.js",
22
+ "dist/**/*.d.ts"
23
+ ],
24
+ "keywords": [
25
+ "egg",
26
+ "egg-born",
27
+ "framework",
28
+ "cabloy"
29
+ ],
30
+ "author": "zhennann",
31
+ "scripts": {
32
+ "lint": "eslint ."
33
+ }
34
+ }