@yelon/util 15.2.4 → 16.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -21
- package/README.md +22 -22
- package/analysis/global-event.subject.d.ts +7 -7
- package/analysis/index.d.ts +1 -1
- package/array/array-type.service.d.ts +68 -68
- package/array/array.service.d.ts +78 -78
- package/array/index.d.ts +2 -2
- package/browser/cookie.service.d.ts +67 -67
- package/browser/copy.d.ts +6 -6
- package/browser/index.d.ts +5 -5
- package/browser/is-empty.d.ts +6 -6
- package/browser/scroll.service.d.ts +37 -37
- package/browser/style.d.ts +21 -21
- package/config/abc/date-picker.type.d.ts +52 -52
- package/config/abc/error-collect.type.d.ts +10 -10
- package/config/abc/exception.type.d.ts +9 -9
- package/config/abc/image.type.d.ts +15 -15
- package/config/abc/index.d.ts +17 -17
- package/config/abc/loading.type.d.ts +31 -31
- package/config/abc/lodop.type.d.ts +33 -33
- package/config/abc/media.type.d.ts +11 -11
- package/config/abc/onboarding.type.d.ts +4 -4
- package/config/abc/page-header.type.d.ts +39 -39
- package/config/abc/pdf.type.d.ts +35 -35
- package/config/abc/qr.type.d.ts +25 -25
- package/config/abc/se.type.d.ts +32 -32
- package/config/abc/sg.type.d.ts +10 -10
- package/config/abc/st.type.d.ts +465 -465
- package/config/abc/sv.type.d.ts +14 -14
- package/config/abc/xlsx.type.d.ts +12 -12
- package/config/abc/zip.type.d.ts +10 -10
- package/config/acl/acl.type.d.ts +32 -32
- package/config/auth/auth.type.d.ts +55 -55
- package/config/bis/bis.type.d.ts +7 -7
- package/config/cache/cache.type.d.ts +40 -40
- package/config/chart/chart.type.d.ts +33 -33
- package/config/config.service.d.ts +13 -13
- package/config/config.types.d.ts +48 -48
- package/config/index.d.ts +14 -14
- package/config/mock/mock.type.d.ts +18 -18
- package/config/sf/sf.type.d.ts +133 -133
- package/config/socket/socket.type.d.ts +4 -4
- package/config/theme/http.type.d.ts +14 -14
- package/config/theme/i18n.type.d.ts +14 -14
- package/config/theme/index.d.ts +3 -3
- package/config/theme/responsive.type.d.ts +12 -12
- package/config/util/array.type.d.ts +22 -22
- package/config/util/currency.type.d.ts +39 -39
- package/date-time/index.d.ts +2 -2
- package/date-time/picker.d.ts +68 -68
- package/date-time/time.d.ts +29 -29
- package/decorator/convert.d.ts +24 -24
- package/decorator/index.d.ts +2 -2
- package/decorator/zone-outside.d.ts +35 -35
- package/{esm2020 → esm2022}/analysis/analysis.mjs +4 -4
- package/esm2022/analysis/global-event.subject.mjs +18 -0
- package/{esm2020 → esm2022}/analysis/index.mjs +2 -2
- package/{esm2020 → esm2022}/array/array-type.service.mjs +1 -1
- package/{esm2020 → esm2022}/array/array.mjs +4 -4
- package/esm2022/array/array.service.mjs +278 -0
- package/{esm2020 → esm2022}/array/index.mjs +2 -2
- package/{esm2020 → esm2022}/browser/browser.mjs +4 -4
- package/esm2022/browser/cookie.service.mjs +104 -0
- package/{esm2020 → esm2022}/browser/copy.mjs +27 -27
- package/{esm2020 → esm2022}/browser/index.mjs +5 -5
- package/{esm2020 → esm2022}/browser/is-empty.mjs +18 -18
- package/esm2022/browser/scroll.service.mjs +89 -0
- package/{esm2020 → esm2022}/browser/style.mjs +37 -37
- package/{esm2020 → esm2022}/config/abc/date-picker.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/error-collect.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/exception.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/image.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/index.mjs +17 -17
- package/{esm2020 → esm2022}/config/abc/loading.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/lodop.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/media.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/onboarding.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/page-header.type.mjs +1 -1
- package/esm2022/config/abc/pdf.type.mjs +2 -0
- package/{esm2020 → esm2022}/config/abc/qr.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/se.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/sg.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/st.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/sv.type.mjs +2 -2
- package/{esm2020 → esm2022}/config/abc/xlsx.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/abc/zip.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/acl/acl.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/auth/auth.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/bis/bis.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/cache/cache.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/chart/chart.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/config.mjs +4 -4
- package/esm2022/config/config.service.mjs +38 -0
- package/{esm2020 → esm2022}/config/config.types.mjs +8 -8
- package/{esm2020 → esm2022}/config/index.mjs +14 -14
- package/{esm2020 → esm2022}/config/mock/mock.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/sf/sf.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/socket/socket.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/theme/http.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/theme/i18n.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/theme/index.mjs +3 -3
- package/{esm2020 → esm2022}/config/theme/responsive.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/util/array.type.mjs +1 -1
- package/{esm2020 → esm2022}/config/util/currency.type.mjs +1 -1
- package/{esm2020 → esm2022}/date-time/date-time.mjs +4 -4
- package/{esm2020 → esm2022}/date-time/index.mjs +2 -2
- package/{esm2020 → esm2022}/date-time/picker.mjs +115 -115
- package/{esm2020 → esm2022}/date-time/time.mjs +88 -88
- package/{esm2020 → esm2022}/decorator/convert.mjs +57 -57
- package/{esm2020 → esm2022}/decorator/decorator.mjs +4 -4
- package/{esm2020 → esm2022}/decorator/index.mjs +2 -2
- package/{esm2020 → esm2022}/decorator/zone-outside.mjs +56 -56
- package/{esm2020 → esm2022}/form/form.mjs +4 -4
- package/{esm2020 → esm2022}/form/index.mjs +2 -2
- package/{esm2020 → esm2022}/form/match-control.mjs +29 -29
- package/{esm2020 → esm2022}/form/validators.mjs +80 -80
- package/esm2022/format/currency.service.mjs +239 -0
- package/{esm2020 → esm2022}/format/currency.types.mjs +7 -7
- package/{esm2020 → esm2022}/format/format.mjs +4 -4
- package/{esm2020 → esm2022}/format/index.mjs +4 -4
- package/{esm2020 → esm2022}/format/string.mjs +84 -84
- package/{esm2020 → esm2022}/format/validate.mjs +93 -93
- package/{esm2020 → esm2022}/index.mjs +12 -12
- package/{esm2020 → esm2022}/math/in-range.mjs +24 -24
- package/{esm2020 → esm2022}/math/index.mjs +2 -2
- package/{esm2020 → esm2022}/math/math.mjs +4 -4
- package/{esm2020 → esm2022}/math/round.mjs +52 -52
- package/{esm2020 → esm2022}/other/assert.mjs +66 -66
- package/{esm2020 → esm2022}/other/deep.mjs +77 -77
- package/{esm2020 → esm2022}/other/favicon.mjs +28 -28
- package/{esm2020 → esm2022}/other/index.mjs +7 -7
- package/esm2022/other/lazy.service.mjs +133 -0
- package/{esm2020 → esm2022}/other/logger.mjs +32 -32
- package/{esm2020 → esm2022}/other/other.mjs +4 -4
- package/esm2022/other/path-to-regexp.service.mjs +240 -0
- package/{esm2020 → esm2022}/other/resize.mjs +3 -3
- package/esm2022/pipes/currency/cny.pipe.mjs +24 -0
- package/{esm2020 → esm2022}/pipes/currency/index.mjs +4 -4
- package/esm2022/pipes/currency/mega.pipe.mjs +30 -0
- package/esm2022/pipes/currency/module.mjs +20 -0
- package/{esm2020 → esm2022}/pipes/currency/pipe-currency.mjs +4 -4
- package/esm2022/pipes/currency/price.pipe.mjs +28 -0
- package/esm2022/pipes/filter/filter.pipe.mjs +20 -0
- package/{esm2020 → esm2022}/pipes/filter/index.mjs +2 -2
- package/esm2022/pipes/filter/module.mjs +18 -0
- package/{esm2020 → esm2022}/pipes/filter/pipe-filter.mjs +4 -4
- package/{esm2020 → esm2022}/pipes/format/index.mjs +2 -2
- package/esm2022/pipes/format/mask.pipe.mjs +35 -0
- package/esm2022/pipes/format/module.mjs +18 -0
- package/{esm2020 → esm2022}/pipes/format/pipe-format.mjs +4 -4
- package/{esm2020 → esm2022}/pipes/index.mjs +3 -3
- package/{esm2020 → esm2022}/pipes/pipes.mjs +4 -4
- package/{esm2020 → esm2022}/token/index.mjs +2 -2
- package/{esm2020 → esm2022}/token/page-visibility.mjs +14 -14
- package/{esm2020 → esm2022}/token/token.mjs +4 -4
- package/{esm2020 → esm2022}/token/window.mjs +16 -16
- package/{esm2020 → esm2022}/yelon-util.mjs +4 -4
- package/{fesm2020 → fesm2022}/analysis.mjs +24 -24
- package/fesm2022/analysis.mjs.map +1 -0
- package/{fesm2020 → fesm2022}/array.mjs +284 -284
- package/{fesm2015 → fesm2022}/array.mjs.map +1 -1
- package/{fesm2020 → fesm2022}/browser.mjs +279 -279
- package/{fesm2020 → fesm2022}/browser.mjs.map +1 -1
- package/{fesm2020 → fesm2022}/config.mjs +54 -54
- package/{fesm2020 → fesm2022}/config.mjs.map +1 -1
- package/{fesm2020 → fesm2022}/date-time.mjs +211 -211
- package/{fesm2020 → fesm2022}/decorator.mjs +121 -121
- package/{fesm2020 → fesm2022}/form.mjs +118 -118
- package/{fesm2020 → fesm2022}/format.mjs +431 -431
- package/{fesm2020 → fesm2022}/format.mjs.map +1 -1
- package/{fesm2020 → fesm2022}/math.mjs +85 -85
- package/{fesm2020 → fesm2022}/other.mjs +586 -586
- package/{fesm2020 → fesm2022}/other.mjs.map +1 -1
- package/{fesm2020 → fesm2022}/pipe-currency.mjs +94 -94
- package/{fesm2020 → fesm2022}/pipe-currency.mjs.map +1 -1
- package/fesm2022/pipe-filter.mjs +40 -0
- package/{fesm2015 → fesm2022}/pipe-filter.mjs.map +1 -1
- package/{fesm2020 → fesm2022}/pipe-format.mjs +55 -55
- package/{fesm2015 → fesm2022}/pipe-format.mjs.map +1 -1
- package/{fesm2015 → fesm2022}/pipes.mjs +8 -8
- package/{fesm2020 → fesm2022}/token.mjs +37 -37
- package/{fesm2015 → fesm2022}/yelon-util.mjs +17 -17
- package/form/index.d.ts +2 -2
- package/form/match-control.d.ts +15 -15
- package/form/validators.d.ts +62 -62
- package/format/currency.service.d.ts +37 -37
- package/format/currency.types.d.ts +77 -77
- package/format/index.d.ts +4 -4
- package/format/string.d.ts +44 -44
- package/format/validate.d.ts +72 -72
- package/index.d.ts +12 -12
- package/math/in-range.d.ts +14 -14
- package/math/index.d.ts +2 -2
- package/math/round.d.ts +33 -33
- package/other/assert.d.ts +36 -36
- package/other/deep.d.ts +35 -35
- package/other/favicon.d.ts +2 -2
- package/other/index.d.ts +7 -7
- package/other/lazy.service.d.ts +48 -48
- package/other/logger.d.ts +5 -5
- package/other/path-to-regexp.service.d.ts +26 -26
- package/other/resize.d.ts +1 -1
- package/package.json +129 -165
- package/pipes/currency/cny.pipe.d.ts +15 -15
- package/pipes/currency/index.d.ts +4 -4
- package/pipes/currency/mega.pipe.d.ts +16 -16
- package/pipes/currency/module.d.ts +9 -9
- package/pipes/currency/price.pipe.d.ts +19 -19
- package/pipes/filter/filter.pipe.d.ts +13 -13
- package/pipes/filter/index.d.ts +2 -2
- package/pipes/filter/module.d.ts +7 -7
- package/pipes/format/index.d.ts +2 -2
- package/pipes/format/mask.pipe.d.ts +27 -27
- package/pipes/format/module.d.ts +7 -7
- package/pipes/index.d.ts +3 -3
- package/token/index.d.ts +2 -2
- package/token/page-visibility.d.ts +8 -8
- package/token/window.d.ts +7 -7
- package/esm2020/analysis/global-event.subject.mjs +0 -17
- package/esm2020/array/array.service.mjs +0 -277
- package/esm2020/browser/cookie.service.mjs +0 -103
- package/esm2020/browser/scroll.service.mjs +0 -88
- package/esm2020/config/abc/pdf.type.mjs +0 -2
- package/esm2020/config/config.service.mjs +0 -37
- package/esm2020/format/currency.service.mjs +0 -238
- package/esm2020/other/lazy.service.mjs +0 -132
- package/esm2020/other/path-to-regexp.service.mjs +0 -239
- package/esm2020/pipes/currency/cny.pipe.mjs +0 -23
- package/esm2020/pipes/currency/mega.pipe.mjs +0 -29
- package/esm2020/pipes/currency/module.mjs +0 -19
- package/esm2020/pipes/currency/price.pipe.mjs +0 -27
- package/esm2020/pipes/filter/filter.pipe.mjs +0 -19
- package/esm2020/pipes/filter/module.mjs +0 -17
- package/esm2020/pipes/format/mask.pipe.mjs +0 -34
- package/esm2020/pipes/format/module.mjs +0 -17
- package/fesm2015/analysis.mjs +0 -24
- package/fesm2015/analysis.mjs.map +0 -1
- package/fesm2015/array.mjs +0 -254
- package/fesm2015/browser.mjs +0 -283
- package/fesm2015/browser.mjs.map +0 -1
- package/fesm2015/config.mjs +0 -56
- package/fesm2015/config.mjs.map +0 -1
- package/fesm2015/date-time.mjs +0 -207
- package/fesm2015/date-time.mjs.map +0 -1
- package/fesm2015/decorator.mjs +0 -121
- package/fesm2015/decorator.mjs.map +0 -1
- package/fesm2015/form.mjs +0 -118
- package/fesm2015/format.mjs +0 -413
- package/fesm2015/format.mjs.map +0 -1
- package/fesm2015/math.mjs +0 -85
- package/fesm2015/other.mjs +0 -589
- package/fesm2015/other.mjs.map +0 -1
- package/fesm2015/pipe-currency.mjs +0 -96
- package/fesm2015/pipe-currency.mjs.map +0 -1
- package/fesm2015/pipe-filter.mjs +0 -40
- package/fesm2015/pipe-format.mjs +0 -55
- package/fesm2015/token.mjs +0 -37
- package/fesm2020/analysis.mjs.map +0 -1
- package/fesm2020/array.mjs.map +0 -1
- package/fesm2020/form.mjs.map +0 -1
- package/fesm2020/math.mjs.map +0 -1
- package/fesm2020/pipe-filter.mjs +0 -40
- package/fesm2020/pipe-filter.mjs.map +0 -1
- package/fesm2020/pipe-format.mjs.map +0 -1
- package/fesm2020/pipes.mjs +0 -8
- package/fesm2020/pipes.mjs.map +0 -1
- package/fesm2020/token.mjs.map +0 -1
- package/fesm2020/yelon-util.mjs +0 -17
- package/fesm2020/yelon-util.mjs.map +0 -1
- /package/{fesm2020 → fesm2022}/date-time.mjs.map +0 -0
- /package/{fesm2020 → fesm2022}/decorator.mjs.map +0 -0
- /package/{fesm2015 → fesm2022}/form.mjs.map +0 -0
- /package/{fesm2015 → fesm2022}/math.mjs.map +0 -0
- /package/{fesm2015 → fesm2022}/pipes.mjs.map +0 -0
- /package/{fesm2015 → fesm2022}/token.mjs.map +0 -0
- /package/{fesm2015 → fesm2022}/yelon-util.mjs.map +0 -0
@@ -1,431 +1,431 @@
|
|
1
|
-
import { deepGet } from '@yelon/util/other';
|
2
|
-
import { CurrencyPipe, formatNumber } from '@angular/common';
|
3
|
-
import * as i0 from '@angular/core';
|
4
|
-
import { LOCALE_ID, DEFAULT_CURRENCY_CODE, Injectable, Inject } from '@angular/core';
|
5
|
-
import * as i1 from '@yelon/util/config';
|
6
|
-
|
7
|
-
/**
|
8
|
-
* String formatting
|
9
|
-
*
|
10
|
-
* 字符串格式化
|
11
|
-
* ```
|
12
|
-
* format('this is ${name}', { name: 'asdf' })
|
13
|
-
* // output: this is asdf
|
14
|
-
* format('this is ${user.name}', { user: { name: 'asdf' } }, true)
|
15
|
-
* // output: this is asdf
|
16
|
-
* ```
|
17
|
-
*/
|
18
|
-
function format(str, obj, needDeepGet = false) {
|
19
|
-
return (str || '').replace(/\${([^}]+)}/g, (_work, key) => needDeepGet ? deepGet(obj, key.split('.'), '') : (obj || {})[key] || '');
|
20
|
-
}
|
21
|
-
/**
|
22
|
-
* Format mask
|
23
|
-
*
|
24
|
-
* 格式化掩码
|
25
|
-
*
|
26
|
-
* | 字符 | 描述 |
|
27
|
-
* | --- | --- |
|
28
|
-
* | `0` | 任意数字,若该位置字符不符合,则默认为 `0` 填充 |
|
29
|
-
* | `9` | 任意数字 |
|
30
|
-
* | `#` | 任意字符 |
|
31
|
-
* | `U` | 转换大写 |
|
32
|
-
* | `L` | 转换小写 |
|
33
|
-
* | `*` | 转换为 `*` 字符 |
|
34
|
-
*
|
35
|
-
* ```ts
|
36
|
-
* formatMask('123', '(###)') => (123)
|
37
|
-
* formatMask('15900000000', '999****9999') => 159****0000
|
38
|
-
* ```
|
39
|
-
*/
|
40
|
-
function formatMask(value, option) {
|
41
|
-
if (!value) {
|
42
|
-
return '';
|
43
|
-
}
|
44
|
-
const opt = {
|
45
|
-
...(typeof option === 'string' ? { mask: option } : option)
|
46
|
-
};
|
47
|
-
const tokens = {
|
48
|
-
'0': { pattern: /\d/, default: '0' },
|
49
|
-
'9': { pattern: /\d/ },
|
50
|
-
'#': { pattern: /[a-zA-Z0-9]/ },
|
51
|
-
U: {
|
52
|
-
pattern: /[a-zA-Z]/,
|
53
|
-
transform: char => char.toLocaleUpperCase()
|
54
|
-
},
|
55
|
-
L: {
|
56
|
-
pattern: /[a-zA-Z]/,
|
57
|
-
transform: char => char.toLocaleLowerCase()
|
58
|
-
},
|
59
|
-
'*': {
|
60
|
-
pattern: /.*/,
|
61
|
-
transform: _ => `*`
|
62
|
-
},
|
63
|
-
...opt.tokens
|
64
|
-
};
|
65
|
-
const splitValue = value.split('');
|
66
|
-
return opt.mask
|
67
|
-
.split('')
|
68
|
-
.reduce((res, cur) => {
|
69
|
-
const token = tokens[cur];
|
70
|
-
if (!token) {
|
71
|
-
res.push(cur);
|
72
|
-
return res;
|
73
|
-
}
|
74
|
-
const value = splitValue.shift() ?? '';
|
75
|
-
if (!token.pattern.test(value)) {
|
76
|
-
if (token.default)
|
77
|
-
res.push(token.default);
|
78
|
-
return res;
|
79
|
-
}
|
80
|
-
if (typeof token.transform === 'function') {
|
81
|
-
res.push(token.transform(value));
|
82
|
-
}
|
83
|
-
else {
|
84
|
-
res.push(value);
|
85
|
-
}
|
86
|
-
return res;
|
87
|
-
}, [])
|
88
|
-
.join('');
|
89
|
-
}
|
90
|
-
|
91
|
-
const REGEX_STR = {
|
92
|
-
num: `(([-+]?\\d+\\.\\d+)|([-+]?\\d+)|([-+]?\\.\\d+))(?:[eE]([-+]?\\d+))?`,
|
93
|
-
idCard: `(^\\d{15}$)|(^\\d{17}(?:[0-9]|X)$)`,
|
94
|
-
mobile: `^(0|\\+?86|17951)?1[0-9]{10}$`,
|
95
|
-
url: `(((^https?:(?:\/\/)?)(?:[-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9.-]+(?::\\d+)?|(?:www.|[-;:&=\\+\\$,\\w]+@)[A-Za-z0-9.-]+)((?:\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%@.\\w_]*)#?(?:[\\w]*))?)`,
|
96
|
-
ip: `(?:^(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}$)|(?:^(?:(?:[a-fA-F\\d]{1,4}:){7}(?:[a-fA-F\\d]{1,4}|:)|(?:[a-fA-F\\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|:[a-fA-F\\d]{1,4}|:)|(?:[a-fA-F\\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,2}|:)|(?:[a-fA-F\\d]{1,4}:){4}(?:(?::[a-fA-F\\d]{1,4}){0,1}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,3}|:)|(?:[a-fA-F\\d]{1,4}:){3}(?:(?::[a-fA-F\\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,4}|:)|(?:[a-fA-F\\d]{1,4}:){2}(?:(?::[a-fA-F\\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,5}|:)|(?:[a-fA-F\\d]{1,4}:){1}(?:(?::[a-fA-F\\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,6}|:)|(?::(?:(?::[a-fA-F\\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,7}|:)))(?:%[0-9a-zA-Z]{1,})?$)`,
|
97
|
-
color: `(?:#|0x)(?:[a-f0-9]{3}|[a-f0-9]{6})\\b|(?:rgb|hsl)a?\\([^\\)]*\\)`,
|
98
|
-
chinese: `[\u4e00-\u9fa5]+`
|
99
|
-
};
|
100
|
-
function genRegex(str, flags) {
|
101
|
-
return new RegExp(`^${str}$`, flags);
|
102
|
-
}
|
103
|
-
const REGEX = {
|
104
|
-
num: genRegex(REGEX_STR.num),
|
105
|
-
idCard: genRegex(REGEX_STR.idCard, 'i'),
|
106
|
-
mobile: genRegex(REGEX_STR.mobile),
|
107
|
-
url: genRegex(REGEX_STR.url),
|
108
|
-
ip: genRegex(REGEX_STR.ip),
|
109
|
-
color: genRegex(REGEX_STR.color),
|
110
|
-
chinese: genRegex(REGEX_STR.chinese)
|
111
|
-
};
|
112
|
-
/**
|
113
|
-
* Wheter is number
|
114
|
-
*
|
115
|
-
* 是否为数字
|
116
|
-
*/
|
117
|
-
function isNum(value) {
|
118
|
-
return REGEX.num.test(value.toString());
|
119
|
-
}
|
120
|
-
/**
|
121
|
-
* Wheter is integer
|
122
|
-
*
|
123
|
-
* 是否为整数
|
124
|
-
*/
|
125
|
-
function isInt(value) {
|
126
|
-
return isNum(value) && parseInt(value.toString(), 10).toString() === value.toString();
|
127
|
-
}
|
128
|
-
/**
|
129
|
-
* Wheter is decimal
|
130
|
-
*
|
131
|
-
* 是否为小数点数值
|
132
|
-
*/
|
133
|
-
function isDecimal(value) {
|
134
|
-
return isNum(value) && !isInt(value);
|
135
|
-
}
|
136
|
-
/**
|
137
|
-
* Wheter is People's Republic of China identity card
|
138
|
-
*
|
139
|
-
* 是否为中华人民共和国居民身份证
|
140
|
-
*/
|
141
|
-
function isIdCard(value) {
|
142
|
-
return REGEX.idCard.test(value);
|
143
|
-
}
|
144
|
-
/**
|
145
|
-
* Wheter is china mobile (China)
|
146
|
-
*
|
147
|
-
* 是否为手机号(中国)
|
148
|
-
*/
|
149
|
-
function isMobile(value) {
|
150
|
-
return REGEX.mobile.test(value);
|
151
|
-
}
|
152
|
-
/**
|
153
|
-
* Wheter is url address
|
154
|
-
*
|
155
|
-
* 是否URL地址
|
156
|
-
*/
|
157
|
-
function isUrl(url) {
|
158
|
-
return REGEX.url.test(url);
|
159
|
-
}
|
160
|
-
/**
|
161
|
-
* Wheter is IPv4 address (Support v4, v6)
|
162
|
-
*
|
163
|
-
* 是否IP4地址(支持v4、v6)
|
164
|
-
*/
|
165
|
-
function isIp(ip) {
|
166
|
-
return REGEX.ip.test(ip);
|
167
|
-
}
|
168
|
-
/**
|
169
|
-
* Wheter is color
|
170
|
-
*
|
171
|
-
* 是否颜色代码值
|
172
|
-
*/
|
173
|
-
function isColor(color) {
|
174
|
-
return REGEX.color.test(color);
|
175
|
-
}
|
176
|
-
/**
|
177
|
-
* Wheter is chinese
|
178
|
-
*
|
179
|
-
* 是否中文
|
180
|
-
*/
|
181
|
-
function isChinese(value) {
|
182
|
-
return REGEX.chinese.test(value);
|
183
|
-
}
|
184
|
-
|
185
|
-
const CurrencyMega_Powers = [
|
186
|
-
{ unit: 'Q', value: Math.pow(10, 15) },
|
187
|
-
{ unit: 'T', value: Math.pow(10, 12) },
|
188
|
-
{ unit: 'B', value: Math.pow(10, 9) },
|
189
|
-
{ unit: 'M', value: Math.pow(10, 6) },
|
190
|
-
{ unit: 'K', value: 1000 }
|
191
|
-
];
|
192
|
-
|
193
|
-
class CurrencyService {
|
194
|
-
constructor(cog, locale, _defaultCurrencyCode = 'USD') {
|
195
|
-
this.locale = locale;
|
196
|
-
this.currencyPipe = new CurrencyPipe(locale, _defaultCurrencyCode);
|
197
|
-
this.c = cog.merge('utilCurrency', {
|
198
|
-
startingUnit: 'yuan',
|
199
|
-
megaUnit: { Q: '京', T: '兆', B: '亿', M: '万', K: '千' },
|
200
|
-
precision: 2,
|
201
|
-
ignoreZeroPrecision: true
|
202
|
-
});
|
203
|
-
}
|
204
|
-
/**
|
205
|
-
* Format a number with commas as thousands separators
|
206
|
-
*
|
207
|
-
* 格式化货币,用逗号将数字格式化为千位分隔符
|
208
|
-
* ```ts
|
209
|
-
* 10000 => `10,000`
|
210
|
-
* 10000.567 => `10,000.57`
|
211
|
-
* ```
|
212
|
-
*/
|
213
|
-
format(value, options) {
|
214
|
-
options = {
|
215
|
-
startingUnit: this.c.startingUnit,
|
216
|
-
precision: this.c.precision,
|
217
|
-
ignoreZeroPrecision: this.c.ignoreZeroPrecision,
|
218
|
-
ngCurrency: this.c.ngCurrency,
|
219
|
-
...options
|
220
|
-
};
|
221
|
-
let truthValue = Number(value);
|
222
|
-
if (value == null || isNaN(truthValue)) {
|
223
|
-
return '';
|
224
|
-
}
|
225
|
-
if (options.startingUnit === 'cent') {
|
226
|
-
truthValue = truthValue / 100;
|
227
|
-
}
|
228
|
-
if (options.ngCurrency != null) {
|
229
|
-
const cur = options.ngCurrency;
|
230
|
-
return this.currencyPipe.transform(truthValue, cur.currencyCode, cur.display, cur.digitsInfo, cur.locale || this.locale);
|
231
|
-
}
|
232
|
-
const res = formatNumber(truthValue, this.locale, `.${options.ignoreZeroPrecision ? 1 : options.precision}-${options.precision}`);
|
233
|
-
return options.ignoreZeroPrecision ? res.replace(/(?:\.[0]+)$/g, '') : res;
|
234
|
-
}
|
235
|
-
/**
|
236
|
-
* Large number format filter
|
237
|
-
*
|
238
|
-
* 大数据格式化
|
239
|
-
* ```ts
|
240
|
-
* 1000 => { value: '1', unit: 'K', unitI18n: '千' }
|
241
|
-
* 12456 => { value: '12.46', unit: 'K', unitI18n: '千' }
|
242
|
-
* ```
|
243
|
-
*/
|
244
|
-
mega(value, options) {
|
245
|
-
options = { precision: this.c.precision, unitI18n: this.c.megaUnit, startingUnit: this.c.startingUnit, ...options };
|
246
|
-
let num = Number(value);
|
247
|
-
const res = { raw: value, value: '', unit: '', unitI18n: '' };
|
248
|
-
if (isNaN(num) || num === 0) {
|
249
|
-
res.value = value.toString();
|
250
|
-
return res;
|
251
|
-
}
|
252
|
-
if (options.startingUnit === 'cent') {
|
253
|
-
num = num / 100;
|
254
|
-
}
|
255
|
-
let abs = Math.abs(+num);
|
256
|
-
const rounder = Math.pow(10, options.precision);
|
257
|
-
const isNegative = num < 0;
|
258
|
-
for (const p of CurrencyMega_Powers) {
|
259
|
-
let reduced = abs / p.value;
|
260
|
-
reduced = Math.round(reduced * rounder) / rounder;
|
261
|
-
if (reduced >= 1) {
|
262
|
-
abs = reduced;
|
263
|
-
res.unit = p.unit;
|
264
|
-
break;
|
265
|
-
}
|
266
|
-
}
|
267
|
-
res.value = (isNegative ? '-' : '') + abs;
|
268
|
-
res.unitI18n = options.unitI18n[res.unit];
|
269
|
-
return res;
|
270
|
-
}
|
271
|
-
/**
|
272
|
-
* Converted into RMB notation.
|
273
|
-
*
|
274
|
-
* 转化成人民币表示法
|
275
|
-
*/
|
276
|
-
cny(value, options) {
|
277
|
-
options = {
|
278
|
-
inWords: true,
|
279
|
-
minusSymbol: '负',
|
280
|
-
startingUnit: this.c.startingUnit,
|
281
|
-
...options
|
282
|
-
};
|
283
|
-
value = Number(value);
|
284
|
-
if (isNaN(value)) {
|
285
|
-
return '';
|
286
|
-
}
|
287
|
-
if (options.startingUnit === 'cent') {
|
288
|
-
value = value / 100;
|
289
|
-
}
|
290
|
-
value = value.toString();
|
291
|
-
let integer;
|
292
|
-
let decimal;
|
293
|
-
[integer, decimal] = value.split('.');
|
294
|
-
let symbol = '';
|
295
|
-
if (integer.startsWith('-')) {
|
296
|
-
symbol = options.minusSymbol;
|
297
|
-
integer = integer.substring(1);
|
298
|
-
}
|
299
|
-
if (/^-?\d+$/.test(value)) {
|
300
|
-
decimal = null;
|
301
|
-
}
|
302
|
-
integer = (+integer).toString();
|
303
|
-
const inWords = options.inWords;
|
304
|
-
const unit = {
|
305
|
-
num: inWords
|
306
|
-
? ['', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '点']
|
307
|
-
: ['', '一', '二', '三', '四', '五', '六', '七', '八', '九', '点'],
|
308
|
-
radice: inWords
|
309
|
-
? [
|
310
|
-
'',
|
311
|
-
'拾',
|
312
|
-
'佰',
|
313
|
-
'仟',
|
314
|
-
'万',
|
315
|
-
'拾',
|
316
|
-
'佰',
|
317
|
-
'仟',
|
318
|
-
'亿',
|
319
|
-
'拾',
|
320
|
-
'佰',
|
321
|
-
'仟',
|
322
|
-
'万亿',
|
323
|
-
'拾',
|
324
|
-
'佰',
|
325
|
-
'仟',
|
326
|
-
'兆',
|
327
|
-
'拾',
|
328
|
-
'佰',
|
329
|
-
'仟'
|
330
|
-
]
|
331
|
-
: [
|
332
|
-
'',
|
333
|
-
'十',
|
334
|
-
'百',
|
335
|
-
'千',
|
336
|
-
'万',
|
337
|
-
'十',
|
338
|
-
'百',
|
339
|
-
'千',
|
340
|
-
'亿',
|
341
|
-
'十',
|
342
|
-
'百',
|
343
|
-
'千',
|
344
|
-
'万亿',
|
345
|
-
'十',
|
346
|
-
'百',
|
347
|
-
'千',
|
348
|
-
'兆',
|
349
|
-
'十',
|
350
|
-
'百',
|
351
|
-
'千'
|
352
|
-
],
|
353
|
-
dec: ['角', '分', '厘', '毫']
|
354
|
-
};
|
355
|
-
if (inWords) {
|
356
|
-
value = (+value).toFixed(5).toString();
|
357
|
-
}
|
358
|
-
let integerRes = '';
|
359
|
-
const integerCount = integer.length;
|
360
|
-
if (integer === '0' || integerCount === 0) {
|
361
|
-
integerRes = '零';
|
362
|
-
}
|
363
|
-
else {
|
364
|
-
let cnDesc = '';
|
365
|
-
for (let i = 0; i < integerCount; i++) {
|
366
|
-
const n = +integer[i];
|
367
|
-
const j = integerCount - i - 1;
|
368
|
-
const isZero = i > 1 && n !== 0 && integer[i - 1] === '0';
|
369
|
-
const cnZero = isZero ? '零' : '';
|
370
|
-
const isEmpptyUnit = (n === 0 && j % 4 !== 0) || integer.substring(i - 3, i - 3 + 4) === '0000';
|
371
|
-
const descMark = cnDesc;
|
372
|
-
let cnNum = unit.num[n];
|
373
|
-
cnDesc = isEmpptyUnit ? '' : unit.radice[j];
|
374
|
-
// 第一位是一十
|
375
|
-
if (i === 0 && cnNum === '一' && cnDesc === '十')
|
376
|
-
cnNum = '';
|
377
|
-
const isChangeEr = n > 1 &&
|
378
|
-
cnNum === '二' && // 去除首位
|
379
|
-
['', '十', '百'].indexOf(cnDesc) === -1 && // 不读两\两十\两百
|
380
|
-
descMark !== '十'; // 不读十两
|
381
|
-
if (isChangeEr)
|
382
|
-
cnNum = '两';
|
383
|
-
integerRes += cnZero + cnNum + cnDesc;
|
384
|
-
}
|
385
|
-
}
|
386
|
-
// 小数部分拼接
|
387
|
-
let decimalRes = '';
|
388
|
-
const decimalCount = decimal ? decimal.toString().length : 0;
|
389
|
-
if (decimal === null) {
|
390
|
-
decimalRes = inWords ? '整' : '';
|
391
|
-
}
|
392
|
-
else if (decimal === '0') {
|
393
|
-
decimalRes = '零';
|
394
|
-
}
|
395
|
-
else {
|
396
|
-
for (let i = 0; i < decimalCount; i++) {
|
397
|
-
if (inWords && i > unit.dec.length - 1)
|
398
|
-
break;
|
399
|
-
const n = decimal[i];
|
400
|
-
const cnZero = n === '0' ? '零' : '';
|
401
|
-
const cnNum = unit.num[+n];
|
402
|
-
const cnDesc = inWords ? unit.dec[i] : '';
|
403
|
-
decimalRes += cnZero + cnNum + cnDesc;
|
404
|
-
}
|
405
|
-
}
|
406
|
-
const ret = symbol +
|
407
|
-
(inWords
|
408
|
-
? integerRes + (decimalRes === '零' ? '元整' : `元${decimalRes}`)
|
409
|
-
: integerRes + (decimalRes === '' ? '' : `点${decimalRes}`));
|
410
|
-
return ret;
|
411
|
-
}
|
412
|
-
}
|
413
|
-
|
414
|
-
|
415
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "
|
416
|
-
type: Injectable,
|
417
|
-
args: [{ providedIn: 'root' }]
|
418
|
-
}], ctorParameters: function () { return [{ type: i1.YunzaiConfigService }, { type: undefined, decorators: [{
|
419
|
-
type: Inject,
|
420
|
-
args: [LOCALE_ID]
|
421
|
-
}] }, { type: undefined, decorators: [{
|
422
|
-
type: Inject,
|
423
|
-
args: [DEFAULT_CURRENCY_CODE]
|
424
|
-
}] }]; } });
|
425
|
-
|
426
|
-
/**
|
427
|
-
* Generated bundle index. Do not edit.
|
428
|
-
*/
|
429
|
-
|
430
|
-
export { CurrencyMega_Powers, CurrencyService, REGEX, REGEX_STR, format, formatMask, isChinese, isColor, isDecimal, isIdCard, isInt, isIp, isMobile, isNum, isUrl };
|
431
|
-
//# sourceMappingURL=format.mjs.map
|
1
|
+
import { deepGet } from '@yelon/util/other';
|
2
|
+
import { CurrencyPipe, formatNumber } from '@angular/common';
|
3
|
+
import * as i0 from '@angular/core';
|
4
|
+
import { LOCALE_ID, DEFAULT_CURRENCY_CODE, Injectable, Inject } from '@angular/core';
|
5
|
+
import * as i1 from '@yelon/util/config';
|
6
|
+
|
7
|
+
/**
|
8
|
+
* String formatting
|
9
|
+
*
|
10
|
+
* 字符串格式化
|
11
|
+
* ```
|
12
|
+
* format('this is ${name}', { name: 'asdf' })
|
13
|
+
* // output: this is asdf
|
14
|
+
* format('this is ${user.name}', { user: { name: 'asdf' } }, true)
|
15
|
+
* // output: this is asdf
|
16
|
+
* ```
|
17
|
+
*/
|
18
|
+
function format(str, obj, needDeepGet = false) {
|
19
|
+
return (str || '').replace(/\${([^}]+)}/g, (_work, key) => needDeepGet ? deepGet(obj, key.split('.'), '') : (obj || {})[key] || '');
|
20
|
+
}
|
21
|
+
/**
|
22
|
+
* Format mask
|
23
|
+
*
|
24
|
+
* 格式化掩码
|
25
|
+
*
|
26
|
+
* | 字符 | 描述 |
|
27
|
+
* | --- | --- |
|
28
|
+
* | `0` | 任意数字,若该位置字符不符合,则默认为 `0` 填充 |
|
29
|
+
* | `9` | 任意数字 |
|
30
|
+
* | `#` | 任意字符 |
|
31
|
+
* | `U` | 转换大写 |
|
32
|
+
* | `L` | 转换小写 |
|
33
|
+
* | `*` | 转换为 `*` 字符 |
|
34
|
+
*
|
35
|
+
* ```ts
|
36
|
+
* formatMask('123', '(###)') => (123)
|
37
|
+
* formatMask('15900000000', '999****9999') => 159****0000
|
38
|
+
* ```
|
39
|
+
*/
|
40
|
+
function formatMask(value, option) {
|
41
|
+
if (!value) {
|
42
|
+
return '';
|
43
|
+
}
|
44
|
+
const opt = {
|
45
|
+
...(typeof option === 'string' ? { mask: option } : option)
|
46
|
+
};
|
47
|
+
const tokens = {
|
48
|
+
'0': { pattern: /\d/, default: '0' },
|
49
|
+
'9': { pattern: /\d/ },
|
50
|
+
'#': { pattern: /[a-zA-Z0-9]/ },
|
51
|
+
U: {
|
52
|
+
pattern: /[a-zA-Z]/,
|
53
|
+
transform: char => char.toLocaleUpperCase()
|
54
|
+
},
|
55
|
+
L: {
|
56
|
+
pattern: /[a-zA-Z]/,
|
57
|
+
transform: char => char.toLocaleLowerCase()
|
58
|
+
},
|
59
|
+
'*': {
|
60
|
+
pattern: /.*/,
|
61
|
+
transform: _ => `*`
|
62
|
+
},
|
63
|
+
...opt.tokens
|
64
|
+
};
|
65
|
+
const splitValue = value.split('');
|
66
|
+
return opt.mask
|
67
|
+
.split('')
|
68
|
+
.reduce((res, cur) => {
|
69
|
+
const token = tokens[cur];
|
70
|
+
if (!token) {
|
71
|
+
res.push(cur);
|
72
|
+
return res;
|
73
|
+
}
|
74
|
+
const value = splitValue.shift() ?? '';
|
75
|
+
if (!token.pattern.test(value)) {
|
76
|
+
if (token.default)
|
77
|
+
res.push(token.default);
|
78
|
+
return res;
|
79
|
+
}
|
80
|
+
if (typeof token.transform === 'function') {
|
81
|
+
res.push(token.transform(value));
|
82
|
+
}
|
83
|
+
else {
|
84
|
+
res.push(value);
|
85
|
+
}
|
86
|
+
return res;
|
87
|
+
}, [])
|
88
|
+
.join('');
|
89
|
+
}
|
90
|
+
|
91
|
+
const REGEX_STR = {
|
92
|
+
num: `(([-+]?\\d+\\.\\d+)|([-+]?\\d+)|([-+]?\\.\\d+))(?:[eE]([-+]?\\d+))?`,
|
93
|
+
idCard: `(^\\d{15}$)|(^\\d{17}(?:[0-9]|X)$)`,
|
94
|
+
mobile: `^(0|\\+?86|17951)?1[0-9]{10}$`,
|
95
|
+
url: `(((^https?:(?:\/\/)?)(?:[-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9.-]+(?::\\d+)?|(?:www.|[-;:&=\\+\\$,\\w]+@)[A-Za-z0-9.-]+)((?:\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%@.\\w_]*)#?(?:[\\w]*))?)`,
|
96
|
+
ip: `(?:^(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}$)|(?:^(?:(?:[a-fA-F\\d]{1,4}:){7}(?:[a-fA-F\\d]{1,4}|:)|(?:[a-fA-F\\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|:[a-fA-F\\d]{1,4}|:)|(?:[a-fA-F\\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,2}|:)|(?:[a-fA-F\\d]{1,4}:){4}(?:(?::[a-fA-F\\d]{1,4}){0,1}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,3}|:)|(?:[a-fA-F\\d]{1,4}:){3}(?:(?::[a-fA-F\\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,4}|:)|(?:[a-fA-F\\d]{1,4}:){2}(?:(?::[a-fA-F\\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,5}|:)|(?:[a-fA-F\\d]{1,4}:){1}(?:(?::[a-fA-F\\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,6}|:)|(?::(?:(?::[a-fA-F\\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,7}|:)))(?:%[0-9a-zA-Z]{1,})?$)`,
|
97
|
+
color: `(?:#|0x)(?:[a-f0-9]{3}|[a-f0-9]{6})\\b|(?:rgb|hsl)a?\\([^\\)]*\\)`,
|
98
|
+
chinese: `[\u4e00-\u9fa5]+`
|
99
|
+
};
|
100
|
+
function genRegex(str, flags) {
|
101
|
+
return new RegExp(`^${str}$`, flags);
|
102
|
+
}
|
103
|
+
const REGEX = {
|
104
|
+
num: genRegex(REGEX_STR.num),
|
105
|
+
idCard: genRegex(REGEX_STR.idCard, 'i'),
|
106
|
+
mobile: genRegex(REGEX_STR.mobile),
|
107
|
+
url: genRegex(REGEX_STR.url),
|
108
|
+
ip: genRegex(REGEX_STR.ip),
|
109
|
+
color: genRegex(REGEX_STR.color),
|
110
|
+
chinese: genRegex(REGEX_STR.chinese)
|
111
|
+
};
|
112
|
+
/**
|
113
|
+
* Wheter is number
|
114
|
+
*
|
115
|
+
* 是否为数字
|
116
|
+
*/
|
117
|
+
function isNum(value) {
|
118
|
+
return REGEX.num.test(value.toString());
|
119
|
+
}
|
120
|
+
/**
|
121
|
+
* Wheter is integer
|
122
|
+
*
|
123
|
+
* 是否为整数
|
124
|
+
*/
|
125
|
+
function isInt(value) {
|
126
|
+
return isNum(value) && parseInt(value.toString(), 10).toString() === value.toString();
|
127
|
+
}
|
128
|
+
/**
|
129
|
+
* Wheter is decimal
|
130
|
+
*
|
131
|
+
* 是否为小数点数值
|
132
|
+
*/
|
133
|
+
function isDecimal(value) {
|
134
|
+
return isNum(value) && !isInt(value);
|
135
|
+
}
|
136
|
+
/**
|
137
|
+
* Wheter is People's Republic of China identity card
|
138
|
+
*
|
139
|
+
* 是否为中华人民共和国居民身份证
|
140
|
+
*/
|
141
|
+
function isIdCard(value) {
|
142
|
+
return REGEX.idCard.test(value);
|
143
|
+
}
|
144
|
+
/**
|
145
|
+
* Wheter is china mobile (China)
|
146
|
+
*
|
147
|
+
* 是否为手机号(中国)
|
148
|
+
*/
|
149
|
+
function isMobile(value) {
|
150
|
+
return REGEX.mobile.test(value);
|
151
|
+
}
|
152
|
+
/**
|
153
|
+
* Wheter is url address
|
154
|
+
*
|
155
|
+
* 是否URL地址
|
156
|
+
*/
|
157
|
+
function isUrl(url) {
|
158
|
+
return REGEX.url.test(url);
|
159
|
+
}
|
160
|
+
/**
|
161
|
+
* Wheter is IPv4 address (Support v4, v6)
|
162
|
+
*
|
163
|
+
* 是否IP4地址(支持v4、v6)
|
164
|
+
*/
|
165
|
+
function isIp(ip) {
|
166
|
+
return REGEX.ip.test(ip);
|
167
|
+
}
|
168
|
+
/**
|
169
|
+
* Wheter is color
|
170
|
+
*
|
171
|
+
* 是否颜色代码值
|
172
|
+
*/
|
173
|
+
function isColor(color) {
|
174
|
+
return REGEX.color.test(color);
|
175
|
+
}
|
176
|
+
/**
|
177
|
+
* Wheter is chinese
|
178
|
+
*
|
179
|
+
* 是否中文
|
180
|
+
*/
|
181
|
+
function isChinese(value) {
|
182
|
+
return REGEX.chinese.test(value);
|
183
|
+
}
|
184
|
+
|
185
|
+
const CurrencyMega_Powers = [
|
186
|
+
{ unit: 'Q', value: Math.pow(10, 15) },
|
187
|
+
{ unit: 'T', value: Math.pow(10, 12) },
|
188
|
+
{ unit: 'B', value: Math.pow(10, 9) },
|
189
|
+
{ unit: 'M', value: Math.pow(10, 6) },
|
190
|
+
{ unit: 'K', value: 1000 }
|
191
|
+
];
|
192
|
+
|
193
|
+
class CurrencyService {
|
194
|
+
constructor(cog, locale, _defaultCurrencyCode = 'USD') {
|
195
|
+
this.locale = locale;
|
196
|
+
this.currencyPipe = new CurrencyPipe(locale, _defaultCurrencyCode);
|
197
|
+
this.c = cog.merge('utilCurrency', {
|
198
|
+
startingUnit: 'yuan',
|
199
|
+
megaUnit: { Q: '京', T: '兆', B: '亿', M: '万', K: '千' },
|
200
|
+
precision: 2,
|
201
|
+
ignoreZeroPrecision: true
|
202
|
+
});
|
203
|
+
}
|
204
|
+
/**
|
205
|
+
* Format a number with commas as thousands separators
|
206
|
+
*
|
207
|
+
* 格式化货币,用逗号将数字格式化为千位分隔符
|
208
|
+
* ```ts
|
209
|
+
* 10000 => `10,000`
|
210
|
+
* 10000.567 => `10,000.57`
|
211
|
+
* ```
|
212
|
+
*/
|
213
|
+
format(value, options) {
|
214
|
+
options = {
|
215
|
+
startingUnit: this.c.startingUnit,
|
216
|
+
precision: this.c.precision,
|
217
|
+
ignoreZeroPrecision: this.c.ignoreZeroPrecision,
|
218
|
+
ngCurrency: this.c.ngCurrency,
|
219
|
+
...options
|
220
|
+
};
|
221
|
+
let truthValue = Number(value);
|
222
|
+
if (value == null || isNaN(truthValue)) {
|
223
|
+
return '';
|
224
|
+
}
|
225
|
+
if (options.startingUnit === 'cent') {
|
226
|
+
truthValue = truthValue / 100;
|
227
|
+
}
|
228
|
+
if (options.ngCurrency != null) {
|
229
|
+
const cur = options.ngCurrency;
|
230
|
+
return this.currencyPipe.transform(truthValue, cur.currencyCode, cur.display, cur.digitsInfo, cur.locale || this.locale);
|
231
|
+
}
|
232
|
+
const res = formatNumber(truthValue, this.locale, `.${options.ignoreZeroPrecision ? 1 : options.precision}-${options.precision}`);
|
233
|
+
return options.ignoreZeroPrecision ? res.replace(/(?:\.[0]+)$/g, '') : res;
|
234
|
+
}
|
235
|
+
/**
|
236
|
+
* Large number format filter
|
237
|
+
*
|
238
|
+
* 大数据格式化
|
239
|
+
* ```ts
|
240
|
+
* 1000 => { value: '1', unit: 'K', unitI18n: '千' }
|
241
|
+
* 12456 => { value: '12.46', unit: 'K', unitI18n: '千' }
|
242
|
+
* ```
|
243
|
+
*/
|
244
|
+
mega(value, options) {
|
245
|
+
options = { precision: this.c.precision, unitI18n: this.c.megaUnit, startingUnit: this.c.startingUnit, ...options };
|
246
|
+
let num = Number(value);
|
247
|
+
const res = { raw: value, value: '', unit: '', unitI18n: '' };
|
248
|
+
if (isNaN(num) || num === 0) {
|
249
|
+
res.value = value.toString();
|
250
|
+
return res;
|
251
|
+
}
|
252
|
+
if (options.startingUnit === 'cent') {
|
253
|
+
num = num / 100;
|
254
|
+
}
|
255
|
+
let abs = Math.abs(+num);
|
256
|
+
const rounder = Math.pow(10, options.precision);
|
257
|
+
const isNegative = num < 0;
|
258
|
+
for (const p of CurrencyMega_Powers) {
|
259
|
+
let reduced = abs / p.value;
|
260
|
+
reduced = Math.round(reduced * rounder) / rounder;
|
261
|
+
if (reduced >= 1) {
|
262
|
+
abs = reduced;
|
263
|
+
res.unit = p.unit;
|
264
|
+
break;
|
265
|
+
}
|
266
|
+
}
|
267
|
+
res.value = (isNegative ? '-' : '') + abs;
|
268
|
+
res.unitI18n = options.unitI18n[res.unit];
|
269
|
+
return res;
|
270
|
+
}
|
271
|
+
/**
|
272
|
+
* Converted into RMB notation.
|
273
|
+
*
|
274
|
+
* 转化成人民币表示法
|
275
|
+
*/
|
276
|
+
cny(value, options) {
|
277
|
+
options = {
|
278
|
+
inWords: true,
|
279
|
+
minusSymbol: '负',
|
280
|
+
startingUnit: this.c.startingUnit,
|
281
|
+
...options
|
282
|
+
};
|
283
|
+
value = Number(value);
|
284
|
+
if (isNaN(value)) {
|
285
|
+
return '';
|
286
|
+
}
|
287
|
+
if (options.startingUnit === 'cent') {
|
288
|
+
value = value / 100;
|
289
|
+
}
|
290
|
+
value = value.toString();
|
291
|
+
let integer;
|
292
|
+
let decimal;
|
293
|
+
[integer, decimal] = value.split('.');
|
294
|
+
let symbol = '';
|
295
|
+
if (integer.startsWith('-')) {
|
296
|
+
symbol = options.minusSymbol;
|
297
|
+
integer = integer.substring(1);
|
298
|
+
}
|
299
|
+
if (/^-?\d+$/.test(value)) {
|
300
|
+
decimal = null;
|
301
|
+
}
|
302
|
+
integer = (+integer).toString();
|
303
|
+
const inWords = options.inWords;
|
304
|
+
const unit = {
|
305
|
+
num: inWords
|
306
|
+
? ['', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '点']
|
307
|
+
: ['', '一', '二', '三', '四', '五', '六', '七', '八', '九', '点'],
|
308
|
+
radice: inWords
|
309
|
+
? [
|
310
|
+
'',
|
311
|
+
'拾',
|
312
|
+
'佰',
|
313
|
+
'仟',
|
314
|
+
'万',
|
315
|
+
'拾',
|
316
|
+
'佰',
|
317
|
+
'仟',
|
318
|
+
'亿',
|
319
|
+
'拾',
|
320
|
+
'佰',
|
321
|
+
'仟',
|
322
|
+
'万亿',
|
323
|
+
'拾',
|
324
|
+
'佰',
|
325
|
+
'仟',
|
326
|
+
'兆',
|
327
|
+
'拾',
|
328
|
+
'佰',
|
329
|
+
'仟'
|
330
|
+
]
|
331
|
+
: [
|
332
|
+
'',
|
333
|
+
'十',
|
334
|
+
'百',
|
335
|
+
'千',
|
336
|
+
'万',
|
337
|
+
'十',
|
338
|
+
'百',
|
339
|
+
'千',
|
340
|
+
'亿',
|
341
|
+
'十',
|
342
|
+
'百',
|
343
|
+
'千',
|
344
|
+
'万亿',
|
345
|
+
'十',
|
346
|
+
'百',
|
347
|
+
'千',
|
348
|
+
'兆',
|
349
|
+
'十',
|
350
|
+
'百',
|
351
|
+
'千'
|
352
|
+
],
|
353
|
+
dec: ['角', '分', '厘', '毫']
|
354
|
+
};
|
355
|
+
if (inWords) {
|
356
|
+
value = (+value).toFixed(5).toString();
|
357
|
+
}
|
358
|
+
let integerRes = '';
|
359
|
+
const integerCount = integer.length;
|
360
|
+
if (integer === '0' || integerCount === 0) {
|
361
|
+
integerRes = '零';
|
362
|
+
}
|
363
|
+
else {
|
364
|
+
let cnDesc = '';
|
365
|
+
for (let i = 0; i < integerCount; i++) {
|
366
|
+
const n = +integer[i];
|
367
|
+
const j = integerCount - i - 1;
|
368
|
+
const isZero = i > 1 && n !== 0 && integer[i - 1] === '0';
|
369
|
+
const cnZero = isZero ? '零' : '';
|
370
|
+
const isEmpptyUnit = (n === 0 && j % 4 !== 0) || integer.substring(i - 3, i - 3 + 4) === '0000';
|
371
|
+
const descMark = cnDesc;
|
372
|
+
let cnNum = unit.num[n];
|
373
|
+
cnDesc = isEmpptyUnit ? '' : unit.radice[j];
|
374
|
+
// 第一位是一十
|
375
|
+
if (i === 0 && cnNum === '一' && cnDesc === '十')
|
376
|
+
cnNum = '';
|
377
|
+
const isChangeEr = n > 1 &&
|
378
|
+
cnNum === '二' && // 去除首位
|
379
|
+
['', '十', '百'].indexOf(cnDesc) === -1 && // 不读两\两十\两百
|
380
|
+
descMark !== '十'; // 不读十两
|
381
|
+
if (isChangeEr)
|
382
|
+
cnNum = '两';
|
383
|
+
integerRes += cnZero + cnNum + cnDesc;
|
384
|
+
}
|
385
|
+
}
|
386
|
+
// 小数部分拼接
|
387
|
+
let decimalRes = '';
|
388
|
+
const decimalCount = decimal ? decimal.toString().length : 0;
|
389
|
+
if (decimal === null) {
|
390
|
+
decimalRes = inWords ? '整' : '';
|
391
|
+
}
|
392
|
+
else if (decimal === '0') {
|
393
|
+
decimalRes = '零';
|
394
|
+
}
|
395
|
+
else {
|
396
|
+
for (let i = 0; i < decimalCount; i++) {
|
397
|
+
if (inWords && i > unit.dec.length - 1)
|
398
|
+
break;
|
399
|
+
const n = decimal[i];
|
400
|
+
const cnZero = n === '0' ? '零' : '';
|
401
|
+
const cnNum = unit.num[+n];
|
402
|
+
const cnDesc = inWords ? unit.dec[i] : '';
|
403
|
+
decimalRes += cnZero + cnNum + cnDesc;
|
404
|
+
}
|
405
|
+
}
|
406
|
+
const ret = symbol +
|
407
|
+
(inWords
|
408
|
+
? integerRes + (decimalRes === '零' ? '元整' : `元${decimalRes}`)
|
409
|
+
: integerRes + (decimalRes === '' ? '' : `点${decimalRes}`));
|
410
|
+
return ret;
|
411
|
+
}
|
412
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.1.2", ngImport: i0, type: CurrencyService, deps: [{ token: i1.YunzaiConfigService }, { token: LOCALE_ID }, { token: DEFAULT_CURRENCY_CODE }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
413
|
+
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.1.2", ngImport: i0, type: CurrencyService, providedIn: 'root' }); }
|
414
|
+
}
|
415
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.1.2", ngImport: i0, type: CurrencyService, decorators: [{
|
416
|
+
type: Injectable,
|
417
|
+
args: [{ providedIn: 'root' }]
|
418
|
+
}], ctorParameters: function () { return [{ type: i1.YunzaiConfigService }, { type: undefined, decorators: [{
|
419
|
+
type: Inject,
|
420
|
+
args: [LOCALE_ID]
|
421
|
+
}] }, { type: undefined, decorators: [{
|
422
|
+
type: Inject,
|
423
|
+
args: [DEFAULT_CURRENCY_CODE]
|
424
|
+
}] }]; } });
|
425
|
+
|
426
|
+
/**
|
427
|
+
* Generated bundle index. Do not edit.
|
428
|
+
*/
|
429
|
+
|
430
|
+
export { CurrencyMega_Powers, CurrencyService, REGEX, REGEX_STR, format, formatMask, isChinese, isColor, isDecimal, isIdCard, isInt, isIp, isMobile, isNum, isUrl };
|
431
|
+
//# sourceMappingURL=format.mjs.map
|