@polyv/utils 1.8.0-rc.1 → 2.0.0-beta.3

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.
Files changed (56) hide show
  1. package/README.md +34 -19
  2. package/dist/cjs/boolean.d.ts +45 -0
  3. package/dist/cjs/boolean.js +1 -0
  4. package/dist/cjs/countdown.d.ts +97 -0
  5. package/dist/cjs/countdown.js +1 -0
  6. package/dist/cjs/date.d.ts +44 -0
  7. package/dist/cjs/date.js +1 -0
  8. package/dist/cjs/json.d.ts +43 -0
  9. package/dist/cjs/json.js +1 -0
  10. package/dist/cjs/net.d.ts +32 -0
  11. package/dist/cjs/net.js +1 -0
  12. package/dist/cjs/string.d.ts +94 -0
  13. package/dist/cjs/string.js +1 -0
  14. package/dist/cjs/validate.d.ts +26 -0
  15. package/dist/cjs/validate.js +1 -0
  16. package/dist/es/boolean.d.ts +45 -0
  17. package/dist/es/boolean.js +1 -0
  18. package/dist/es/countdown.d.ts +97 -0
  19. package/dist/es/countdown.js +1 -0
  20. package/dist/es/date.d.ts +44 -0
  21. package/dist/es/date.js +1 -0
  22. package/dist/es/json.d.ts +43 -0
  23. package/dist/es/json.js +1 -0
  24. package/dist/es/net.d.ts +32 -0
  25. package/dist/es/net.js +1 -0
  26. package/dist/es/string.d.ts +94 -0
  27. package/dist/es/string.js +1 -0
  28. package/dist/es/validate.d.ts +26 -0
  29. package/dist/es/validate.js +1 -0
  30. package/package.json +29 -24
  31. package/dist/boolean.js +0 -1
  32. package/dist/browser.js +0 -1
  33. package/dist/cookie.js +0 -1
  34. package/dist/countdown.js +0 -1
  35. package/dist/date.js +0 -1
  36. package/dist/lang.js +0 -1
  37. package/dist/net.js +0 -1
  38. package/dist/polling.js +0 -1
  39. package/dist/querystring.js +0 -1
  40. package/dist/storage.js +0 -1
  41. package/dist/string.js +0 -1
  42. package/dist/validate.js +0 -1
  43. package/src/boolean.js +0 -67
  44. package/src/browser.js +0 -137
  45. package/src/cookie.js +0 -115
  46. package/src/countdown.js +0 -92
  47. package/src/date.js +0 -89
  48. package/src/internal/core.js +0 -59
  49. package/src/internal/timeunit.js +0 -63
  50. package/src/lang.js +0 -121
  51. package/src/net.js +0 -54
  52. package/src/polling.js +0 -142
  53. package/src/querystring.js +0 -123
  54. package/src/storage.js +0 -106
  55. package/src/string.js +0 -190
  56. package/src/validate.js +0 -42
package/README.md CHANGED
@@ -5,18 +5,20 @@
5
5
  ## 安装
6
6
 
7
7
  ```
8
- npm install @polyv/utils
8
+ npm install @polyv/utils@next
9
9
  ```
10
10
 
11
- NPM 包同时提供了源文件(ES 2015 Modules)以及构建后的文件(CommonJS Modules),分别位于 src 和 dist 两个子文件夹。它们使用上的优缺点和区别在于:
11
+ NPM 包同时提供了 ES 模块以及 CommonJS 模块,分别位于 `dist/es``dist/cjs` 两个文件夹。它们使用上的区别和优缺点在于:
12
12
 
13
- | 文件类型 | Tree shaking | 自行编写构建逻辑 |
13
+ | 模块类型 | Tree shaking | Babel 编译 |
14
14
  | --- | --- | --- |
15
- | 源文件 | 有效,只有用到的代码会被打包 | 需要 |
16
- | 构建后文件 | 无效,依赖的文件会被整个打包 | 不需要 |
15
+ | ES 模块 | 有效,只有用到的代码会被打包 | 需要 |
16
+ | CommonJS 模块 | 无效,依赖的文件会被整个打包 | 不需要 |
17
17
 
18
18
 
19
- ## 在基于 Vue.js 框架的项目中使用
19
+ ## 使用
20
+
21
+ ### 在基于 Vue.js 框架的项目中使用
20
22
 
21
23
  以 Vue CLI 3.x 创建的项目为例,编辑 vue.config.js 增加相关配置:
22
24
 
@@ -28,12 +30,12 @@ module.exports = {
28
30
  resolve: {
29
31
  alias: {
30
32
  // 配置别名缩短引用路径
31
- '@utils': path.resolve(__dirname, './node_modules/@polyv/utils/src')
33
+ '@utils': path.resolve(__dirname, './node_modules/@polyv/utils/dist/es')
32
34
  }
33
35
  }
34
36
  },
