@unikue/ts-lang-utils 1.3.0 → 1.3.1

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.
@@ -1 +1 @@
1
- export declare function isMobile(text?: string | null, pattern?: string | RegExp): boolean;
1
+ export declare function isMobile(text?: string | null, alphaOrPattern?: string | RegExp): boolean;
@@ -24,15 +24,23 @@ __export(isMobile_exports, {
24
24
  module.exports = __toCommonJS(isMobile_exports);
25
25
  var import_regex_pattern = require("../../constant/regex-pattern");
26
26
  var import_compilePattern = require("./compilePattern");
27
- function isMobile(text, pattern = import_regex_pattern.CHINESE_MOBILE_REGEX) {
27
+ var import_libphonenumber_js = require("libphonenumber-js");
28
+ function isMobile(text, alphaOrPattern = import_regex_pattern.CHINESE_MOBILE_REGEX) {
28
29
  if (!text) {
29
30
  return false;
30
31
  }
31
- if (typeof pattern === "string") {
32
- const regex = (0, import_compilePattern.compilePattern)(pattern);
32
+ if (typeof alphaOrPattern === "string" && /^[A-Za-z]{2}$/i.test(alphaOrPattern)) {
33
+ try {
34
+ return (0, import_libphonenumber_js.isValidPhoneNumber)(text, alphaOrPattern.toUpperCase());
35
+ } catch {
36
+ return false;
37
+ }
38
+ }
39
+ if (typeof alphaOrPattern === "string") {
40
+ const regex = (0, import_compilePattern.compilePattern)(alphaOrPattern);
33
41
  return !!regex && regex.test(text);
34
42
  }
35
- return pattern.test(text);
43
+ return alphaOrPattern.test(text);
36
44
  }
37
45
  // Annotate the CommonJS export names for ESM import in node:
38
46
  0 && (module.exports = {
@@ -3,3 +3,4 @@ export { maskChineseIdCard } from './maskChineseIdCard';
3
3
  export { maskChineseMobile } from './maskChineseMobile';
4
4
  export { maskChineseName } from './maskChineseName';
5
5
  export { maskEmail } from './maskEmail';
6
+ export { maskMobile } from './maskMobile';
@@ -23,7 +23,8 @@ __export(SensitiveUtils_exports, {
23
23
  maskChineseIdCard: () => import_maskChineseIdCard.maskChineseIdCard,
24
24
  maskChineseMobile: () => import_maskChineseMobile.maskChineseMobile,
25
25
  maskChineseName: () => import_maskChineseName.maskChineseName,
26
- maskEmail: () => import_maskEmail.maskEmail
26
+ maskEmail: () => import_maskEmail.maskEmail,
27
+ maskMobile: () => import_maskMobile.maskMobile
27
28
  });
28
29
  module.exports = __toCommonJS(SensitiveUtils_exports);
29
30
  var import_maskBankCard = require("./maskBankCard");
@@ -31,11 +32,13 @@ var import_maskChineseIdCard = require("./maskChineseIdCard");
31
32
  var import_maskChineseMobile = require("./maskChineseMobile");
32
33
  var import_maskChineseName = require("./maskChineseName");
33
34
  var import_maskEmail = require("./maskEmail");
35
+ var import_maskMobile = require("./maskMobile");
34
36
  // Annotate the CommonJS export names for ESM import in node:
35
37
  0 && (module.exports = {
36
38
  maskBankCard,
37
39
  maskChineseIdCard,
38
40
  maskChineseMobile,
39
41
  maskChineseName,
40
- maskEmail
42
+ maskEmail,
43
+ maskMobile
41
44
  });
@@ -0,0 +1 @@
1
+ export declare function maskMobile(text?: string | null, alphaOrDial?: string): string | undefined;
@@ -0,0 +1,57 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/util/SensitiveUtils/maskMobile.ts
20
+ var maskMobile_exports = {};
21
+ __export(maskMobile_exports, {
22
+ maskMobile: () => maskMobile
23
+ });
24
+ module.exports = __toCommonJS(maskMobile_exports);
25
+ var import_libphonenumber_js = require("libphonenumber-js");
26
+ function maskMobile(text, alphaOrDial) {
27
+ if (!text) {
28
+ return void 0;
29
+ }
30
+ let country;
31
+ let input = text;
32
+ if (alphaOrDial) {
33
+ if (/^[A-Za-z]{2}$/i.test(alphaOrDial)) {
34
+ country = alphaOrDial.toUpperCase();
35
+ } else if (!text.startsWith("+")) {
36
+ input = "+" + alphaOrDial.replace(/^\+/, "") + text;
37
+ }
38
+ }
39
+ let parsed;
40
+ try {
41
+ parsed = country ? (0, import_libphonenumber_js.parsePhoneNumberFromString)(input, country) : (0, import_libphonenumber_js.parsePhoneNumberFromString)(input);
42
+ } catch {
43
+ return void 0;
44
+ }
45
+ if (!parsed || !parsed.country) {
46
+ return void 0;
47
+ }
48
+ const phone = parsed.nationalNumber;
49
+ if (phone.length < 8) {
50
+ return void 0;
51
+ }
52
+ return phone.slice(0, phone.length - 8) + "****" + phone.slice(phone.length - 4);
53
+ }
54
+ // Annotate the CommonJS export names for ESM import in node:
55
+ 0 && (module.exports = {
56
+ maskMobile
57
+ });
@@ -1 +1 @@
1
- export declare function isMobile(text?: string | null, pattern?: string | RegExp): boolean;
1
+ export declare function isMobile(text?: string | null, alphaOrPattern?: string | RegExp): boolean;
@@ -1,13 +1,21 @@
1
1
  import { CHINESE_MOBILE_REGEX } from "../../constant/regex-pattern";
2
2
  import { compilePattern } from "./compilePattern";
3
+ import { isValidPhoneNumber } from 'libphonenumber-js';
3
4
  export function isMobile(text) {
4
- var pattern = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : CHINESE_MOBILE_REGEX;
5
+ var alphaOrPattern = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : CHINESE_MOBILE_REGEX;
5
6
  if (!text) {
6
7
  return false;
7
8
  }
8
- if (typeof pattern === 'string') {
9
- var regex = compilePattern(pattern);
9
+ if (typeof alphaOrPattern === 'string' && /^[A-Za-z]{2}$/i.test(alphaOrPattern)) {
10
+ try {
11
+ return isValidPhoneNumber(text, alphaOrPattern.toUpperCase());
12
+ } catch (_unused) {
13
+ return false;
14
+ }
15
+ }
16
+ if (typeof alphaOrPattern === 'string') {
17
+ var regex = compilePattern(alphaOrPattern);
10
18
  return !!regex && regex.test(text);
11
19
  }
12
- return pattern.test(text);
20
+ return alphaOrPattern.test(text);
13
21
  }
@@ -3,3 +3,4 @@ export { maskChineseIdCard } from './maskChineseIdCard';
3
3
  export { maskChineseMobile } from './maskChineseMobile';
4
4
  export { maskChineseName } from './maskChineseName';
5
5
  export { maskEmail } from './maskEmail';
6
+ export { maskMobile } from './maskMobile';
@@ -2,4 +2,5 @@ export { maskBankCard } from "./maskBankCard";
2
2
  export { maskChineseIdCard } from "./maskChineseIdCard";
3
3
  export { maskChineseMobile } from "./maskChineseMobile";
4
4
  export { maskChineseName } from "./maskChineseName";
5
- export { maskEmail } from "./maskEmail";
5
+ export { maskEmail } from "./maskEmail";
6
+ export { maskMobile } from "./maskMobile";
@@ -0,0 +1 @@
1
+ export declare function maskMobile(text?: string | null, alphaOrDial?: string): string | undefined;
@@ -0,0 +1,29 @@
1
+ import { parsePhoneNumberFromString } from 'libphonenumber-js';
2
+ export function maskMobile(text, alphaOrDial) {
3
+ if (!text) {
4
+ return undefined;
5
+ }
6
+ var country;
7
+ var input = text;
8
+ if (alphaOrDial) {
9
+ if (/^[A-Za-z]{2}$/i.test(alphaOrDial)) {
10
+ country = alphaOrDial.toUpperCase();
11
+ } else if (!text.startsWith('+')) {
12
+ input = '+' + alphaOrDial.replace(/^\+/, '') + text;
13
+ }
14
+ }
15
+ var parsed;
16
+ try {
17
+ parsed = country ? parsePhoneNumberFromString(input, country) : parsePhoneNumberFromString(input);
18
+ } catch (_unused) {
19
+ return undefined;
20
+ }
21
+ if (!parsed || !parsed.country) {
22
+ return undefined;
23
+ }
24
+ var phone = parsed.nationalNumber;
25
+ if (phone.length < 8) {
26
+ return undefined;
27
+ }
28
+ return phone.slice(0, phone.length - 8) + '****' + phone.slice(phone.length - 4);
29
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unikue/ts-lang-utils",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "title": "TsLangUtils",
5
5
  "description": "Common lang utilities for typescript",
6
6
  "homepage": "https://unikueltd.github.io/ts-lang-utils",
@@ -54,12 +54,13 @@
54
54
  "dependencies": {
55
55
  "@babel/runtime": "^7.29.7",
56
56
  "@imranbarbhuiya/duration": "^5.1.8",
57
+ "libphonenumber-js": "^1.13.13",
57
58
  "lodash": "^4.18.1",
58
- "nanoid": "^3.3.18",
59
+ "nanoid": "^3.3.19",
59
60
  "universal-cookie": "^8.1.2"
60
61
  },
61
62
  "devDependencies": {
62
- "@eslint/eslintrc": "^3.3.6",
63
+ "@eslint/eslintrc": "^3.3.7",
63
64
  "@eslint/js": "^10.0.1",
64
65
  "@types/jest": "^30.0.0",
65
66
  "@types/lodash": "^4.17.25",
@@ -67,16 +68,16 @@
67
68
  "@unikue/typedoc-plugin-raw-content": "^1.1.1",
68
69
  "@unikue/typedoc-theme-dumi": "^1.1.3",
69
70
  "del-cli": "^7.0.0",
70
- "eslint": "^10.9.1",
71
- "eslint-plugin-jest": "^29.16.5",
72
- "father": "^4.6.36",
71
+ "eslint": "^10.10.0",
72
+ "eslint-plugin-jest": "^29.16.6",
73
+ "father": "^4.6.37",
73
74
  "gh-pages": "^6.3.0",
74
- "globals": "^17.11.0",
75
- "jest-environment-jsdom": "^30.5.0",
75
+ "globals": "^17.12.0",
76
+ "jest-environment-jsdom": "^30.5.1",
76
77
  "ts-jest": "^29.4.12",
77
78
  "typedoc": "~0.28.20",
78
79
  "typescript": "^6.0.3",
79
- "typescript-eslint": "^8.68.0"
80
+ "typescript-eslint": "^8.70.0"
80
81
  },
81
82
  "peerDependencies": {
82
83
  "typescript": ">=4.0.0"
@@ -118,10 +119,10 @@
118
119
  }
119
120
  ],
120
121
  "allowScripts": {
121
- "core-js": true,
122
- "core-js-pure": true,
123
- "esbuild": true,
124
- "fsevents": true,
125
- "unrs-resolver": true
122
+ "core-js@*": true,
123
+ "core-js-pure@*": true,
124
+ "esbuild@*": true,
125
+ "fsevents@*": true,
126
+ "unrs-resolver@*": true
126
127
  }
127
128
  }