@beecode/msh-util 2.0.0-alpha → 2.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/lib/array-util.d.ts +23 -0
- package/lib/array-util.d.ts.map +1 -0
- package/lib/array-util.js +32 -0
- package/lib/class-factory-pattern.d.ts +25 -0
- package/lib/class-factory-pattern.d.ts.map +1 -0
- package/lib/class-factory-pattern.js +39 -0
- package/lib/express/error-handler.d.ts +17 -0
- package/lib/express/error-handler.d.ts.map +1 -0
- package/lib/express/error-handler.js +32 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +108 -0
- package/lib/joi-util.d.ts +47 -0
- package/lib/joi-util.d.ts.map +1 -0
- package/lib/joi-util.js +107 -0
- package/lib/memoize-factory.d.ts +16 -0
- package/lib/memoize-factory.d.ts.map +1 -0
- package/lib/memoize-factory.js +34 -0
- package/lib/object-util.d.ts +72 -0
- package/lib/object-util.d.ts.map +1 -0
- package/lib/object-util.js +173 -0
- package/lib/package.json +1 -0
- package/lib/regex-util.d.ts +12 -0
- package/lib/regex-util.d.ts.map +1 -0
- package/lib/regex-util.js +17 -0
- package/lib/single-threshold-promise.d.ts +31 -0
- package/lib/single-threshold-promise.d.ts.map +1 -0
- package/lib/single-threshold-promise.js +95 -0
- package/lib/singleton/async.d.ts +50 -0
- package/lib/singleton/async.d.ts.map +1 -0
- package/lib/singleton/async.js +138 -0
- package/lib/singleton/pattern.d.ts +34 -0
- package/lib/singleton/pattern.d.ts.map +1 -0
- package/lib/singleton/pattern.js +46 -0
- package/lib/string-util.d.ts +10 -0
- package/lib/string-util.d.ts.map +1 -0
- package/lib/string-util.js +23 -0
- package/lib/time-util.d.ts +74 -0
- package/lib/time-util.d.ts.map +1 -0
- package/lib/time-util.js +127 -0
- package/lib/time-zone.d.ts +467 -0
- package/lib/time-zone.d.ts.map +1 -0
- package/lib/time-zone.js +473 -0
- package/lib/timeout.d.ts +15 -0
- package/lib/timeout.d.ts.map +1 -0
- package/lib/timeout.js +24 -0
- package/lib/type-util.d.ts +50 -0
- package/lib/type-util.d.ts.map +1 -0
- package/lib/type-util.js +59 -0
- package/lib/types/global.d.js +5 -0
- package/lib/types/types.d.js +3 -0
- package/package.json +2 -2
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.singletonPattern = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Singleton patter wrapper function
|
|
9
|
+
* @param {AnyFunctionNoParams<R>} factoryFn Factory function that is used to generate value that is going to be cached and return by
|
|
10
|
+
* singleton.
|
|
11
|
+
* @return {AnyFunctionNoParams<R>} Function result that returns cached value.
|
|
12
|
+
* @example
|
|
13
|
+
* export class SomeClass {
|
|
14
|
+
* constructor(protected _param: string){ }
|
|
15
|
+
* get param(): string {
|
|
16
|
+
* return this._param
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* export const someClassSingleton = singletonPattern((): SomeClass => {
|
|
20
|
+
* return new SomeClass('some param value')
|
|
21
|
+
* })
|
|
22
|
+
*
|
|
23
|
+
* // using
|
|
24
|
+
* console.log('param: ', someClassSingleton().param) // param: some param value
|
|
25
|
+
*
|
|
26
|
+
* ///////////////////////////////////////////
|
|
27
|
+
* // Or we can use it with simple function //
|
|
28
|
+
* ///////////////////////////////////////////
|
|
29
|
+
* export const config = singletonPattern(() => {
|
|
30
|
+
* return {
|
|
31
|
+
* env: process.NODE_ENV,
|
|
32
|
+
* } as const
|
|
33
|
+
* })
|
|
34
|
+
*
|
|
35
|
+
* // using
|
|
36
|
+
* console.log('NODE_ENV: ', config().env) // NODE_ENV: prod
|
|
37
|
+
*/
|
|
38
|
+
var singletonPattern = exports.singletonPattern = function singletonPattern(factoryFn) {
|
|
39
|
+
var cache = {};
|
|
40
|
+
return function () {
|
|
41
|
+
if ('singleton' in cache) {
|
|
42
|
+
return cache.singleton;
|
|
43
|
+
}
|
|
44
|
+
return cache.singleton = factoryFn();
|
|
45
|
+
};
|
|
46
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const stringUtil: {
|
|
2
|
+
/**
|
|
3
|
+
* Generate random UUID
|
|
4
|
+
* @return {string}
|
|
5
|
+
* @example
|
|
6
|
+
* console.log(stringUtil.uuid()) // "69bfda25-df3f-46b4-8bbb-955cf5193426"
|
|
7
|
+
*/
|
|
8
|
+
generateUUID: () => string;
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=string-util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string-util.d.ts","sourceRoot":"","sources":["../src/string-util.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU;IACtB;;;;;OAKG;wBACe,MAAM;CAUxB,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.stringUtil = void 0;
|
|
7
|
+
var stringUtil = exports.stringUtil = {
|
|
8
|
+
/**
|
|
9
|
+
* Generate random UUID
|
|
10
|
+
* @return {string}
|
|
11
|
+
* @example
|
|
12
|
+
* console.log(stringUtil.uuid()) // "69bfda25-df3f-46b4-8bbb-955cf5193426"
|
|
13
|
+
*/
|
|
14
|
+
generateUUID: function generateUUID() {
|
|
15
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
16
|
+
var r = Math.random() * 16 | 0;
|
|
17
|
+
if (c == 'x') {
|
|
18
|
+
return r.toString(16);
|
|
19
|
+
}
|
|
20
|
+
return (r & 0x3 | 0x8).toString(16);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export declare enum DurationUnit {
|
|
2
|
+
MILLISECOND = "MILLISECOND",
|
|
3
|
+
SECOND = "SECOND",
|
|
4
|
+
MINUTE = "MINUTE",
|
|
5
|
+
HOUR = "HOUR",
|
|
6
|
+
DAY = "DAY",
|
|
7
|
+
WEEK = "WEEK",
|
|
8
|
+
MONTH = "MONTH",
|
|
9
|
+
YEAR = "YEAR"
|
|
10
|
+
}
|
|
11
|
+
export type DurationUnitType = `${DurationUnit}`;
|
|
12
|
+
export declare class TimeUtil {
|
|
13
|
+
/**
|
|
14
|
+
* return date object with the current time
|
|
15
|
+
* @return {Date}
|
|
16
|
+
* @example
|
|
17
|
+
* console.log(new TimeUtil().now().toISOString()) // 2023-03-08T19:45:01.991Z
|
|
18
|
+
*/
|
|
19
|
+
now(): Date;
|
|
20
|
+
/**
|
|
21
|
+
* Convert date object to unix timestamp (milliseconds)
|
|
22
|
+
* @param {Date} date
|
|
23
|
+
* @return {number}
|
|
24
|
+
* @example
|
|
25
|
+
* // timeUtil.now().toISOString() === 2023-03-08T19:45:01.991Z
|
|
26
|
+
* const timeUtil = new TimeUtil()
|
|
27
|
+
* console.log(timeUtil.dateToUnix(timeUtil.now())) // 1678304701991
|
|
28
|
+
*/
|
|
29
|
+
dateToUnix(date: Date): number;
|
|
30
|
+
/**
|
|
31
|
+
* Convert date object to unix timestamp (seconds)
|
|
32
|
+
* @param {Date} date
|
|
33
|
+
* @return {number}
|
|
34
|
+
* @example
|
|
35
|
+
* // timeUtil.now().toISOString() === 2023-03-08T19:45:01.991Z
|
|
36
|
+
* const timeUtil = new TimeUtil()
|
|
37
|
+
* console.log(timeUtil.dateToUnix(timeUtil.now())) // 1678304701
|
|
38
|
+
*/
|
|
39
|
+
dateToUnixSec(date: Date): number;
|
|
40
|
+
/**
|
|
41
|
+
* Convert unix timestamp (milliseconds) to date object
|
|
42
|
+
* @param {number} unix
|
|
43
|
+
* @return {Date}
|
|
44
|
+
* @example
|
|
45
|
+
* const timeUtil = new TimeUtil()
|
|
46
|
+
* console.log(timeUtil.unixToDate(1678304701991).toISOString()) // 2023-03-08T19:45:01.991Z
|
|
47
|
+
*/
|
|
48
|
+
unixToDate(unix: number): Date;
|
|
49
|
+
/**
|
|
50
|
+
* Convert unix timestamp (seconds) to date object
|
|
51
|
+
* @param {number} unix
|
|
52
|
+
* @return {Date}
|
|
53
|
+
* @example
|
|
54
|
+
* const timeUtil = new TimeUtil()
|
|
55
|
+
* console.log(timeUtil.unixToDate(1678304701).toISOString()) // 2023-03-08T19:45:01.000Z
|
|
56
|
+
*/
|
|
57
|
+
unixSecToDate(unix: number): Date;
|
|
58
|
+
/**
|
|
59
|
+
* Change the value of date by unit/value pare.
|
|
60
|
+
* @param {{units: DurationUnitType, value: number, date: Date}} params
|
|
61
|
+
* @return {Date}
|
|
62
|
+
* @example
|
|
63
|
+
* // timeUtil.now().toISOString() === 2023-03-08T19:45:01.991Z
|
|
64
|
+
* const timeUtil = new TimeUtil()
|
|
65
|
+
* console.log(timeUtil.addToDate({date: timeUtil.now(), unit: 'DAY', value: 1 }).toISOString()) // 2023-03-09T19:45:01.991Z
|
|
66
|
+
* console.log(timeUtil.addToDate({date: timeUtil.now(), unit: DurationUnit.MONTH, value: -1 }).toISOString()) //2023-02-08T19:45:01.991Z
|
|
67
|
+
*/
|
|
68
|
+
addToDate(params: {
|
|
69
|
+
unit: DurationUnitType | DurationUnit;
|
|
70
|
+
value: number;
|
|
71
|
+
date: Date;
|
|
72
|
+
}): Date;
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=time-util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"time-util.d.ts","sourceRoot":"","sources":["../src/time-util.ts"],"names":[],"mappings":"AAKA,oBAAY,YAAY;IACvB,WAAW,gBAAgB;IAC3B,MAAM,WAAW;IACjB,MAAM,WAAW;IACjB,IAAI,SAAS;IACb,GAAG,QAAQ;IACX,IAAI,SAAS;IACb,KAAK,UAAU;IACf,IAAI,SAAS;CACb;AAED,MAAM,MAAM,gBAAgB,GAAG,GAAG,YAAY,EAAE,CAAA;AAEhD,qBAAa,QAAQ;IACpB;;;;;OAKG;IACH,GAAG,IAAI,IAAI;IAIX;;;;;;;;OAQG;IACH,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM;IAI9B;;;;;;;;OAQG;IACH,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM;IAIjC;;;;;;;OAOG;IACH,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAI9B;;;;;;;OAOG;IACH,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAIjC;;;;;;;;;OASG;IACH,SAAS,CAAC,MAAM,EAAE;QAAE,IAAI,EAAE,gBAAgB,GAAG,YAAY,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,IAAI,CAAA;KAAE,GAAG,IAAI;CAQ7F"}
|
package/lib/time-util.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.TimeUtil = exports.DurationUnit = void 0;
|
|
7
|
+
var _index = _interopRequireDefault(require("date-fns/add/index.js"));
|
|
8
|
+
var _index2 = _interopRequireDefault(require("date-fns/addMilliseconds/index.js"));
|
|
9
|
+
var _index3 = _interopRequireDefault(require("date-fns/format/index.js"));
|
|
10
|
+
var _index4 = _interopRequireDefault(require("date-fns/parse/index.js"));
|
|
11
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
12
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
13
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
14
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
15
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
16
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
17
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
18
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
19
|
+
var DurationUnit = exports.DurationUnit = /*#__PURE__*/function (DurationUnit) {
|
|
20
|
+
DurationUnit["MILLISECOND"] = "MILLISECOND";
|
|
21
|
+
DurationUnit["SECOND"] = "SECOND";
|
|
22
|
+
DurationUnit["MINUTE"] = "MINUTE";
|
|
23
|
+
DurationUnit["HOUR"] = "HOUR";
|
|
24
|
+
DurationUnit["DAY"] = "DAY";
|
|
25
|
+
DurationUnit["WEEK"] = "WEEK";
|
|
26
|
+
DurationUnit["MONTH"] = "MONTH";
|
|
27
|
+
DurationUnit["YEAR"] = "YEAR";
|
|
28
|
+
return DurationUnit;
|
|
29
|
+
}({});
|
|
30
|
+
var TimeUtil = exports.TimeUtil = /*#__PURE__*/function () {
|
|
31
|
+
function TimeUtil() {
|
|
32
|
+
_classCallCheck(this, TimeUtil);
|
|
33
|
+
}
|
|
34
|
+
return _createClass(TimeUtil, [{
|
|
35
|
+
key: "now",
|
|
36
|
+
value:
|
|
37
|
+
/**
|
|
38
|
+
* return date object with the current time
|
|
39
|
+
* @return {Date}
|
|
40
|
+
* @example
|
|
41
|
+
* console.log(new TimeUtil().now().toISOString()) // 2023-03-08T19:45:01.991Z
|
|
42
|
+
*/
|
|
43
|
+
function now() {
|
|
44
|
+
return new Date();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Convert date object to unix timestamp (milliseconds)
|
|
49
|
+
* @param {Date} date
|
|
50
|
+
* @return {number}
|
|
51
|
+
* @example
|
|
52
|
+
* // timeUtil.now().toISOString() === 2023-03-08T19:45:01.991Z
|
|
53
|
+
* const timeUtil = new TimeUtil()
|
|
54
|
+
* console.log(timeUtil.dateToUnix(timeUtil.now())) // 1678304701991
|
|
55
|
+
*/
|
|
56
|
+
}, {
|
|
57
|
+
key: "dateToUnix",
|
|
58
|
+
value: function dateToUnix(date) {
|
|
59
|
+
return +(0, _index3["default"])(date, 'T');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Convert date object to unix timestamp (seconds)
|
|
64
|
+
* @param {Date} date
|
|
65
|
+
* @return {number}
|
|
66
|
+
* @example
|
|
67
|
+
* // timeUtil.now().toISOString() === 2023-03-08T19:45:01.991Z
|
|
68
|
+
* const timeUtil = new TimeUtil()
|
|
69
|
+
* console.log(timeUtil.dateToUnix(timeUtil.now())) // 1678304701
|
|
70
|
+
*/
|
|
71
|
+
}, {
|
|
72
|
+
key: "dateToUnixSec",
|
|
73
|
+
value: function dateToUnixSec(date) {
|
|
74
|
+
return +(0, _index3["default"])(date, 't');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Convert unix timestamp (milliseconds) to date object
|
|
79
|
+
* @param {number} unix
|
|
80
|
+
* @return {Date}
|
|
81
|
+
* @example
|
|
82
|
+
* const timeUtil = new TimeUtil()
|
|
83
|
+
* console.log(timeUtil.unixToDate(1678304701991).toISOString()) // 2023-03-08T19:45:01.991Z
|
|
84
|
+
*/
|
|
85
|
+
}, {
|
|
86
|
+
key: "unixToDate",
|
|
87
|
+
value: function unixToDate(unix) {
|
|
88
|
+
return (0, _index4["default"])(unix.toString(), 'T', this.now());
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Convert unix timestamp (seconds) to date object
|
|
93
|
+
* @param {number} unix
|
|
94
|
+
* @return {Date}
|
|
95
|
+
* @example
|
|
96
|
+
* const timeUtil = new TimeUtil()
|
|
97
|
+
* console.log(timeUtil.unixToDate(1678304701).toISOString()) // 2023-03-08T19:45:01.000Z
|
|
98
|
+
*/
|
|
99
|
+
}, {
|
|
100
|
+
key: "unixSecToDate",
|
|
101
|
+
value: function unixSecToDate(unix) {
|
|
102
|
+
return (0, _index4["default"])(unix.toString(), 't', this.now());
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Change the value of date by unit/value pare.
|
|
107
|
+
* @param {{units: DurationUnitType, value: number, date: Date}} params
|
|
108
|
+
* @return {Date}
|
|
109
|
+
* @example
|
|
110
|
+
* // timeUtil.now().toISOString() === 2023-03-08T19:45:01.991Z
|
|
111
|
+
* const timeUtil = new TimeUtil()
|
|
112
|
+
* console.log(timeUtil.addToDate({date: timeUtil.now(), unit: 'DAY', value: 1 }).toISOString()) // 2023-03-09T19:45:01.991Z
|
|
113
|
+
* console.log(timeUtil.addToDate({date: timeUtil.now(), unit: DurationUnit.MONTH, value: -1 }).toISOString()) //2023-02-08T19:45:01.991Z
|
|
114
|
+
*/
|
|
115
|
+
}, {
|
|
116
|
+
key: "addToDate",
|
|
117
|
+
value: function addToDate(params) {
|
|
118
|
+
var date = params.date,
|
|
119
|
+
unit = params.unit,
|
|
120
|
+
value = params.value;
|
|
121
|
+
if ("".concat(unit) === 'MILLISECOND') {
|
|
122
|
+
return (0, _index2["default"])(date, value);
|
|
123
|
+
}
|
|
124
|
+
return (0, _index["default"])(date, _defineProperty({}, "".concat(unit.toLowerCase(), "s"), value));
|
|
125
|
+
}
|
|
126
|
+
}]);
|
|
127
|
+
}();
|