35
37
 
36
- // 需要 Babel 转译
38
+ // ES 模块需要 Babel 转译
37
39
  transpileDependencies: [
38
40
  '@polyv/utils'
39
41
  ]
@@ -47,7 +49,7 @@ import { cutStr } from '@utils/string';
47
49
  import Countdown from '@utils/countdown';
48
50
  ```
49
51
 
50
- ## 在基于 Nuxt.js 框架的项目中使用
52
+ ### 在基于 Nuxt.js 框架的项目中使用
51
53
 
52
54
  以 create-nuxt-app 创建的项目为例,编辑 nuxt.config.js 增加相关配置:
53
55
 
@@ -56,22 +58,16 @@ module.exports = {
56
58
  // 省略其他配置
57
59
 
58
60
  build: {
59
- // 需要 Babel 转译
61
+ // ES 模块需要 Babel 转译
60
62
  transpile: [
61
63
  '@polyv/utils'
62
64
  ]
63
65
  },
64
66
 
65
67
  extend(config, ctx) {
66
- // 浏览器端需要 polyfill
67
- if (ctx.isClient) {
68
- config.entry = config.entry || {};
69
- config.entry.polyfill = ['core-js'];
70
- }
71
-
72
68
  // 配置别名缩短引用路径
73
69
  config.resolve.alias['@utils'] = path.resolve(
74
- __dirname, './node_modules/@polyv/utils/src'
70
+ __dirname, './node_modules/@polyv/utils/dist/es'
75
71
  );
76
72
  }
77
73
  };
@@ -81,7 +77,7 @@ module.exports = {
81
77
 
82
78
  ``` javascript
83
79
  import { cutStr } from '@utils/string';
