1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | const _ = require("lodash");
|
20 | const Enumerable = require("node-enumerable");
|
21 | const index_1 = require("../index");
|
22 | let knownFormatProviders;
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function format(formatStr, ...args) {
|
32 | return formatArray(formatStr, args);
|
33 | }
|
34 | exports.format = format;
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function formatArray(formatStr, args) {
|
44 | formatStr = index_1.toStringSafe(formatStr);
|
45 | if (!_.isArrayLike(args)) {
|
46 | args = Enumerable.from(args)
|
47 | .toArray();
|
48 | }
|
49 |
|
50 |
|
51 | return formatStr.replace(/{(\d+)(\:)?([^}]*)}/g, (match, index, separator, providerName) => {
|
52 | index = parseInt(index_1.toStringSafe(index));
|
53 | let resultValue = args[index];
|
54 | if (':' === separator) {
|
55 |
|
56 | const FORMAT_PROVIDERS = index_1.toStringSafe(providerName).split(',')
|
57 | .map(x => index_1.normalizeString(x))
|
58 | .filter(x => '' !== x);
|
59 |
|
60 |
|
61 | FORMAT_PROVIDERS.forEach(fp => {
|
62 | let provider = knownFormatProviders[fp];
|
63 | if (_.isNil(provider)) {
|
64 |
|
65 | provider = knownFormatProviders[''];
|
66 | }
|
67 | if (provider) {
|
68 | resultValue = provider(resultValue, match);
|
69 | }
|
70 | });
|
71 | }
|
72 | if (_.isUndefined(resultValue)) {
|
73 | return match;
|
74 | }
|
75 | return index_1.toStringSafe(resultValue);
|
76 | });
|
77 | }
|
78 | exports.formatArray = formatArray;
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function getDefaultStringFormatProviders() {
|
85 | return {
|
86 | 'ending_space': (val) => {
|
87 | val = index_1.toStringSafe(val);
|
88 | if ('' !== val) {
|
89 | val = val + ' ';
|
90 | }
|
91 | return val;
|
92 | },
|
93 | 'leading_space': (val) => {
|
94 | val = index_1.toStringSafe(val);
|
95 | if ('' !== val) {
|
96 | val = ' ' + val;
|
97 | }
|
98 | return val;
|
99 | },
|
100 | 'lower': (val) => {
|
101 | return index_1.toStringSafe(val).toLowerCase();
|
102 | },
|
103 | 'surround': (val) => {
|
104 | val = index_1.toStringSafe(val);
|
105 | if ('' !== val) {
|
106 | val = "'" + index_1.toStringSafe(val) + "'";
|
107 | }
|
108 | return val;
|
109 | },
|
110 | 'trim': (val) => {
|
111 | return index_1.toStringSafe(val).trim();
|
112 | },
|
113 | 'upper': (val) => {
|
114 | return index_1.toStringSafe(val).toUpperCase();
|
115 | },
|
116 | };
|
117 | }
|
118 | exports.getDefaultStringFormatProviders = getDefaultStringFormatProviders;
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | function registerStringFormatProviders(providers, withDefaults) {
|
126 | withDefaults = index_1.toBooleanSafe(withDefaults, true);
|
127 | const NEW_LIST = {};
|
128 | const APPEND_TO_LIST = (list) => {
|
129 | if (_.isNil(list)) {
|
130 | return;
|
131 | }
|
132 | for (const PROVIDER_NAME in list) {
|
133 | NEW_LIST[index_1.normalizeString(PROVIDER_NAME)] = list[PROVIDER_NAME];
|
134 | }
|
135 | };
|
136 | if (withDefaults) {
|
137 | APPEND_TO_LIST(getDefaultStringFormatProviders());
|
138 | }
|
139 | APPEND_TO_LIST(providers);
|
140 | knownFormatProviders = NEW_LIST;
|
141 | }
|
142 | exports.registerStringFormatProviders = registerStringFormatProviders;
|
143 |
|
144 | registerStringFormatProviders();
|
145 |
|
\ | No newline at end of file |