util-helpers 4.3.0 → 4.4.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/README.md +2 -2
- package/dist/util-helpers.js +36 -1
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/index.js +1 -0
- package/esm/normalizeString.js +1 -0
- package/esm/safeDate.js +40 -0
- package/lib/index.js +8 -0
- package/lib/normalizeString.js +1 -0
- package/lib/safeDate.js +56 -0
- package/package.json +1 -1
- package/types/index.d.ts +1 -0
- package/types/normalizeString.d.ts +1 -0
- package/types/safeDate.d.ts +21 -0
package/esm/safeDate.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import _construct from "@babel/runtime/helpers/construct";
|
|
2
|
+
import isNil from './utils/type/isNil'; // TODO: 函数重载,类型参照 Date
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 创建一个 Date 实例,同 new Date() 。<br/><br/>主要是规避了 iphone 设备浏览器不支持部分格式(YYYY-MM-DD HH-mm 或 YYYY.MM.DD)。
|
|
6
|
+
*
|
|
7
|
+
* @static
|
|
8
|
+
* @alias module:Processor.safeDate
|
|
9
|
+
* @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date|Date}
|
|
10
|
+
* @since 4.4.0
|
|
11
|
+
* @param {string|number|Date} [value] 日期时间字符串、毫秒数、日期对象
|
|
12
|
+
* @param {...number} args 月/日/时/分/秒/毫秒
|
|
13
|
+
* @returns {Date} Date 实例
|
|
14
|
+
* @example
|
|
15
|
+
* safeDate('2022-1-1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
|
|
16
|
+
* safeDate('2022/1/1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
|
|
17
|
+
* safeDate('2022.1.1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
|
|
18
|
+
* safeDate('2022.1.1 11:11'); // Sat Jan 01 2022 11:11:00 GMT+0800 (中国标准时间)
|
|
19
|
+
* safeDate(99, 1); // Mon Feb 01 1999 00:00:00 GMT+0800 (中国标准时间)
|
|
20
|
+
* safeDate(1646711233171); // Tue Mar 08 2022 11:47:13 GMT+0800 (中国标准时间)
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
function safeDate(value) {
|
|
25
|
+
var safeValue = typeof value === 'string' ? value.replace(/[\\.-]/g, '/') : value;
|
|
26
|
+
|
|
27
|
+
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
28
|
+
args[_key - 1] = arguments[_key];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (args && args.length > 0) {
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
return _construct(Date, [safeValue].concat(args));
|
|
34
|
+
} // @ts-ignore
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
return isNil(safeValue) ? new Date() : new Date(safeValue);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default safeDate;
|
package/lib/index.js
CHANGED
|
@@ -189,6 +189,12 @@ Object.defineProperty(exports, "normalizeString", {
|
|
|
189
189
|
return _normalizeString["default"];
|
|
190
190
|
}
|
|
191
191
|
});
|
|
192
|
+
Object.defineProperty(exports, "safeDate", {
|
|
193
|
+
enumerable: true,
|
|
194
|
+
get: function get() {
|
|
195
|
+
return _safeDate["default"];
|
|
196
|
+
}
|
|
197
|
+
});
|
|
192
198
|
Object.defineProperty(exports, "plus", {
|
|
193
199
|
enumerable: true,
|
|
194
200
|
get: function get() {
|
|
@@ -294,6 +300,8 @@ var _setDataURLPrefix = _interopRequireDefault(require("./setDataURLPrefix"));
|
|
|
294
300
|
|
|
295
301
|
var _normalizeString = _interopRequireDefault(require("./normalizeString"));
|
|
296
302
|
|
|
303
|
+
var _safeDate = _interopRequireDefault(require("./safeDate"));
|
|
304
|
+
|
|
297
305
|
var _plus = _interopRequireDefault(require("./plus"));
|
|
298
306
|
|
|
299
307
|
var _minus = _interopRequireDefault(require("./minus"));
|
package/lib/normalizeString.js
CHANGED
|
@@ -16,6 +16,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
|
|
|
16
16
|
*
|
|
17
17
|
* @static
|
|
18
18
|
* @alias module:Processor.normalizeString
|
|
19
|
+
* @see 参考 {@link tttps://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#string_instances|String}
|
|
19
20
|
* @since 4.3.0
|
|
20
21
|
* @param {*} value 待处理的值
|
|
21
22
|
* @returns {string} 规整化的值
|
package/lib/safeDate.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
|
|
8
|
+
var _isNil = _interopRequireDefault(require("./utils/type/isNil"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
11
|
+
|
|
12
|
+
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
|
|
13
|
+
|
|
14
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
15
|
+
|
|
16
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
17
|
+
|
|
18
|
+
// TODO: 函数重载,类型参照 Date
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 创建一个 Date 实例,同 new Date() 。<br/><br/>主要是规避了 iphone 设备浏览器不支持部分格式(YYYY-MM-DD HH-mm 或 YYYY.MM.DD)。
|
|
22
|
+
*
|
|
23
|
+
* @static
|
|
24
|
+
* @alias module:Processor.safeDate
|
|
25
|
+
* @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date|Date}
|
|
26
|
+
* @since 4.4.0
|
|
27
|
+
* @param {string|number|Date} [value] 日期时间字符串、毫秒数、日期对象
|
|
28
|
+
* @param {...number} args 月/日/时/分/秒/毫秒
|
|
29
|
+
* @returns {Date} Date 实例
|
|
30
|
+
* @example
|
|
31
|
+
* safeDate('2022-1-1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
|
|
32
|
+
* safeDate('2022/1/1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
|
|
33
|
+
* safeDate('2022.1.1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
|
|
34
|
+
* safeDate('2022.1.1 11:11'); // Sat Jan 01 2022 11:11:00 GMT+0800 (中国标准时间)
|
|
35
|
+
* safeDate(99, 1); // Mon Feb 01 1999 00:00:00 GMT+0800 (中国标准时间)
|
|
36
|
+
* safeDate(1646711233171); // Tue Mar 08 2022 11:47:13 GMT+0800 (中国标准时间)
|
|
37
|
+
*
|
|
38
|
+
*/
|
|
39
|
+
function safeDate(value) {
|
|
40
|
+
var safeValue = typeof value === 'string' ? value.replace(/[\\.-]/g, '/') : value;
|
|
41
|
+
|
|
42
|
+
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
43
|
+
args[_key - 1] = arguments[_key];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (args && args.length > 0) {
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
return _construct(Date, [safeValue].concat(args));
|
|
49
|
+
} // @ts-ignore
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
return (0, _isNil["default"])(safeValue) ? new Date() : new Date(safeValue);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
var _default = safeDate;
|
|
56
|
+
exports["default"] = _default;
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ export { default as blobToDataURL } from "./blobToDataURL";
|
|
|
29
29
|
export { default as dataURLToBlob } from "./dataURLToBlob";
|
|
30
30
|
export { default as setDataURLPrefix } from "./setDataURLPrefix";
|
|
31
31
|
export { default as normalizeString } from "./normalizeString";
|
|
32
|
+
export { default as safeDate } from "./safeDate";
|
|
32
33
|
export { default as plus } from "./plus";
|
|
33
34
|
export { default as minus } from "./minus";
|
|
34
35
|
export { default as times } from "./times";
|
|
@@ -4,6 +4,7 @@ export default normalizeString;
|
|
|
4
4
|
*
|
|
5
5
|
* @static
|
|
6
6
|
* @alias module:Processor.normalizeString
|
|
7
|
+
* @see 参考 {@link tttps://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#string_instances|String}
|
|
7
8
|
* @since 4.3.0
|
|
8
9
|
* @param {*} value 待处理的值
|
|
9
10
|
* @returns {string} 规整化的值
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export default safeDate;
|
|
2
|
+
/**
|
|
3
|
+
* 创建一个 Date 实例,同 new Date() 。<br/><br/>主要是规避了 iphone 设备浏览器不支持部分格式(YYYY-MM-DD HH-mm 或 YYYY.MM.DD)。
|
|
4
|
+
*
|
|
5
|
+
* @static
|
|
6
|
+
* @alias module:Processor.safeDate
|
|
7
|
+
* @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date|Date}
|
|
8
|
+
* @since 4.4.0
|
|
9
|
+
* @param {string|number|Date} [value] 日期时间字符串、毫秒数、日期对象
|
|
10
|
+
* @param {...number} args 月/日/时/分/秒/毫秒
|
|
11
|
+
* @returns {Date} Date 实例
|
|
12
|
+
* @example
|
|
13
|
+
* safeDate('2022-1-1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
|
|
14
|
+
* safeDate('2022/1/1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
|
|
15
|
+
* safeDate('2022.1.1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
|
|
16
|
+
* safeDate('2022.1.1 11:11'); // Sat Jan 01 2022 11:11:00 GMT+0800 (中国标准时间)
|
|
17
|
+
* safeDate(99, 1); // Mon Feb 01 1999 00:00:00 GMT+0800 (中国标准时间)
|
|
18
|
+
* safeDate(1646711233171); // Tue Mar 08 2022 11:47:13 GMT+0800 (中国标准时间)
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
declare function safeDate(value?: string | number | Date | undefined, ...args: number[]): Date;
|