84
- import Countdown from '@utils/countdown';
80
+ import { Countdown } from '@utils/countdown';
85
81
  ```
86
82
 
87
83
  ## 兼容性
@@ -90,4 +86,23 @@ import Countdown from '@utils/countdown';
90
86
  - Android >= 5 (未测试更低版本)
91
87
 
92
88
  ## 其他
93
- - [API 文档](https://polyv.github.io/fed-common-utils/1.x/index.html)
89
+ - [API 文档(1.6.3)](https://polyv.github.io/fed-common-utils/1.6.3/index.html)
90
+ - [API 文档(2.x)](https://polyv.github.io/fed-common-utils/2.x/index.html)
91
+
92
+ ## 2.0.0 版本的变更
93
+
94
+ 2.0.0 与 1.x 相比有较大的变更,如需升级,请参阅下方变更说明:
95
+
96
+ - 模块变更:
97
+ - 原 lang 模块已移除,可以使用 [@just4/util](https://www.npmjs.com/package/@just4/util) 或 [lodash-es](https://www.npmjs.com/package/lodash-es) 代替。
98
+ - 原 browser 模块已移除,可以使用 [@just4/ua-info](https://www.npmjs.com/package/@just4/ua-info) 代替。
99
+ - 原 cookie 模块已移除,可以使用 [@just4/cookie](https://www.npmjs.com/package/@just4/cookie) 代替。
100
+ - 原 polling 模块已移除,可以使用 [@just4/polling](https://www.npmjs.com/package/@just4/polling) 代替。
101
+ - 原 storage 模块已移除,可以使用 [@just4/storage](https://www.npmjs.com/package/@just4/storage) 代替。
102
+ - 部分 API 的变更:
103
+ - boolean 模块的 `ynToBool` 支持指定默认值。
104
+ - countdown 模块的 `Countdown` 类增加 `pause` 方法。
105
+ - string 模块的 `cutStr`、`strLen` 两个方法的选项,不再支持 `mode` 属性。
106
+ - string 模块新增 `uuidV4` 方法。
107
+ - validate 模块的 `isPhoneNO` 方法改名为 `isChsPhoneNO`。
108
+ - CommonJS 模块的路径是 dist/cjs(原来是 dist),ES 模块的路径是 dist/es(原来是 src)。
@@ -0,0 +1,45 @@
1
+ /**
2
+ * 本模块提供布尔值处理相关方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * Y 或者 N。
7
+ */
8
+ export declare type YOrN = 'Y' | 'N';
9
+ /**
10
+ * 布尔值转换为 Y 或者 N。
11
+ * @param value 布尔值。
12
+ * @return Y 或者 N。
13
+ */
14
+ export declare function boolToYN(value: boolean): YOrN;
15
+ /**
16
+ * Y 或者 N 转换为布尔值。
17
+ * @param value Y 或者 N。
18
+ * @param defaultValue 当 value 为非法值时的默认值。
19
+ * @return 布尔值。
20
+ */
21
+ export declare function ynToBool(value: YOrN, defaultValue?: YOrN): boolean;
22
+ /**
23
+ * 检查指定数组元素的值是否都为 Y。
24
+ * @param values 指定数组。
25
+ * @return 指定数组元素的值是否都为 Y。
26
+ */
27
+ export declare function allY(values: YOrN[]): boolean;
28
+ /**
29
+ * 检查指定数组元素的值是否至少有一个为 Y。
30
+ * @param values 指定数组。
31
+ * @return 指定数组元素的值是否至少有一个为 Y。
32
+ */
33
+ export declare function someY(values: YOrN[]): boolean;
34
+ /**
35
+ * 检查指定数组元素的值是否都为 N。
36
+ * @param values 指定数组。
37
+ * @return 指定数组元素的值是否都为 N。
38
+ */
39
+ export declare function allN(values: YOrN[]): boolean;
40
+ /**
41
+ * 检查指定数组元素的值是否至少有一个为 N。
42
+ * @param values 指定数组。
43
+ * @return 指定数组元素的值是否至少有一个为 N。
44
+ */
45
+ export declare function someN(values: YOrN[]): boolean;
@@ -0,0 +1 @@
1
+ "use strict";function boolToYN(o){if("boolean"!=typeof o)throw new Error("The value argument must be a boolean type");return o?"Y":"N"}function ynToBool(o,e){var r=String(o).toUpperCase();if("Y"!==r&&"N"!==r){if(null==e)throw new Error('The value argument must be "Y" or "N"');r=String(e).toUpperCase()}return"Y"===r}function allY(o){return o.every((function(o){return ynToBool(o)}))}function someY(o){return o.some((function(o){return ynToBool(o)}))}function allN(o){return o.every((function(o){return!ynToBool(o)}))}function someN(o){return o.some((function(o){return!ynToBool(o)}))}Object.defineProperty(exports,"__esModule",{value:!0}),exports.someN=exports.allN=exports.someY=exports.allY=exports.ynToBool=exports.boolToYN=void 0,exports.boolToYN=boolToYN,exports.ynToBool=ynToBool,exports.allY=allY,exports.someY=someY,exports.allN=allN,exports.someN=someN;
@@ -0,0 +1,97 @@
1
+ /**
2
+ * 本模块提供倒计时功能。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 表示剩余时长。
7
+ */
8
+ export interface IRemaining {
9
+ /**
10
+ * 剩余天数。
11
+ */
12
+ days: number;
13
+ /**
14
+ * 剩余小时数。
15
+ */
16
+ hours: number;
17
+ /**
18
+ * 剩余分钟数。
19
+ */
20
+ minutes: number;
21
+ /**
22
+ * 剩余秒数。
23
+ */
24
+ seconds: number;
25
+ /**
26
+ * 剩余总时间的毫秒表示。
27
+ */
28
+ totalMsecs: number;
29
+ }
30
+ /**
31
+ * 倒计时的回调函数。
32
+ */
33
+ export interface ICountdownCallback {
34
+ (remaining: IRemaining): void;
35
+ }
36
+ /**
37
+ * 倒计时类。
38
+ * @example
39
+ * ```javascript
40
+ * const countdown = new Countdown(60, (remaining) => {
41
+ * console.dir(remaining);
42
+ * });
43
+ * countdown.start();
44
+ * ```
45
+ */
46
+ export declare class Countdown {
47
+ /**
48
+ * 倒计时总时间的毫秒表示。
49
+ */
50
+ protected readonly _totalMSecs: number;
51
+ /**
52
+ * 倒计时回调函数。
53
+ */
54
+ protected readonly _cb: ICountdownCallback;
55
+ /**
56
+ * 倒计时剩余时间的毫秒表示。
57
+ */
58
+ protected _remainingMSecs?: number;
59
+ /**
60
+ * 当前正在使用的倒计时秒数。
61
+ */
62
+ protected _usingMSecs?: number;
63
+ /**
64
+ * 倒计时开始时间的毫秒级时间戳,为 undefined 时表示倒计时未开始。
65
+ */
66
+ protected _startTime?: number;
67
+ /**
68
+ * 倒计时定时器 id。
69
+ */
70
+ protected _timerId?: number;
71
+ /**
72
+ * 倒计时类构造函数。
73
+ * @param secs 倒计时总秒数。
74
+ * @param cb 剩余时长的回调函数,正常情况下大概 1 秒回调一次。
75
+ */
76
+ constructor(totalSecs: number, cb: ICountdownCallback);
77
+ /**
78
+ * 倒计时的具体操作。
79
+ */
80
+ protected _exec(): void;
81
+ /**
82
+ * 开始倒计时。
83
+ */
84
+ start(): void;
85
+ /**
86
+ * 中断倒计时。
87
+ */
88
+ protected _break(): void;
89
+ /**
90
+ * 停止倒计时。如果再次调用 start,则重新开始倒计时。
91
+ */
92
+ stop(): void;
93
+ /**
94
+ * 暂停倒计时。如果再次调用 start,则继续未完成的倒计时。
95
+ */
96
+ pause(): void;
97
+ }
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Countdown=void 0;var computes=[{divisor:86400,unit:"days"},{divisor:3600,unit:"hours"},{divisor:60,unit:"minutes"},{divisor:1,unit:"seconds"}],Countdown=function(){function t(t,i){if(t|=0,isNaN(t))throw new Error("Total seconds must be a number");this._totalMSecs=1e3*t,this._cb=i}return t.prototype._exec=function(){var t=this;if(this._startTime&&null!=this._usingMSecs){var i=Math.max(0,this._usingMSecs-(Date.now()-this._startTime));if(i>0&&setTimeout((function(){t._exec()}),1e3),this._remainingMSecs=i,i>=0){var s={days:0,hours:0,minutes:0,seconds:0,totalMsecs:i};i=Math.round(i/1e3),computes.forEach((function(t,e){s[t.unit]=i/t.divisor,e===computes.length-1?s[t.unit]=Math.round(s[t.unit]):(s[t.unit]=Math.floor(s[t.unit]),i%=t.divisor)})),this._cb(s)}}},t.prototype.start=function(){null==this._usingMSecs&&(this._usingMSecs=this._totalMSecs),this._usingMSecs<=0||(this._startTime=Date.now(),this._exec())},t.prototype._break=function(){this._timerId&&clearTimeout(this._timerId),this._startTime=void 0},t.prototype.stop=function(){this._break(),this._usingMSecs=this._totalMSecs},t.prototype.pause=function(){this._break(),this._usingMSecs=this._remainingMSecs},t}();exports.Countdown=Countdown;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * 本模块提供日期和时间处理的相关方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 格式化日期。
7
+ * @param date 日期对象或时间戳(毫秒)。
8
+ * @param formation 格式。
9
+ * @return 格式化结果。
10
+ * @example
11
+ * ```javascript
12
+ * formatDate(new Date(2018, 9, 8, 8, 50, 56), 'YYYY-MM-DD hh:mm:ss'); // '2018-10-08 08:50:56'
13
+ * ```
14
+ */
15
+ export declare function formatDate(date: number | Date, formation: string): string;
16
+ /**
17
+ * 秒数格式化配置。
18
+ */
19
+ export interface FormatSecondsOptions {
20
+ /**
21
+ * 段数,2 或者 3。
22
+ * 为 3 时,格式化样式为「时:分:秒」。
23
+ * 为 2 时,格式化样式为「分:秒」。
24
+ */
25
+ segments?: number;
26
+ /**
27
+ * 每一段数字的最小位数,不足位数时补 0。
28
+ */
29
+ digits?: number;
30
+ }
31
+ /**
32
+ * 把秒数格式化成“时:分:秒”格式。
33
+ * @param secs 秒数。
34
+ * @param options 格式化配置。
35
+ * @return 格式化结果。
36
+ * @example
37
+ * ```javascript
38
+ * formatSeconds(3682); // '61:22'
39
+ * formatSeconds(82); // '01:22'
40
+ * formatSeconds(82, { segments: 3 }); // '00:01:22'
41
+ * formatSeconds(3682, { segments: 3, digits: 1 }); // '1:1:22'
42
+ * ```
43
+ */
44
+ export declare function formatSeconds(secs: number, options?: FormatSecondsOptions): string;
@@ -0,0 +1 @@
1
+ "use strict";function ensureDate(e){return"object"!=typeof e&&(e=new Date(e)),e}function formatDate(e,t){var r={Y:(e=ensureDate(e)).getFullYear(),M:e.getMonth()+1,D:e.getDate(),h:e.getHours(),m:e.getMinutes(),s:e.getSeconds()};return t.replace(/([YMDhms])\1*/g,(function(e){var t=r[e[0]].toString(),n=e.length;return n>t.length&&(t=new Array(n-t.length+1).join("0")+t),t}))}function formatSeconds(e,t){if(void 0===t&&(t={segments:2,digits:2}),e=Number(e),isNaN(e)||e<0)throw new Error('"secs" must be a positive integer');var r=0|(t.segments||2),n=0|(t.digits||2);n=Math.max(1,n),-1===[2,3].indexOf(r)&&(r=2);var o=new Array(n+1).join("0"),a=[60,1];return 3===t.segments&&a.unshift(3600),a.map((function(t){var r=Math.floor(e/t),a=r.toString().length;return e%=t,(o+r).slice(-Math.max(a,n))})).join(":")}Object.defineProperty(exports,"__esModule",{value:!0}),exports.formatSeconds=exports.formatDate=void 0,exports.formatDate=formatDate,exports.formatSeconds=formatSeconds;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * 本模块提供 json 处理相关方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 深度克隆指定对象(仅限 JSON 支持的数据类型)。
7
+ * @param obj 指定对象。
8
+ * @return 克隆结果。
9
+ * @example
10
+ * ```javascript
11
+ * cloneJSON({ a: 1, b: 2 }); // { a: 1, b: 2 }
12
+ * ```
13
+ */
14
+ export declare function cloneJSON(obj: unknown): any;
15
+ /**
16
+ * JSON 字符串解析失败时的回调。
17
+ */
18
+ export interface IErrorCallback {
19
+ (e: Error): unknown;
20
+ }
21
+ /**
22
+ * 尝试把指定字符串解析为 JSON 对象。
23
+ * @param str 指定字符串。
24
+ * @param onError 解析出错时执行的回调函数。函数返回值会作为解析失败时的解析结果。
25
+ * @return 解析结果。
26
+ * @example
27
+ * ```javascript
28
+ * tryParseJSON('ss&&**'); // undefined
29
+ * tryParseJSON('{"a": 1}'); // { a: 1 }
30
+ * ```
31
+ */
32
+ export declare function tryParseJSON(str: string, onError?: IErrorCallback): unknown;
33
+ /**
34
+ * 尝试把指定字符串解析为 JSON 对象。
35
+ * @param str 指定字符串。
36
+ * @param defaultValue 解析出错时的默认结果。
37
+ * @return 解析结果。
38
+ * @example
39
+ * ```javascript
40
+ * tryParseJSON('12&&**', 1); // 1
41
+ * ```
42
+ */
43
+ export declare function tryParseJSON(str: string, defaultValue?: unknown): unknown;
@@ -0,0 +1 @@
1
+ "use strict";function cloneJSON(e){return null==e?e:JSON.parse(JSON.stringify(e))}function tryParseJSON(e,r){var t;try{t=JSON.parse(e)}catch(e){t="function"==typeof r?r(e):r}return t}Object.defineProperty(exports,"__esModule",{value:!0}),exports.tryParseJSON=exports.cloneJSON=void 0,exports.cloneJSON=cloneJSON,exports.tryParseJSON=tryParseJSON;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * 本模块提供网络协议相关方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 检查目标字符串是否以特定 URL 协议开头。
7
+ * @param str 目标字符串。
8
+ * @param protocols 特定协议(不含冒号和斜杠),不指定时表示允许任何协议。
9
+ * @return 目标字符串是否以特定 URL 协议开头。
10
+ * @example
11
+ * ```javascript
12
+ * startsWithProtocol('//abc.com'); // true
13
+ * startsWithProtocol('https://abc.com'); // true
14
+ * startsWithProtocol('file:///Users/'); // true
15
+ * startsWithProtocol('abc.com'); // false
16
+ * startsWithProtocol('http://abc.com', ['http', 'https']); // true
17
+ * startsWithProtocol('ftp://abc.com', ['http', 'https']); // false
18
+ * ```
19
+ */
20
+ export declare function startsWithProtocol(str: string, protocols?: string[]): boolean;
21
+ /**
22
+ * 替换目标字符串中的 URL 协议。如果字符串中不包含协议,则加上协议。
23
+ * @param url 目标字符串。
24
+ * @param protocol 协议。
25
+ * @return 替换结果。
26
+ * @example
27
+ * ```javascript
28
+ * changeProtocol('abc.com', 'https'); // 'https://abc.com'
29
+ * changeProtocol('http://abc.com', 'https'); // 'https://abc.com'
30
+ * ```
31
+ */
32
+ export declare function changeProtocol(url: string, protocol: string): string;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.changeProtocol=exports.startsWithProtocol=void 0;var reProtocol=/^(?:([a-z]+):)?\/{2,3}/i;function startsWithProtocol(o,t){var r=reProtocol.test(o);if(r&&t){for(var e=(RegExp.$1||"").toLowerCase(),c=t.length-1;c>=0;c--)if(e===t[c].toLowerCase())return!0;return!1}return r}function changeProtocol(o,t){return reProtocol.test(t)||(t+="://"),startsWithProtocol(o)?o.replace(reProtocol,t):t+o}exports.startsWithProtocol=startsWithProtocol,exports.changeProtocol=changeProtocol;
@@ -0,0 +1,94 @@
1
+ /**
2
+ * 本模块提供字符串处理相关方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 计算字符串长度的选项。
7
+ */
8
+ export interface IStrLenOptions {
9
+ /**
10
+ * 英文字符单位长度。
11
+ */
12
+ enLen?: number;
13
+ /**
14
+ * 非英文字符单位长度。
15
+ */
16
+ nonEnLen?: number;
17
+ }
18
+ /**
19
+ * 裁剪字符串的选项。
20
+ */
21
+ export interface ICutStrOptions extends IStrLenOptions {
22
+ /**
23
+ * 省略符,默认为三个点号。
24
+ */
25
+ ellipsis?: string;
26
+ }
27
+ /**
28
+ * 计算字符串长度。默认情况下,英文字符的单位长度为 1,非英文字符的单位长度为 2。
29
+ * @param str 字符串。
30
+ * @param options 为数字时表示非英文字符单位长度(此时英文字符单位长度为 1);为 Object 时表示选项。
31
+ * @return 字符串长度。
32
+ * @example
33
+ * ```javascript
34
+ * strLen('abcde;'); // 6
35
+ * strLen('abc测试;'); // 9
36
+ * strLen('abc测试;', 1); // 6
37
+ * strLen('abc测试;', { enLen: 0.5, nonEnLen: 1 }); // 4.5
38
+ * ```
39
+ */
40
+ export declare function strLen(str: string, options?: number | IStrLenOptions): number;
41
+ /**
42
+ * 如果目标字符串超出限制长度,则进行截断并拼接省略符号;否则返回目标字符串。
43
+ * 默认情况下,英文字符的单位长度为 1,非英文字符的单位长度为 2。
44
+ * @param str 目标字符串。
45
+ * @param length 限制的长度。
46
+ * @param options 选项。
47
+ * @return 截断后的字符串。
48
+ * @example
49
+ * ```javascript
50
+ * cutStr('测试一下', 5); // '测试...'
51
+ * cutStr('测试一下', 8); // '测试一下'
52
+ * curStr('1测试2测试3', 3.5, { enLen: 0.5, nonEnLen: 1 }); // 1测...
53
+ * ```
54
+ */
55
+ export declare function cutStr(str: string, length: number, options?: ICutStrOptions): string;
56
+ /**
57
+ * 把指定字符串中的 HTML 预留字符替换成 HTML 实体。
58
+ * @param str 指定字符串。
59
+ * @return 替换后的字符串。
60
+ */
61
+ export declare function escapeHTML(str: string): string;
62
+ /**
63
+ * 移除指定字符串中的 HTML 标签。
64
+ * @param str 指定字符串。
65
+ * @return 处理后的字符串。
66
+ */
67
+ export declare function removeTags(str: string): string;
68
+ /**
69
+ * 把指定字符串中的换行符替换成 &lt;br /&gt;。
70
+ * @param str 指定字符串。
71
+ * @return 替换后的字符串。
72
+ */
73
+ export declare function nl2br(str: string): string;
74
+ /**
75
+ * 生成随机字符串。
76
+ * @param length 字符串长度。
77
+ * @param prefix 字符串前缀(不计入长度)。
78
+ * @returns 生成的随机字符串。
79
+ */
80
+ export declare function randomStr(length: number, prefix?: string): string;
81
+ /**
82
+ * 生成 uuid(v4)。
83
+ * @returns uuid。
84
+ */
85
+ export declare function uuidV4(): string;
86
+ /**
87
+ * 版本号对比。
88
+ * @param versionA 待比较版本 A。
89
+ * @param versionB 待比较版本 B。
90
+ * @return 大于 0 时,表示版本 A 大于版本 B;
91
+ * 小于 0 时,表示版本 B 大于版本 A;
92
+ * 等于 0 时,表示两个版本号一致。
93
+ */
94
+ export declare function compareVersions(verA: string, verB: string): number;
@@ -0,0 +1 @@
1
+ "use strict";function strLen(e,r){var n,t;"number"==typeof r?r={enLen:1,nonEnLen:r}:((r=r||{}).enLen=null!==(n=r.enLen)&&void 0!==n?n:1,r.nonEnLen=null!==(t=r.nonEnLen)&&void 0!==t?t:2);for(var o=0,s=e.length-1;s>=0;s--)o+=e.charCodeAt(s)>255?r.nonEnLen||0:r.enLen||0;return o}function cutStr(e,r,n){(n=n||{}).enLen=Number(n.enLen)||1,n.nonEnLen=Number(n.nonEnLen)||2,n.ellipsis=n.ellipsis||"...";var t=strLen(e=String(e),n);if(t<=r)return e;r-=strLen(n.ellipsis,n);for(var o="",s=-1;r>0&&++s<t;)(r-=e.charCodeAt(s)>255?n.nonEnLen:n.enLen)>=0&&(o+=e.charAt(s));return o+=n.ellipsis}function escapeHTML(e){if(null==e)return e;var r={'"':"&quot;","'":"&#39;","&":"&amp;","<":"&lt;",">":"&gt;"};return String(e).replace(/["'&<>]/g,(function(e){return r[e]}))}function removeTags(e){return null==e?"":String(e).replace(/<.+?>/g,"")}function nl2br(e){return null==e?e:String(e).replace(/\r?\n/g,"<br />")}function randomStr(e,r){if(!(e|=0)||e<0)throw new Error('"length" must be a number greater than 0');var n="";do{n+=Math.random().toString(36).substr(2)}while(n.length<e);return n=n.substr(0,e),null!=r&&(n=r+n),n}function uuidV4(){var e=Date.now();return"undefined"!=typeof performance&&"function"==typeof performance.now&&(e+=performance.now()),"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(r){var n=(e+16*Math.random())%16|0;return e=Math.floor(e/16),("x"===r?n:3&n|8).toString(16)}))}function compareVersions(e,r){if(!e||!r)throw new Error("Please specify both verA and verB");for(var n=/(\.0+)+$/,t=String(e).replace(n,"").split("."),o=String(r).replace(n,"").split("."),s=Math.min(t.length,o.length),a=0;a<s;a++){var u=parseInt(t[a])-parseInt(o[a]);if(u)return u}return t.length-o.length}Object.defineProperty(exports,"__esModule",{value:!0}),exports.compareVersions=exports.uuidV4=exports.randomStr=exports.nl2br=exports.removeTags=exports.escapeHTML=exports.cutStr=exports.strLen=void 0,exports.strLen=strLen,exports.cutStr=cutStr,exports.escapeHTML=escapeHTML,exports.removeTags=removeTags,exports.nl2br=nl2br,exports.randomStr=randomStr,exports.uuidV4=uuidV4,exports.compareVersions=compareVersions;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * 本模块提供数据验证方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 检查目标字符串是否中国大陆手机号。
7
+ * @param str 目标字符串。
8
+ * @return 目标字符串是否中国大陆手机号。
9
+ * @example
10
+ * ```javascript
11
+ * isChsPhoneNO('13800138000'); // true
12
+ * isChsPhoneNO('a13800138000c'); // false
13
+ * ```
14
+ */
15
+ export declare function isChsPhoneNO(str: string): boolean;
16
+ /**
17
+ * 检查目标字符串是否电子邮箱地址。
18
+ * @param str 目标字符串。
19
+ * @return 目标字符串是否电子邮箱地址。
20
+ * @example
21
+ * ```javascript
22
+ * isEmail('me@polyv.net'); // true
23
+ * isEmail('me@polyv_.net-'); // false
24
+ * ```
25
+ */
26
+ export declare function isEmail(str: string): boolean;
@@ -0,0 +1 @@
1
+ "use strict";function isChsPhoneNO(e){return/^1[3-9]\d{9}$/.test(e)}function isEmail(e){if(/^[\w-]+(?:\.[\w-]+)*@[\w-]+(?:\.[\w-]+)*\.[a-zA-Z]{2,}$/.test(e)){for(var s=e.replace("@",".").split("."),t=s.length-1;t>=0;t--)if(/^[-_]/.test(s[t])||/[_-]$/.test(s[t]))return!1;return!0}return!1}Object.defineProperty(exports,"__esModule",{value:!0}),exports.isEmail=exports.isChsPhoneNO=void 0,exports.isChsPhoneNO=isChsPhoneNO,exports.isEmail=isEmail;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * 本模块提供布尔值处理相关方法。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * Y 或者 N。
7
+ */
8
+ export declare type YOrN = 'Y' | 'N';
9
+ /**
10
+ * 布尔值转换为 Y 或者 N。
11
+ * @param value 布尔值。
12
+ * @return Y 或者 N。
13
+ */
14
+ export declare function boolToYN(value: boolean): YOrN;
15
+ /**
16
+ * Y 或者 N 转换为布尔值。
17
+ * @param value Y 或者 N。
18
+ * @param defaultValue 当 value 为非法值时的默认值。
19
+ * @return 布尔值。
20
+ */
21
+ export declare function ynToBool(value: YOrN, defaultValue?: YOrN): boolean;
22
+ /**
23
+ * 检查指定数组元素的值是否都为 Y。
24
+ * @param values 指定数组。
25
+ * @return 指定数组元素的值是否都为 Y。
26
+ */
27
+ export declare function allY(values: YOrN[]): boolean;
28
+ /**
29
+ * 检查指定数组元素的值是否至少有一个为 Y。
30
+ * @param values 指定数组。
31
+ * @return 指定数组元素的值是否至少有一个为 Y。
32
+ */
33
+ export declare function someY(values: YOrN[]): boolean;
34
+ /**
35
+ * 检查指定数组元素的值是否都为 N。
36
+ * @param values 指定数组。
37
+ * @return 指定数组元素的值是否都为 N。
38
+ */
39
+ export declare function allN(values: YOrN[]): boolean;
40
+ /**
41
+ * 检查指定数组元素的值是否至少有一个为 N。
42
+ * @param values 指定数组。
43
+ * @return 指定数组元素的值是否至少有一个为 N。
44
+ */
45
+ export declare function someN(values: YOrN[]): boolean;
@@ -0,0 +1 @@
1
+ export function boolToYN(o){if("boolean"!=typeof o)throw new Error("The value argument must be a boolean type");return o?"Y":"N"}export function ynToBool(o,n){let e=String(o).toUpperCase();if("Y"!==e&&"N"!==e){if(null==n)throw new Error('The value argument must be "Y" or "N"');e=String(n).toUpperCase()}return"Y"===e}export function allY(o){return o.every((function(o){return ynToBool(o)}))}export function someY(o){return o.some((function(o){return ynToBool(o)}))}export function allN(o){return o.every((function(o){return!ynToBool(o)}))}export function someN(o){return o.some((function(o){return!ynToBool(o)}))}
@@ -0,0 +1,97 @@
1
+ /**
2
+ * 本模块提供倒计时功能。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 表示剩余时长。
7
+ */
8
+ export interface IRemaining {
9
+ /**
10
+ * 剩余天数。
11
+ */
12
+ days: number;
13
+ /**
14
+ * 剩余小时数。
15
+ */
16
+ hours: number;
17
+ /**
18
+ * 剩余分钟数。
19
+ */
20
+ minutes: number;
21
+ /**
22
+ * 剩余秒数。
23
+ */
24
+ seconds: number;
25
+ /**
26
+ * 剩余总时间的毫秒表示。
27
+ */
28
+ totalMsecs: number;
29
+ }
30
+ /**
31
+ * 倒计时的回调函数。
32
+ */
33
+ export interface ICountdownCallback {
34
+ (remaining: IRemaining): void;
35
+ }
36
+ /**
37
+ * 倒计时类。
38
+ * @example
39
+ * ```javascript
40
+ * const countdown = new Countdown(60, (remaining) => {
41
+ * console.dir(remaining);
42
+ * });
43
+ * countdown.start();
44
+ * ```
45
+ */
46
+ export declare class Countdown {
47
+ /**
48
+ * 倒计时总时间的毫秒表示。
49
+ */
50
+ protected readonly _totalMSecs: number;
51
+ /**
52
+ * 倒计时回调函数。
53
+ */
54
+ protected readonly _cb: ICountdownCallback;
55
+ /**
56
+ * 倒计时剩余时间的毫秒表示。
57
+ */
58
+ protected _remainingMSecs?: number;
59
+ /**
60
+ * 当前正在使用的倒计时秒数。
61
+ */
62
+ protected _usingMSecs?: number;
63
+ /**
64
+ * 倒计时开始时间的毫秒级时间戳,为 undefined 时表示倒计时未开始。
65
+ */
66
+ protected _startTime?: number;
67
+ /**
68
+ * 倒计时定时器 id。
69
+ */
70
+ protected _timerId?: number;
71
+ /**
72
+ * 倒计时类构造函数。
73
+ * @param secs 倒计时总秒数。
74
+ * @param cb 剩余时长的回调函数,正常情况下大概 1 秒回调一次。
75
+ */
76
+ constructor(totalSecs: number, cb: ICountdownCallback);
77
+ /**
78
+ * 倒计时的具体操作。
79
+ */
80
+ protected _exec(): void;
81
+ /**
82
+ * 开始倒计时。
83
+ */
84
+ start(): void;
85
+ /**
86
+ * 中断倒计时。
87
+ */
88
+ protected _break(): void;
89
+ /**
90
+ * 停止倒计时。如果再次调用 start,则重新开始倒计时。
91
+ */
92
+ stop(): void;
93
+ /**
94
+ * 暂停倒计时。如果再次调用 start,则继续未完成的倒计时。
95
+ */
96
+ pause(): void;
97
+ }
@@ -0,0 +1 @@
1
+ const computes=[{divisor:86400,unit:"days"},{divisor:3600,unit:"hours"},{divisor:60,unit:"minutes"},{divisor:1,unit:"seconds"}];export class Countdown{constructor(s,t){if(s|=0,isNaN(s))throw new Error("Total seconds must be a number");this._totalMSecs=1e3*s,this._cb=t}_exec(){if(!this._startTime||null==this._usingMSecs)return;let s=Math.max(0,this._usingMSecs-(Date.now()-this._startTime));if(s>0&&setTimeout((()=>{this._exec()}),1e3),this._remainingMSecs=s,s>=0){const t={days:0,hours:0,minutes:0,seconds:0,totalMsecs:s};s=Math.round(s/1e3),computes.forEach(((i,e)=>{t[i.unit]=s/i.divisor,e===computes.length-1?t[i.unit]=Math.round(t[i.unit]):(t[i.unit]=Math.floor(t[i.unit]),s%=i.divisor)})),this._cb(t)}}start(){null==this._usingMSecs&&(this._usingMSecs=this._totalMSecs),this._usingMSecs<=0||(this._startTime=Date.now(),this._exec())}_break(){this._timerId&&clearTimeout(this._timerId),this._startTime=void 0}stop(){this._break(),this._usingMSecs=this._totalMSecs}pause(){this._break(),this._usingMSecs=this._remainingMSecs}}