mazey 3.5.4 → 3.6.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/src/index.ts CHANGED
@@ -22,6 +22,7 @@
22
22
  * @param {string} aStr String
23
23
  * @param {string} bStr String
24
24
  * @returns {number} Length
25
+ * @category Calculate and Formula
25
26
  */
26
27
  export function calLongestCommonSubstring(aStr: string, bStr: string): number {
27
28
  const aLen = aStr.length;
@@ -66,6 +67,7 @@ export function calLongestCommonSubstring(aStr: string, bStr: string): number {
66
67
  * @param {string} aStr 字符串
67
68
  * @param {string} bStr 字符串
68
69
  * @returns {number} 长度
70
+ * @category Calculate and Formula
69
71
  */
70
72
  export function calLongestCommonSubsequence(
71
73
  aStr: string,
@@ -270,6 +272,7 @@ export function getDomain(url: string, rules = ['hostname']): string {
270
272
  *
271
273
  * @param {string} camelCase 'aBC' or 'ABC'
272
274
  * @returns {string} 'a-b-c'
275
+ * @category Util
273
276
  */
274
277
  export function camelCaseToKebabCase(camelCase: string): string {
275
278
  const kebabCase = camelCase.replace(/([A-Z])/g, '-$1').toLowerCase();
@@ -286,6 +289,7 @@ export function camelCaseToKebabCase(camelCase: string): string {
286
289
  *
287
290
  * @param {string} camelCase 'aBC' or 'ABC'
288
291
  * @returns {string} 'a_b_c'
292
+ * @category Util
289
293
  */
290
294
  export function camelCase2Underscore(camelCase: string): string {
291
295
  const kebabCase = camelCase.replace(/([A-Z])/g, '_$1').toLowerCase();
@@ -302,6 +306,7 @@ export function camelCase2Underscore(camelCase: string): string {
302
306
  *
303
307
  * @param {string} str The string to trim.
304
308
  * @returns {string} Trimmed string.
309
+ * @category Util
305
310
  */
306
311
  export function mTrim(str: string): string {
307
312
  str = str.replace(/^\s+/, ''); // 去除头部空格
@@ -314,10 +319,16 @@ export function mTrim(str: string): string {
314
319
  }
315
320
 
316
321
  /**
317
- * @method newLine
318
- * @description Make a newline of HTML.
322
+ * Make a newline of HTML.
323
+ *
324
+ * ```
325
+ * newLine('a\nb\nc'); // 'a<br />b<br />c'
326
+ * newLine('a\n\nbc'); // 'a<br /><br />bc'
327
+ * ```
328
+ *
319
329
  * @param {string} str The string to make a newline.
320
330
  * @returns {string} A newline with `br`.
331
+ * @category DOM
321
332
  */
322
333
  export function newLine(str: string): string {
323
334
  if (!str) {
@@ -346,6 +357,7 @@ export function newLine(str: string): string {
346
357
  *
347
358
  * @param {object} obj The value to clone.
348
359
  * @returns {object} Returns the deep cloned value.
360
+ * @category Util
349
361
  */
350
362
  export function deepCopyObject(obj: any): any {
351
363
  return JSON.parse(JSON.stringify(obj));
@@ -367,6 +379,10 @@ export function deepCopyObject(obj: any): any {
367
379
  * false
368
380
  * true
369
381
  * ```
382
+ *
383
+ * @param {string} str The string to check.
384
+ * @returns {boolean} Return the result of checking.
385
+ * @category Util
370
386
  */
371
387
  export function isJsonString(str: string): boolean {
372
388
  try {
@@ -380,10 +396,16 @@ export function isJsonString(str: string): boolean {
380
396
  }
381
397
 
382
398
  /**
383
- * @method generateRndNum
384
- * @description Produce a random string of number, `generateRndNum(7)` => '7658495'.
399
+ * Produce a random string of number, `generateRndNum(7)` => '7658495'.
400
+ *
401
+ * ```
402
+ * generateRndNum(4); // '9730'
403
+ * generateRndNum(7); // '2262490'
404
+ * ```
405
+ *
385
406
  * @param {number} n Length
386
407
  * @returns {string} Return the random string.
408
+ * @category Util
387
409
  */
388
410
  export function generateRndNum(n = 5): string {
389
411
  let ret = '';
@@ -443,7 +465,20 @@ export function cancelBubble(e: any): void {
443
465
  }
444
466
 
445
467
  /**
446
- * @method hasClass
468
+ * Modify `class`.
469
+ *
470
+ * ```
471
+ * const dom = document.querySelector('#box');
472
+ *
473
+ * // Determine `class`
474
+ * hasClass(dom, 'test');
475
+ * // Add `class`
476
+ * addClass(dom, 'test');
477
+ * // Remove `class`
478
+ * removeClass(dom, 'test');
479
+ * ```
480
+ *
481
+ * @category DOM
447
482
  */
448
483
  export function hasClass(obj: any, cls: string): boolean {
449
484
  const oriCls = obj.className; // 获取对象的class值
@@ -457,7 +492,20 @@ export function hasClass(obj: any, cls: string): boolean {
457
492
  }
458
493
 
459
494
  /**
460
- * @method addClass
495
+ * Modify `class`.
496
+ *
497
+ * ```
498
+ * const dom = document.querySelector('#box');
499
+ *
500
+ * // Determine `class`
501
+ * hasClass(dom, 'test');
502
+ * // Add `class`
503
+ * addClass(dom, 'test');
504
+ * // Remove `class`
505
+ * removeClass(dom, 'test');
506
+ * ```
507
+ *
508
+ * @category DOM
461
509
  */
462
510
  export function addClass(obj: any, cls: string): void {
463
511
  const oriCls = obj.className;
@@ -471,7 +519,20 @@ export function addClass(obj: any, cls: string): void {
471
519
  }
472
520
 
473
521
  /**
474
- * @method removeClass
522
+ * Modify `class`.
523
+ *
524
+ * ```
525
+ * const dom = document.querySelector('#box');
526
+ *
527
+ * // Determine `class`
528
+ * hasClass(dom, 'test');
529
+ * // Add `class`
530
+ * addClass(dom, 'test');
531
+ * // Remove `class`
532
+ * removeClass(dom, 'test');
533
+ * ```
534
+ *
535
+ * @category DOM
475
536
  */
476
537
  export function removeClass(obj: any, cls: string): void {
477
538
  const oriCls = obj.className;
@@ -495,6 +556,8 @@ export function removeClass(obj: any, cls: string): void {
495
556
  * ```
496
557
  *
497
558
  * Reference: [Lodash](https://lodash.com/docs/4.17.15#throttle)
559
+ *
560
+ * @category Util
498
561
  */
499
562
  export function throttle(
500
563
  func: any,
@@ -508,9 +571,6 @@ export function throttle(
508
571
  let args: any = null;
509
572
  let timeout: any = null;
510
573
  let [result, previous] = [null, 0];
511
- // if (!options) {
512
- // options = {};
513
- // }
514
574
  const later = function() {
515
575
  previous = options.leading === false ? 0 : mNow();
516
576
  timeout = null;
@@ -554,6 +614,8 @@ export function throttle(
554
614
  * console.log('The debounced function will only be invoked in 1000 milliseconds, the other invoking will disappear during the wait time.');
555
615
  * }, 1000, true);
556
616
  * ```
617
+ *
618
+ * @category Util
557
619
  */
558
620
  export function debounce(func: any, wait: number, immediate?: any): any {
559
621
  let context: any = null;
@@ -652,6 +714,7 @@ export function friendlyInterval(
652
714
  * @param {boolean} options.isNaNAsNumber 是否 NaN 算数字(默认不算)
653
715
  * @param {boolean} options.isUnFiniteAsNumber 是否 无限 算数字(默认不算)
654
716
  * @returns {boolean} true 是数字
717
+ * @category Util
655
718
  */
656
719
  export function isNumber(
657
720
  num: any,
@@ -696,10 +759,30 @@ export function doFn(fn: any, ...params: any[]): any {
696
759
  }
697
760
 
698
761
  /**
699
- * @method setSessionStorage
700
- * @description 存储数据到 sessionStorage
762
+ * EN: Handle Storage (Keep fit for JSON, it can tansfer format automatically).
763
+ *
764
+ * ZH: 存储/获取数据到 sessionStorage/localStorage
765
+ *
766
+ * ```
767
+ * setSessionStorage('test', '123');
768
+ * getSessionStorage('test'); // 123
769
+ * setLocalStorage('test', '123');
770
+ * getLocalStorage('test'); // 123
771
+ *
772
+ * // or package in usage
773
+ * const projectName = 'mazey';
774
+ * function mSetLocalStorage (key, value) {
775
+ * return setLocalStorage(`${projectName}_${key}`, value);
776
+ * }
777
+ *
778
+ * function mGetLocalStorage (key) {
779
+ * return getLocalStorage(`${projectName}_${key}`);
780
+ * }
781
+ * ```
782
+ *
701
783
  * @param {string} key 键
702
- * @param {string} value 值
784
+ * @returns {any} 返回值
785
+ * @category Cache Data
703
786
  */
704
787
  export function setSessionStorage(key: string, value: any = null): void {
705
788
  if (key) {
@@ -708,10 +791,30 @@ export function setSessionStorage(key: string, value: any = null): void {
708
791
  }
709
792
 
710
793
  /**
711
- * @method getSessionStorage
712
- * @description 存储数据到 sessionStorage
794
+ * EN: Handle Storage (Keep fit for JSON, it can tansfer format automatically).
795
+ *
796
+ * ZH: 存储/获取数据到 sessionStorage/localStorage
797
+ *
798
+ * ```
799
+ * setSessionStorage('test', '123');
800
+ * getSessionStorage('test'); // 123
801
+ * setLocalStorage('test', '123');
802
+ * getLocalStorage('test'); // 123
803
+ *
804
+ * // or package in usage
805
+ * const projectName = 'mazey';
806
+ * function mSetLocalStorage (key, value) {
807
+ * return setLocalStorage(`${projectName}_${key}`, value);
808
+ * }
809
+ *
810
+ * function mGetLocalStorage (key) {
811
+ * return getLocalStorage(`${projectName}_${key}`);
812
+ * }
813
+ * ```
814
+ *
713
815
  * @param {string} key 键
714
- * @returns {*} 返回值
816
+ * @returns {any} 返回值
817
+ * @category Cache Data
715
818
  */
716
819
  export function getSessionStorage(key: string): any {
717
820
  let ret = null;
@@ -725,10 +828,30 @@ export function getSessionStorage(key: string): any {
725
828
  }
726
829
 
727
830
  /**
728
- * @method setLocalStorage
729
- * @description 存储数据到 localStorage
831
+ * EN: Handle Storage (Keep fit for JSON, it can tansfer format automatically).
832
+ *
833
+ * ZH: 存储/获取数据到 sessionStorage/localStorage
834
+ *
835
+ * ```
836
+ * setSessionStorage('test', '123');
837
+ * getSessionStorage('test'); // 123
838
+ * setLocalStorage('test', '123');
839
+ * getLocalStorage('test'); // 123
840
+ *
841
+ * // or package in usage
842
+ * const projectName = 'mazey';
843
+ * function mSetLocalStorage (key, value) {
844
+ * return setLocalStorage(`${projectName}_${key}`, value);
845
+ * }
846
+ *
847
+ * function mGetLocalStorage (key) {
848
+ * return getLocalStorage(`${projectName}_${key}`);
849
+ * }
850
+ * ```
851
+ *
730
852
  * @param {string} key 键
731
- * @param {string} value 值
853
+ * @returns {any} 返回值
854
+ * @category Cache Data
732
855
  */
733
856
  export function setLocalStorage(key: string, value: any = null): void {
734
857
  if (key) {
@@ -737,10 +860,30 @@ export function setLocalStorage(key: string, value: any = null): void {
737
860
  }
738
861
 
739
862
  /**
740
- * @method getLocalStorage
741
- * @description 存储数据到 localStorage
863
+ * EN: Handle Storage (Keep fit for JSON, it can tansfer format automatically).
864
+ *
865
+ * ZH: 存储/获取数据到 sessionStorage/localStorage
866
+ *
867
+ * ```
868
+ * setSessionStorage('test', '123');
869
+ * getSessionStorage('test'); // 123
870
+ * setLocalStorage('test', '123');
871
+ * getLocalStorage('test'); // 123
872
+ *
873
+ * // or package in usage
874
+ * const projectName = 'mazey';
875
+ * function mSetLocalStorage (key, value) {
876
+ * return setLocalStorage(`${projectName}_${key}`, value);
877
+ * }
878
+ *
879
+ * function mGetLocalStorage (key) {
880
+ * return getLocalStorage(`${projectName}_${key}`);
881
+ * }
882
+ * ```
883
+ *
742
884
  * @param {string} key 键
743
- * @returns {*} 返回值
885
+ * @returns {any} 返回值
886
+ * @category Cache Data
744
887
  */
745
888
  export function getLocalStorage(key: string): any {
746
889
  let ret = null;
@@ -1019,8 +1162,16 @@ export function mNow(): number {
1019
1162
  }
1020
1163
 
1021
1164
  /**
1022
- * @method setCookie
1023
- * @description 设置 Cookie
1165
+ * EN: Handle Cookie.
1166
+ *
1167
+ * ZH: 设置/获取 Cookie
1168
+ *
1169
+ * ```
1170
+ * setCookie('test', '123', 30, 'example.com'); // key value day domain
1171
+ * getCookie('test'); // 123
1172
+ * ```
1173
+ *
1174
+ * @category Cache Data
1024
1175
  */
1025
1176
  export function setCookie(
1026
1177
  name: string,
@@ -1068,8 +1219,16 @@ export function setCookie(
1068
1219
  }
1069
1220
 
1070
1221
  /**
1071
- * @method getCookie
1072
- * @description 获取 Cookie
1222
+ * EN: Handle Cookie.
1223
+ *
1224
+ * ZH: 设置/获取 Cookie
1225
+ *
1226
+ * ```
1227
+ * setCookie('test', '123', 30, 'example.com'); // key value day domain
1228
+ * getCookie('test'); // 123
1229
+ * ```
1230
+ *
1231
+ * @category Cache Data
1073
1232
  */
1074
1233
  export function getCookie(name: string): string {
1075
1234
  const nameEQ = name + '=';
@@ -1132,6 +1291,7 @@ interface WebPerformance {
1132
1291
  *
1133
1292
  * @param {boolean} camelCase -- false(默认) 以下划线形式返回数据 true 以驼峰形式返回数据
1134
1293
  * @returns {Promise<object>} 加载数据
1294
+ * @category Web Performance
1135
1295
  */
1136
1296
  export function getPerformance(
1137
1297
  camelCase = false
@@ -1418,6 +1578,7 @@ export function getPerformance(
1418
1578
  *
1419
1579
  * @param {number} rate -- 0.1 ~ 1 => 1% ~ 100%
1420
1580
  * @returns {boolean} true 命中
1581
+ * @category Calculate and Formula
1421
1582
  */
1422
1583
  export function inRate(rate: number): boolean {
1423
1584
  if (Math.random() < rate) {
@@ -1427,7 +1588,7 @@ export function inRate(rate: number): boolean {
1427
1588
  }
1428
1589
 
1429
1590
  /**
1430
- * EN: Determine if it is a secure PWA environment that it can run.
1591
+ * EN: Detect the margin of Safety. Determine if it is a secure PWA environment that it can run.
1431
1592
  *
1432
1593
  * ZH: 判断是否是安全的 PWA 环境
1433
1594
  *
@@ -1444,6 +1605,7 @@ export function inRate(rate: number): boolean {
1444
1605
  * ```
1445
1606
  *
1446
1607
  * @returns {boolean} true 是
1608
+ * @category Browser Information
1447
1609
  */
1448
1610
  export function isSafePWAEnv(): boolean {
1449
1611
  // 判断是否支持 async await
@@ -1507,18 +1669,18 @@ export function isSafePWAEnv(): boolean {
1507
1669
  *
1508
1670
  * Results:
1509
1671
  *
1510
- * | Index | Field | Description |
1511
- * | --- | --- | --- |
1512
- * | System | system | android, ios, windows, macos, linux |
1513
- * | System version | systemVs | windows: 2000, xp, 2003, vista, 7, 8, 8.1, 10 <br />macos: ... |
1514
- * | Platform | platform | desktop, mobile |
1515
- * | Engine | engine | webkit, gecko, presto, trident |
1516
- * | Engine version | engineVs | - |
1517
- * | Supporter | supporter | edge, opera, chrome, safari, firefox, iexplore |
1518
- * | Supporter version | supporterVs | - |
1519
- * | Shell | shell | (Optional) wechat, qq_browser, qq_app, uc, 360, 2345, sougou, liebao, maxthon, bilibili |
1520
- * | Shell version | shellVs | (Optional) 20/... |
1521
- * | Apple device type | appleType | (Optional) ipad, iphone, ipod, iwatch |
1672
+ * | Attribute | Description | Type | Values |
1673
+ * | --- | --- | --- | --- |
1674
+ * | **system** | System | string | android, ios, windows, macos, linux |
1675
+ * | systemVs | System version | string | windows: 2000, xp, 2003, vista, 7, 8, 8.1, 10 <br />macos: ... |
1676
+ * | platform | Platform | string | desktop, mobile |
1677
+ * | engine | Engine | string | webkit, gecko, presto, trident |
1678
+ * | engineVs | Engine version | string | - |
1679
+ * | supporter | Supporter | string | edge, opera, chrome, safari, firefox, iexplore |
1680
+ * | supporterVs | Supporter version | string | - |
1681
+ * | shell | Shell | string | (Optional) wechat, qq_browser, qq_app, uc, 360, 2345, sougou, liebao, maxthon, bilibili |
1682
+ * | shellVs | Shell version | string | (Optional) 20/... |
1683
+ * | appleType | Apple device type | string | (Optional) ipad, iphone, ipod, iwatch |
1522
1684
  *
1523
1685
  * Example: Determine the environment of the mobile QQ.
1524
1686
  *
@@ -1813,7 +1975,7 @@ export function windowLoaded(timeout = 90): Promise<string | Error> {
1813
1975
  /**
1814
1976
  * EN: Add `<style>` in `<head>`.
1815
1977
  *
1816
- * ZH: 添加样式标签; style: 样式标签内的字符串; id: `<style>` 标签的 `id`; 返回: 添加成功/失败.
1978
+ * ZH: 添加样式标签; style: 样式标签内的字符串; id: `<style>` 标签的 `id`; 返回: 添加成功/失败
1817
1979
  *
1818
1980
  * Example 1: Add the `<style>` with `id`, and repeated invoking will update the content instead of adding a new one.
1819
1981
  *
@@ -1841,20 +2003,18 @@ export function windowLoaded(timeout = 90): Promise<string | Error> {
1841
2003
  * addStyle(
1842
2004
  * `
1843
2005
  * body {
1844
- * background-color: #333;
2006
+ * background-color: #444;
1845
2007
  * }
1846
2008
  * `
1847
2009
  * );
1848
2010
  * // <style>
1849
2011
  * // body {
1850
- * // background-color: #333;
2012
+ * // background-color: #444;
1851
2013
  * // }
1852
2014
  * // </style>
1853
2015
  * ```
1854
2016
  *
1855
- * @param style Style string.
1856
- * @param options.id `id` in `<style>`
1857
- * @returns Success/Fail
2017
+ * @category DOM
1858
2018
  */
1859
2019
  export function addStyle(
1860
2020
  style: string,
@@ -1923,6 +2083,7 @@ export function addStyle(
1923
2083
  * @param {function} logFn The function with Log.
1924
2084
  * @param {function} errorFn The function with Error.
1925
2085
  * @returns {object} 新实例
2086
+ * @category Debug
1926
2087
  */
1927
2088
  export function genCustomConsole(
1928
2089
  prefix = '',
@@ -2114,6 +2275,7 @@ export function isNonEmptyArray(arr: any[]): boolean {
2114
2275
  * @param {string[]} attributes Data Attributes
2115
2276
  * @param {any} validValue Given Value for verifying.
2116
2277
  * @returns {boolean} Return TRUE if the data is valid.
2278
+ * @category Util
2117
2279
  */
2118
2280
  export function isValidData(
2119
2281
  data: any,
@@ -2220,6 +2382,7 @@ export function genHashCode(str: string): number {
2220
2382
  * @param {Date|number|string} dateIns Original Date
2221
2383
  * @param {string} format Format String
2222
2384
  * @returns {string} Return the formatted date string.
2385
+ * @category Util
2223
2386
  */
2224
2387
  export function formatDate(
2225
2388
  dateIns?: Date | number | string,
@@ -2258,3 +2421,123 @@ export function formatDate(
2258
2421
  });
2259
2422
  return tempFormat;
2260
2423
  }
2424
+
2425
+ /**
2426
+ * Get event container.
2427
+ *
2428
+ * @category Event
2429
+ * @hidden
2430
+ */
2431
+ export function getDefineListeners(): DefineListeners {
2432
+ let defineListeners = window.MAZEY_DEFINE_LISTENERS;
2433
+ if (typeof defineListeners !== 'object') {
2434
+ defineListeners = {};
2435
+ window.MAZEY_DEFINE_LISTENERS = defineListeners;
2436
+ }
2437
+ return defineListeners;
2438
+ }
2439
+
2440
+ /**
2441
+ * Add event.
2442
+ *
2443
+ * @param type
2444
+ * @param fn
2445
+ * @category Event
2446
+ */
2447
+ export function addEvent(type: string, fn: any): void {
2448
+ const defineListeners = getDefineListeners();
2449
+ if (typeof defineListeners[type] === 'undefined') {
2450
+ defineListeners[type] = [];
2451
+ }
2452
+ if (typeof fn === 'function') {
2453
+ defineListeners[type].push(fn);
2454
+ }
2455
+ }
2456
+
2457
+ /**
2458
+ * Invoke event.
2459
+ *
2460
+ * @param type
2461
+ * @category Event
2462
+ */
2463
+ export function invokeEvent(type: string): void {
2464
+ const defineListeners = getDefineListeners();
2465
+ const arrayEvent = defineListeners[type];
2466
+ if (arrayEvent instanceof Array) {
2467
+ for (let i = 0, length = arrayEvent.length; i < length; i++) {
2468
+ if (typeof arrayEvent[i] === 'function') {
2469
+ arrayEvent[i]({
2470
+ type: type
2471
+ });
2472
+ }
2473
+ }
2474
+ }
2475
+ }
2476
+
2477
+ /**
2478
+ * Remove event.
2479
+ *
2480
+ * @param type
2481
+ * @param fn
2482
+ * @category Event
2483
+ */
2484
+ export function removeEvent(type: string, fn: any): void {
2485
+ const defineListeners = getDefineListeners();
2486
+ const arrayEvent = defineListeners[type];
2487
+ if (typeof type === 'string' && arrayEvent instanceof Array) {
2488
+ if (typeof fn === 'function') {
2489
+ for (let i = 0, length = arrayEvent.length; i < length; i++) {
2490
+ if (arrayEvent[i] === fn) {
2491
+ defineListeners[type].splice(i, 1);
2492
+ break;
2493
+ }
2494
+ }
2495
+ } else {
2496
+ delete defineListeners[type];
2497
+ }
2498
+ }
2499
+ }
2500
+
2501
+ /**
2502
+ * Check if the given string is a valid url.
2503
+ *
2504
+ * @example
2505
+ * ```js
2506
+ * console.log(isUrl('https://www.google.com')); // true
2507
+ * console.log(isUrl('https://www.google.com/')); // true
2508
+ * console.log(isUrl('https://www.google.com/?q=hello')); // true
2509
+ * console.log(isUrl('https://www.google.com/?q=hello#world')); // true
2510
+ * console.log(isUrl('https://www.google.com/#world')); // true
2511
+ * console.log(isUrl('https://www.google.com/#')); // true
2512
+ * console.log(isUrl('https://www.google.com/#?q=hello')); // true
2513
+ * console.log(isUrl('https://www.google.com/#?q=hello#world')); // true
2514
+ * ```
2515
+ *
2516
+ * @param url
2517
+ * @returns {boolean} Return true if the given url is a valid url.
2518
+ * @category URL
2519
+ */
2520
+ export function isValidUrl(url: string): boolean {
2521
+ const reg = /[a-zA-Z0-9]+:\/\/[-a-zA-Z0-9@:%._+~#=]{1,256}\b([-a-zA-Z0-9\u4E00-\u9FA5()!@:%_+.~#?&//=]*)/gm;
2522
+ return reg.test(url);
2523
+ }
2524
+
2525
+ /**
2526
+ * Check if the given string is a mobile phone number.
2527
+ *
2528
+ * @example
2529
+ * ```js
2530
+ * console.log(isMobile('13800138000')); // true
2531
+ * console.log(isMobile('1380013800')); // false
2532
+ * console.log(isMobile('138001380000')); // false
2533
+ * console.log(isMobile('1380013800a')); // false
2534
+ * ```
2535
+ *
2536
+ * @param mobile
2537
+ * @returns {boolean} Return true if the given string is a mobile phone number.
2538
+ * @category Util
2539
+ */
2540
+ export function isValidPhoneNumber(mobile: string): boolean {
2541
+ const reg = /^1[3456789]\d{9}$/;
2542
+ return reg.test(mobile);
2543
+ }
package/src/preview.js CHANGED
@@ -1,47 +1,3 @@
1
1
  /**
2
2
  * @author: Mazey Chu
3
3
  */
4
-
5
- // Define event.
6
- let defineListeners = {};
7
-
8
- // Add event.
9
- function addEvent(type, fn) {
10
- if (typeof defineListeners[type] === 'undefined') {
11
- defineListeners[type] = [];
12
- }
13
- if (typeof fn === 'function') {
14
- defineListeners[type].push(fn);
15
- }
16
- }
17
-
18
- // Invoke event.
19
- function fireEvent(type) {
20
- let arrayEvent = defineListeners[type];
21
- if (arrayEvent instanceof Array) {
22
- for (let i = 0, length = arrayEvent.length; i < length; i++) {
23
- if (typeof arrayEvent[i] === 'function') {
24
- arrayEvent[i]({
25
- type: type
26
- });
27
- }
28
- }
29
- }
30
- }
31
-
32
- // Remove event.
33
- function removeEvent(type, fn) {
34
- let arrayEvent = defineListeners[type];
35
- if (typeof type === 'string' && arrayEvent instanceof Array) {
36
- if (typeof fn === 'function') {
37
- for (let i = 0, length = arrayEvent.length; i < length; i++) {
38
- if (arrayEvent[i] === fn) {
39
- defineListeners[type].splice(i, 1);
40
- break;
41
- }
42
- }
43
- } else {
44
- delete defineListeners[type];
45
- }
46
- }
47
- }