@aidc-toolkit/utility 1.0.23-beta → 1.0.25-beta
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/README.md +30 -30
- package/dist/index.cjs +3570 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +953 -0
- package/dist/index.d.ts +953 -26
- package/dist/index.js +3505 -8
- package/dist/index.js.map +1 -1
- package/package.json +7 -11
- package/src/character-set.ts +20 -19
- package/src/exclusion.ts +6 -1
- package/src/index.ts +9 -9
- package/src/locale/i18n.ts +3 -5
- package/src/locale/i18next.d.ts +1 -1
- package/src/record.ts +2 -2
- package/src/reg-exp.ts +7 -7
- package/src/string.ts +3 -3
- package/src/transformer.ts +19 -18
- package/test/record.test.ts +3 -1
- package/tsup.config.ts +3 -0
- package/typedoc.json +1 -3
- package/dist/character-set.d.ts +0 -307
- package/dist/character-set.d.ts.map +0 -1
- package/dist/character-set.js +0 -563
- package/dist/character-set.js.map +0 -1
- package/dist/exclusion.d.ts +0 -22
- package/dist/exclusion.d.ts.map +0 -1
- package/dist/exclusion.js +0 -18
- package/dist/exclusion.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/iterable-utility.d.ts +0 -39
- package/dist/iterable-utility.d.ts.map +0 -1
- package/dist/iterable-utility.js +0 -35
- package/dist/iterable-utility.js.map +0 -1
- package/dist/locale/en/locale-resources.d.ts +0 -33
- package/dist/locale/en/locale-resources.d.ts.map +0 -1
- package/dist/locale/en/locale-resources.js +0 -32
- package/dist/locale/en/locale-resources.js.map +0 -1
- package/dist/locale/fr/locale-resources.d.ts +0 -33
- package/dist/locale/fr/locale-resources.d.ts.map +0 -1
- package/dist/locale/fr/locale-resources.js +0 -32
- package/dist/locale/fr/locale-resources.js.map +0 -1
- package/dist/locale/i18n.d.ts +0 -27
- package/dist/locale/i18n.d.ts.map +0 -1
- package/dist/locale/i18n.js +0 -35
- package/dist/locale/i18n.js.map +0 -1
- package/dist/record.d.ts +0 -44
- package/dist/record.d.ts.map +0 -1
- package/dist/record.js +0 -58
- package/dist/record.js.map +0 -1
- package/dist/reg-exp.d.ts +0 -43
- package/dist/reg-exp.d.ts.map +0 -1
- package/dist/reg-exp.js +0 -55
- package/dist/reg-exp.js.map +0 -1
- package/dist/sequence.d.ts +0 -68
- package/dist/sequence.d.ts.map +0 -1
- package/dist/sequence.js +0 -96
- package/dist/sequence.js.map +0 -1
- package/dist/string.d.ts +0 -25
- package/dist/string.d.ts.map +0 -1
- package/dist/string.js +0 -2
- package/dist/string.js.map +0 -1
- package/dist/transformer.d.ts +0 -346
- package/dist/transformer.d.ts.map +0 -1
- package/dist/transformer.js +0 -456
- package/dist/transformer.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3508 @@
|
|
|
1
|
+
// src/locale/i18n.ts
|
|
2
|
+
import { i18nCoreInit } from "@aidc-toolkit/core";
|
|
3
|
+
|
|
4
|
+
// node_modules/i18next/dist/esm/i18next.js
|
|
5
|
+
var isString = (obj) => typeof obj === "string";
|
|
6
|
+
var defer = () => {
|
|
7
|
+
let res;
|
|
8
|
+
let rej;
|
|
9
|
+
const promise = new Promise((resolve, reject) => {
|
|
10
|
+
res = resolve;
|
|
11
|
+
rej = reject;
|
|
12
|
+
});
|
|
13
|
+
promise.resolve = res;
|
|
14
|
+
promise.reject = rej;
|
|
15
|
+
return promise;
|
|
16
|
+
};
|
|
17
|
+
var makeString = (object) => {
|
|
18
|
+
if (object == null) return "";
|
|
19
|
+
return "" + object;
|
|
20
|
+
};
|
|
21
|
+
var copy = (a, s, t2) => {
|
|
22
|
+
a.forEach((m) => {
|
|
23
|
+
if (s[m]) t2[m] = s[m];
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
var lastOfPathSeparatorRegExp = /###/g;
|
|
27
|
+
var cleanKey = (key) => key && key.indexOf("###") > -1 ? key.replace(lastOfPathSeparatorRegExp, ".") : key;
|
|
28
|
+
var canNotTraverseDeeper = (object) => !object || isString(object);
|
|
29
|
+
var getLastOfPath = (object, path, Empty) => {
|
|
30
|
+
const stack = !isString(path) ? path : path.split(".");
|
|
31
|
+
let stackIndex = 0;
|
|
32
|
+
while (stackIndex < stack.length - 1) {
|
|
33
|
+
if (canNotTraverseDeeper(object)) return {};
|
|
34
|
+
const key = cleanKey(stack[stackIndex]);
|
|
35
|
+
if (!object[key] && Empty) object[key] = new Empty();
|
|
36
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
37
|
+
object = object[key];
|
|
38
|
+
} else {
|
|
39
|
+
object = {};
|
|
40
|
+
}
|
|
41
|
+
++stackIndex;
|
|
42
|
+
}
|
|
43
|
+
if (canNotTraverseDeeper(object)) return {};
|
|
44
|
+
return {
|
|
45
|
+
obj: object,
|
|
46
|
+
k: cleanKey(stack[stackIndex])
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
var setPath = (object, path, newValue) => {
|
|
50
|
+
const {
|
|
51
|
+
obj,
|
|
52
|
+
k
|
|
53
|
+
} = getLastOfPath(object, path, Object);
|
|
54
|
+
if (obj !== void 0 || path.length === 1) {
|
|
55
|
+
obj[k] = newValue;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
let e = path[path.length - 1];
|
|
59
|
+
let p = path.slice(0, path.length - 1);
|
|
60
|
+
let last = getLastOfPath(object, p, Object);
|
|
61
|
+
while (last.obj === void 0 && p.length) {
|
|
62
|
+
e = `${p[p.length - 1]}.${e}`;
|
|
63
|
+
p = p.slice(0, p.length - 1);
|
|
64
|
+
last = getLastOfPath(object, p, Object);
|
|
65
|
+
if (last?.obj && typeof last.obj[`${last.k}.${e}`] !== "undefined") {
|
|
66
|
+
last.obj = void 0;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
last.obj[`${last.k}.${e}`] = newValue;
|
|
70
|
+
};
|
|
71
|
+
var pushPath = (object, path, newValue, concat) => {
|
|
72
|
+
const {
|
|
73
|
+
obj,
|
|
74
|
+
k
|
|
75
|
+
} = getLastOfPath(object, path, Object);
|
|
76
|
+
obj[k] = obj[k] || [];
|
|
77
|
+
obj[k].push(newValue);
|
|
78
|
+
};
|
|
79
|
+
var getPath = (object, path) => {
|
|
80
|
+
const {
|
|
81
|
+
obj,
|
|
82
|
+
k
|
|
83
|
+
} = getLastOfPath(object, path);
|
|
84
|
+
if (!obj) return void 0;
|
|
85
|
+
if (!Object.prototype.hasOwnProperty.call(obj, k)) return void 0;
|
|
86
|
+
return obj[k];
|
|
87
|
+
};
|
|
88
|
+
var getPathWithDefaults = (data, defaultData, key) => {
|
|
89
|
+
const value = getPath(data, key);
|
|
90
|
+
if (value !== void 0) {
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
return getPath(defaultData, key);
|
|
94
|
+
};
|
|
95
|
+
var deepExtend = (target, source, overwrite) => {
|
|
96
|
+
for (const prop in source) {
|
|
97
|
+
if (prop !== "__proto__" && prop !== "constructor") {
|
|
98
|
+
if (prop in target) {
|
|
99
|
+
if (isString(target[prop]) || target[prop] instanceof String || isString(source[prop]) || source[prop] instanceof String) {
|
|
100
|
+
if (overwrite) target[prop] = source[prop];
|
|
101
|
+
} else {
|
|
102
|
+
deepExtend(target[prop], source[prop], overwrite);
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
target[prop] = source[prop];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return target;
|
|
110
|
+
};
|
|
111
|
+
var regexEscape = (str) => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
|
112
|
+
var _entityMap = {
|
|
113
|
+
"&": "&",
|
|
114
|
+
"<": "<",
|
|
115
|
+
">": ">",
|
|
116
|
+
'"': """,
|
|
117
|
+
"'": "'",
|
|
118
|
+
"/": "/"
|
|
119
|
+
};
|
|
120
|
+
var escape = (data) => {
|
|
121
|
+
if (isString(data)) {
|
|
122
|
+
return data.replace(/[&<>"'\/]/g, (s) => _entityMap[s]);
|
|
123
|
+
}
|
|
124
|
+
return data;
|
|
125
|
+
};
|
|
126
|
+
var RegExpCache = class {
|
|
127
|
+
constructor(capacity) {
|
|
128
|
+
this.capacity = capacity;
|
|
129
|
+
this.regExpMap = /* @__PURE__ */ new Map();
|
|
130
|
+
this.regExpQueue = [];
|
|
131
|
+
}
|
|
132
|
+
getRegExp(pattern) {
|
|
133
|
+
const regExpFromCache = this.regExpMap.get(pattern);
|
|
134
|
+
if (regExpFromCache !== void 0) {
|
|
135
|
+
return regExpFromCache;
|
|
136
|
+
}
|
|
137
|
+
const regExpNew = new RegExp(pattern);
|
|
138
|
+
if (this.regExpQueue.length === this.capacity) {
|
|
139
|
+
this.regExpMap.delete(this.regExpQueue.shift());
|
|
140
|
+
}
|
|
141
|
+
this.regExpMap.set(pattern, regExpNew);
|
|
142
|
+
this.regExpQueue.push(pattern);
|
|
143
|
+
return regExpNew;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
var chars = [" ", ",", "?", "!", ";"];
|
|
147
|
+
var looksLikeObjectPathRegExpCache = new RegExpCache(20);
|
|
148
|
+
var looksLikeObjectPath = (key, nsSeparator, keySeparator) => {
|
|
149
|
+
nsSeparator = nsSeparator || "";
|
|
150
|
+
keySeparator = keySeparator || "";
|
|
151
|
+
const possibleChars = chars.filter((c) => nsSeparator.indexOf(c) < 0 && keySeparator.indexOf(c) < 0);
|
|
152
|
+
if (possibleChars.length === 0) return true;
|
|
153
|
+
const r = looksLikeObjectPathRegExpCache.getRegExp(`(${possibleChars.map((c) => c === "?" ? "\\?" : c).join("|")})`);
|
|
154
|
+
let matched = !r.test(key);
|
|
155
|
+
if (!matched) {
|
|
156
|
+
const ki = key.indexOf(keySeparator);
|
|
157
|
+
if (ki > 0 && !r.test(key.substring(0, ki))) {
|
|
158
|
+
matched = true;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return matched;
|
|
162
|
+
};
|
|
163
|
+
var deepFind = (obj, path, keySeparator = ".") => {
|
|
164
|
+
if (!obj) return void 0;
|
|
165
|
+
if (obj[path]) {
|
|
166
|
+
if (!Object.prototype.hasOwnProperty.call(obj, path)) return void 0;
|
|
167
|
+
return obj[path];
|
|
168
|
+
}
|
|
169
|
+
const tokens = path.split(keySeparator);
|
|
170
|
+
let current = obj;
|
|
171
|
+
for (let i = 0; i < tokens.length; ) {
|
|
172
|
+
if (!current || typeof current !== "object") {
|
|
173
|
+
return void 0;
|
|
174
|
+
}
|
|
175
|
+
let next;
|
|
176
|
+
let nextPath = "";
|
|
177
|
+
for (let j = i; j < tokens.length; ++j) {
|
|
178
|
+
if (j !== i) {
|
|
179
|
+
nextPath += keySeparator;
|
|
180
|
+
}
|
|
181
|
+
nextPath += tokens[j];
|
|
182
|
+
next = current[nextPath];
|
|
183
|
+
if (next !== void 0) {
|
|
184
|
+
if (["string", "number", "boolean"].indexOf(typeof next) > -1 && j < tokens.length - 1) {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
i += j - i + 1;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
current = next;
|
|
192
|
+
}
|
|
193
|
+
return current;
|
|
194
|
+
};
|
|
195
|
+
var getCleanedCode = (code) => code?.replace("_", "-");
|
|
196
|
+
var consoleLogger = {
|
|
197
|
+
type: "logger",
|
|
198
|
+
log(args) {
|
|
199
|
+
this.output("log", args);
|
|
200
|
+
},
|
|
201
|
+
warn(args) {
|
|
202
|
+
this.output("warn", args);
|
|
203
|
+
},
|
|
204
|
+
error(args) {
|
|
205
|
+
this.output("error", args);
|
|
206
|
+
},
|
|
207
|
+
output(type, args) {
|
|
208
|
+
console?.[type]?.apply?.(console, args);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
var Logger = class _Logger {
|
|
212
|
+
constructor(concreteLogger, options = {}) {
|
|
213
|
+
this.init(concreteLogger, options);
|
|
214
|
+
}
|
|
215
|
+
init(concreteLogger, options = {}) {
|
|
216
|
+
this.prefix = options.prefix || "i18next:";
|
|
217
|
+
this.logger = concreteLogger || consoleLogger;
|
|
218
|
+
this.options = options;
|
|
219
|
+
this.debug = options.debug;
|
|
220
|
+
}
|
|
221
|
+
log(...args) {
|
|
222
|
+
return this.forward(args, "log", "", true);
|
|
223
|
+
}
|
|
224
|
+
warn(...args) {
|
|
225
|
+
return this.forward(args, "warn", "", true);
|
|
226
|
+
}
|
|
227
|
+
error(...args) {
|
|
228
|
+
return this.forward(args, "error", "");
|
|
229
|
+
}
|
|
230
|
+
deprecate(...args) {
|
|
231
|
+
return this.forward(args, "warn", "WARNING DEPRECATED: ", true);
|
|
232
|
+
}
|
|
233
|
+
forward(args, lvl, prefix, debugOnly) {
|
|
234
|
+
if (debugOnly && !this.debug) return null;
|
|
235
|
+
if (isString(args[0])) args[0] = `${prefix}${this.prefix} ${args[0]}`;
|
|
236
|
+
return this.logger[lvl](args);
|
|
237
|
+
}
|
|
238
|
+
create(moduleName) {
|
|
239
|
+
return new _Logger(this.logger, {
|
|
240
|
+
...{
|
|
241
|
+
prefix: `${this.prefix}:${moduleName}:`
|
|
242
|
+
},
|
|
243
|
+
...this.options
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
clone(options) {
|
|
247
|
+
options = options || this.options;
|
|
248
|
+
options.prefix = options.prefix || this.prefix;
|
|
249
|
+
return new _Logger(this.logger, options);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
var baseLogger = new Logger();
|
|
253
|
+
var EventEmitter = class {
|
|
254
|
+
constructor() {
|
|
255
|
+
this.observers = {};
|
|
256
|
+
}
|
|
257
|
+
on(events, listener) {
|
|
258
|
+
events.split(" ").forEach((event) => {
|
|
259
|
+
if (!this.observers[event]) this.observers[event] = /* @__PURE__ */ new Map();
|
|
260
|
+
const numListeners = this.observers[event].get(listener) || 0;
|
|
261
|
+
this.observers[event].set(listener, numListeners + 1);
|
|
262
|
+
});
|
|
263
|
+
return this;
|
|
264
|
+
}
|
|
265
|
+
off(event, listener) {
|
|
266
|
+
if (!this.observers[event]) return;
|
|
267
|
+
if (!listener) {
|
|
268
|
+
delete this.observers[event];
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
this.observers[event].delete(listener);
|
|
272
|
+
}
|
|
273
|
+
emit(event, ...args) {
|
|
274
|
+
if (this.observers[event]) {
|
|
275
|
+
const cloned = Array.from(this.observers[event].entries());
|
|
276
|
+
cloned.forEach(([observer, numTimesAdded]) => {
|
|
277
|
+
for (let i = 0; i < numTimesAdded; i++) {
|
|
278
|
+
observer(...args);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
if (this.observers["*"]) {
|
|
283
|
+
const cloned = Array.from(this.observers["*"].entries());
|
|
284
|
+
cloned.forEach(([observer, numTimesAdded]) => {
|
|
285
|
+
for (let i = 0; i < numTimesAdded; i++) {
|
|
286
|
+
observer.apply(observer, [event, ...args]);
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
var ResourceStore = class extends EventEmitter {
|
|
293
|
+
constructor(data, options = {
|
|
294
|
+
ns: ["translation"],
|
|
295
|
+
defaultNS: "translation"
|
|
296
|
+
}) {
|
|
297
|
+
super();
|
|
298
|
+
this.data = data || {};
|
|
299
|
+
this.options = options;
|
|
300
|
+
if (this.options.keySeparator === void 0) {
|
|
301
|
+
this.options.keySeparator = ".";
|
|
302
|
+
}
|
|
303
|
+
if (this.options.ignoreJSONStructure === void 0) {
|
|
304
|
+
this.options.ignoreJSONStructure = true;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
addNamespaces(ns) {
|
|
308
|
+
if (this.options.ns.indexOf(ns) < 0) {
|
|
309
|
+
this.options.ns.push(ns);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
removeNamespaces(ns) {
|
|
313
|
+
const index = this.options.ns.indexOf(ns);
|
|
314
|
+
if (index > -1) {
|
|
315
|
+
this.options.ns.splice(index, 1);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
getResource(lng, ns, key, options = {}) {
|
|
319
|
+
const keySeparator = options.keySeparator !== void 0 ? options.keySeparator : this.options.keySeparator;
|
|
320
|
+
const ignoreJSONStructure = options.ignoreJSONStructure !== void 0 ? options.ignoreJSONStructure : this.options.ignoreJSONStructure;
|
|
321
|
+
let path;
|
|
322
|
+
if (lng.indexOf(".") > -1) {
|
|
323
|
+
path = lng.split(".");
|
|
324
|
+
} else {
|
|
325
|
+
path = [lng, ns];
|
|
326
|
+
if (key) {
|
|
327
|
+
if (Array.isArray(key)) {
|
|
328
|
+
path.push(...key);
|
|
329
|
+
} else if (isString(key) && keySeparator) {
|
|
330
|
+
path.push(...key.split(keySeparator));
|
|
331
|
+
} else {
|
|
332
|
+
path.push(key);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
const result = getPath(this.data, path);
|
|
337
|
+
if (!result && !ns && !key && lng.indexOf(".") > -1) {
|
|
338
|
+
lng = path[0];
|
|
339
|
+
ns = path[1];
|
|
340
|
+
key = path.slice(2).join(".");
|
|
341
|
+
}
|
|
342
|
+
if (result || !ignoreJSONStructure || !isString(key)) return result;
|
|
343
|
+
return deepFind(this.data?.[lng]?.[ns], key, keySeparator);
|
|
344
|
+
}
|
|
345
|
+
addResource(lng, ns, key, value, options = {
|
|
346
|
+
silent: false
|
|
347
|
+
}) {
|
|
348
|
+
const keySeparator = options.keySeparator !== void 0 ? options.keySeparator : this.options.keySeparator;
|
|
349
|
+
let path = [lng, ns];
|
|
350
|
+
if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
|
|
351
|
+
if (lng.indexOf(".") > -1) {
|
|
352
|
+
path = lng.split(".");
|
|
353
|
+
value = ns;
|
|
354
|
+
ns = path[1];
|
|
355
|
+
}
|
|
356
|
+
this.addNamespaces(ns);
|
|
357
|
+
setPath(this.data, path, value);
|
|
358
|
+
if (!options.silent) this.emit("added", lng, ns, key, value);
|
|
359
|
+
}
|
|
360
|
+
addResources(lng, ns, resources, options = {
|
|
361
|
+
silent: false
|
|
362
|
+
}) {
|
|
363
|
+
for (const m in resources) {
|
|
364
|
+
if (isString(resources[m]) || Array.isArray(resources[m])) this.addResource(lng, ns, m, resources[m], {
|
|
365
|
+
silent: true
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
if (!options.silent) this.emit("added", lng, ns, resources);
|
|
369
|
+
}
|
|
370
|
+
addResourceBundle(lng, ns, resources, deep, overwrite, options = {
|
|
371
|
+
silent: false,
|
|
372
|
+
skipCopy: false
|
|
373
|
+
}) {
|
|
374
|
+
let path = [lng, ns];
|
|
375
|
+
if (lng.indexOf(".") > -1) {
|
|
376
|
+
path = lng.split(".");
|
|
377
|
+
deep = resources;
|
|
378
|
+
resources = ns;
|
|
379
|
+
ns = path[1];
|
|
380
|
+
}
|
|
381
|
+
this.addNamespaces(ns);
|
|
382
|
+
let pack = getPath(this.data, path) || {};
|
|
383
|
+
if (!options.skipCopy) resources = JSON.parse(JSON.stringify(resources));
|
|
384
|
+
if (deep) {
|
|
385
|
+
deepExtend(pack, resources, overwrite);
|
|
386
|
+
} else {
|
|
387
|
+
pack = {
|
|
388
|
+
...pack,
|
|
389
|
+
...resources
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
setPath(this.data, path, pack);
|
|
393
|
+
if (!options.silent) this.emit("added", lng, ns, resources);
|
|
394
|
+
}
|
|
395
|
+
removeResourceBundle(lng, ns) {
|
|
396
|
+
if (this.hasResourceBundle(lng, ns)) {
|
|
397
|
+
delete this.data[lng][ns];
|
|
398
|
+
}
|
|
399
|
+
this.removeNamespaces(ns);
|
|
400
|
+
this.emit("removed", lng, ns);
|
|
401
|
+
}
|
|
402
|
+
hasResourceBundle(lng, ns) {
|
|
403
|
+
return this.getResource(lng, ns) !== void 0;
|
|
404
|
+
}
|
|
405
|
+
getResourceBundle(lng, ns) {
|
|
406
|
+
if (!ns) ns = this.options.defaultNS;
|
|
407
|
+
return this.getResource(lng, ns);
|
|
408
|
+
}
|
|
409
|
+
getDataByLanguage(lng) {
|
|
410
|
+
return this.data[lng];
|
|
411
|
+
}
|
|
412
|
+
hasLanguageSomeTranslations(lng) {
|
|
413
|
+
const data = this.getDataByLanguage(lng);
|
|
414
|
+
const n = data && Object.keys(data) || [];
|
|
415
|
+
return !!n.find((v) => data[v] && Object.keys(data[v]).length > 0);
|
|
416
|
+
}
|
|
417
|
+
toJSON() {
|
|
418
|
+
return this.data;
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
var postProcessor = {
|
|
422
|
+
processors: {},
|
|
423
|
+
addPostProcessor(module) {
|
|
424
|
+
this.processors[module.name] = module;
|
|
425
|
+
},
|
|
426
|
+
handle(processors, value, key, options, translator) {
|
|
427
|
+
processors.forEach((processor) => {
|
|
428
|
+
value = this.processors[processor]?.process(value, key, options, translator) ?? value;
|
|
429
|
+
});
|
|
430
|
+
return value;
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
var PATH_KEY = /* @__PURE__ */ Symbol("i18next/PATH_KEY");
|
|
434
|
+
function createProxy() {
|
|
435
|
+
const state = [];
|
|
436
|
+
const handler = /* @__PURE__ */ Object.create(null);
|
|
437
|
+
let proxy;
|
|
438
|
+
handler.get = (target, key) => {
|
|
439
|
+
proxy?.revoke?.();
|
|
440
|
+
if (key === PATH_KEY) return state;
|
|
441
|
+
state.push(key);
|
|
442
|
+
proxy = Proxy.revocable(target, handler);
|
|
443
|
+
return proxy.proxy;
|
|
444
|
+
};
|
|
445
|
+
return Proxy.revocable(/* @__PURE__ */ Object.create(null), handler).proxy;
|
|
446
|
+
}
|
|
447
|
+
function keysFromSelector(selector, opts) {
|
|
448
|
+
const {
|
|
449
|
+
[PATH_KEY]: path
|
|
450
|
+
} = selector(createProxy());
|
|
451
|
+
return path.join(opts?.keySeparator ?? ".");
|
|
452
|
+
}
|
|
453
|
+
var checkedLoadedFor = {};
|
|
454
|
+
var shouldHandleAsObject = (res) => !isString(res) && typeof res !== "boolean" && typeof res !== "number";
|
|
455
|
+
var Translator = class _Translator extends EventEmitter {
|
|
456
|
+
constructor(services, options = {}) {
|
|
457
|
+
super();
|
|
458
|
+
copy(["resourceStore", "languageUtils", "pluralResolver", "interpolator", "backendConnector", "i18nFormat", "utils"], services, this);
|
|
459
|
+
this.options = options;
|
|
460
|
+
if (this.options.keySeparator === void 0) {
|
|
461
|
+
this.options.keySeparator = ".";
|
|
462
|
+
}
|
|
463
|
+
this.logger = baseLogger.create("translator");
|
|
464
|
+
}
|
|
465
|
+
changeLanguage(lng) {
|
|
466
|
+
if (lng) this.language = lng;
|
|
467
|
+
}
|
|
468
|
+
exists(key, o = {
|
|
469
|
+
interpolation: {}
|
|
470
|
+
}) {
|
|
471
|
+
const opt = {
|
|
472
|
+
...o
|
|
473
|
+
};
|
|
474
|
+
if (key == null) return false;
|
|
475
|
+
const resolved = this.resolve(key, opt);
|
|
476
|
+
if (resolved?.res === void 0) return false;
|
|
477
|
+
const isObject = shouldHandleAsObject(resolved.res);
|
|
478
|
+
if (opt.returnObjects === false && isObject) {
|
|
479
|
+
return false;
|
|
480
|
+
}
|
|
481
|
+
return true;
|
|
482
|
+
}
|
|
483
|
+
extractFromKey(key, opt) {
|
|
484
|
+
let nsSeparator = opt.nsSeparator !== void 0 ? opt.nsSeparator : this.options.nsSeparator;
|
|
485
|
+
if (nsSeparator === void 0) nsSeparator = ":";
|
|
486
|
+
const keySeparator = opt.keySeparator !== void 0 ? opt.keySeparator : this.options.keySeparator;
|
|
487
|
+
let namespaces = opt.ns || this.options.defaultNS || [];
|
|
488
|
+
const wouldCheckForNsInKey = nsSeparator && key.indexOf(nsSeparator) > -1;
|
|
489
|
+
const seemsNaturalLanguage = !this.options.userDefinedKeySeparator && !opt.keySeparator && !this.options.userDefinedNsSeparator && !opt.nsSeparator && !looksLikeObjectPath(key, nsSeparator, keySeparator);
|
|
490
|
+
if (wouldCheckForNsInKey && !seemsNaturalLanguage) {
|
|
491
|
+
const m = key.match(this.interpolator.nestingRegexp);
|
|
492
|
+
if (m && m.length > 0) {
|
|
493
|
+
return {
|
|
494
|
+
key,
|
|
495
|
+
namespaces: isString(namespaces) ? [namespaces] : namespaces
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
const parts = key.split(nsSeparator);
|
|
499
|
+
if (nsSeparator !== keySeparator || nsSeparator === keySeparator && this.options.ns.indexOf(parts[0]) > -1) namespaces = parts.shift();
|
|
500
|
+
key = parts.join(keySeparator);
|
|
501
|
+
}
|
|
502
|
+
return {
|
|
503
|
+
key,
|
|
504
|
+
namespaces: isString(namespaces) ? [namespaces] : namespaces
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
translate(keys, o, lastKey) {
|
|
508
|
+
let opt = typeof o === "object" ? {
|
|
509
|
+
...o
|
|
510
|
+
} : o;
|
|
511
|
+
if (typeof opt !== "object" && this.options.overloadTranslationOptionHandler) {
|
|
512
|
+
opt = this.options.overloadTranslationOptionHandler(arguments);
|
|
513
|
+
}
|
|
514
|
+
if (typeof opt === "object") opt = {
|
|
515
|
+
...opt
|
|
516
|
+
};
|
|
517
|
+
if (!opt) opt = {};
|
|
518
|
+
if (keys == null) return "";
|
|
519
|
+
if (typeof keys === "function") keys = keysFromSelector(keys, {
|
|
520
|
+
...this.options,
|
|
521
|
+
...opt
|
|
522
|
+
});
|
|
523
|
+
if (!Array.isArray(keys)) keys = [String(keys)];
|
|
524
|
+
const returnDetails = opt.returnDetails !== void 0 ? opt.returnDetails : this.options.returnDetails;
|
|
525
|
+
const keySeparator = opt.keySeparator !== void 0 ? opt.keySeparator : this.options.keySeparator;
|
|
526
|
+
const {
|
|
527
|
+
key,
|
|
528
|
+
namespaces
|
|
529
|
+
} = this.extractFromKey(keys[keys.length - 1], opt);
|
|
530
|
+
const namespace = namespaces[namespaces.length - 1];
|
|
531
|
+
let nsSeparator = opt.nsSeparator !== void 0 ? opt.nsSeparator : this.options.nsSeparator;
|
|
532
|
+
if (nsSeparator === void 0) nsSeparator = ":";
|
|
533
|
+
const lng = opt.lng || this.language;
|
|
534
|
+
const appendNamespaceToCIMode = opt.appendNamespaceToCIMode || this.options.appendNamespaceToCIMode;
|
|
535
|
+
if (lng?.toLowerCase() === "cimode") {
|
|
536
|
+
if (appendNamespaceToCIMode) {
|
|
537
|
+
if (returnDetails) {
|
|
538
|
+
return {
|
|
539
|
+
res: `${namespace}${nsSeparator}${key}`,
|
|
540
|
+
usedKey: key,
|
|
541
|
+
exactUsedKey: key,
|
|
542
|
+
usedLng: lng,
|
|
543
|
+
usedNS: namespace,
|
|
544
|
+
usedParams: this.getUsedParamsDetails(opt)
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
return `${namespace}${nsSeparator}${key}`;
|
|
548
|
+
}
|
|
549
|
+
if (returnDetails) {
|
|
550
|
+
return {
|
|
551
|
+
res: key,
|
|
552
|
+
usedKey: key,
|
|
553
|
+
exactUsedKey: key,
|
|
554
|
+
usedLng: lng,
|
|
555
|
+
usedNS: namespace,
|
|
556
|
+
usedParams: this.getUsedParamsDetails(opt)
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
return key;
|
|
560
|
+
}
|
|
561
|
+
const resolved = this.resolve(keys, opt);
|
|
562
|
+
let res = resolved?.res;
|
|
563
|
+
const resUsedKey = resolved?.usedKey || key;
|
|
564
|
+
const resExactUsedKey = resolved?.exactUsedKey || key;
|
|
565
|
+
const noObject = ["[object Number]", "[object Function]", "[object RegExp]"];
|
|
566
|
+
const joinArrays = opt.joinArrays !== void 0 ? opt.joinArrays : this.options.joinArrays;
|
|
567
|
+
const handleAsObjectInI18nFormat = !this.i18nFormat || this.i18nFormat.handleAsObject;
|
|
568
|
+
const needsPluralHandling = opt.count !== void 0 && !isString(opt.count);
|
|
569
|
+
const hasDefaultValue = _Translator.hasDefaultValue(opt);
|
|
570
|
+
const defaultValueSuffix = needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, opt) : "";
|
|
571
|
+
const defaultValueSuffixOrdinalFallback = opt.ordinal && needsPluralHandling ? this.pluralResolver.getSuffix(lng, opt.count, {
|
|
572
|
+
ordinal: false
|
|
573
|
+
}) : "";
|
|
574
|
+
const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
|
|
575
|
+
const defaultValue = needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] || opt[`defaultValue${defaultValueSuffix}`] || opt[`defaultValue${defaultValueSuffixOrdinalFallback}`] || opt.defaultValue;
|
|
576
|
+
let resForObjHndl = res;
|
|
577
|
+
if (handleAsObjectInI18nFormat && !res && hasDefaultValue) {
|
|
578
|
+
resForObjHndl = defaultValue;
|
|
579
|
+
}
|
|
580
|
+
const handleAsObject = shouldHandleAsObject(resForObjHndl);
|
|
581
|
+
const resType = Object.prototype.toString.apply(resForObjHndl);
|
|
582
|
+
if (handleAsObjectInI18nFormat && resForObjHndl && handleAsObject && noObject.indexOf(resType) < 0 && !(isString(joinArrays) && Array.isArray(resForObjHndl))) {
|
|
583
|
+
if (!opt.returnObjects && !this.options.returnObjects) {
|
|
584
|
+
if (!this.options.returnedObjectHandler) {
|
|
585
|
+
this.logger.warn("accessing an object - but returnObjects options is not enabled!");
|
|
586
|
+
}
|
|
587
|
+
const r = this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, resForObjHndl, {
|
|
588
|
+
...opt,
|
|
589
|
+
ns: namespaces
|
|
590
|
+
}) : `key '${key} (${this.language})' returned an object instead of string.`;
|
|
591
|
+
if (returnDetails) {
|
|
592
|
+
resolved.res = r;
|
|
593
|
+
resolved.usedParams = this.getUsedParamsDetails(opt);
|
|
594
|
+
return resolved;
|
|
595
|
+
}
|
|
596
|
+
return r;
|
|
597
|
+
}
|
|
598
|
+
if (keySeparator) {
|
|
599
|
+
const resTypeIsArray = Array.isArray(resForObjHndl);
|
|
600
|
+
const copy2 = resTypeIsArray ? [] : {};
|
|
601
|
+
const newKeyToUse = resTypeIsArray ? resExactUsedKey : resUsedKey;
|
|
602
|
+
for (const m in resForObjHndl) {
|
|
603
|
+
if (Object.prototype.hasOwnProperty.call(resForObjHndl, m)) {
|
|
604
|
+
const deepKey = `${newKeyToUse}${keySeparator}${m}`;
|
|
605
|
+
if (hasDefaultValue && !res) {
|
|
606
|
+
copy2[m] = this.translate(deepKey, {
|
|
607
|
+
...opt,
|
|
608
|
+
defaultValue: shouldHandleAsObject(defaultValue) ? defaultValue[m] : void 0,
|
|
609
|
+
...{
|
|
610
|
+
joinArrays: false,
|
|
611
|
+
ns: namespaces
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
} else {
|
|
615
|
+
copy2[m] = this.translate(deepKey, {
|
|
616
|
+
...opt,
|
|
617
|
+
...{
|
|
618
|
+
joinArrays: false,
|
|
619
|
+
ns: namespaces
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
if (copy2[m] === deepKey) copy2[m] = resForObjHndl[m];
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
res = copy2;
|
|
627
|
+
}
|
|
628
|
+
} else if (handleAsObjectInI18nFormat && isString(joinArrays) && Array.isArray(res)) {
|
|
629
|
+
res = res.join(joinArrays);
|
|
630
|
+
if (res) res = this.extendTranslation(res, keys, opt, lastKey);
|
|
631
|
+
} else {
|
|
632
|
+
let usedDefault = false;
|
|
633
|
+
let usedKey = false;
|
|
634
|
+
if (!this.isValidLookup(res) && hasDefaultValue) {
|
|
635
|
+
usedDefault = true;
|
|
636
|
+
res = defaultValue;
|
|
637
|
+
}
|
|
638
|
+
if (!this.isValidLookup(res)) {
|
|
639
|
+
usedKey = true;
|
|
640
|
+
res = key;
|
|
641
|
+
}
|
|
642
|
+
const missingKeyNoValueFallbackToKey = opt.missingKeyNoValueFallbackToKey || this.options.missingKeyNoValueFallbackToKey;
|
|
643
|
+
const resForMissing = missingKeyNoValueFallbackToKey && usedKey ? void 0 : res;
|
|
644
|
+
const updateMissing = hasDefaultValue && defaultValue !== res && this.options.updateMissing;
|
|
645
|
+
if (usedKey || usedDefault || updateMissing) {
|
|
646
|
+
this.logger.log(updateMissing ? "updateKey" : "missingKey", lng, namespace, key, updateMissing ? defaultValue : res);
|
|
647
|
+
if (keySeparator) {
|
|
648
|
+
const fk = this.resolve(key, {
|
|
649
|
+
...opt,
|
|
650
|
+
keySeparator: false
|
|
651
|
+
});
|
|
652
|
+
if (fk && fk.res) this.logger.warn("Seems the loaded translations were in flat JSON format instead of nested. Either set keySeparator: false on init or make sure your translations are published in nested format.");
|
|
653
|
+
}
|
|
654
|
+
let lngs = [];
|
|
655
|
+
const fallbackLngs = this.languageUtils.getFallbackCodes(this.options.fallbackLng, opt.lng || this.language);
|
|
656
|
+
if (this.options.saveMissingTo === "fallback" && fallbackLngs && fallbackLngs[0]) {
|
|
657
|
+
for (let i = 0; i < fallbackLngs.length; i++) {
|
|
658
|
+
lngs.push(fallbackLngs[i]);
|
|
659
|
+
}
|
|
660
|
+
} else if (this.options.saveMissingTo === "all") {
|
|
661
|
+
lngs = this.languageUtils.toResolveHierarchy(opt.lng || this.language);
|
|
662
|
+
} else {
|
|
663
|
+
lngs.push(opt.lng || this.language);
|
|
664
|
+
}
|
|
665
|
+
const send = (l, k, specificDefaultValue) => {
|
|
666
|
+
const defaultForMissing = hasDefaultValue && specificDefaultValue !== res ? specificDefaultValue : resForMissing;
|
|
667
|
+
if (this.options.missingKeyHandler) {
|
|
668
|
+
this.options.missingKeyHandler(l, namespace, k, defaultForMissing, updateMissing, opt);
|
|
669
|
+
} else if (this.backendConnector?.saveMissing) {
|
|
670
|
+
this.backendConnector.saveMissing(l, namespace, k, defaultForMissing, updateMissing, opt);
|
|
671
|
+
}
|
|
672
|
+
this.emit("missingKey", l, namespace, k, res);
|
|
673
|
+
};
|
|
674
|
+
if (this.options.saveMissing) {
|
|
675
|
+
if (this.options.saveMissingPlurals && needsPluralHandling) {
|
|
676
|
+
lngs.forEach((language) => {
|
|
677
|
+
const suffixes = this.pluralResolver.getSuffixes(language, opt);
|
|
678
|
+
if (needsZeroSuffixLookup && opt[`defaultValue${this.options.pluralSeparator}zero`] && suffixes.indexOf(`${this.options.pluralSeparator}zero`) < 0) {
|
|
679
|
+
suffixes.push(`${this.options.pluralSeparator}zero`);
|
|
680
|
+
}
|
|
681
|
+
suffixes.forEach((suffix) => {
|
|
682
|
+
send([language], key + suffix, opt[`defaultValue${suffix}`] || defaultValue);
|
|
683
|
+
});
|
|
684
|
+
});
|
|
685
|
+
} else {
|
|
686
|
+
send(lngs, key, defaultValue);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
res = this.extendTranslation(res, keys, opt, resolved, lastKey);
|
|
691
|
+
if (usedKey && res === key && this.options.appendNamespaceToMissingKey) {
|
|
692
|
+
res = `${namespace}${nsSeparator}${key}`;
|
|
693
|
+
}
|
|
694
|
+
if ((usedKey || usedDefault) && this.options.parseMissingKeyHandler) {
|
|
695
|
+
res = this.options.parseMissingKeyHandler(this.options.appendNamespaceToMissingKey ? `${namespace}${nsSeparator}${key}` : key, usedDefault ? res : void 0, opt);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
if (returnDetails) {
|
|
699
|
+
resolved.res = res;
|
|
700
|
+
resolved.usedParams = this.getUsedParamsDetails(opt);
|
|
701
|
+
return resolved;
|
|
702
|
+
}
|
|
703
|
+
return res;
|
|
704
|
+
}
|
|
705
|
+
extendTranslation(res, key, opt, resolved, lastKey) {
|
|
706
|
+
if (this.i18nFormat?.parse) {
|
|
707
|
+
res = this.i18nFormat.parse(res, {
|
|
708
|
+
...this.options.interpolation.defaultVariables,
|
|
709
|
+
...opt
|
|
710
|
+
}, opt.lng || this.language || resolved.usedLng, resolved.usedNS, resolved.usedKey, {
|
|
711
|
+
resolved
|
|
712
|
+
});
|
|
713
|
+
} else if (!opt.skipInterpolation) {
|
|
714
|
+
if (opt.interpolation) this.interpolator.init({
|
|
715
|
+
...opt,
|
|
716
|
+
...{
|
|
717
|
+
interpolation: {
|
|
718
|
+
...this.options.interpolation,
|
|
719
|
+
...opt.interpolation
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
const skipOnVariables = isString(res) && (opt?.interpolation?.skipOnVariables !== void 0 ? opt.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables);
|
|
724
|
+
let nestBef;
|
|
725
|
+
if (skipOnVariables) {
|
|
726
|
+
const nb = res.match(this.interpolator.nestingRegexp);
|
|
727
|
+
nestBef = nb && nb.length;
|
|
728
|
+
}
|
|
729
|
+
let data = opt.replace && !isString(opt.replace) ? opt.replace : opt;
|
|
730
|
+
if (this.options.interpolation.defaultVariables) data = {
|
|
731
|
+
...this.options.interpolation.defaultVariables,
|
|
732
|
+
...data
|
|
733
|
+
};
|
|
734
|
+
res = this.interpolator.interpolate(res, data, opt.lng || this.language || resolved.usedLng, opt);
|
|
735
|
+
if (skipOnVariables) {
|
|
736
|
+
const na = res.match(this.interpolator.nestingRegexp);
|
|
737
|
+
const nestAft = na && na.length;
|
|
738
|
+
if (nestBef < nestAft) opt.nest = false;
|
|
739
|
+
}
|
|
740
|
+
if (!opt.lng && resolved && resolved.res) opt.lng = this.language || resolved.usedLng;
|
|
741
|
+
if (opt.nest !== false) res = this.interpolator.nest(res, (...args) => {
|
|
742
|
+
if (lastKey?.[0] === args[0] && !opt.context) {
|
|
743
|
+
this.logger.warn(`It seems you are nesting recursively key: ${args[0]} in key: ${key[0]}`);
|
|
744
|
+
return null;
|
|
745
|
+
}
|
|
746
|
+
return this.translate(...args, key);
|
|
747
|
+
}, opt);
|
|
748
|
+
if (opt.interpolation) this.interpolator.reset();
|
|
749
|
+
}
|
|
750
|
+
const postProcess = opt.postProcess || this.options.postProcess;
|
|
751
|
+
const postProcessorNames = isString(postProcess) ? [postProcess] : postProcess;
|
|
752
|
+
if (res != null && postProcessorNames?.length && opt.applyPostProcessor !== false) {
|
|
753
|
+
res = postProcessor.handle(postProcessorNames, res, key, this.options && this.options.postProcessPassResolved ? {
|
|
754
|
+
i18nResolved: {
|
|
755
|
+
...resolved,
|
|
756
|
+
usedParams: this.getUsedParamsDetails(opt)
|
|
757
|
+
},
|
|
758
|
+
...opt
|
|
759
|
+
} : opt, this);
|
|
760
|
+
}
|
|
761
|
+
return res;
|
|
762
|
+
}
|
|
763
|
+
resolve(keys, opt = {}) {
|
|
764
|
+
let found;
|
|
765
|
+
let usedKey;
|
|
766
|
+
let exactUsedKey;
|
|
767
|
+
let usedLng;
|
|
768
|
+
let usedNS;
|
|
769
|
+
if (isString(keys)) keys = [keys];
|
|
770
|
+
keys.forEach((k) => {
|
|
771
|
+
if (this.isValidLookup(found)) return;
|
|
772
|
+
const extracted = this.extractFromKey(k, opt);
|
|
773
|
+
const key = extracted.key;
|
|
774
|
+
usedKey = key;
|
|
775
|
+
let namespaces = extracted.namespaces;
|
|
776
|
+
if (this.options.fallbackNS) namespaces = namespaces.concat(this.options.fallbackNS);
|
|
777
|
+
const needsPluralHandling = opt.count !== void 0 && !isString(opt.count);
|
|
778
|
+
const needsZeroSuffixLookup = needsPluralHandling && !opt.ordinal && opt.count === 0;
|
|
779
|
+
const needsContextHandling = opt.context !== void 0 && (isString(opt.context) || typeof opt.context === "number") && opt.context !== "";
|
|
780
|
+
const codes = opt.lngs ? opt.lngs : this.languageUtils.toResolveHierarchy(opt.lng || this.language, opt.fallbackLng);
|
|
781
|
+
namespaces.forEach((ns) => {
|
|
782
|
+
if (this.isValidLookup(found)) return;
|
|
783
|
+
usedNS = ns;
|
|
784
|
+
if (!checkedLoadedFor[`${codes[0]}-${ns}`] && this.utils?.hasLoadedNamespace && !this.utils?.hasLoadedNamespace(usedNS)) {
|
|
785
|
+
checkedLoadedFor[`${codes[0]}-${ns}`] = true;
|
|
786
|
+
this.logger.warn(`key "${usedKey}" for languages "${codes.join(", ")}" won't get resolved as namespace "${usedNS}" was not yet loaded`, "This means something IS WRONG in your setup. You access the t function before i18next.init / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!");
|
|
787
|
+
}
|
|
788
|
+
codes.forEach((code) => {
|
|
789
|
+
if (this.isValidLookup(found)) return;
|
|
790
|
+
usedLng = code;
|
|
791
|
+
const finalKeys = [key];
|
|
792
|
+
if (this.i18nFormat?.addLookupKeys) {
|
|
793
|
+
this.i18nFormat.addLookupKeys(finalKeys, key, code, ns, opt);
|
|
794
|
+
} else {
|
|
795
|
+
let pluralSuffix;
|
|
796
|
+
if (needsPluralHandling) pluralSuffix = this.pluralResolver.getSuffix(code, opt.count, opt);
|
|
797
|
+
const zeroSuffix = `${this.options.pluralSeparator}zero`;
|
|
798
|
+
const ordinalPrefix = `${this.options.pluralSeparator}ordinal${this.options.pluralSeparator}`;
|
|
799
|
+
if (needsPluralHandling) {
|
|
800
|
+
if (opt.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
|
|
801
|
+
finalKeys.push(key + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
|
|
802
|
+
}
|
|
803
|
+
finalKeys.push(key + pluralSuffix);
|
|
804
|
+
if (needsZeroSuffixLookup) {
|
|
805
|
+
finalKeys.push(key + zeroSuffix);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
if (needsContextHandling) {
|
|
809
|
+
const contextKey = `${key}${this.options.contextSeparator || "_"}${opt.context}`;
|
|
810
|
+
finalKeys.push(contextKey);
|
|
811
|
+
if (needsPluralHandling) {
|
|
812
|
+
if (opt.ordinal && pluralSuffix.indexOf(ordinalPrefix) === 0) {
|
|
813
|
+
finalKeys.push(contextKey + pluralSuffix.replace(ordinalPrefix, this.options.pluralSeparator));
|
|
814
|
+
}
|
|
815
|
+
finalKeys.push(contextKey + pluralSuffix);
|
|
816
|
+
if (needsZeroSuffixLookup) {
|
|
817
|
+
finalKeys.push(contextKey + zeroSuffix);
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
let possibleKey;
|
|
823
|
+
while (possibleKey = finalKeys.pop()) {
|
|
824
|
+
if (!this.isValidLookup(found)) {
|
|
825
|
+
exactUsedKey = possibleKey;
|
|
826
|
+
found = this.getResource(code, ns, possibleKey, opt);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
});
|
|
831
|
+
});
|
|
832
|
+
return {
|
|
833
|
+
res: found,
|
|
834
|
+
usedKey,
|
|
835
|
+
exactUsedKey,
|
|
836
|
+
usedLng,
|
|
837
|
+
usedNS
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
isValidLookup(res) {
|
|
841
|
+
return res !== void 0 && !(!this.options.returnNull && res === null) && !(!this.options.returnEmptyString && res === "");
|
|
842
|
+
}
|
|
843
|
+
getResource(code, ns, key, options = {}) {
|
|
844
|
+
if (this.i18nFormat?.getResource) return this.i18nFormat.getResource(code, ns, key, options);
|
|
845
|
+
return this.resourceStore.getResource(code, ns, key, options);
|
|
846
|
+
}
|
|
847
|
+
getUsedParamsDetails(options = {}) {
|
|
848
|
+
const optionsKeys = ["defaultValue", "ordinal", "context", "replace", "lng", "lngs", "fallbackLng", "ns", "keySeparator", "nsSeparator", "returnObjects", "returnDetails", "joinArrays", "postProcess", "interpolation"];
|
|
849
|
+
const useOptionsReplaceForData = options.replace && !isString(options.replace);
|
|
850
|
+
let data = useOptionsReplaceForData ? options.replace : options;
|
|
851
|
+
if (useOptionsReplaceForData && typeof options.count !== "undefined") {
|
|
852
|
+
data.count = options.count;
|
|
853
|
+
}
|
|
854
|
+
if (this.options.interpolation.defaultVariables) {
|
|
855
|
+
data = {
|
|
856
|
+
...this.options.interpolation.defaultVariables,
|
|
857
|
+
...data
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
if (!useOptionsReplaceForData) {
|
|
861
|
+
data = {
|
|
862
|
+
...data
|
|
863
|
+
};
|
|
864
|
+
for (const key of optionsKeys) {
|
|
865
|
+
delete data[key];
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
return data;
|
|
869
|
+
}
|
|
870
|
+
static hasDefaultValue(options) {
|
|
871
|
+
const prefix = "defaultValue";
|
|
872
|
+
for (const option in options) {
|
|
873
|
+
if (Object.prototype.hasOwnProperty.call(options, option) && prefix === option.substring(0, prefix.length) && void 0 !== options[option]) {
|
|
874
|
+
return true;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
return false;
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
var LanguageUtil = class {
|
|
881
|
+
constructor(options) {
|
|
882
|
+
this.options = options;
|
|
883
|
+
this.supportedLngs = this.options.supportedLngs || false;
|
|
884
|
+
this.logger = baseLogger.create("languageUtils");
|
|
885
|
+
}
|
|
886
|
+
getScriptPartFromCode(code) {
|
|
887
|
+
code = getCleanedCode(code);
|
|
888
|
+
if (!code || code.indexOf("-") < 0) return null;
|
|
889
|
+
const p = code.split("-");
|
|
890
|
+
if (p.length === 2) return null;
|
|
891
|
+
p.pop();
|
|
892
|
+
if (p[p.length - 1].toLowerCase() === "x") return null;
|
|
893
|
+
return this.formatLanguageCode(p.join("-"));
|
|
894
|
+
}
|
|
895
|
+
getLanguagePartFromCode(code) {
|
|
896
|
+
code = getCleanedCode(code);
|
|
897
|
+
if (!code || code.indexOf("-") < 0) return code;
|
|
898
|
+
const p = code.split("-");
|
|
899
|
+
return this.formatLanguageCode(p[0]);
|
|
900
|
+
}
|
|
901
|
+
formatLanguageCode(code) {
|
|
902
|
+
if (isString(code) && code.indexOf("-") > -1) {
|
|
903
|
+
let formattedCode;
|
|
904
|
+
try {
|
|
905
|
+
formattedCode = Intl.getCanonicalLocales(code)[0];
|
|
906
|
+
} catch (e) {
|
|
907
|
+
}
|
|
908
|
+
if (formattedCode && this.options.lowerCaseLng) {
|
|
909
|
+
formattedCode = formattedCode.toLowerCase();
|
|
910
|
+
}
|
|
911
|
+
if (formattedCode) return formattedCode;
|
|
912
|
+
if (this.options.lowerCaseLng) {
|
|
913
|
+
return code.toLowerCase();
|
|
914
|
+
}
|
|
915
|
+
return code;
|
|
916
|
+
}
|
|
917
|
+
return this.options.cleanCode || this.options.lowerCaseLng ? code.toLowerCase() : code;
|
|
918
|
+
}
|
|
919
|
+
isSupportedCode(code) {
|
|
920
|
+
if (this.options.load === "languageOnly" || this.options.nonExplicitSupportedLngs) {
|
|
921
|
+
code = this.getLanguagePartFromCode(code);
|
|
922
|
+
}
|
|
923
|
+
return !this.supportedLngs || !this.supportedLngs.length || this.supportedLngs.indexOf(code) > -1;
|
|
924
|
+
}
|
|
925
|
+
getBestMatchFromCodes(codes) {
|
|
926
|
+
if (!codes) return null;
|
|
927
|
+
let found;
|
|
928
|
+
codes.forEach((code) => {
|
|
929
|
+
if (found) return;
|
|
930
|
+
const cleanedLng = this.formatLanguageCode(code);
|
|
931
|
+
if (!this.options.supportedLngs || this.isSupportedCode(cleanedLng)) found = cleanedLng;
|
|
932
|
+
});
|
|
933
|
+
if (!found && this.options.supportedLngs) {
|
|
934
|
+
codes.forEach((code) => {
|
|
935
|
+
if (found) return;
|
|
936
|
+
const lngScOnly = this.getScriptPartFromCode(code);
|
|
937
|
+
if (this.isSupportedCode(lngScOnly)) return found = lngScOnly;
|
|
938
|
+
const lngOnly = this.getLanguagePartFromCode(code);
|
|
939
|
+
if (this.isSupportedCode(lngOnly)) return found = lngOnly;
|
|
940
|
+
found = this.options.supportedLngs.find((supportedLng) => {
|
|
941
|
+
if (supportedLng === lngOnly) return supportedLng;
|
|
942
|
+
if (supportedLng.indexOf("-") < 0 && lngOnly.indexOf("-") < 0) return;
|
|
943
|
+
if (supportedLng.indexOf("-") > 0 && lngOnly.indexOf("-") < 0 && supportedLng.substring(0, supportedLng.indexOf("-")) === lngOnly) return supportedLng;
|
|
944
|
+
if (supportedLng.indexOf(lngOnly) === 0 && lngOnly.length > 1) return supportedLng;
|
|
945
|
+
});
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
if (!found) found = this.getFallbackCodes(this.options.fallbackLng)[0];
|
|
949
|
+
return found;
|
|
950
|
+
}
|
|
951
|
+
getFallbackCodes(fallbacks, code) {
|
|
952
|
+
if (!fallbacks) return [];
|
|
953
|
+
if (typeof fallbacks === "function") fallbacks = fallbacks(code);
|
|
954
|
+
if (isString(fallbacks)) fallbacks = [fallbacks];
|
|
955
|
+
if (Array.isArray(fallbacks)) return fallbacks;
|
|
956
|
+
if (!code) return fallbacks.default || [];
|
|
957
|
+
let found = fallbacks[code];
|
|
958
|
+
if (!found) found = fallbacks[this.getScriptPartFromCode(code)];
|
|
959
|
+
if (!found) found = fallbacks[this.formatLanguageCode(code)];
|
|
960
|
+
if (!found) found = fallbacks[this.getLanguagePartFromCode(code)];
|
|
961
|
+
if (!found) found = fallbacks.default;
|
|
962
|
+
return found || [];
|
|
963
|
+
}
|
|
964
|
+
toResolveHierarchy(code, fallbackCode) {
|
|
965
|
+
const fallbackCodes = this.getFallbackCodes((fallbackCode === false ? [] : fallbackCode) || this.options.fallbackLng || [], code);
|
|
966
|
+
const codes = [];
|
|
967
|
+
const addCode = (c) => {
|
|
968
|
+
if (!c) return;
|
|
969
|
+
if (this.isSupportedCode(c)) {
|
|
970
|
+
codes.push(c);
|
|
971
|
+
} else {
|
|
972
|
+
this.logger.warn(`rejecting language code not found in supportedLngs: ${c}`);
|
|
973
|
+
}
|
|
974
|
+
};
|
|
975
|
+
if (isString(code) && (code.indexOf("-") > -1 || code.indexOf("_") > -1)) {
|
|
976
|
+
if (this.options.load !== "languageOnly") addCode(this.formatLanguageCode(code));
|
|
977
|
+
if (this.options.load !== "languageOnly" && this.options.load !== "currentOnly") addCode(this.getScriptPartFromCode(code));
|
|
978
|
+
if (this.options.load !== "currentOnly") addCode(this.getLanguagePartFromCode(code));
|
|
979
|
+
} else if (isString(code)) {
|
|
980
|
+
addCode(this.formatLanguageCode(code));
|
|
981
|
+
}
|
|
982
|
+
fallbackCodes.forEach((fc) => {
|
|
983
|
+
if (codes.indexOf(fc) < 0) addCode(this.formatLanguageCode(fc));
|
|
984
|
+
});
|
|
985
|
+
return codes;
|
|
986
|
+
}
|
|
987
|
+
};
|
|
988
|
+
var suffixesOrder = {
|
|
989
|
+
zero: 0,
|
|
990
|
+
one: 1,
|
|
991
|
+
two: 2,
|
|
992
|
+
few: 3,
|
|
993
|
+
many: 4,
|
|
994
|
+
other: 5
|
|
995
|
+
};
|
|
996
|
+
var dummyRule = {
|
|
997
|
+
select: (count) => count === 1 ? "one" : "other",
|
|
998
|
+
resolvedOptions: () => ({
|
|
999
|
+
pluralCategories: ["one", "other"]
|
|
1000
|
+
})
|
|
1001
|
+
};
|
|
1002
|
+
var PluralResolver = class {
|
|
1003
|
+
constructor(languageUtils, options = {}) {
|
|
1004
|
+
this.languageUtils = languageUtils;
|
|
1005
|
+
this.options = options;
|
|
1006
|
+
this.logger = baseLogger.create("pluralResolver");
|
|
1007
|
+
this.pluralRulesCache = {};
|
|
1008
|
+
}
|
|
1009
|
+
addRule(lng, obj) {
|
|
1010
|
+
this.rules[lng] = obj;
|
|
1011
|
+
}
|
|
1012
|
+
clearCache() {
|
|
1013
|
+
this.pluralRulesCache = {};
|
|
1014
|
+
}
|
|
1015
|
+
getRule(code, options = {}) {
|
|
1016
|
+
const cleanedCode = getCleanedCode(code === "dev" ? "en" : code);
|
|
1017
|
+
const type = options.ordinal ? "ordinal" : "cardinal";
|
|
1018
|
+
const cacheKey = JSON.stringify({
|
|
1019
|
+
cleanedCode,
|
|
1020
|
+
type
|
|
1021
|
+
});
|
|
1022
|
+
if (cacheKey in this.pluralRulesCache) {
|
|
1023
|
+
return this.pluralRulesCache[cacheKey];
|
|
1024
|
+
}
|
|
1025
|
+
let rule;
|
|
1026
|
+
try {
|
|
1027
|
+
rule = new Intl.PluralRules(cleanedCode, {
|
|
1028
|
+
type
|
|
1029
|
+
});
|
|
1030
|
+
} catch (err) {
|
|
1031
|
+
if (!Intl) {
|
|
1032
|
+
this.logger.error("No Intl support, please use an Intl polyfill!");
|
|
1033
|
+
return dummyRule;
|
|
1034
|
+
}
|
|
1035
|
+
if (!code.match(/-|_/)) return dummyRule;
|
|
1036
|
+
const lngPart = this.languageUtils.getLanguagePartFromCode(code);
|
|
1037
|
+
rule = this.getRule(lngPart, options);
|
|
1038
|
+
}
|
|
1039
|
+
this.pluralRulesCache[cacheKey] = rule;
|
|
1040
|
+
return rule;
|
|
1041
|
+
}
|
|
1042
|
+
needsPlural(code, options = {}) {
|
|
1043
|
+
let rule = this.getRule(code, options);
|
|
1044
|
+
if (!rule) rule = this.getRule("dev", options);
|
|
1045
|
+
return rule?.resolvedOptions().pluralCategories.length > 1;
|
|
1046
|
+
}
|
|
1047
|
+
getPluralFormsOfKey(code, key, options = {}) {
|
|
1048
|
+
return this.getSuffixes(code, options).map((suffix) => `${key}${suffix}`);
|
|
1049
|
+
}
|
|
1050
|
+
getSuffixes(code, options = {}) {
|
|
1051
|
+
let rule = this.getRule(code, options);
|
|
1052
|
+
if (!rule) rule = this.getRule("dev", options);
|
|
1053
|
+
if (!rule) return [];
|
|
1054
|
+
return rule.resolvedOptions().pluralCategories.sort((pluralCategory1, pluralCategory2) => suffixesOrder[pluralCategory1] - suffixesOrder[pluralCategory2]).map((pluralCategory) => `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ""}${pluralCategory}`);
|
|
1055
|
+
}
|
|
1056
|
+
getSuffix(code, count, options = {}) {
|
|
1057
|
+
const rule = this.getRule(code, options);
|
|
1058
|
+
if (rule) {
|
|
1059
|
+
return `${this.options.prepend}${options.ordinal ? `ordinal${this.options.prepend}` : ""}${rule.select(count)}`;
|
|
1060
|
+
}
|
|
1061
|
+
this.logger.warn(`no plural rule found for: ${code}`);
|
|
1062
|
+
return this.getSuffix("dev", count, options);
|
|
1063
|
+
}
|
|
1064
|
+
};
|
|
1065
|
+
var deepFindWithDefaults = (data, defaultData, key, keySeparator = ".", ignoreJSONStructure = true) => {
|
|
1066
|
+
let path = getPathWithDefaults(data, defaultData, key);
|
|
1067
|
+
if (!path && ignoreJSONStructure && isString(key)) {
|
|
1068
|
+
path = deepFind(data, key, keySeparator);
|
|
1069
|
+
if (path === void 0) path = deepFind(defaultData, key, keySeparator);
|
|
1070
|
+
}
|
|
1071
|
+
return path;
|
|
1072
|
+
};
|
|
1073
|
+
var regexSafe = (val) => val.replace(/\$/g, "$$$$");
|
|
1074
|
+
var Interpolator = class {
|
|
1075
|
+
constructor(options = {}) {
|
|
1076
|
+
this.logger = baseLogger.create("interpolator");
|
|
1077
|
+
this.options = options;
|
|
1078
|
+
this.format = options?.interpolation?.format || ((value) => value);
|
|
1079
|
+
this.init(options);
|
|
1080
|
+
}
|
|
1081
|
+
init(options = {}) {
|
|
1082
|
+
if (!options.interpolation) options.interpolation = {
|
|
1083
|
+
escapeValue: true
|
|
1084
|
+
};
|
|
1085
|
+
const {
|
|
1086
|
+
escape: escape$1,
|
|
1087
|
+
escapeValue,
|
|
1088
|
+
useRawValueToEscape,
|
|
1089
|
+
prefix,
|
|
1090
|
+
prefixEscaped,
|
|
1091
|
+
suffix,
|
|
1092
|
+
suffixEscaped,
|
|
1093
|
+
formatSeparator,
|
|
1094
|
+
unescapeSuffix,
|
|
1095
|
+
unescapePrefix,
|
|
1096
|
+
nestingPrefix,
|
|
1097
|
+
nestingPrefixEscaped,
|
|
1098
|
+
nestingSuffix,
|
|
1099
|
+
nestingSuffixEscaped,
|
|
1100
|
+
nestingOptionsSeparator,
|
|
1101
|
+
maxReplaces,
|
|
1102
|
+
alwaysFormat
|
|
1103
|
+
} = options.interpolation;
|
|
1104
|
+
this.escape = escape$1 !== void 0 ? escape$1 : escape;
|
|
1105
|
+
this.escapeValue = escapeValue !== void 0 ? escapeValue : true;
|
|
1106
|
+
this.useRawValueToEscape = useRawValueToEscape !== void 0 ? useRawValueToEscape : false;
|
|
1107
|
+
this.prefix = prefix ? regexEscape(prefix) : prefixEscaped || "{{";
|
|
1108
|
+
this.suffix = suffix ? regexEscape(suffix) : suffixEscaped || "}}";
|
|
1109
|
+
this.formatSeparator = formatSeparator || ",";
|
|
1110
|
+
this.unescapePrefix = unescapeSuffix ? "" : unescapePrefix || "-";
|
|
1111
|
+
this.unescapeSuffix = this.unescapePrefix ? "" : unescapeSuffix || "";
|
|
1112
|
+
this.nestingPrefix = nestingPrefix ? regexEscape(nestingPrefix) : nestingPrefixEscaped || regexEscape("$t(");
|
|
1113
|
+
this.nestingSuffix = nestingSuffix ? regexEscape(nestingSuffix) : nestingSuffixEscaped || regexEscape(")");
|
|
1114
|
+
this.nestingOptionsSeparator = nestingOptionsSeparator || ",";
|
|
1115
|
+
this.maxReplaces = maxReplaces || 1e3;
|
|
1116
|
+
this.alwaysFormat = alwaysFormat !== void 0 ? alwaysFormat : false;
|
|
1117
|
+
this.resetRegExp();
|
|
1118
|
+
}
|
|
1119
|
+
reset() {
|
|
1120
|
+
if (this.options) this.init(this.options);
|
|
1121
|
+
}
|
|
1122
|
+
resetRegExp() {
|
|
1123
|
+
const getOrResetRegExp = (existingRegExp, pattern) => {
|
|
1124
|
+
if (existingRegExp?.source === pattern) {
|
|
1125
|
+
existingRegExp.lastIndex = 0;
|
|
1126
|
+
return existingRegExp;
|
|
1127
|
+
}
|
|
1128
|
+
return new RegExp(pattern, "g");
|
|
1129
|
+
};
|
|
1130
|
+
this.regexp = getOrResetRegExp(this.regexp, `${this.prefix}(.+?)${this.suffix}`);
|
|
1131
|
+
this.regexpUnescape = getOrResetRegExp(this.regexpUnescape, `${this.prefix}${this.unescapePrefix}(.+?)${this.unescapeSuffix}${this.suffix}`);
|
|
1132
|
+
this.nestingRegexp = getOrResetRegExp(this.nestingRegexp, `${this.nestingPrefix}((?:[^()"']+|"[^"]*"|'[^']*'|\\((?:[^()]|"[^"]*"|'[^']*')*\\))*?)${this.nestingSuffix}`);
|
|
1133
|
+
}
|
|
1134
|
+
interpolate(str, data, lng, options) {
|
|
1135
|
+
let match;
|
|
1136
|
+
let value;
|
|
1137
|
+
let replaces;
|
|
1138
|
+
const defaultData = this.options && this.options.interpolation && this.options.interpolation.defaultVariables || {};
|
|
1139
|
+
const handleFormat = (key) => {
|
|
1140
|
+
if (key.indexOf(this.formatSeparator) < 0) {
|
|
1141
|
+
const path = deepFindWithDefaults(data, defaultData, key, this.options.keySeparator, this.options.ignoreJSONStructure);
|
|
1142
|
+
return this.alwaysFormat ? this.format(path, void 0, lng, {
|
|
1143
|
+
...options,
|
|
1144
|
+
...data,
|
|
1145
|
+
interpolationkey: key
|
|
1146
|
+
}) : path;
|
|
1147
|
+
}
|
|
1148
|
+
const p = key.split(this.formatSeparator);
|
|
1149
|
+
const k = p.shift().trim();
|
|
1150
|
+
const f = p.join(this.formatSeparator).trim();
|
|
1151
|
+
return this.format(deepFindWithDefaults(data, defaultData, k, this.options.keySeparator, this.options.ignoreJSONStructure), f, lng, {
|
|
1152
|
+
...options,
|
|
1153
|
+
...data,
|
|
1154
|
+
interpolationkey: k
|
|
1155
|
+
});
|
|
1156
|
+
};
|
|
1157
|
+
this.resetRegExp();
|
|
1158
|
+
const missingInterpolationHandler = options?.missingInterpolationHandler || this.options.missingInterpolationHandler;
|
|
1159
|
+
const skipOnVariables = options?.interpolation?.skipOnVariables !== void 0 ? options.interpolation.skipOnVariables : this.options.interpolation.skipOnVariables;
|
|
1160
|
+
const todos = [{
|
|
1161
|
+
regex: this.regexpUnescape,
|
|
1162
|
+
safeValue: (val) => regexSafe(val)
|
|
1163
|
+
}, {
|
|
1164
|
+
regex: this.regexp,
|
|
1165
|
+
safeValue: (val) => this.escapeValue ? regexSafe(this.escape(val)) : regexSafe(val)
|
|
1166
|
+
}];
|
|
1167
|
+
todos.forEach((todo) => {
|
|
1168
|
+
replaces = 0;
|
|
1169
|
+
while (match = todo.regex.exec(str)) {
|
|
1170
|
+
const matchedVar = match[1].trim();
|
|
1171
|
+
value = handleFormat(matchedVar);
|
|
1172
|
+
if (value === void 0) {
|
|
1173
|
+
if (typeof missingInterpolationHandler === "function") {
|
|
1174
|
+
const temp = missingInterpolationHandler(str, match, options);
|
|
1175
|
+
value = isString(temp) ? temp : "";
|
|
1176
|
+
} else if (options && Object.prototype.hasOwnProperty.call(options, matchedVar)) {
|
|
1177
|
+
value = "";
|
|
1178
|
+
} else if (skipOnVariables) {
|
|
1179
|
+
value = match[0];
|
|
1180
|
+
continue;
|
|
1181
|
+
} else {
|
|
1182
|
+
this.logger.warn(`missed to pass in variable ${matchedVar} for interpolating ${str}`);
|
|
1183
|
+
value = "";
|
|
1184
|
+
}
|
|
1185
|
+
} else if (!isString(value) && !this.useRawValueToEscape) {
|
|
1186
|
+
value = makeString(value);
|
|
1187
|
+
}
|
|
1188
|
+
const safeValue = todo.safeValue(value);
|
|
1189
|
+
str = str.replace(match[0], safeValue);
|
|
1190
|
+
if (skipOnVariables) {
|
|
1191
|
+
todo.regex.lastIndex += value.length;
|
|
1192
|
+
todo.regex.lastIndex -= match[0].length;
|
|
1193
|
+
} else {
|
|
1194
|
+
todo.regex.lastIndex = 0;
|
|
1195
|
+
}
|
|
1196
|
+
replaces++;
|
|
1197
|
+
if (replaces >= this.maxReplaces) {
|
|
1198
|
+
break;
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
});
|
|
1202
|
+
return str;
|
|
1203
|
+
}
|
|
1204
|
+
nest(str, fc, options = {}) {
|
|
1205
|
+
let match;
|
|
1206
|
+
let value;
|
|
1207
|
+
let clonedOptions;
|
|
1208
|
+
const handleHasOptions = (key, inheritedOptions) => {
|
|
1209
|
+
const sep = this.nestingOptionsSeparator;
|
|
1210
|
+
if (key.indexOf(sep) < 0) return key;
|
|
1211
|
+
const c = key.split(new RegExp(`${sep}[ ]*{`));
|
|
1212
|
+
let optionsString = `{${c[1]}`;
|
|
1213
|
+
key = c[0];
|
|
1214
|
+
optionsString = this.interpolate(optionsString, clonedOptions);
|
|
1215
|
+
const matchedSingleQuotes = optionsString.match(/'/g);
|
|
1216
|
+
const matchedDoubleQuotes = optionsString.match(/"/g);
|
|
1217
|
+
if ((matchedSingleQuotes?.length ?? 0) % 2 === 0 && !matchedDoubleQuotes || matchedDoubleQuotes.length % 2 !== 0) {
|
|
1218
|
+
optionsString = optionsString.replace(/'/g, '"');
|
|
1219
|
+
}
|
|
1220
|
+
try {
|
|
1221
|
+
clonedOptions = JSON.parse(optionsString);
|
|
1222
|
+
if (inheritedOptions) clonedOptions = {
|
|
1223
|
+
...inheritedOptions,
|
|
1224
|
+
...clonedOptions
|
|
1225
|
+
};
|
|
1226
|
+
} catch (e) {
|
|
1227
|
+
this.logger.warn(`failed parsing options string in nesting for key ${key}`, e);
|
|
1228
|
+
return `${key}${sep}${optionsString}`;
|
|
1229
|
+
}
|
|
1230
|
+
if (clonedOptions.defaultValue && clonedOptions.defaultValue.indexOf(this.prefix) > -1) delete clonedOptions.defaultValue;
|
|
1231
|
+
return key;
|
|
1232
|
+
};
|
|
1233
|
+
while (match = this.nestingRegexp.exec(str)) {
|
|
1234
|
+
let formatters = [];
|
|
1235
|
+
clonedOptions = {
|
|
1236
|
+
...options
|
|
1237
|
+
};
|
|
1238
|
+
clonedOptions = clonedOptions.replace && !isString(clonedOptions.replace) ? clonedOptions.replace : clonedOptions;
|
|
1239
|
+
clonedOptions.applyPostProcessor = false;
|
|
1240
|
+
delete clonedOptions.defaultValue;
|
|
1241
|
+
const keyEndIndex = /{.*}/.test(match[1]) ? match[1].lastIndexOf("}") + 1 : match[1].indexOf(this.formatSeparator);
|
|
1242
|
+
if (keyEndIndex !== -1) {
|
|
1243
|
+
formatters = match[1].slice(keyEndIndex).split(this.formatSeparator).map((elem) => elem.trim()).filter(Boolean);
|
|
1244
|
+
match[1] = match[1].slice(0, keyEndIndex);
|
|
1245
|
+
}
|
|
1246
|
+
value = fc(handleHasOptions.call(this, match[1].trim(), clonedOptions), clonedOptions);
|
|
1247
|
+
if (value && match[0] === str && !isString(value)) return value;
|
|
1248
|
+
if (!isString(value)) value = makeString(value);
|
|
1249
|
+
if (!value) {
|
|
1250
|
+
this.logger.warn(`missed to resolve ${match[1]} for nesting ${str}`);
|
|
1251
|
+
value = "";
|
|
1252
|
+
}
|
|
1253
|
+
if (formatters.length) {
|
|
1254
|
+
value = formatters.reduce((v, f) => this.format(v, f, options.lng, {
|
|
1255
|
+
...options,
|
|
1256
|
+
interpolationkey: match[1].trim()
|
|
1257
|
+
}), value.trim());
|
|
1258
|
+
}
|
|
1259
|
+
str = str.replace(match[0], value);
|
|
1260
|
+
this.regexp.lastIndex = 0;
|
|
1261
|
+
}
|
|
1262
|
+
return str;
|
|
1263
|
+
}
|
|
1264
|
+
};
|
|
1265
|
+
var parseFormatStr = (formatStr) => {
|
|
1266
|
+
let formatName = formatStr.toLowerCase().trim();
|
|
1267
|
+
const formatOptions = {};
|
|
1268
|
+
if (formatStr.indexOf("(") > -1) {
|
|
1269
|
+
const p = formatStr.split("(");
|
|
1270
|
+
formatName = p[0].toLowerCase().trim();
|
|
1271
|
+
const optStr = p[1].substring(0, p[1].length - 1);
|
|
1272
|
+
if (formatName === "currency" && optStr.indexOf(":") < 0) {
|
|
1273
|
+
if (!formatOptions.currency) formatOptions.currency = optStr.trim();
|
|
1274
|
+
} else if (formatName === "relativetime" && optStr.indexOf(":") < 0) {
|
|
1275
|
+
if (!formatOptions.range) formatOptions.range = optStr.trim();
|
|
1276
|
+
} else {
|
|
1277
|
+
const opts = optStr.split(";");
|
|
1278
|
+
opts.forEach((opt) => {
|
|
1279
|
+
if (opt) {
|
|
1280
|
+
const [key, ...rest] = opt.split(":");
|
|
1281
|
+
const val = rest.join(":").trim().replace(/^'+|'+$/g, "");
|
|
1282
|
+
const trimmedKey = key.trim();
|
|
1283
|
+
if (!formatOptions[trimmedKey]) formatOptions[trimmedKey] = val;
|
|
1284
|
+
if (val === "false") formatOptions[trimmedKey] = false;
|
|
1285
|
+
if (val === "true") formatOptions[trimmedKey] = true;
|
|
1286
|
+
if (!isNaN(val)) formatOptions[trimmedKey] = parseInt(val, 10);
|
|
1287
|
+
}
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
return {
|
|
1292
|
+
formatName,
|
|
1293
|
+
formatOptions
|
|
1294
|
+
};
|
|
1295
|
+
};
|
|
1296
|
+
var createCachedFormatter = (fn) => {
|
|
1297
|
+
const cache = {};
|
|
1298
|
+
return (v, l, o) => {
|
|
1299
|
+
let optForCache = o;
|
|
1300
|
+
if (o && o.interpolationkey && o.formatParams && o.formatParams[o.interpolationkey] && o[o.interpolationkey]) {
|
|
1301
|
+
optForCache = {
|
|
1302
|
+
...optForCache,
|
|
1303
|
+
[o.interpolationkey]: void 0
|
|
1304
|
+
};
|
|
1305
|
+
}
|
|
1306
|
+
const key = l + JSON.stringify(optForCache);
|
|
1307
|
+
let frm = cache[key];
|
|
1308
|
+
if (!frm) {
|
|
1309
|
+
frm = fn(getCleanedCode(l), o);
|
|
1310
|
+
cache[key] = frm;
|
|
1311
|
+
}
|
|
1312
|
+
return frm(v);
|
|
1313
|
+
};
|
|
1314
|
+
};
|
|
1315
|
+
var createNonCachedFormatter = (fn) => (v, l, o) => fn(getCleanedCode(l), o)(v);
|
|
1316
|
+
var Formatter = class {
|
|
1317
|
+
constructor(options = {}) {
|
|
1318
|
+
this.logger = baseLogger.create("formatter");
|
|
1319
|
+
this.options = options;
|
|
1320
|
+
this.init(options);
|
|
1321
|
+
}
|
|
1322
|
+
init(services, options = {
|
|
1323
|
+
interpolation: {}
|
|
1324
|
+
}) {
|
|
1325
|
+
this.formatSeparator = options.interpolation.formatSeparator || ",";
|
|
1326
|
+
const cf = options.cacheInBuiltFormats ? createCachedFormatter : createNonCachedFormatter;
|
|
1327
|
+
this.formats = {
|
|
1328
|
+
number: cf((lng, opt) => {
|
|
1329
|
+
const formatter = new Intl.NumberFormat(lng, {
|
|
1330
|
+
...opt
|
|
1331
|
+
});
|
|
1332
|
+
return (val) => formatter.format(val);
|
|
1333
|
+
}),
|
|
1334
|
+
currency: cf((lng, opt) => {
|
|
1335
|
+
const formatter = new Intl.NumberFormat(lng, {
|
|
1336
|
+
...opt,
|
|
1337
|
+
style: "currency"
|
|
1338
|
+
});
|
|
1339
|
+
return (val) => formatter.format(val);
|
|
1340
|
+
}),
|
|
1341
|
+
datetime: cf((lng, opt) => {
|
|
1342
|
+
const formatter = new Intl.DateTimeFormat(lng, {
|
|
1343
|
+
...opt
|
|
1344
|
+
});
|
|
1345
|
+
return (val) => formatter.format(val);
|
|
1346
|
+
}),
|
|
1347
|
+
relativetime: cf((lng, opt) => {
|
|
1348
|
+
const formatter = new Intl.RelativeTimeFormat(lng, {
|
|
1349
|
+
...opt
|
|
1350
|
+
});
|
|
1351
|
+
return (val) => formatter.format(val, opt.range || "day");
|
|
1352
|
+
}),
|
|
1353
|
+
list: cf((lng, opt) => {
|
|
1354
|
+
const formatter = new Intl.ListFormat(lng, {
|
|
1355
|
+
...opt
|
|
1356
|
+
});
|
|
1357
|
+
return (val) => formatter.format(val);
|
|
1358
|
+
})
|
|
1359
|
+
};
|
|
1360
|
+
}
|
|
1361
|
+
add(name, fc) {
|
|
1362
|
+
this.formats[name.toLowerCase().trim()] = fc;
|
|
1363
|
+
}
|
|
1364
|
+
addCached(name, fc) {
|
|
1365
|
+
this.formats[name.toLowerCase().trim()] = createCachedFormatter(fc);
|
|
1366
|
+
}
|
|
1367
|
+
format(value, format, lng, options = {}) {
|
|
1368
|
+
const formats = format.split(this.formatSeparator);
|
|
1369
|
+
if (formats.length > 1 && formats[0].indexOf("(") > 1 && formats[0].indexOf(")") < 0 && formats.find((f) => f.indexOf(")") > -1)) {
|
|
1370
|
+
const lastIndex = formats.findIndex((f) => f.indexOf(")") > -1);
|
|
1371
|
+
formats[0] = [formats[0], ...formats.splice(1, lastIndex)].join(this.formatSeparator);
|
|
1372
|
+
}
|
|
1373
|
+
const result = formats.reduce((mem, f) => {
|
|
1374
|
+
const {
|
|
1375
|
+
formatName,
|
|
1376
|
+
formatOptions
|
|
1377
|
+
} = parseFormatStr(f);
|
|
1378
|
+
if (this.formats[formatName]) {
|
|
1379
|
+
let formatted = mem;
|
|
1380
|
+
try {
|
|
1381
|
+
const valOptions = options?.formatParams?.[options.interpolationkey] || {};
|
|
1382
|
+
const l = valOptions.locale || valOptions.lng || options.locale || options.lng || lng;
|
|
1383
|
+
formatted = this.formats[formatName](mem, l, {
|
|
1384
|
+
...formatOptions,
|
|
1385
|
+
...options,
|
|
1386
|
+
...valOptions
|
|
1387
|
+
});
|
|
1388
|
+
} catch (error) {
|
|
1389
|
+
this.logger.warn(error);
|
|
1390
|
+
}
|
|
1391
|
+
return formatted;
|
|
1392
|
+
} else {
|
|
1393
|
+
this.logger.warn(`there was no format function for ${formatName}`);
|
|
1394
|
+
}
|
|
1395
|
+
return mem;
|
|
1396
|
+
}, value);
|
|
1397
|
+
return result;
|
|
1398
|
+
}
|
|
1399
|
+
};
|
|
1400
|
+
var removePending = (q, name) => {
|
|
1401
|
+
if (q.pending[name] !== void 0) {
|
|
1402
|
+
delete q.pending[name];
|
|
1403
|
+
q.pendingCount--;
|
|
1404
|
+
}
|
|
1405
|
+
};
|
|
1406
|
+
var Connector = class extends EventEmitter {
|
|
1407
|
+
constructor(backend, store, services, options = {}) {
|
|
1408
|
+
super();
|
|
1409
|
+
this.backend = backend;
|
|
1410
|
+
this.store = store;
|
|
1411
|
+
this.services = services;
|
|
1412
|
+
this.languageUtils = services.languageUtils;
|
|
1413
|
+
this.options = options;
|
|
1414
|
+
this.logger = baseLogger.create("backendConnector");
|
|
1415
|
+
this.waitingReads = [];
|
|
1416
|
+
this.maxParallelReads = options.maxParallelReads || 10;
|
|
1417
|
+
this.readingCalls = 0;
|
|
1418
|
+
this.maxRetries = options.maxRetries >= 0 ? options.maxRetries : 5;
|
|
1419
|
+
this.retryTimeout = options.retryTimeout >= 1 ? options.retryTimeout : 350;
|
|
1420
|
+
this.state = {};
|
|
1421
|
+
this.queue = [];
|
|
1422
|
+
this.backend?.init?.(services, options.backend, options);
|
|
1423
|
+
}
|
|
1424
|
+
queueLoad(languages, namespaces, options, callback) {
|
|
1425
|
+
const toLoad = {};
|
|
1426
|
+
const pending = {};
|
|
1427
|
+
const toLoadLanguages = {};
|
|
1428
|
+
const toLoadNamespaces = {};
|
|
1429
|
+
languages.forEach((lng) => {
|
|
1430
|
+
let hasAllNamespaces = true;
|
|
1431
|
+
namespaces.forEach((ns) => {
|
|
1432
|
+
const name = `${lng}|${ns}`;
|
|
1433
|
+
if (!options.reload && this.store.hasResourceBundle(lng, ns)) {
|
|
1434
|
+
this.state[name] = 2;
|
|
1435
|
+
} else if (this.state[name] < 0) ;
|
|
1436
|
+
else if (this.state[name] === 1) {
|
|
1437
|
+
if (pending[name] === void 0) pending[name] = true;
|
|
1438
|
+
} else {
|
|
1439
|
+
this.state[name] = 1;
|
|
1440
|
+
hasAllNamespaces = false;
|
|
1441
|
+
if (pending[name] === void 0) pending[name] = true;
|
|
1442
|
+
if (toLoad[name] === void 0) toLoad[name] = true;
|
|
1443
|
+
if (toLoadNamespaces[ns] === void 0) toLoadNamespaces[ns] = true;
|
|
1444
|
+
}
|
|
1445
|
+
});
|
|
1446
|
+
if (!hasAllNamespaces) toLoadLanguages[lng] = true;
|
|
1447
|
+
});
|
|
1448
|
+
if (Object.keys(toLoad).length || Object.keys(pending).length) {
|
|
1449
|
+
this.queue.push({
|
|
1450
|
+
pending,
|
|
1451
|
+
pendingCount: Object.keys(pending).length,
|
|
1452
|
+
loaded: {},
|
|
1453
|
+
errors: [],
|
|
1454
|
+
callback
|
|
1455
|
+
});
|
|
1456
|
+
}
|
|
1457
|
+
return {
|
|
1458
|
+
toLoad: Object.keys(toLoad),
|
|
1459
|
+
pending: Object.keys(pending),
|
|
1460
|
+
toLoadLanguages: Object.keys(toLoadLanguages),
|
|
1461
|
+
toLoadNamespaces: Object.keys(toLoadNamespaces)
|
|
1462
|
+
};
|
|
1463
|
+
}
|
|
1464
|
+
loaded(name, err, data) {
|
|
1465
|
+
const s = name.split("|");
|
|
1466
|
+
const lng = s[0];
|
|
1467
|
+
const ns = s[1];
|
|
1468
|
+
if (err) this.emit("failedLoading", lng, ns, err);
|
|
1469
|
+
if (!err && data) {
|
|
1470
|
+
this.store.addResourceBundle(lng, ns, data, void 0, void 0, {
|
|
1471
|
+
skipCopy: true
|
|
1472
|
+
});
|
|
1473
|
+
}
|
|
1474
|
+
this.state[name] = err ? -1 : 2;
|
|
1475
|
+
if (err && data) this.state[name] = 0;
|
|
1476
|
+
const loaded = {};
|
|
1477
|
+
this.queue.forEach((q) => {
|
|
1478
|
+
pushPath(q.loaded, [lng], ns);
|
|
1479
|
+
removePending(q, name);
|
|
1480
|
+
if (err) q.errors.push(err);
|
|
1481
|
+
if (q.pendingCount === 0 && !q.done) {
|
|
1482
|
+
Object.keys(q.loaded).forEach((l) => {
|
|
1483
|
+
if (!loaded[l]) loaded[l] = {};
|
|
1484
|
+
const loadedKeys = q.loaded[l];
|
|
1485
|
+
if (loadedKeys.length) {
|
|
1486
|
+
loadedKeys.forEach((n) => {
|
|
1487
|
+
if (loaded[l][n] === void 0) loaded[l][n] = true;
|
|
1488
|
+
});
|
|
1489
|
+
}
|
|
1490
|
+
});
|
|
1491
|
+
q.done = true;
|
|
1492
|
+
if (q.errors.length) {
|
|
1493
|
+
q.callback(q.errors);
|
|
1494
|
+
} else {
|
|
1495
|
+
q.callback();
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
});
|
|
1499
|
+
this.emit("loaded", loaded);
|
|
1500
|
+
this.queue = this.queue.filter((q) => !q.done);
|
|
1501
|
+
}
|
|
1502
|
+
read(lng, ns, fcName, tried = 0, wait = this.retryTimeout, callback) {
|
|
1503
|
+
if (!lng.length) return callback(null, {});
|
|
1504
|
+
if (this.readingCalls >= this.maxParallelReads) {
|
|
1505
|
+
this.waitingReads.push({
|
|
1506
|
+
lng,
|
|
1507
|
+
ns,
|
|
1508
|
+
fcName,
|
|
1509
|
+
tried,
|
|
1510
|
+
wait,
|
|
1511
|
+
callback
|
|
1512
|
+
});
|
|
1513
|
+
return;
|
|
1514
|
+
}
|
|
1515
|
+
this.readingCalls++;
|
|
1516
|
+
const resolver = (err, data) => {
|
|
1517
|
+
this.readingCalls--;
|
|
1518
|
+
if (this.waitingReads.length > 0) {
|
|
1519
|
+
const next = this.waitingReads.shift();
|
|
1520
|
+
this.read(next.lng, next.ns, next.fcName, next.tried, next.wait, next.callback);
|
|
1521
|
+
}
|
|
1522
|
+
if (err && data && tried < this.maxRetries) {
|
|
1523
|
+
setTimeout(() => {
|
|
1524
|
+
this.read.call(this, lng, ns, fcName, tried + 1, wait * 2, callback);
|
|
1525
|
+
}, wait);
|
|
1526
|
+
return;
|
|
1527
|
+
}
|
|
1528
|
+
callback(err, data);
|
|
1529
|
+
};
|
|
1530
|
+
const fc = this.backend[fcName].bind(this.backend);
|
|
1531
|
+
if (fc.length === 2) {
|
|
1532
|
+
try {
|
|
1533
|
+
const r = fc(lng, ns);
|
|
1534
|
+
if (r && typeof r.then === "function") {
|
|
1535
|
+
r.then((data) => resolver(null, data)).catch(resolver);
|
|
1536
|
+
} else {
|
|
1537
|
+
resolver(null, r);
|
|
1538
|
+
}
|
|
1539
|
+
} catch (err) {
|
|
1540
|
+
resolver(err);
|
|
1541
|
+
}
|
|
1542
|
+
return;
|
|
1543
|
+
}
|
|
1544
|
+
return fc(lng, ns, resolver);
|
|
1545
|
+
}
|
|
1546
|
+
prepareLoading(languages, namespaces, options = {}, callback) {
|
|
1547
|
+
if (!this.backend) {
|
|
1548
|
+
this.logger.warn("No backend was added via i18next.use. Will not load resources.");
|
|
1549
|
+
return callback && callback();
|
|
1550
|
+
}
|
|
1551
|
+
if (isString(languages)) languages = this.languageUtils.toResolveHierarchy(languages);
|
|
1552
|
+
if (isString(namespaces)) namespaces = [namespaces];
|
|
1553
|
+
const toLoad = this.queueLoad(languages, namespaces, options, callback);
|
|
1554
|
+
if (!toLoad.toLoad.length) {
|
|
1555
|
+
if (!toLoad.pending.length) callback();
|
|
1556
|
+
return null;
|
|
1557
|
+
}
|
|
1558
|
+
toLoad.toLoad.forEach((name) => {
|
|
1559
|
+
this.loadOne(name);
|
|
1560
|
+
});
|
|
1561
|
+
}
|
|
1562
|
+
load(languages, namespaces, callback) {
|
|
1563
|
+
this.prepareLoading(languages, namespaces, {}, callback);
|
|
1564
|
+
}
|
|
1565
|
+
reload(languages, namespaces, callback) {
|
|
1566
|
+
this.prepareLoading(languages, namespaces, {
|
|
1567
|
+
reload: true
|
|
1568
|
+
}, callback);
|
|
1569
|
+
}
|
|
1570
|
+
loadOne(name, prefix = "") {
|
|
1571
|
+
const s = name.split("|");
|
|
1572
|
+
const lng = s[0];
|
|
1573
|
+
const ns = s[1];
|
|
1574
|
+
this.read(lng, ns, "read", void 0, void 0, (err, data) => {
|
|
1575
|
+
if (err) this.logger.warn(`${prefix}loading namespace ${ns} for language ${lng} failed`, err);
|
|
1576
|
+
if (!err && data) this.logger.log(`${prefix}loaded namespace ${ns} for language ${lng}`, data);
|
|
1577
|
+
this.loaded(name, err, data);
|
|
1578
|
+
});
|
|
1579
|
+
}
|
|
1580
|
+
saveMissing(languages, namespace, key, fallbackValue, isUpdate, options = {}, clb = () => {
|
|
1581
|
+
}) {
|
|
1582
|
+
if (this.services?.utils?.hasLoadedNamespace && !this.services?.utils?.hasLoadedNamespace(namespace)) {
|
|
1583
|
+
this.logger.warn(`did not save key "${key}" as the namespace "${namespace}" was not yet loaded`, "This means something IS WRONG in your setup. You access the t function before i18next.init / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!");
|
|
1584
|
+
return;
|
|
1585
|
+
}
|
|
1586
|
+
if (key === void 0 || key === null || key === "") return;
|
|
1587
|
+
if (this.backend?.create) {
|
|
1588
|
+
const opts = {
|
|
1589
|
+
...options,
|
|
1590
|
+
isUpdate
|
|
1591
|
+
};
|
|
1592
|
+
const fc = this.backend.create.bind(this.backend);
|
|
1593
|
+
if (fc.length < 6) {
|
|
1594
|
+
try {
|
|
1595
|
+
let r;
|
|
1596
|
+
if (fc.length === 5) {
|
|
1597
|
+
r = fc(languages, namespace, key, fallbackValue, opts);
|
|
1598
|
+
} else {
|
|
1599
|
+
r = fc(languages, namespace, key, fallbackValue);
|
|
1600
|
+
}
|
|
1601
|
+
if (r && typeof r.then === "function") {
|
|
1602
|
+
r.then((data) => clb(null, data)).catch(clb);
|
|
1603
|
+
} else {
|
|
1604
|
+
clb(null, r);
|
|
1605
|
+
}
|
|
1606
|
+
} catch (err) {
|
|
1607
|
+
clb(err);
|
|
1608
|
+
}
|
|
1609
|
+
} else {
|
|
1610
|
+
fc(languages, namespace, key, fallbackValue, clb, opts);
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
if (!languages || !languages[0]) return;
|
|
1614
|
+
this.store.addResource(languages[0], namespace, key, fallbackValue);
|
|
1615
|
+
}
|
|
1616
|
+
};
|
|
1617
|
+
var get = () => ({
|
|
1618
|
+
debug: false,
|
|
1619
|
+
initAsync: true,
|
|
1620
|
+
ns: ["translation"],
|
|
1621
|
+
defaultNS: ["translation"],
|
|
1622
|
+
fallbackLng: ["dev"],
|
|
1623
|
+
fallbackNS: false,
|
|
1624
|
+
supportedLngs: false,
|
|
1625
|
+
nonExplicitSupportedLngs: false,
|
|
1626
|
+
load: "all",
|
|
1627
|
+
preload: false,
|
|
1628
|
+
simplifyPluralSuffix: true,
|
|
1629
|
+
keySeparator: ".",
|
|
1630
|
+
nsSeparator: ":",
|
|
1631
|
+
pluralSeparator: "_",
|
|
1632
|
+
contextSeparator: "_",
|
|
1633
|
+
partialBundledLanguages: false,
|
|
1634
|
+
saveMissing: false,
|
|
1635
|
+
updateMissing: false,
|
|
1636
|
+
saveMissingTo: "fallback",
|
|
1637
|
+
saveMissingPlurals: true,
|
|
1638
|
+
missingKeyHandler: false,
|
|
1639
|
+
missingInterpolationHandler: false,
|
|
1640
|
+
postProcess: false,
|
|
1641
|
+
postProcessPassResolved: false,
|
|
1642
|
+
returnNull: false,
|
|
1643
|
+
returnEmptyString: true,
|
|
1644
|
+
returnObjects: false,
|
|
1645
|
+
joinArrays: false,
|
|
1646
|
+
returnedObjectHandler: false,
|
|
1647
|
+
parseMissingKeyHandler: false,
|
|
1648
|
+
appendNamespaceToMissingKey: false,
|
|
1649
|
+
appendNamespaceToCIMode: false,
|
|
1650
|
+
overloadTranslationOptionHandler: (args) => {
|
|
1651
|
+
let ret = {};
|
|
1652
|
+
if (typeof args[1] === "object") ret = args[1];
|
|
1653
|
+
if (isString(args[1])) ret.defaultValue = args[1];
|
|
1654
|
+
if (isString(args[2])) ret.tDescription = args[2];
|
|
1655
|
+
if (typeof args[2] === "object" || typeof args[3] === "object") {
|
|
1656
|
+
const options = args[3] || args[2];
|
|
1657
|
+
Object.keys(options).forEach((key) => {
|
|
1658
|
+
ret[key] = options[key];
|
|
1659
|
+
});
|
|
1660
|
+
}
|
|
1661
|
+
return ret;
|
|
1662
|
+
},
|
|
1663
|
+
interpolation: {
|
|
1664
|
+
escapeValue: true,
|
|
1665
|
+
format: (value) => value,
|
|
1666
|
+
prefix: "{{",
|
|
1667
|
+
suffix: "}}",
|
|
1668
|
+
formatSeparator: ",",
|
|
1669
|
+
unescapePrefix: "-",
|
|
1670
|
+
nestingPrefix: "$t(",
|
|
1671
|
+
nestingSuffix: ")",
|
|
1672
|
+
nestingOptionsSeparator: ",",
|
|
1673
|
+
maxReplaces: 1e3,
|
|
1674
|
+
skipOnVariables: true
|
|
1675
|
+
},
|
|
1676
|
+
cacheInBuiltFormats: true
|
|
1677
|
+
});
|
|
1678
|
+
var transformOptions = (options) => {
|
|
1679
|
+
if (isString(options.ns)) options.ns = [options.ns];
|
|
1680
|
+
if (isString(options.fallbackLng)) options.fallbackLng = [options.fallbackLng];
|
|
1681
|
+
if (isString(options.fallbackNS)) options.fallbackNS = [options.fallbackNS];
|
|
1682
|
+
if (options.supportedLngs?.indexOf?.("cimode") < 0) {
|
|
1683
|
+
options.supportedLngs = options.supportedLngs.concat(["cimode"]);
|
|
1684
|
+
}
|
|
1685
|
+
if (typeof options.initImmediate === "boolean") options.initAsync = options.initImmediate;
|
|
1686
|
+
return options;
|
|
1687
|
+
};
|
|
1688
|
+
var noop = () => {
|
|
1689
|
+
};
|
|
1690
|
+
var bindMemberFunctions = (inst) => {
|
|
1691
|
+
const mems = Object.getOwnPropertyNames(Object.getPrototypeOf(inst));
|
|
1692
|
+
mems.forEach((mem) => {
|
|
1693
|
+
if (typeof inst[mem] === "function") {
|
|
1694
|
+
inst[mem] = inst[mem].bind(inst);
|
|
1695
|
+
}
|
|
1696
|
+
});
|
|
1697
|
+
};
|
|
1698
|
+
var I18n = class _I18n extends EventEmitter {
|
|
1699
|
+
constructor(options = {}, callback) {
|
|
1700
|
+
super();
|
|
1701
|
+
this.options = transformOptions(options);
|
|
1702
|
+
this.services = {};
|
|
1703
|
+
this.logger = baseLogger;
|
|
1704
|
+
this.modules = {
|
|
1705
|
+
external: []
|
|
1706
|
+
};
|
|
1707
|
+
bindMemberFunctions(this);
|
|
1708
|
+
if (callback && !this.isInitialized && !options.isClone) {
|
|
1709
|
+
if (!this.options.initAsync) {
|
|
1710
|
+
this.init(options, callback);
|
|
1711
|
+
return this;
|
|
1712
|
+
}
|
|
1713
|
+
setTimeout(() => {
|
|
1714
|
+
this.init(options, callback);
|
|
1715
|
+
}, 0);
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
init(options = {}, callback) {
|
|
1719
|
+
this.isInitializing = true;
|
|
1720
|
+
if (typeof options === "function") {
|
|
1721
|
+
callback = options;
|
|
1722
|
+
options = {};
|
|
1723
|
+
}
|
|
1724
|
+
if (options.defaultNS == null && options.ns) {
|
|
1725
|
+
if (isString(options.ns)) {
|
|
1726
|
+
options.defaultNS = options.ns;
|
|
1727
|
+
} else if (options.ns.indexOf("translation") < 0) {
|
|
1728
|
+
options.defaultNS = options.ns[0];
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1731
|
+
const defOpts = get();
|
|
1732
|
+
this.options = {
|
|
1733
|
+
...defOpts,
|
|
1734
|
+
...this.options,
|
|
1735
|
+
...transformOptions(options)
|
|
1736
|
+
};
|
|
1737
|
+
this.options.interpolation = {
|
|
1738
|
+
...defOpts.interpolation,
|
|
1739
|
+
...this.options.interpolation
|
|
1740
|
+
};
|
|
1741
|
+
if (options.keySeparator !== void 0) {
|
|
1742
|
+
this.options.userDefinedKeySeparator = options.keySeparator;
|
|
1743
|
+
}
|
|
1744
|
+
if (options.nsSeparator !== void 0) {
|
|
1745
|
+
this.options.userDefinedNsSeparator = options.nsSeparator;
|
|
1746
|
+
}
|
|
1747
|
+
const createClassOnDemand = (ClassOrObject) => {
|
|
1748
|
+
if (!ClassOrObject) return null;
|
|
1749
|
+
if (typeof ClassOrObject === "function") return new ClassOrObject();
|
|
1750
|
+
return ClassOrObject;
|
|
1751
|
+
};
|
|
1752
|
+
if (!this.options.isClone) {
|
|
1753
|
+
if (this.modules.logger) {
|
|
1754
|
+
baseLogger.init(createClassOnDemand(this.modules.logger), this.options);
|
|
1755
|
+
} else {
|
|
1756
|
+
baseLogger.init(null, this.options);
|
|
1757
|
+
}
|
|
1758
|
+
let formatter;
|
|
1759
|
+
if (this.modules.formatter) {
|
|
1760
|
+
formatter = this.modules.formatter;
|
|
1761
|
+
} else {
|
|
1762
|
+
formatter = Formatter;
|
|
1763
|
+
}
|
|
1764
|
+
const lu = new LanguageUtil(this.options);
|
|
1765
|
+
this.store = new ResourceStore(this.options.resources, this.options);
|
|
1766
|
+
const s = this.services;
|
|
1767
|
+
s.logger = baseLogger;
|
|
1768
|
+
s.resourceStore = this.store;
|
|
1769
|
+
s.languageUtils = lu;
|
|
1770
|
+
s.pluralResolver = new PluralResolver(lu, {
|
|
1771
|
+
prepend: this.options.pluralSeparator,
|
|
1772
|
+
simplifyPluralSuffix: this.options.simplifyPluralSuffix
|
|
1773
|
+
});
|
|
1774
|
+
const usingLegacyFormatFunction = this.options.interpolation.format && this.options.interpolation.format !== defOpts.interpolation.format;
|
|
1775
|
+
if (usingLegacyFormatFunction) {
|
|
1776
|
+
this.logger.deprecate(`init: you are still using the legacy format function, please use the new approach: https://www.i18next.com/translation-function/formatting`);
|
|
1777
|
+
}
|
|
1778
|
+
if (formatter && (!this.options.interpolation.format || this.options.interpolation.format === defOpts.interpolation.format)) {
|
|
1779
|
+
s.formatter = createClassOnDemand(formatter);
|
|
1780
|
+
if (s.formatter.init) s.formatter.init(s, this.options);
|
|
1781
|
+
this.options.interpolation.format = s.formatter.format.bind(s.formatter);
|
|
1782
|
+
}
|
|
1783
|
+
s.interpolator = new Interpolator(this.options);
|
|
1784
|
+
s.utils = {
|
|
1785
|
+
hasLoadedNamespace: this.hasLoadedNamespace.bind(this)
|
|
1786
|
+
};
|
|
1787
|
+
s.backendConnector = new Connector(createClassOnDemand(this.modules.backend), s.resourceStore, s, this.options);
|
|
1788
|
+
s.backendConnector.on("*", (event, ...args) => {
|
|
1789
|
+
this.emit(event, ...args);
|
|
1790
|
+
});
|
|
1791
|
+
if (this.modules.languageDetector) {
|
|
1792
|
+
s.languageDetector = createClassOnDemand(this.modules.languageDetector);
|
|
1793
|
+
if (s.languageDetector.init) s.languageDetector.init(s, this.options.detection, this.options);
|
|
1794
|
+
}
|
|
1795
|
+
if (this.modules.i18nFormat) {
|
|
1796
|
+
s.i18nFormat = createClassOnDemand(this.modules.i18nFormat);
|
|
1797
|
+
if (s.i18nFormat.init) s.i18nFormat.init(this);
|
|
1798
|
+
}
|
|
1799
|
+
this.translator = new Translator(this.services, this.options);
|
|
1800
|
+
this.translator.on("*", (event, ...args) => {
|
|
1801
|
+
this.emit(event, ...args);
|
|
1802
|
+
});
|
|
1803
|
+
this.modules.external.forEach((m) => {
|
|
1804
|
+
if (m.init) m.init(this);
|
|
1805
|
+
});
|
|
1806
|
+
}
|
|
1807
|
+
this.format = this.options.interpolation.format;
|
|
1808
|
+
if (!callback) callback = noop;
|
|
1809
|
+
if (this.options.fallbackLng && !this.services.languageDetector && !this.options.lng) {
|
|
1810
|
+
const codes = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
|
|
1811
|
+
if (codes.length > 0 && codes[0] !== "dev") this.options.lng = codes[0];
|
|
1812
|
+
}
|
|
1813
|
+
if (!this.services.languageDetector && !this.options.lng) {
|
|
1814
|
+
this.logger.warn("init: no languageDetector is used and no lng is defined");
|
|
1815
|
+
}
|
|
1816
|
+
const storeApi = ["getResource", "hasResourceBundle", "getResourceBundle", "getDataByLanguage"];
|
|
1817
|
+
storeApi.forEach((fcName) => {
|
|
1818
|
+
this[fcName] = (...args) => this.store[fcName](...args);
|
|
1819
|
+
});
|
|
1820
|
+
const storeApiChained = ["addResource", "addResources", "addResourceBundle", "removeResourceBundle"];
|
|
1821
|
+
storeApiChained.forEach((fcName) => {
|
|
1822
|
+
this[fcName] = (...args) => {
|
|
1823
|
+
this.store[fcName](...args);
|
|
1824
|
+
return this;
|
|
1825
|
+
};
|
|
1826
|
+
});
|
|
1827
|
+
const deferred = defer();
|
|
1828
|
+
const load = () => {
|
|
1829
|
+
const finish = (err, t2) => {
|
|
1830
|
+
this.isInitializing = false;
|
|
1831
|
+
if (this.isInitialized && !this.initializedStoreOnce) this.logger.warn("init: i18next is already initialized. You should call init just once!");
|
|
1832
|
+
this.isInitialized = true;
|
|
1833
|
+
if (!this.options.isClone) this.logger.log("initialized", this.options);
|
|
1834
|
+
this.emit("initialized", this.options);
|
|
1835
|
+
deferred.resolve(t2);
|
|
1836
|
+
callback(err, t2);
|
|
1837
|
+
};
|
|
1838
|
+
if (this.languages && !this.isInitialized) return finish(null, this.t.bind(this));
|
|
1839
|
+
this.changeLanguage(this.options.lng, finish);
|
|
1840
|
+
};
|
|
1841
|
+
if (this.options.resources || !this.options.initAsync) {
|
|
1842
|
+
load();
|
|
1843
|
+
} else {
|
|
1844
|
+
setTimeout(load, 0);
|
|
1845
|
+
}
|
|
1846
|
+
return deferred;
|
|
1847
|
+
}
|
|
1848
|
+
loadResources(language, callback = noop) {
|
|
1849
|
+
let usedCallback = callback;
|
|
1850
|
+
const usedLng = isString(language) ? language : this.language;
|
|
1851
|
+
if (typeof language === "function") usedCallback = language;
|
|
1852
|
+
if (!this.options.resources || this.options.partialBundledLanguages) {
|
|
1853
|
+
if (usedLng?.toLowerCase() === "cimode" && (!this.options.preload || this.options.preload.length === 0)) return usedCallback();
|
|
1854
|
+
const toLoad = [];
|
|
1855
|
+
const append = (lng) => {
|
|
1856
|
+
if (!lng) return;
|
|
1857
|
+
if (lng === "cimode") return;
|
|
1858
|
+
const lngs = this.services.languageUtils.toResolveHierarchy(lng);
|
|
1859
|
+
lngs.forEach((l) => {
|
|
1860
|
+
if (l === "cimode") return;
|
|
1861
|
+
if (toLoad.indexOf(l) < 0) toLoad.push(l);
|
|
1862
|
+
});
|
|
1863
|
+
};
|
|
1864
|
+
if (!usedLng) {
|
|
1865
|
+
const fallbacks = this.services.languageUtils.getFallbackCodes(this.options.fallbackLng);
|
|
1866
|
+
fallbacks.forEach((l) => append(l));
|
|
1867
|
+
} else {
|
|
1868
|
+
append(usedLng);
|
|
1869
|
+
}
|
|
1870
|
+
this.options.preload?.forEach?.((l) => append(l));
|
|
1871
|
+
this.services.backendConnector.load(toLoad, this.options.ns, (e) => {
|
|
1872
|
+
if (!e && !this.resolvedLanguage && this.language) this.setResolvedLanguage(this.language);
|
|
1873
|
+
usedCallback(e);
|
|
1874
|
+
});
|
|
1875
|
+
} else {
|
|
1876
|
+
usedCallback(null);
|
|
1877
|
+
}
|
|
1878
|
+
}
|
|
1879
|
+
reloadResources(lngs, ns, callback) {
|
|
1880
|
+
const deferred = defer();
|
|
1881
|
+
if (typeof lngs === "function") {
|
|
1882
|
+
callback = lngs;
|
|
1883
|
+
lngs = void 0;
|
|
1884
|
+
}
|
|
1885
|
+
if (typeof ns === "function") {
|
|
1886
|
+
callback = ns;
|
|
1887
|
+
ns = void 0;
|
|
1888
|
+
}
|
|
1889
|
+
if (!lngs) lngs = this.languages;
|
|
1890
|
+
if (!ns) ns = this.options.ns;
|
|
1891
|
+
if (!callback) callback = noop;
|
|
1892
|
+
this.services.backendConnector.reload(lngs, ns, (err) => {
|
|
1893
|
+
deferred.resolve();
|
|
1894
|
+
callback(err);
|
|
1895
|
+
});
|
|
1896
|
+
return deferred;
|
|
1897
|
+
}
|
|
1898
|
+
use(module) {
|
|
1899
|
+
if (!module) throw new Error("You are passing an undefined module! Please check the object you are passing to i18next.use()");
|
|
1900
|
+
if (!module.type) throw new Error("You are passing a wrong module! Please check the object you are passing to i18next.use()");
|
|
1901
|
+
if (module.type === "backend") {
|
|
1902
|
+
this.modules.backend = module;
|
|
1903
|
+
}
|
|
1904
|
+
if (module.type === "logger" || module.log && module.warn && module.error) {
|
|
1905
|
+
this.modules.logger = module;
|
|
1906
|
+
}
|
|
1907
|
+
if (module.type === "languageDetector") {
|
|
1908
|
+
this.modules.languageDetector = module;
|
|
1909
|
+
}
|
|
1910
|
+
if (module.type === "i18nFormat") {
|
|
1911
|
+
this.modules.i18nFormat = module;
|
|
1912
|
+
}
|
|
1913
|
+
if (module.type === "postProcessor") {
|
|
1914
|
+
postProcessor.addPostProcessor(module);
|
|
1915
|
+
}
|
|
1916
|
+
if (module.type === "formatter") {
|
|
1917
|
+
this.modules.formatter = module;
|
|
1918
|
+
}
|
|
1919
|
+
if (module.type === "3rdParty") {
|
|
1920
|
+
this.modules.external.push(module);
|
|
1921
|
+
}
|
|
1922
|
+
return this;
|
|
1923
|
+
}
|
|
1924
|
+
setResolvedLanguage(l) {
|
|
1925
|
+
if (!l || !this.languages) return;
|
|
1926
|
+
if (["cimode", "dev"].indexOf(l) > -1) return;
|
|
1927
|
+
for (let li = 0; li < this.languages.length; li++) {
|
|
1928
|
+
const lngInLngs = this.languages[li];
|
|
1929
|
+
if (["cimode", "dev"].indexOf(lngInLngs) > -1) continue;
|
|
1930
|
+
if (this.store.hasLanguageSomeTranslations(lngInLngs)) {
|
|
1931
|
+
this.resolvedLanguage = lngInLngs;
|
|
1932
|
+
break;
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
if (!this.resolvedLanguage && this.languages.indexOf(l) < 0 && this.store.hasLanguageSomeTranslations(l)) {
|
|
1936
|
+
this.resolvedLanguage = l;
|
|
1937
|
+
this.languages.unshift(l);
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
changeLanguage(lng, callback) {
|
|
1941
|
+
this.isLanguageChangingTo = lng;
|
|
1942
|
+
const deferred = defer();
|
|
1943
|
+
this.emit("languageChanging", lng);
|
|
1944
|
+
const setLngProps = (l) => {
|
|
1945
|
+
this.language = l;
|
|
1946
|
+
this.languages = this.services.languageUtils.toResolveHierarchy(l);
|
|
1947
|
+
this.resolvedLanguage = void 0;
|
|
1948
|
+
this.setResolvedLanguage(l);
|
|
1949
|
+
};
|
|
1950
|
+
const done = (err, l) => {
|
|
1951
|
+
if (l) {
|
|
1952
|
+
if (this.isLanguageChangingTo === lng) {
|
|
1953
|
+
setLngProps(l);
|
|
1954
|
+
this.translator.changeLanguage(l);
|
|
1955
|
+
this.isLanguageChangingTo = void 0;
|
|
1956
|
+
this.emit("languageChanged", l);
|
|
1957
|
+
this.logger.log("languageChanged", l);
|
|
1958
|
+
}
|
|
1959
|
+
} else {
|
|
1960
|
+
this.isLanguageChangingTo = void 0;
|
|
1961
|
+
}
|
|
1962
|
+
deferred.resolve((...args) => this.t(...args));
|
|
1963
|
+
if (callback) callback(err, (...args) => this.t(...args));
|
|
1964
|
+
};
|
|
1965
|
+
const setLng = (lngs) => {
|
|
1966
|
+
if (!lng && !lngs && this.services.languageDetector) lngs = [];
|
|
1967
|
+
const fl = isString(lngs) ? lngs : lngs && lngs[0];
|
|
1968
|
+
const l = this.store.hasLanguageSomeTranslations(fl) ? fl : this.services.languageUtils.getBestMatchFromCodes(isString(lngs) ? [lngs] : lngs);
|
|
1969
|
+
if (l) {
|
|
1970
|
+
if (!this.language) {
|
|
1971
|
+
setLngProps(l);
|
|
1972
|
+
}
|
|
1973
|
+
if (!this.translator.language) this.translator.changeLanguage(l);
|
|
1974
|
+
this.services.languageDetector?.cacheUserLanguage?.(l);
|
|
1975
|
+
}
|
|
1976
|
+
this.loadResources(l, (err) => {
|
|
1977
|
+
done(err, l);
|
|
1978
|
+
});
|
|
1979
|
+
};
|
|
1980
|
+
if (!lng && this.services.languageDetector && !this.services.languageDetector.async) {
|
|
1981
|
+
setLng(this.services.languageDetector.detect());
|
|
1982
|
+
} else if (!lng && this.services.languageDetector && this.services.languageDetector.async) {
|
|
1983
|
+
if (this.services.languageDetector.detect.length === 0) {
|
|
1984
|
+
this.services.languageDetector.detect().then(setLng);
|
|
1985
|
+
} else {
|
|
1986
|
+
this.services.languageDetector.detect(setLng);
|
|
1987
|
+
}
|
|
1988
|
+
} else {
|
|
1989
|
+
setLng(lng);
|
|
1990
|
+
}
|
|
1991
|
+
return deferred;
|
|
1992
|
+
}
|
|
1993
|
+
getFixedT(lng, ns, keyPrefix) {
|
|
1994
|
+
const fixedT = (key, opts, ...rest) => {
|
|
1995
|
+
let o;
|
|
1996
|
+
if (typeof opts !== "object") {
|
|
1997
|
+
o = this.options.overloadTranslationOptionHandler([key, opts].concat(rest));
|
|
1998
|
+
} else {
|
|
1999
|
+
o = {
|
|
2000
|
+
...opts
|
|
2001
|
+
};
|
|
2002
|
+
}
|
|
2003
|
+
o.lng = o.lng || fixedT.lng;
|
|
2004
|
+
o.lngs = o.lngs || fixedT.lngs;
|
|
2005
|
+
o.ns = o.ns || fixedT.ns;
|
|
2006
|
+
if (o.keyPrefix !== "") o.keyPrefix = o.keyPrefix || keyPrefix || fixedT.keyPrefix;
|
|
2007
|
+
const keySeparator = this.options.keySeparator || ".";
|
|
2008
|
+
let resultKey;
|
|
2009
|
+
if (o.keyPrefix && Array.isArray(key)) {
|
|
2010
|
+
resultKey = key.map((k) => {
|
|
2011
|
+
if (typeof k === "function") k = keysFromSelector(k, {
|
|
2012
|
+
...this.options,
|
|
2013
|
+
...opts
|
|
2014
|
+
});
|
|
2015
|
+
return `${o.keyPrefix}${keySeparator}${k}`;
|
|
2016
|
+
});
|
|
2017
|
+
} else {
|
|
2018
|
+
if (typeof key === "function") key = keysFromSelector(key, {
|
|
2019
|
+
...this.options,
|
|
2020
|
+
...opts
|
|
2021
|
+
});
|
|
2022
|
+
resultKey = o.keyPrefix ? `${o.keyPrefix}${keySeparator}${key}` : key;
|
|
2023
|
+
}
|
|
2024
|
+
return this.t(resultKey, o);
|
|
2025
|
+
};
|
|
2026
|
+
if (isString(lng)) {
|
|
2027
|
+
fixedT.lng = lng;
|
|
2028
|
+
} else {
|
|
2029
|
+
fixedT.lngs = lng;
|
|
2030
|
+
}
|
|
2031
|
+
fixedT.ns = ns;
|
|
2032
|
+
fixedT.keyPrefix = keyPrefix;
|
|
2033
|
+
return fixedT;
|
|
2034
|
+
}
|
|
2035
|
+
t(...args) {
|
|
2036
|
+
return this.translator?.translate(...args);
|
|
2037
|
+
}
|
|
2038
|
+
exists(...args) {
|
|
2039
|
+
return this.translator?.exists(...args);
|
|
2040
|
+
}
|
|
2041
|
+
setDefaultNamespace(ns) {
|
|
2042
|
+
this.options.defaultNS = ns;
|
|
2043
|
+
}
|
|
2044
|
+
hasLoadedNamespace(ns, options = {}) {
|
|
2045
|
+
if (!this.isInitialized) {
|
|
2046
|
+
this.logger.warn("hasLoadedNamespace: i18next was not initialized", this.languages);
|
|
2047
|
+
return false;
|
|
2048
|
+
}
|
|
2049
|
+
if (!this.languages || !this.languages.length) {
|
|
2050
|
+
this.logger.warn("hasLoadedNamespace: i18n.languages were undefined or empty", this.languages);
|
|
2051
|
+
return false;
|
|
2052
|
+
}
|
|
2053
|
+
const lng = options.lng || this.resolvedLanguage || this.languages[0];
|
|
2054
|
+
const fallbackLng = this.options ? this.options.fallbackLng : false;
|
|
2055
|
+
const lastLng = this.languages[this.languages.length - 1];
|
|
2056
|
+
if (lng.toLowerCase() === "cimode") return true;
|
|
2057
|
+
const loadNotPending = (l, n) => {
|
|
2058
|
+
const loadState = this.services.backendConnector.state[`${l}|${n}`];
|
|
2059
|
+
return loadState === -1 || loadState === 0 || loadState === 2;
|
|
2060
|
+
};
|
|
2061
|
+
if (options.precheck) {
|
|
2062
|
+
const preResult = options.precheck(this, loadNotPending);
|
|
2063
|
+
if (preResult !== void 0) return preResult;
|
|
2064
|
+
}
|
|
2065
|
+
if (this.hasResourceBundle(lng, ns)) return true;
|
|
2066
|
+
if (!this.services.backendConnector.backend || this.options.resources && !this.options.partialBundledLanguages) return true;
|
|
2067
|
+
if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
|
|
2068
|
+
return false;
|
|
2069
|
+
}
|
|
2070
|
+
loadNamespaces(ns, callback) {
|
|
2071
|
+
const deferred = defer();
|
|
2072
|
+
if (!this.options.ns) {
|
|
2073
|
+
if (callback) callback();
|
|
2074
|
+
return Promise.resolve();
|
|
2075
|
+
}
|
|
2076
|
+
if (isString(ns)) ns = [ns];
|
|
2077
|
+
ns.forEach((n) => {
|
|
2078
|
+
if (this.options.ns.indexOf(n) < 0) this.options.ns.push(n);
|
|
2079
|
+
});
|
|
2080
|
+
this.loadResources((err) => {
|
|
2081
|
+
deferred.resolve();
|
|
2082
|
+
if (callback) callback(err);
|
|
2083
|
+
});
|
|
2084
|
+
return deferred;
|
|
2085
|
+
}
|
|
2086
|
+
loadLanguages(lngs, callback) {
|
|
2087
|
+
const deferred = defer();
|
|
2088
|
+
if (isString(lngs)) lngs = [lngs];
|
|
2089
|
+
const preloaded = this.options.preload || [];
|
|
2090
|
+
const newLngs = lngs.filter((lng) => preloaded.indexOf(lng) < 0 && this.services.languageUtils.isSupportedCode(lng));
|
|
2091
|
+
if (!newLngs.length) {
|
|
2092
|
+
if (callback) callback();
|
|
2093
|
+
return Promise.resolve();
|
|
2094
|
+
}
|
|
2095
|
+
this.options.preload = preloaded.concat(newLngs);
|
|
2096
|
+
this.loadResources((err) => {
|
|
2097
|
+
deferred.resolve();
|
|
2098
|
+
if (callback) callback(err);
|
|
2099
|
+
});
|
|
2100
|
+
return deferred;
|
|
2101
|
+
}
|
|
2102
|
+
dir(lng) {
|
|
2103
|
+
if (!lng) lng = this.resolvedLanguage || (this.languages?.length > 0 ? this.languages[0] : this.language);
|
|
2104
|
+
if (!lng) return "rtl";
|
|
2105
|
+
try {
|
|
2106
|
+
const l = new Intl.Locale(lng);
|
|
2107
|
+
if (l && l.getTextInfo) {
|
|
2108
|
+
const ti = l.getTextInfo();
|
|
2109
|
+
if (ti && ti.direction) return ti.direction;
|
|
2110
|
+
}
|
|
2111
|
+
} catch (e) {
|
|
2112
|
+
}
|
|
2113
|
+
const rtlLngs = ["ar", "shu", "sqr", "ssh", "xaa", "yhd", "yud", "aao", "abh", "abv", "acm", "acq", "acw", "acx", "acy", "adf", "ads", "aeb", "aec", "afb", "ajp", "apc", "apd", "arb", "arq", "ars", "ary", "arz", "auz", "avl", "ayh", "ayl", "ayn", "ayp", "bbz", "pga", "he", "iw", "ps", "pbt", "pbu", "pst", "prp", "prd", "ug", "ur", "ydd", "yds", "yih", "ji", "yi", "hbo", "men", "xmn", "fa", "jpr", "peo", "pes", "prs", "dv", "sam", "ckb"];
|
|
2114
|
+
const languageUtils = this.services?.languageUtils || new LanguageUtil(get());
|
|
2115
|
+
if (lng.toLowerCase().indexOf("-latn") > 1) return "ltr";
|
|
2116
|
+
return rtlLngs.indexOf(languageUtils.getLanguagePartFromCode(lng)) > -1 || lng.toLowerCase().indexOf("-arab") > 1 ? "rtl" : "ltr";
|
|
2117
|
+
}
|
|
2118
|
+
static createInstance(options = {}, callback) {
|
|
2119
|
+
const instance2 = new _I18n(options, callback);
|
|
2120
|
+
instance2.createInstance = _I18n.createInstance;
|
|
2121
|
+
return instance2;
|
|
2122
|
+
}
|
|
2123
|
+
cloneInstance(options = {}, callback = noop) {
|
|
2124
|
+
const forkResourceStore = options.forkResourceStore;
|
|
2125
|
+
if (forkResourceStore) delete options.forkResourceStore;
|
|
2126
|
+
const mergedOptions = {
|
|
2127
|
+
...this.options,
|
|
2128
|
+
...options,
|
|
2129
|
+
...{
|
|
2130
|
+
isClone: true
|
|
2131
|
+
}
|
|
2132
|
+
};
|
|
2133
|
+
const clone = new _I18n(mergedOptions);
|
|
2134
|
+
if (options.debug !== void 0 || options.prefix !== void 0) {
|
|
2135
|
+
clone.logger = clone.logger.clone(options);
|
|
2136
|
+
}
|
|
2137
|
+
const membersToCopy = ["store", "services", "language"];
|
|
2138
|
+
membersToCopy.forEach((m) => {
|
|
2139
|
+
clone[m] = this[m];
|
|
2140
|
+
});
|
|
2141
|
+
clone.services = {
|
|
2142
|
+
...this.services
|
|
2143
|
+
};
|
|
2144
|
+
clone.services.utils = {
|
|
2145
|
+
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
|
|
2146
|
+
};
|
|
2147
|
+
if (forkResourceStore) {
|
|
2148
|
+
const clonedData = Object.keys(this.store.data).reduce((prev, l) => {
|
|
2149
|
+
prev[l] = {
|
|
2150
|
+
...this.store.data[l]
|
|
2151
|
+
};
|
|
2152
|
+
prev[l] = Object.keys(prev[l]).reduce((acc, n) => {
|
|
2153
|
+
acc[n] = {
|
|
2154
|
+
...prev[l][n]
|
|
2155
|
+
};
|
|
2156
|
+
return acc;
|
|
2157
|
+
}, prev[l]);
|
|
2158
|
+
return prev;
|
|
2159
|
+
}, {});
|
|
2160
|
+
clone.store = new ResourceStore(clonedData, mergedOptions);
|
|
2161
|
+
clone.services.resourceStore = clone.store;
|
|
2162
|
+
}
|
|
2163
|
+
if (options.interpolation) clone.services.interpolator = new Interpolator(mergedOptions);
|
|
2164
|
+
clone.translator = new Translator(clone.services, mergedOptions);
|
|
2165
|
+
clone.translator.on("*", (event, ...args) => {
|
|
2166
|
+
clone.emit(event, ...args);
|
|
2167
|
+
});
|
|
2168
|
+
clone.init(mergedOptions, callback);
|
|
2169
|
+
clone.translator.options = mergedOptions;
|
|
2170
|
+
clone.translator.backendConnector.services.utils = {
|
|
2171
|
+
hasLoadedNamespace: clone.hasLoadedNamespace.bind(clone)
|
|
2172
|
+
};
|
|
2173
|
+
return clone;
|
|
2174
|
+
}
|
|
2175
|
+
toJSON() {
|
|
2176
|
+
return {
|
|
2177
|
+
options: this.options,
|
|
2178
|
+
store: this.store,
|
|
2179
|
+
language: this.language,
|
|
2180
|
+
languages: this.languages,
|
|
2181
|
+
resolvedLanguage: this.resolvedLanguage
|
|
2182
|
+
};
|
|
2183
|
+
}
|
|
2184
|
+
};
|
|
2185
|
+
var instance = I18n.createInstance();
|
|
2186
|
+
var createInstance = instance.createInstance;
|
|
2187
|
+
var dir = instance.dir;
|
|
2188
|
+
var init = instance.init;
|
|
2189
|
+
var loadResources = instance.loadResources;
|
|
2190
|
+
var reloadResources = instance.reloadResources;
|
|
2191
|
+
var use = instance.use;
|
|
2192
|
+
var changeLanguage = instance.changeLanguage;
|
|
2193
|
+
var getFixedT = instance.getFixedT;
|
|
2194
|
+
var t = instance.t;
|
|
2195
|
+
var exists = instance.exists;
|
|
2196
|
+
var setDefaultNamespace = instance.setDefaultNamespace;
|
|
2197
|
+
var hasLoadedNamespace = instance.hasLoadedNamespace;
|
|
2198
|
+
var loadNamespaces = instance.loadNamespaces;
|
|
2199
|
+
var loadLanguages = instance.loadLanguages;
|
|
2200
|
+
|
|
2201
|
+
// src/locale/en/locale-resources.ts
|
|
2202
|
+
var locale_resources_default = {
|
|
2203
|
+
Transformer: {
|
|
2204
|
+
domainMustBeGreaterThanZero: "Domain {{domain, number}} must be greater than 0",
|
|
2205
|
+
tweakMustBeGreaterThanOrEqualToZero: "Tweak {{tweak, number}} must be greater than or equal to 0",
|
|
2206
|
+
valueMustBeGreaterThanOrEqualToZero: "Value {{value, number}} must be greater than or equal to 0",
|
|
2207
|
+
valueMustBeLessThan: "Value {{value, number}} must be less than {{domain, number}}",
|
|
2208
|
+
minimumValueMustBeGreaterThanOrEqualToZero: "Minimum value {{minimumValue, number}} must be greater than or equal to 0",
|
|
2209
|
+
maximumValueMustBeLessThan: "Maximum value {{maximumValue, number}} must be less than {{domain, number}}"
|
|
2210
|
+
},
|
|
2211
|
+
RegExpValidator: {
|
|
2212
|
+
stringDoesNotMatchPattern: "String {{s}} does not match pattern"
|
|
2213
|
+
},
|
|
2214
|
+
CharacterSetValidator: {
|
|
2215
|
+
firstZeroFirstCharacter: "Character set must support zero as first character",
|
|
2216
|
+
allNumericAllNumericCharacters: "Character set must support all numeric characters in sequence",
|
|
2217
|
+
stringMustNotBeAllNumeric: "String must not be all numeric",
|
|
2218
|
+
lengthMustBeGreaterThanOrEqualTo: "Length {{length, number}} must be greater than or equal to {{minimumLength, number}}",
|
|
2219
|
+
lengthMustBeLessThanOrEqualTo: "Length {{length, number}} must be less than or equal to {{maximumLength, number}}",
|
|
2220
|
+
lengthMustBeEqualTo: "Length {{length, number}} must be equal to {{exactLength, number}}",
|
|
2221
|
+
lengthOfComponentMustBeGreaterThanOrEqualTo: "Length {{length, number}} of {{component}} must be greater than or equal to {{minimumLength, number}}",
|
|
2222
|
+
lengthOfComponentMustBeLessThanOrEqualTo: "Length {{length, number}} of {{component}} must be less than or equal to {{maximumLength, number}}",
|
|
2223
|
+
lengthOfComponentMustBeEqualTo: "Length {{length, number}} of {{component}} must be equal to {{exactLength, number}}",
|
|
2224
|
+
invalidCharacterAtPosition: "Invalid character '{{c}}' at position {{position, number}}",
|
|
2225
|
+
invalidCharacterAtPositionOfComponent: "Invalid character '{{c}}' at position {{position, number}} of {{component}}",
|
|
2226
|
+
exclusionNotSupported: "Exclusion value of {{exclusion, number}} is not supported",
|
|
2227
|
+
endSequenceValueMustBeLessThanOrEqualTo: "End sequence value (start sequence value + count - 1) must be less than {{domain, number}}"
|
|
2228
|
+
},
|
|
2229
|
+
RecordValidator: {
|
|
2230
|
+
typeNameKeyNotFound: '{{typeName}} "{{key}}" not found'
|
|
2231
|
+
}
|
|
2232
|
+
};
|
|
2233
|
+
|
|
2234
|
+
// src/locale/fr/locale-resources.ts
|
|
2235
|
+
var locale_resources_default2 = {
|
|
2236
|
+
Transformer: {
|
|
2237
|
+
domainMustBeGreaterThanZero: "Le domaine {{domain, number}} doit \xEAtre sup\xE9rieur \xE0 0",
|
|
2238
|
+
tweakMustBeGreaterThanOrEqualToZero: "Le r\xE9glage {{tweak, number}} doit \xEAtre sup\xE9rieur ou \xE9gal \xE0 0",
|
|
2239
|
+
valueMustBeGreaterThanOrEqualToZero: "La valeur {{value, number}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 0",
|
|
2240
|
+
valueMustBeLessThan: "La valeur {{value, number}} doit \xEAtre inf\xE9rieure \xE0 {{domain, number}}",
|
|
2241
|
+
minimumValueMustBeGreaterThanOrEqualToZero: "La valeur minimale {{minimumValue, number}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 0",
|
|
2242
|
+
maximumValueMustBeLessThan: "La valeur maximale {{maximumValue, number}} doit \xEAtre inf\xE9rieure \xE0 {{domain, number}}"
|
|
2243
|
+
},
|
|
2244
|
+
RegExpValidator: {
|
|
2245
|
+
stringDoesNotMatchPattern: "La cha\xEEne {{s}} ne correspond pas au mod\xE8le"
|
|
2246
|
+
},
|
|
2247
|
+
CharacterSetValidator: {
|
|
2248
|
+
firstZeroFirstCharacter: "Le jeu de caract\xE8res doit prendre en charge z\xE9ro comme premier caract\xE8re",
|
|
2249
|
+
allNumericAllNumericCharacters: "Le jeu de caract\xE8res doit prendre en charge tous les caract\xE8res num\xE9riques en s\xE9quence",
|
|
2250
|
+
stringMustNotBeAllNumeric: "La cha\xEEne ne doit pas \xEAtre enti\xE8rement num\xE9rique",
|
|
2251
|
+
lengthMustBeGreaterThanOrEqualTo: "La longueur {{length, number}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 {{minimumLength, number}}",
|
|
2252
|
+
lengthMustBeLessThanOrEqualTo: "La longueur {{length, number}} doit \xEAtre inf\xE9rieure ou \xE9gale \xE0 {{maximumLength, number}}",
|
|
2253
|
+
lengthMustBeEqualTo: "La longueur {{length, number}} doit \xEAtre \xE9gale \xE0 {{exactLength, number}}",
|
|
2254
|
+
lengthOfComponentMustBeGreaterThanOrEqualTo: "La longueur {{length, number}} de {{component}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 {{minimumLength, number}}",
|
|
2255
|
+
lengthOfComponentMustBeLessThanOrEqualTo: "La longueur {{length, number}} de {{component}} doit \xEAtre inf\xE9rieure ou \xE9gale \xE0 {{maximumLength, number}}",
|
|
2256
|
+
lengthOfComponentMustBeEqualTo: "La longueur {{length, number}} de {{component}} doit \xEAtre \xE9gale \xE0 {{exactLength, number}}",
|
|
2257
|
+
invalidCharacterAtPosition: "Caract\xE8re non valide '{{c}}' \xE0 la position {{position, number}}",
|
|
2258
|
+
invalidCharacterAtPositionOfComponent: "Caract\xE8re non valide '{{c}}' \xE0 la position {{position, number}} de {{component}}",
|
|
2259
|
+
exclusionNotSupported: "La valeur d'exclusion de {{exclusion, number}} n'est pas prise en charge",
|
|
2260
|
+
endSequenceValueMustBeLessThanOrEqualTo: "La valeur de la s\xE9quence de fin (valeur de la s\xE9quence de d\xE9but + nombre - 1) doit \xEAtre inf\xE9rieure \xE0 {{domain, number}}"
|
|
2261
|
+
},
|
|
2262
|
+
RecordValidator: {
|
|
2263
|
+
typeNameKeyNotFound: '{{typeName}} "{{key}}" introuvable'
|
|
2264
|
+
}
|
|
2265
|
+
};
|
|
2266
|
+
|
|
2267
|
+
// src/locale/i18n.ts
|
|
2268
|
+
var utilityNS = "aidct_utility";
|
|
2269
|
+
var utilityResources = {
|
|
2270
|
+
en: {
|
|
2271
|
+
aidct_utility: locale_resources_default
|
|
2272
|
+
},
|
|
2273
|
+
fr: {
|
|
2274
|
+
aidct_utility: locale_resources_default2
|
|
2275
|
+
}
|
|
2276
|
+
};
|
|
2277
|
+
var i18nextUtility = instance.createInstance();
|
|
2278
|
+
async function i18nUtilityInit(environment, debug = false) {
|
|
2279
|
+
await i18nCoreInit(i18nextUtility, environment, debug, utilityNS, utilityResources);
|
|
2280
|
+
}
|
|
2281
|
+
|
|
2282
|
+
// src/sequence.ts
|
|
2283
|
+
var Sequence = class {
|
|
2284
|
+
/**
|
|
2285
|
+
* Start value (inclusive).
|
|
2286
|
+
*/
|
|
2287
|
+
_startValue;
|
|
2288
|
+
/**
|
|
2289
|
+
* End value (exclusive).
|
|
2290
|
+
*/
|
|
2291
|
+
_endValue;
|
|
2292
|
+
/**
|
|
2293
|
+
* Count of values.
|
|
2294
|
+
*/
|
|
2295
|
+
_count;
|
|
2296
|
+
/**
|
|
2297
|
+
* Delta to the next value; equal to the sign of the count.
|
|
2298
|
+
*/
|
|
2299
|
+
_nextDelta;
|
|
2300
|
+
/**
|
|
2301
|
+
* Minimum value (inclusive).
|
|
2302
|
+
*/
|
|
2303
|
+
_minimumValue;
|
|
2304
|
+
/**
|
|
2305
|
+
* Maximum value (inclusive).
|
|
2306
|
+
*/
|
|
2307
|
+
_maximumValue;
|
|
2308
|
+
/**
|
|
2309
|
+
* Constructor.
|
|
2310
|
+
*
|
|
2311
|
+
* @param startValue
|
|
2312
|
+
* Start value.
|
|
2313
|
+
*
|
|
2314
|
+
* @param count
|
|
2315
|
+
* Count of values. If count is zero or positive, iteration ascends from start value, otherwise it descends from
|
|
2316
|
+
* start value.
|
|
2317
|
+
*/
|
|
2318
|
+
constructor(startValue, count) {
|
|
2319
|
+
this._startValue = BigInt(startValue);
|
|
2320
|
+
this._endValue = this._startValue + BigInt(count);
|
|
2321
|
+
this._count = count;
|
|
2322
|
+
if (count >= 0) {
|
|
2323
|
+
this._nextDelta = 1n;
|
|
2324
|
+
this._minimumValue = this._startValue;
|
|
2325
|
+
this._maximumValue = this._endValue - 1n;
|
|
2326
|
+
} else {
|
|
2327
|
+
this._nextDelta = -1n;
|
|
2328
|
+
this._minimumValue = this._endValue + 1n;
|
|
2329
|
+
this._maximumValue = this._startValue;
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
/**
|
|
2333
|
+
* Get the start value (inclusive).
|
|
2334
|
+
*/
|
|
2335
|
+
get startValue() {
|
|
2336
|
+
return this._startValue;
|
|
2337
|
+
}
|
|
2338
|
+
/**
|
|
2339
|
+
* Get the end value (exclusive).
|
|
2340
|
+
*/
|
|
2341
|
+
get endValue() {
|
|
2342
|
+
return this._endValue;
|
|
2343
|
+
}
|
|
2344
|
+
/**
|
|
2345
|
+
* Get the count of values.
|
|
2346
|
+
*/
|
|
2347
|
+
get count() {
|
|
2348
|
+
return this._count;
|
|
2349
|
+
}
|
|
2350
|
+
/**
|
|
2351
|
+
* Get the minimum value (inclusive).
|
|
2352
|
+
*/
|
|
2353
|
+
get minimumValue() {
|
|
2354
|
+
return this._minimumValue;
|
|
2355
|
+
}
|
|
2356
|
+
/**
|
|
2357
|
+
* Get the maximum value (inclusive).
|
|
2358
|
+
*/
|
|
2359
|
+
get maximumValue() {
|
|
2360
|
+
return this._maximumValue;
|
|
2361
|
+
}
|
|
2362
|
+
/**
|
|
2363
|
+
* Iterable implementation.
|
|
2364
|
+
*
|
|
2365
|
+
* @yields
|
|
2366
|
+
* Next value in sequence.
|
|
2367
|
+
*/
|
|
2368
|
+
*[Symbol.iterator]() {
|
|
2369
|
+
for (let value = this._startValue; value !== this._endValue; value += this._nextDelta) {
|
|
2370
|
+
yield value;
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2373
|
+
};
|
|
2374
|
+
|
|
2375
|
+
// src/iterable-utility.ts
|
|
2376
|
+
function mapIterable(values, indexedCallback) {
|
|
2377
|
+
return {
|
|
2378
|
+
/**
|
|
2379
|
+
* Iterable implementation.
|
|
2380
|
+
*
|
|
2381
|
+
* @yields
|
|
2382
|
+
* Next output value.
|
|
2383
|
+
*/
|
|
2384
|
+
*[Symbol.iterator]() {
|
|
2385
|
+
let index = 0;
|
|
2386
|
+
for (const value of values) {
|
|
2387
|
+
yield indexedCallback(value, index++);
|
|
2388
|
+
}
|
|
2389
|
+
}
|
|
2390
|
+
};
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
// src/transformer.ts
|
|
2394
|
+
var Transformer = class _Transformer {
|
|
2395
|
+
/**
|
|
2396
|
+
* Transformers cache, mapping a domain to another map, which maps an optional tweak to a transformer.
|
|
2397
|
+
*/
|
|
2398
|
+
static TRANSFORMER_MAPS_MAP = /* @__PURE__ */ new Map();
|
|
2399
|
+
/**
|
|
2400
|
+
* Domain.
|
|
2401
|
+
*/
|
|
2402
|
+
_domain;
|
|
2403
|
+
/**
|
|
2404
|
+
* Constructor.
|
|
2405
|
+
*
|
|
2406
|
+
* @param domain
|
|
2407
|
+
* Domain.
|
|
2408
|
+
*/
|
|
2409
|
+
constructor(domain) {
|
|
2410
|
+
this._domain = BigInt(domain);
|
|
2411
|
+
if (this._domain <= 0n) {
|
|
2412
|
+
throw new RangeError(i18nextUtility.t("Transformer.domainMustBeGreaterThanZero", {
|
|
2413
|
+
domain
|
|
2414
|
+
}));
|
|
2415
|
+
}
|
|
2416
|
+
}
|
|
2417
|
+
/**
|
|
2418
|
+
* Get a transformer, constructing it if necessary. The type returned is {@linkcode IdentityTransformer} if tweak is
|
|
2419
|
+
* undefined, {@linkcode EncryptionTransformer} if tweak is defined. Note that although an {@linkcode
|
|
2420
|
+
* EncryptionTransformer} with a zero tweak operates as an {@linkcode IdentityTransformer}, {@linkcode
|
|
2421
|
+
* EncryptionTransformer} is still the type returned if a zero tweak is explicitly specified.
|
|
2422
|
+
*
|
|
2423
|
+
* @param domain
|
|
2424
|
+
* Domain.
|
|
2425
|
+
*
|
|
2426
|
+
* @param tweak
|
|
2427
|
+
* Tweak.
|
|
2428
|
+
*
|
|
2429
|
+
* @returns
|
|
2430
|
+
* {@linkcode IdentityTransformer} if tweak is undefined, {@linkcode EncryptionTransformer} if tweak is defined.
|
|
2431
|
+
*/
|
|
2432
|
+
static get(domain, tweak) {
|
|
2433
|
+
const domainN = BigInt(domain);
|
|
2434
|
+
let transformersMap = _Transformer.TRANSFORMER_MAPS_MAP.get(domainN);
|
|
2435
|
+
if (transformersMap === void 0) {
|
|
2436
|
+
transformersMap = /* @__PURE__ */ new Map();
|
|
2437
|
+
_Transformer.TRANSFORMER_MAPS_MAP.set(domainN, transformersMap);
|
|
2438
|
+
}
|
|
2439
|
+
const tweakN = tweak === void 0 ? void 0 : BigInt(tweak);
|
|
2440
|
+
let transformer = transformersMap.get(tweakN);
|
|
2441
|
+
if (transformer === void 0) {
|
|
2442
|
+
transformer = tweakN === void 0 ? new IdentityTransformer(domainN) : new EncryptionTransformer(domainN, tweakN);
|
|
2443
|
+
transformersMap.set(tweakN, transformer);
|
|
2444
|
+
}
|
|
2445
|
+
return transformer;
|
|
2446
|
+
}
|
|
2447
|
+
/**
|
|
2448
|
+
* Get the domain.
|
|
2449
|
+
*/
|
|
2450
|
+
get domain() {
|
|
2451
|
+
return this._domain;
|
|
2452
|
+
}
|
|
2453
|
+
/**
|
|
2454
|
+
* Validate that a value is within the domain.
|
|
2455
|
+
*
|
|
2456
|
+
* @param value
|
|
2457
|
+
* Value.
|
|
2458
|
+
*/
|
|
2459
|
+
validate(value) {
|
|
2460
|
+
if (value < 0n) {
|
|
2461
|
+
throw new RangeError(i18nextUtility.t("Transformer.valueMustBeGreaterThanOrEqualToZero", {
|
|
2462
|
+
value
|
|
2463
|
+
}));
|
|
2464
|
+
}
|
|
2465
|
+
if (value >= this.domain) {
|
|
2466
|
+
throw new RangeError(i18nextUtility.t("Transformer.valueMustBeLessThan", {
|
|
2467
|
+
value,
|
|
2468
|
+
domain: this.domain
|
|
2469
|
+
}));
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
/**
|
|
2473
|
+
* Validate that a value is within the domain and do the work of transforming it forward.
|
|
2474
|
+
*
|
|
2475
|
+
* @param value
|
|
2476
|
+
* Value.
|
|
2477
|
+
*
|
|
2478
|
+
* @returns
|
|
2479
|
+
* Transformed value.
|
|
2480
|
+
*/
|
|
2481
|
+
validateDoForward(value) {
|
|
2482
|
+
const valueN = BigInt(value);
|
|
2483
|
+
this.validate(valueN);
|
|
2484
|
+
return this.doForward(valueN);
|
|
2485
|
+
}
|
|
2486
|
+
/**
|
|
2487
|
+
* Validate that a value is within the domain, do the work of transforming it forward, and apply a callback.
|
|
2488
|
+
*
|
|
2489
|
+
* @param transformerCallback
|
|
2490
|
+
* Called after each value is transformed to convert it to its final value.
|
|
2491
|
+
*
|
|
2492
|
+
* @param value
|
|
2493
|
+
* Value.
|
|
2494
|
+
*
|
|
2495
|
+
* @param index
|
|
2496
|
+
* Index in sequence (0 for single transformation).
|
|
2497
|
+
*
|
|
2498
|
+
* @returns
|
|
2499
|
+
* Transformed value.
|
|
2500
|
+
*/
|
|
2501
|
+
validateDoForwardCallback(transformerCallback, value, index) {
|
|
2502
|
+
return transformerCallback(this.validateDoForward(value), index);
|
|
2503
|
+
}
|
|
2504
|
+
// eslint-disable-next-line jsdoc/require-jsdoc -- Implementation of overloaded signatures.
|
|
2505
|
+
forward(valueOrValues, transformerCallback) {
|
|
2506
|
+
let result;
|
|
2507
|
+
if (typeof valueOrValues !== "object") {
|
|
2508
|
+
result = transformerCallback === void 0 ? this.validateDoForward(valueOrValues) : this.validateDoForwardCallback(transformerCallback, valueOrValues);
|
|
2509
|
+
} else if (valueOrValues instanceof Sequence) {
|
|
2510
|
+
if (valueOrValues.minimumValue < 0n) {
|
|
2511
|
+
throw new RangeError(i18nextUtility.t("Transformer.minimumValueMustBeGreaterThanOrEqualToZero", {
|
|
2512
|
+
minimumValue: valueOrValues.minimumValue
|
|
2513
|
+
}));
|
|
2514
|
+
}
|
|
2515
|
+
if (valueOrValues.maximumValue >= this.domain) {
|
|
2516
|
+
throw new RangeError(i18nextUtility.t("Transformer.maximumValueMustBeLessThan", {
|
|
2517
|
+
maximumValue: valueOrValues.maximumValue,
|
|
2518
|
+
domain: this.domain
|
|
2519
|
+
}));
|
|
2520
|
+
}
|
|
2521
|
+
result = transformerCallback === void 0 ? mapIterable(valueOrValues, (value) => this.doForward(value)) : mapIterable(valueOrValues, (value, index) => transformerCallback(this.doForward(value), index));
|
|
2522
|
+
} else {
|
|
2523
|
+
result = transformerCallback === void 0 ? mapIterable(valueOrValues, (value) => this.validateDoForward(value)) : mapIterable(valueOrValues, (value, index) => this.validateDoForwardCallback(transformerCallback, value, index));
|
|
2524
|
+
}
|
|
2525
|
+
return result;
|
|
2526
|
+
}
|
|
2527
|
+
/**
|
|
2528
|
+
* Transform a value in reverse.
|
|
2529
|
+
*
|
|
2530
|
+
* @param transformedValue
|
|
2531
|
+
* Transformed value.
|
|
2532
|
+
*
|
|
2533
|
+
* @returns
|
|
2534
|
+
* Value.
|
|
2535
|
+
*/
|
|
2536
|
+
reverse(transformedValue) {
|
|
2537
|
+
const transformedValueN = BigInt(transformedValue);
|
|
2538
|
+
this.validate(transformedValueN);
|
|
2539
|
+
return this.doReverse(transformedValueN);
|
|
2540
|
+
}
|
|
2541
|
+
};
|
|
2542
|
+
var IdentityTransformer = class extends Transformer {
|
|
2543
|
+
/**
|
|
2544
|
+
* @inheritDoc
|
|
2545
|
+
*/
|
|
2546
|
+
doForward(value) {
|
|
2547
|
+
return value;
|
|
2548
|
+
}
|
|
2549
|
+
/**
|
|
2550
|
+
* @inheritDoc
|
|
2551
|
+
*/
|
|
2552
|
+
doReverse(transformedValue) {
|
|
2553
|
+
return transformedValue;
|
|
2554
|
+
}
|
|
2555
|
+
};
|
|
2556
|
+
var EncryptionTransformer = class _EncryptionTransformer extends Transformer {
|
|
2557
|
+
/**
|
|
2558
|
+
* Individual bits, pre-calculated for performance.
|
|
2559
|
+
*/
|
|
2560
|
+
static BITS = new Uint8Array([
|
|
2561
|
+
1,
|
|
2562
|
+
2,
|
|
2563
|
+
4,
|
|
2564
|
+
8,
|
|
2565
|
+
16,
|
|
2566
|
+
32,
|
|
2567
|
+
64,
|
|
2568
|
+
128
|
|
2569
|
+
]);
|
|
2570
|
+
/**
|
|
2571
|
+
* Inverse individual bits, pre-calculated for performance.
|
|
2572
|
+
*/
|
|
2573
|
+
static INVERSE_BITS = new Uint8Array([
|
|
2574
|
+
254,
|
|
2575
|
+
253,
|
|
2576
|
+
251,
|
|
2577
|
+
247,
|
|
2578
|
+
239,
|
|
2579
|
+
223,
|
|
2580
|
+
191,
|
|
2581
|
+
127
|
|
2582
|
+
]);
|
|
2583
|
+
/**
|
|
2584
|
+
* Number of bytes covered by the domain.
|
|
2585
|
+
*/
|
|
2586
|
+
_domainBytes;
|
|
2587
|
+
/**
|
|
2588
|
+
* Xor bytes array generated from the domain and tweak.
|
|
2589
|
+
*/
|
|
2590
|
+
_xorBytes;
|
|
2591
|
+
/**
|
|
2592
|
+
* Bits array generated from the domain and tweak.
|
|
2593
|
+
*/
|
|
2594
|
+
_bits;
|
|
2595
|
+
/**
|
|
2596
|
+
* Inverse bits array generated from the domain and tweak.
|
|
2597
|
+
*/
|
|
2598
|
+
_inverseBits;
|
|
2599
|
+
/**
|
|
2600
|
+
* Number of rounds (length of arrays) generated from the domain and tweak.
|
|
2601
|
+
*/
|
|
2602
|
+
_rounds;
|
|
2603
|
+
/**
|
|
2604
|
+
* Constructor.
|
|
2605
|
+
*
|
|
2606
|
+
* @param domain
|
|
2607
|
+
* Domain.
|
|
2608
|
+
*
|
|
2609
|
+
* @param tweak
|
|
2610
|
+
* Tweak.
|
|
2611
|
+
*/
|
|
2612
|
+
constructor(domain, tweak) {
|
|
2613
|
+
super(domain);
|
|
2614
|
+
if (tweak < 0n) {
|
|
2615
|
+
throw new RangeError(i18nextUtility.t("Transformer.tweakMustBeGreaterThanOrEqualToZero", {
|
|
2616
|
+
tweak
|
|
2617
|
+
}));
|
|
2618
|
+
}
|
|
2619
|
+
let domainBytes = 0;
|
|
2620
|
+
for (let reducedDomainMinusOne = this.domain - 1n; reducedDomainMinusOne !== 0n; reducedDomainMinusOne >>= 8n) {
|
|
2621
|
+
domainBytes++;
|
|
2622
|
+
}
|
|
2623
|
+
this._domainBytes = domainBytes;
|
|
2624
|
+
const xorBytes = new Array();
|
|
2625
|
+
const bits = new Array();
|
|
2626
|
+
const inverseBits = new Array();
|
|
2627
|
+
for (let reducedKey = this.domain * BigInt(tweak) * 603868999n; reducedKey !== 0n; reducedKey >>= 8n) {
|
|
2628
|
+
xorBytes.unshift(Number(BigInt.asUintN(8, reducedKey)));
|
|
2629
|
+
const bitNumber = Number(BigInt.asUintN(3, reducedKey));
|
|
2630
|
+
bits.push(_EncryptionTransformer.BITS[bitNumber]);
|
|
2631
|
+
inverseBits.push(_EncryptionTransformer.INVERSE_BITS[bitNumber]);
|
|
2632
|
+
}
|
|
2633
|
+
if (domainBytes === 1) {
|
|
2634
|
+
const domainMask = _EncryptionTransformer.BITS.filter((bit) => bit < domain).reduce((accumulator, bit) => accumulator | bit, 0);
|
|
2635
|
+
this._xorBytes = new Uint8Array([xorBytes.reduce((accumulator, xorByte) => accumulator ^ xorByte, 0) & domainMask]);
|
|
2636
|
+
this._bits = new Uint8Array([_EncryptionTransformer.BITS[0]]);
|
|
2637
|
+
this._inverseBits = new Uint8Array([_EncryptionTransformer.INVERSE_BITS[0]]);
|
|
2638
|
+
this._rounds = 1;
|
|
2639
|
+
} else {
|
|
2640
|
+
this._xorBytes = new Uint8Array(xorBytes);
|
|
2641
|
+
this._bits = new Uint8Array(bits);
|
|
2642
|
+
this._inverseBits = new Uint8Array(inverseBits);
|
|
2643
|
+
this._rounds = xorBytes.length;
|
|
2644
|
+
}
|
|
2645
|
+
}
|
|
2646
|
+
/**
|
|
2647
|
+
* Convert a value to a byte array big enough to handle the entire domain.
|
|
2648
|
+
*
|
|
2649
|
+
* @param value
|
|
2650
|
+
* Value.
|
|
2651
|
+
*
|
|
2652
|
+
* @returns
|
|
2653
|
+
* Big-endian byte array equivalent to the value.
|
|
2654
|
+
*/
|
|
2655
|
+
valueToBytes(value) {
|
|
2656
|
+
const bytes = new Uint8Array(this._domainBytes);
|
|
2657
|
+
for (let index = this._domainBytes - 1, reducedValue = value; index >= 0 && reducedValue !== 0n; index--, reducedValue >>= 8n) {
|
|
2658
|
+
bytes[index] = Number(BigInt.asUintN(8, reducedValue));
|
|
2659
|
+
}
|
|
2660
|
+
return bytes;
|
|
2661
|
+
}
|
|
2662
|
+
/**
|
|
2663
|
+
* Convert a byte array to a value.
|
|
2664
|
+
*
|
|
2665
|
+
* @param bytes
|
|
2666
|
+
* Big-endian byte array equivalent to the value.
|
|
2667
|
+
*
|
|
2668
|
+
* @returns
|
|
2669
|
+
* Value.
|
|
2670
|
+
*/
|
|
2671
|
+
static bytesToValue(bytes) {
|
|
2672
|
+
return bytes.reduce((accumulator, byte) => accumulator << 8n | BigInt(byte), 0n);
|
|
2673
|
+
}
|
|
2674
|
+
/**
|
|
2675
|
+
* Shuffle a byte array.
|
|
2676
|
+
*
|
|
2677
|
+
* The input array to the forward operation (output from the reverse operation) is `bytes` and the output array from
|
|
2678
|
+
* the forward operation (input to the reverse operation) is `bytes'`.
|
|
2679
|
+
*
|
|
2680
|
+
* The shuffle operation starts by testing the bit at `bits[round]` for each `byte` in `bytes`. The indexes for all
|
|
2681
|
+
* bytes with that bit set are put into one array (`shuffleIndexes1`) and the rest are put into another
|
|
2682
|
+
* (`shuffleIndexes0`). The two arrays are concatenated and used to shuffle the input array, using their values
|
|
2683
|
+
* (`shuffleIndex`) and the indexes of those values (`index`) in the concatenated array.
|
|
2684
|
+
*
|
|
2685
|
+
* Forward shuffling moves the entry at `shuffleIndex` to the `index` position.
|
|
2686
|
+
*
|
|
2687
|
+
* Reverse shuffling moves the entry at `index` to the `shuffleIndex` position.
|
|
2688
|
+
*
|
|
2689
|
+
* As each byte is moved, the bit at `bits[round]` is preserved in its original position. This ensures that the
|
|
2690
|
+
* process is reversible.
|
|
2691
|
+
*
|
|
2692
|
+
* @param bytes
|
|
2693
|
+
* Byte array.
|
|
2694
|
+
*
|
|
2695
|
+
* @param round
|
|
2696
|
+
* Round number.
|
|
2697
|
+
*
|
|
2698
|
+
* @param forward
|
|
2699
|
+
* True if operating forward (encrypting), false if operating in reverse (decrypting).
|
|
2700
|
+
*
|
|
2701
|
+
* @returns
|
|
2702
|
+
* Shuffled byte array.
|
|
2703
|
+
*/
|
|
2704
|
+
shuffle(bytes, round, forward) {
|
|
2705
|
+
const bytesLength = bytes.length;
|
|
2706
|
+
const determinants = new Uint8Array(bytesLength);
|
|
2707
|
+
const shuffleIndexes1 = new Array();
|
|
2708
|
+
const shuffleIndexes0 = new Array();
|
|
2709
|
+
const bit = this._bits[round];
|
|
2710
|
+
bytes.forEach((byte, index) => {
|
|
2711
|
+
const determinant = byte & bit;
|
|
2712
|
+
determinants[index] = determinant;
|
|
2713
|
+
(determinant !== 0 ? shuffleIndexes1 : shuffleIndexes0).push(index);
|
|
2714
|
+
});
|
|
2715
|
+
const inverseBit = this._inverseBits[round];
|
|
2716
|
+
const shuffleBytes = new Uint8Array(bytesLength);
|
|
2717
|
+
[...shuffleIndexes1, ...shuffleIndexes0].forEach((shuffleIndex, index) => {
|
|
2718
|
+
if (forward) {
|
|
2719
|
+
shuffleBytes[index] = bytes[shuffleIndex] & inverseBit | determinants[index];
|
|
2720
|
+
} else {
|
|
2721
|
+
shuffleBytes[shuffleIndex] = bytes[index] & inverseBit | determinants[shuffleIndex];
|
|
2722
|
+
}
|
|
2723
|
+
});
|
|
2724
|
+
return shuffleBytes;
|
|
2725
|
+
}
|
|
2726
|
+
/**
|
|
2727
|
+
* Xor a byte array.
|
|
2728
|
+
*
|
|
2729
|
+
* The input array to the forward operation (output from the reverse operation) is `bytes` and the output array from
|
|
2730
|
+
* the forward operation (input to the reverse operation) is `bytes'`.
|
|
2731
|
+
*
|
|
2732
|
+
* Forward:
|
|
2733
|
+
* - `bytes'[0] = bytes[0] ^ xorBytes[round]`
|
|
2734
|
+
* - `bytes'[1] = bytes[1] ^ bytes'[0]`
|
|
2735
|
+
* - `bytes'[2] = bytes[2] ^ bytes'[1]`
|
|
2736
|
+
* - `...`
|
|
2737
|
+
* - `bytes'[domainBytes - 1] = bytes[domainBytes - 1] ^ bytes'[domainBytes - 2]`
|
|
2738
|
+
*
|
|
2739
|
+
* Reverse:
|
|
2740
|
+
* - `bytes[0] = bytes'[0] ^ xorBytes[round]`
|
|
2741
|
+
* - `bytes[1] = bytes'[1] ^ bytes'[0]`
|
|
2742
|
+
* - `bytes[2] = bytes'[2] ^ bytes'[1]`
|
|
2743
|
+
* - `...`
|
|
2744
|
+
* - `bytes[domainBytes - 1] = bytes'[domainBytes - 1] ^ bytes'[domainBytes - 2]`
|
|
2745
|
+
*
|
|
2746
|
+
* @param bytes
|
|
2747
|
+
* Byte array.
|
|
2748
|
+
*
|
|
2749
|
+
* @param round
|
|
2750
|
+
* Round number.
|
|
2751
|
+
*
|
|
2752
|
+
* @param forward
|
|
2753
|
+
* True if operating forward (encrypting), false if operating in reverse (decrypting).
|
|
2754
|
+
*
|
|
2755
|
+
* @returns
|
|
2756
|
+
* Xored byte array.
|
|
2757
|
+
*/
|
|
2758
|
+
xor(bytes, round, forward) {
|
|
2759
|
+
let cumulativeXorByte = this._xorBytes[round];
|
|
2760
|
+
return bytes.map((byte) => {
|
|
2761
|
+
const xorByte = byte ^ cumulativeXorByte;
|
|
2762
|
+
cumulativeXorByte = forward ? xorByte : byte;
|
|
2763
|
+
return xorByte;
|
|
2764
|
+
});
|
|
2765
|
+
}
|
|
2766
|
+
/**
|
|
2767
|
+
* @inheritDoc
|
|
2768
|
+
*/
|
|
2769
|
+
doForward(value) {
|
|
2770
|
+
let bytes = this.valueToBytes(value);
|
|
2771
|
+
let transformedValue;
|
|
2772
|
+
do {
|
|
2773
|
+
for (let round = 0; round < this._rounds; round++) {
|
|
2774
|
+
bytes = this.xor(this.shuffle(bytes, round, true), round, true);
|
|
2775
|
+
}
|
|
2776
|
+
transformedValue = _EncryptionTransformer.bytesToValue(bytes);
|
|
2777
|
+
} while (transformedValue >= this.domain);
|
|
2778
|
+
return transformedValue;
|
|
2779
|
+
}
|
|
2780
|
+
/**
|
|
2781
|
+
* @inheritDoc
|
|
2782
|
+
*/
|
|
2783
|
+
doReverse(transformedValue) {
|
|
2784
|
+
let bytes = this.valueToBytes(transformedValue);
|
|
2785
|
+
let value;
|
|
2786
|
+
do {
|
|
2787
|
+
for (let round = this._rounds - 1; round >= 0; round--) {
|
|
2788
|
+
bytes = this.shuffle(this.xor(bytes, round, false), round, false);
|
|
2789
|
+
}
|
|
2790
|
+
value = _EncryptionTransformer.bytesToValue(bytes);
|
|
2791
|
+
} while (value >= this.domain);
|
|
2792
|
+
return value;
|
|
2793
|
+
}
|
|
2794
|
+
};
|
|
2795
|
+
|
|
2796
|
+
// src/reg-exp.ts
|
|
2797
|
+
var RegExpValidator = class {
|
|
2798
|
+
/**
|
|
2799
|
+
* Regular expression.
|
|
2800
|
+
*/
|
|
2801
|
+
_regExp;
|
|
2802
|
+
/**
|
|
2803
|
+
* Constructor.
|
|
2804
|
+
*
|
|
2805
|
+
* @param regExp
|
|
2806
|
+
* Regular expression. See {@link RegExpValidator | class documentation} for notes.
|
|
2807
|
+
*/
|
|
2808
|
+
constructor(regExp) {
|
|
2809
|
+
this._regExp = regExp;
|
|
2810
|
+
}
|
|
2811
|
+
/**
|
|
2812
|
+
* Get the regular expression.
|
|
2813
|
+
*/
|
|
2814
|
+
get regExp() {
|
|
2815
|
+
return this._regExp;
|
|
2816
|
+
}
|
|
2817
|
+
/**
|
|
2818
|
+
* Create an error message for a string. The generic error message is sufficient for many use cases but a more
|
|
2819
|
+
* domain-specific error message, possibly including the pattern itself, is often required.
|
|
2820
|
+
*
|
|
2821
|
+
* @param s
|
|
2822
|
+
* String.
|
|
2823
|
+
*
|
|
2824
|
+
* @returns
|
|
2825
|
+
* Error message.
|
|
2826
|
+
*/
|
|
2827
|
+
createErrorMessage(s) {
|
|
2828
|
+
return i18nextUtility.t("RegExpValidator.stringDoesNotMatchPattern", {
|
|
2829
|
+
s
|
|
2830
|
+
});
|
|
2831
|
+
}
|
|
2832
|
+
/**
|
|
2833
|
+
* @inheritDoc
|
|
2834
|
+
*/
|
|
2835
|
+
validate(s) {
|
|
2836
|
+
if (!this._regExp.test(s)) {
|
|
2837
|
+
throw new RangeError(this.createErrorMessage(s));
|
|
2838
|
+
}
|
|
2839
|
+
}
|
|
2840
|
+
};
|
|
2841
|
+
|
|
2842
|
+
// src/record.ts
|
|
2843
|
+
var RecordValidator = class {
|
|
2844
|
+
/**
|
|
2845
|
+
* Type name for error message.
|
|
2846
|
+
*/
|
|
2847
|
+
_typeName;
|
|
2848
|
+
/**
|
|
2849
|
+
* Record in which to look up keys.
|
|
2850
|
+
*/
|
|
2851
|
+
_record;
|
|
2852
|
+
/**
|
|
2853
|
+
* Constructor.
|
|
2854
|
+
*
|
|
2855
|
+
* @param typeName
|
|
2856
|
+
* Type name for error message.
|
|
2857
|
+
*
|
|
2858
|
+
* @param record
|
|
2859
|
+
* Record in which to look up keys.
|
|
2860
|
+
*/
|
|
2861
|
+
constructor(typeName, record) {
|
|
2862
|
+
this._typeName = typeName;
|
|
2863
|
+
this._record = record;
|
|
2864
|
+
}
|
|
2865
|
+
/**
|
|
2866
|
+
* Get the type name.
|
|
2867
|
+
*/
|
|
2868
|
+
get typeName() {
|
|
2869
|
+
return this._typeName;
|
|
2870
|
+
}
|
|
2871
|
+
/**
|
|
2872
|
+
* Get the record.
|
|
2873
|
+
*/
|
|
2874
|
+
get record() {
|
|
2875
|
+
return this._record;
|
|
2876
|
+
}
|
|
2877
|
+
/**
|
|
2878
|
+
* Validate a key by looking it up in the record.
|
|
2879
|
+
*
|
|
2880
|
+
* @param key
|
|
2881
|
+
* Record key.
|
|
2882
|
+
*/
|
|
2883
|
+
validate(key) {
|
|
2884
|
+
if (!(key in this.record)) {
|
|
2885
|
+
throw new RangeError(i18nextUtility.t("RecordValidator.typeNameKeyNotFound", {
|
|
2886
|
+
typeName: this.typeName,
|
|
2887
|
+
key
|
|
2888
|
+
}));
|
|
2889
|
+
}
|
|
2890
|
+
}
|
|
2891
|
+
};
|
|
2892
|
+
|
|
2893
|
+
// src/exclusion.ts
|
|
2894
|
+
var Exclusions = {
|
|
2895
|
+
/**
|
|
2896
|
+
* No strings excluded.
|
|
2897
|
+
*/
|
|
2898
|
+
None: 0,
|
|
2899
|
+
/**
|
|
2900
|
+
* Strings that start with zero ('0') excluded.
|
|
2901
|
+
*/
|
|
2902
|
+
FirstZero: 1,
|
|
2903
|
+
/**
|
|
2904
|
+
* Strings that are all-numeric (e.g., "123456") excluded.
|
|
2905
|
+
*/
|
|
2906
|
+
AllNumeric: 2
|
|
2907
|
+
};
|
|
2908
|
+
|
|
2909
|
+
// src/character-set.ts
|
|
2910
|
+
var CharacterSetValidator = class _CharacterSetValidator {
|
|
2911
|
+
static NOT_ALL_NUMERIC_VALIDATOR = new class extends RegExpValidator {
|
|
2912
|
+
/**
|
|
2913
|
+
* Create an error message for an all-numeric string.
|
|
2914
|
+
*
|
|
2915
|
+
* @param _s
|
|
2916
|
+
* String.
|
|
2917
|
+
*
|
|
2918
|
+
* @returns
|
|
2919
|
+
* Error message.
|
|
2920
|
+
*/
|
|
2921
|
+
createErrorMessage(_s) {
|
|
2922
|
+
return i18nextUtility.t("CharacterSetValidator.stringMustNotBeAllNumeric");
|
|
2923
|
+
}
|
|
2924
|
+
}(/\D/);
|
|
2925
|
+
/**
|
|
2926
|
+
* Character set.
|
|
2927
|
+
*/
|
|
2928
|
+
_characterSet;
|
|
2929
|
+
/**
|
|
2930
|
+
* Character set map, mapping each character in the character set to its index such that
|
|
2931
|
+
* `_characterSetMap.get(_characterSet[index]) === index`.
|
|
2932
|
+
*/
|
|
2933
|
+
_characterSetMap;
|
|
2934
|
+
/**
|
|
2935
|
+
* Exclusions supported by the character set.
|
|
2936
|
+
*/
|
|
2937
|
+
_exclusionSupport;
|
|
2938
|
+
/**
|
|
2939
|
+
* Constructor.
|
|
2940
|
+
*
|
|
2941
|
+
* @param characterSet
|
|
2942
|
+
* Character set. Each element is a single-character string, unique within the array, that defines the character
|
|
2943
|
+
* set.
|
|
2944
|
+
*
|
|
2945
|
+
* @param exclusionSupport
|
|
2946
|
+
* Exclusions supported by the character set. All character sets implicitly support {@linkcode Exclusions.None}.
|
|
2947
|
+
*/
|
|
2948
|
+
constructor(characterSet, ...exclusionSupport) {
|
|
2949
|
+
this._characterSet = characterSet;
|
|
2950
|
+
const characterSetMap = /* @__PURE__ */ new Map();
|
|
2951
|
+
characterSet.forEach((c, index) => {
|
|
2952
|
+
characterSetMap.set(c, index);
|
|
2953
|
+
});
|
|
2954
|
+
this._characterSetMap = characterSetMap;
|
|
2955
|
+
this._exclusionSupport = exclusionSupport;
|
|
2956
|
+
}
|
|
2957
|
+
/**
|
|
2958
|
+
* Get the character set.
|
|
2959
|
+
*/
|
|
2960
|
+
get characterSet() {
|
|
2961
|
+
return this._characterSet;
|
|
2962
|
+
}
|
|
2963
|
+
/**
|
|
2964
|
+
* Get the character set size.
|
|
2965
|
+
*/
|
|
2966
|
+
get characterSetSize() {
|
|
2967
|
+
return this._characterSet.length;
|
|
2968
|
+
}
|
|
2969
|
+
/**
|
|
2970
|
+
* Get the exclusions supported by the character set.
|
|
2971
|
+
*/
|
|
2972
|
+
get exclusionSupport() {
|
|
2973
|
+
return this._exclusionSupport;
|
|
2974
|
+
}
|
|
2975
|
+
/**
|
|
2976
|
+
* Get the character at an index.
|
|
2977
|
+
*
|
|
2978
|
+
* @param index
|
|
2979
|
+
* Index into the character set.
|
|
2980
|
+
*
|
|
2981
|
+
* @returns
|
|
2982
|
+
* Character at the index.
|
|
2983
|
+
*/
|
|
2984
|
+
character(index) {
|
|
2985
|
+
return this._characterSet[index];
|
|
2986
|
+
}
|
|
2987
|
+
/**
|
|
2988
|
+
* Get the index for a character.
|
|
2989
|
+
*
|
|
2990
|
+
* @param c
|
|
2991
|
+
* Character.
|
|
2992
|
+
*
|
|
2993
|
+
* @returns
|
|
2994
|
+
* Index for the character or undefined if the character is not in the character set.
|
|
2995
|
+
*/
|
|
2996
|
+
characterIndex(c) {
|
|
2997
|
+
return this._characterSetMap.get(c);
|
|
2998
|
+
}
|
|
2999
|
+
/**
|
|
3000
|
+
* Get the indexes for all characters in a string.
|
|
3001
|
+
*
|
|
3002
|
+
* @param s
|
|
3003
|
+
* String.
|
|
3004
|
+
*
|
|
3005
|
+
* @returns
|
|
3006
|
+
* Array of indexes for each character or undefined if the character is not in the character set.
|
|
3007
|
+
*/
|
|
3008
|
+
characterIndexes(s) {
|
|
3009
|
+
return Array.from(s).map((c) => this._characterSetMap.get(c));
|
|
3010
|
+
}
|
|
3011
|
+
/**
|
|
3012
|
+
* Convert a component definition to a string or undefined. Checks the type of the component and makes the callback
|
|
3013
|
+
* if required.
|
|
3014
|
+
*
|
|
3015
|
+
* @param component
|
|
3016
|
+
* Component definition as a string, callback, or undefined.
|
|
3017
|
+
*
|
|
3018
|
+
* @returns
|
|
3019
|
+
* Component as a string or undefined.
|
|
3020
|
+
*/
|
|
3021
|
+
static componentToString(component) {
|
|
3022
|
+
return typeof component === "function" ? component() : component;
|
|
3023
|
+
}
|
|
3024
|
+
/**
|
|
3025
|
+
* Validate that an exclusion is supported. If not, an error is thrown.
|
|
3026
|
+
*
|
|
3027
|
+
* @param exclusion
|
|
3028
|
+
* Exclusion.
|
|
3029
|
+
*/
|
|
3030
|
+
validateExclusion(exclusion) {
|
|
3031
|
+
if (exclusion !== Exclusions.None && !this._exclusionSupport.includes(exclusion)) {
|
|
3032
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.exclusionNotSupported", {
|
|
3033
|
+
exclusion
|
|
3034
|
+
}));
|
|
3035
|
+
}
|
|
3036
|
+
}
|
|
3037
|
+
/**
|
|
3038
|
+
* Validate a string. If the string violates the character set or any of the character set validation parameters, an
|
|
3039
|
+
* error is thrown.
|
|
3040
|
+
*
|
|
3041
|
+
* @param s
|
|
3042
|
+
* String.
|
|
3043
|
+
*
|
|
3044
|
+
* @param validation
|
|
3045
|
+
* Character set validation parameters.
|
|
3046
|
+
*/
|
|
3047
|
+
validate(s, validation) {
|
|
3048
|
+
const length = s.length;
|
|
3049
|
+
const minimumLength = validation?.minimumLength;
|
|
3050
|
+
const maximumLength = validation?.maximumLength;
|
|
3051
|
+
if (minimumLength !== void 0 && length < minimumLength) {
|
|
3052
|
+
let errorMessage;
|
|
3053
|
+
if (maximumLength !== void 0 && maximumLength === minimumLength) {
|
|
3054
|
+
errorMessage = i18nextUtility.t(validation?.component === void 0 ? "CharacterSetValidator.lengthMustBeEqualTo" : "CharacterSetValidator.lengthOfComponentMustBeEqualTo", {
|
|
3055
|
+
component: _CharacterSetValidator.componentToString(validation?.component),
|
|
3056
|
+
length,
|
|
3057
|
+
exactLength: minimumLength
|
|
3058
|
+
});
|
|
3059
|
+
} else {
|
|
3060
|
+
errorMessage = i18nextUtility.t(validation?.component === void 0 ? "CharacterSetValidator.lengthMustBeGreaterThanOrEqualTo" : "CharacterSetValidator.lengthOfComponentMustBeGreaterThanOrEqualTo", {
|
|
3061
|
+
component: _CharacterSetValidator.componentToString(validation?.component),
|
|
3062
|
+
length,
|
|
3063
|
+
minimumLength
|
|
3064
|
+
});
|
|
3065
|
+
}
|
|
3066
|
+
throw new RangeError(errorMessage);
|
|
3067
|
+
}
|
|
3068
|
+
if (maximumLength !== void 0 && length > maximumLength) {
|
|
3069
|
+
throw new RangeError(i18nextUtility.t(validation?.component === void 0 ? "CharacterSetValidator.lengthMustBeLessThanOrEqualTo" : "CharacterSetValidator.lengthOfComponentMustBeLessThanOrEqualTo", {
|
|
3070
|
+
component: _CharacterSetValidator.componentToString(validation?.component),
|
|
3071
|
+
length,
|
|
3072
|
+
maximumLength
|
|
3073
|
+
}));
|
|
3074
|
+
}
|
|
3075
|
+
const index = this.characterIndexes(s).findIndex((characterIndex) => characterIndex === void 0);
|
|
3076
|
+
if (index !== -1) {
|
|
3077
|
+
throw new RangeError(i18nextUtility.t(validation?.component === void 0 ? "CharacterSetValidator.invalidCharacterAtPosition" : "CharacterSetValidator.invalidCharacterAtPositionOfComponent", {
|
|
3078
|
+
component: _CharacterSetValidator.componentToString(validation?.component),
|
|
3079
|
+
c: s.charAt(index),
|
|
3080
|
+
position: index + (validation?.positionOffset ?? 0) + 1
|
|
3081
|
+
}));
|
|
3082
|
+
}
|
|
3083
|
+
if (validation?.exclusion !== void 0) {
|
|
3084
|
+
this.validateExclusion(validation.exclusion);
|
|
3085
|
+
switch (validation.exclusion) {
|
|
3086
|
+
case Exclusions.None:
|
|
3087
|
+
break;
|
|
3088
|
+
case Exclusions.FirstZero:
|
|
3089
|
+
if (s.startsWith("0")) {
|
|
3090
|
+
throw new RangeError(i18nextUtility.t(validation.component === void 0 ? "CharacterSetValidator.invalidCharacterAtPosition" : "CharacterSetValidator.invalidCharacterAtPositionOfComponent", {
|
|
3091
|
+
component: _CharacterSetValidator.componentToString(validation.component),
|
|
3092
|
+
c: "0",
|
|
3093
|
+
position: (validation.positionOffset ?? 0) + 1
|
|
3094
|
+
}));
|
|
3095
|
+
}
|
|
3096
|
+
break;
|
|
3097
|
+
case Exclusions.AllNumeric:
|
|
3098
|
+
_CharacterSetValidator.NOT_ALL_NUMERIC_VALIDATOR.validate(s);
|
|
3099
|
+
break;
|
|
3100
|
+
}
|
|
3101
|
+
}
|
|
3102
|
+
}
|
|
3103
|
+
};
|
|
3104
|
+
var CharacterSetCreator = class _CharacterSetCreator extends CharacterSetValidator {
|
|
3105
|
+
/**
|
|
3106
|
+
* Maximum string length supported.
|
|
3107
|
+
*/
|
|
3108
|
+
static MAXIMUM_STRING_LENGTH = 40;
|
|
3109
|
+
/**
|
|
3110
|
+
* Powers of 10 from 1 (`10**0`) to `10**MAXIMUM_STRING_LENGTH`.
|
|
3111
|
+
*/
|
|
3112
|
+
static _powersOf10 = _CharacterSetCreator.createPowersOf(10);
|
|
3113
|
+
/**
|
|
3114
|
+
* Create powers of a given base from 1 (`base**0`) to `base**MAXIMUM_STRING_LENGTH`.
|
|
3115
|
+
*
|
|
3116
|
+
* @param base
|
|
3117
|
+
* Number base.
|
|
3118
|
+
*
|
|
3119
|
+
* @returns
|
|
3120
|
+
* Array of powers of base.
|
|
3121
|
+
*/
|
|
3122
|
+
static createPowersOf(base) {
|
|
3123
|
+
const powersOf = new Array(this.MAXIMUM_STRING_LENGTH + 1);
|
|
3124
|
+
const baseN = BigInt(base);
|
|
3125
|
+
for (let index = 0, powerOf = 1n; index <= this.MAXIMUM_STRING_LENGTH; index++, powerOf *= baseN) {
|
|
3126
|
+
powersOf[index] = powerOf;
|
|
3127
|
+
}
|
|
3128
|
+
return powersOf;
|
|
3129
|
+
}
|
|
3130
|
+
/**
|
|
3131
|
+
* Get a power of 10.
|
|
3132
|
+
*
|
|
3133
|
+
* @param power
|
|
3134
|
+
* Power.
|
|
3135
|
+
*
|
|
3136
|
+
* @returns
|
|
3137
|
+
* `10**power`.
|
|
3138
|
+
*/
|
|
3139
|
+
static powerOf10(power) {
|
|
3140
|
+
return this._powersOf10[power];
|
|
3141
|
+
}
|
|
3142
|
+
/**
|
|
3143
|
+
* Character set size as big integer, cached for performance purposes.
|
|
3144
|
+
*/
|
|
3145
|
+
_characterSetSizeN;
|
|
3146
|
+
/**
|
|
3147
|
+
* Character set size minus 1 as big integer, cached for performance purposes.
|
|
3148
|
+
*/
|
|
3149
|
+
_characterSetSizeMinusOneN;
|
|
3150
|
+
/**
|
|
3151
|
+
* Domains for every length for every supported {@linkcode Exclusions}.
|
|
3152
|
+
*/
|
|
3153
|
+
_exclusionDomains;
|
|
3154
|
+
/**
|
|
3155
|
+
* Values that would generate all zeros in the created string.
|
|
3156
|
+
*/
|
|
3157
|
+
_allZerosValues;
|
|
3158
|
+
/**
|
|
3159
|
+
* Constructor.
|
|
3160
|
+
*
|
|
3161
|
+
* @param characterSet
|
|
3162
|
+
* Character set. Each element is a single-character string, unique within the array, that defines the character
|
|
3163
|
+
* set.
|
|
3164
|
+
*
|
|
3165
|
+
* @param exclusionSupport
|
|
3166
|
+
* Exclusions supported by the character set. All character sets implicitly support {@linkcode Exclusions.None}.
|
|
3167
|
+
*/
|
|
3168
|
+
constructor(characterSet, ...exclusionSupport) {
|
|
3169
|
+
super(characterSet, ...exclusionSupport);
|
|
3170
|
+
this._characterSetSizeN = BigInt(this.characterSetSize);
|
|
3171
|
+
this._characterSetSizeMinusOneN = BigInt(this.characterSetSize - 1);
|
|
3172
|
+
const exclusionDomains = [];
|
|
3173
|
+
const exclusionNoneDomains = _CharacterSetCreator.createPowersOf(this.characterSetSize);
|
|
3174
|
+
exclusionDomains[Exclusions.None] = exclusionNoneDomains;
|
|
3175
|
+
if (exclusionSupport.includes(Exclusions.FirstZero)) {
|
|
3176
|
+
if (characterSet[0] !== "0") {
|
|
3177
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.firstZeroFirstCharacter"));
|
|
3178
|
+
}
|
|
3179
|
+
const exclusionFirstZeroDomains = new Array(_CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
|
|
3180
|
+
exclusionFirstZeroDomains[0] = 0n;
|
|
3181
|
+
for (let index = 1; index <= _CharacterSetCreator.MAXIMUM_STRING_LENGTH; index++) {
|
|
3182
|
+
exclusionFirstZeroDomains[index] = this._characterSetSizeMinusOneN * exclusionNoneDomains[index - 1];
|
|
3183
|
+
}
|
|
3184
|
+
exclusionDomains[Exclusions.FirstZero] = exclusionFirstZeroDomains;
|
|
3185
|
+
}
|
|
3186
|
+
if (exclusionSupport.includes(Exclusions.AllNumeric)) {
|
|
3187
|
+
let validateNumberIndexes2 = function(numberIndexes2) {
|
|
3188
|
+
let expectedNumberIndex = numberIndexes2[0];
|
|
3189
|
+
for (const numberIndex of numberIndexes2) {
|
|
3190
|
+
if (numberIndex === void 0 || numberIndex !== expectedNumberIndex) {
|
|
3191
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.allNumericAllNumericCharacters"));
|
|
3192
|
+
}
|
|
3193
|
+
expectedNumberIndex = numberIndex + 1;
|
|
3194
|
+
}
|
|
3195
|
+
};
|
|
3196
|
+
var validateNumberIndexes = validateNumberIndexes2;
|
|
3197
|
+
const exclusionAllNumericDomains = new Array(_CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
|
|
3198
|
+
const numberIndexes = this.characterIndexes("0123456789");
|
|
3199
|
+
validateNumberIndexes2(numberIndexes);
|
|
3200
|
+
const zeroIndex = BigInt(numberIndexes[0]);
|
|
3201
|
+
const allZerosValues = new Array(_CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
|
|
3202
|
+
let allZerosValue = 0n;
|
|
3203
|
+
for (let index = 0; index <= _CharacterSetCreator.MAXIMUM_STRING_LENGTH; index++) {
|
|
3204
|
+
exclusionAllNumericDomains[index] = exclusionNoneDomains[index] - _CharacterSetCreator.powerOf10(index);
|
|
3205
|
+
allZerosValues[index] = allZerosValue;
|
|
3206
|
+
allZerosValue = allZerosValue * this._characterSetSizeN + zeroIndex;
|
|
3207
|
+
}
|
|
3208
|
+
this._allZerosValues = allZerosValues;
|
|
3209
|
+
exclusionDomains[Exclusions.AllNumeric] = exclusionAllNumericDomains;
|
|
3210
|
+
} else {
|
|
3211
|
+
this._allZerosValues = [];
|
|
3212
|
+
}
|
|
3213
|
+
this._exclusionDomains = exclusionDomains;
|
|
3214
|
+
}
|
|
3215
|
+
/**
|
|
3216
|
+
* Get a power of character set size.
|
|
3217
|
+
*
|
|
3218
|
+
* @param power
|
|
3219
|
+
* Power.
|
|
3220
|
+
*
|
|
3221
|
+
* @returns
|
|
3222
|
+
* `characterSetSize**power`.
|
|
3223
|
+
*/
|
|
3224
|
+
powerOfSize(power) {
|
|
3225
|
+
return this._exclusionDomains[Exclusions.None][power];
|
|
3226
|
+
}
|
|
3227
|
+
/**
|
|
3228
|
+
* Determine the shift required to skip all all-numeric strings up to the value.
|
|
3229
|
+
*
|
|
3230
|
+
* @param shiftForward
|
|
3231
|
+
* True to shift forward (value to string), false to shift backward (string to value).
|
|
3232
|
+
*
|
|
3233
|
+
* @param length
|
|
3234
|
+
* Length of string for which to get the all-numeric shift.
|
|
3235
|
+
*
|
|
3236
|
+
* @param value
|
|
3237
|
+
* Value for which to get the all-numeric shift.
|
|
3238
|
+
*
|
|
3239
|
+
* @returns
|
|
3240
|
+
* Shift required to skip all all-numeric strings.
|
|
3241
|
+
*/
|
|
3242
|
+
allNumericShift(shiftForward, length, value) {
|
|
3243
|
+
let shift;
|
|
3244
|
+
if (length === 0) {
|
|
3245
|
+
if (!shiftForward && value < 10n) {
|
|
3246
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.stringMustNotBeAllNumeric"));
|
|
3247
|
+
}
|
|
3248
|
+
shift = 10n;
|
|
3249
|
+
} else {
|
|
3250
|
+
const powerOfSize = this.powerOfSize(length);
|
|
3251
|
+
const powerOf10 = _CharacterSetCreator.powerOf10(length);
|
|
3252
|
+
const gap = shiftForward ? powerOfSize - powerOf10 : powerOfSize;
|
|
3253
|
+
const gaps = value / gap;
|
|
3254
|
+
if (gaps >= 10n) {
|
|
3255
|
+
shift = _CharacterSetCreator.powerOf10(length + 1);
|
|
3256
|
+
} else {
|
|
3257
|
+
shift = gaps * powerOf10 + this.allNumericShift(shiftForward, length - 1, value - gaps * gap);
|
|
3258
|
+
}
|
|
3259
|
+
}
|
|
3260
|
+
return shift;
|
|
3261
|
+
}
|
|
3262
|
+
/**
|
|
3263
|
+
* Validate that a length is less than or equal to {@linkcode MAXIMUM_STRING_LENGTH}. If not, an error is thrown.
|
|
3264
|
+
*
|
|
3265
|
+
* @param length
|
|
3266
|
+
* Length.
|
|
3267
|
+
*/
|
|
3268
|
+
validateLength(length) {
|
|
3269
|
+
if (length < 0) {
|
|
3270
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.lengthMustBeGreaterThanOrEqualTo", {
|
|
3271
|
+
length,
|
|
3272
|
+
minimumLength: 0
|
|
3273
|
+
}));
|
|
3274
|
+
}
|
|
3275
|
+
if (length > _CharacterSetCreator.MAXIMUM_STRING_LENGTH) {
|
|
3276
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.lengthMustBeLessThanOrEqualTo", {
|
|
3277
|
+
length,
|
|
3278
|
+
maximumLength: _CharacterSetCreator.MAXIMUM_STRING_LENGTH
|
|
3279
|
+
}));
|
|
3280
|
+
}
|
|
3281
|
+
}
|
|
3282
|
+
/**
|
|
3283
|
+
* Create string(s) by mapping value(s) to the equivalent characters in the character set across the length of the
|
|
3284
|
+
* string.
|
|
3285
|
+
*
|
|
3286
|
+
* @template TTransformerInput
|
|
3287
|
+
* Transformer input type.
|
|
3288
|
+
*
|
|
3289
|
+
* @param length
|
|
3290
|
+
* Required string length.
|
|
3291
|
+
*
|
|
3292
|
+
* @param valueOrValues
|
|
3293
|
+
* Numeric value(s) of the string(s).
|
|
3294
|
+
*
|
|
3295
|
+
* @param exclusion
|
|
3296
|
+
* String(s) to be excluded from the range of outputs. See {@linkcode Exclusions} for possible values and their
|
|
3297
|
+
* meaning.
|
|
3298
|
+
*
|
|
3299
|
+
* @param tweak
|
|
3300
|
+
* If provided, the numerical value of the string(s) is/are "tweaked" using an {@link EncryptionTransformer |
|
|
3301
|
+
* encryption transformer}.
|
|
3302
|
+
*
|
|
3303
|
+
* @param creatorCallback
|
|
3304
|
+
* If provided, called after each string is constructed to create the final value.
|
|
3305
|
+
*
|
|
3306
|
+
* @returns
|
|
3307
|
+
* String(s) created from the value(s).
|
|
3308
|
+
*/
|
|
3309
|
+
create(length, valueOrValues, exclusion = Exclusions.None, tweak, creatorCallback) {
|
|
3310
|
+
this.validateLength(length);
|
|
3311
|
+
this.validateExclusion(exclusion);
|
|
3312
|
+
const allZerosValue = exclusion === Exclusions.AllNumeric ? this._allZerosValues[length] : 0n;
|
|
3313
|
+
const transformer = Transformer.get(this._exclusionDomains[exclusion][length], tweak);
|
|
3314
|
+
return transformer.forward(valueOrValues, (transformedValue, index) => {
|
|
3315
|
+
let s = "";
|
|
3316
|
+
if (length !== 0) {
|
|
3317
|
+
let convertValue = transformedValue;
|
|
3318
|
+
if (exclusion === Exclusions.AllNumeric && convertValue >= allZerosValue) {
|
|
3319
|
+
convertValue += this.allNumericShift(true, length, convertValue - allZerosValue);
|
|
3320
|
+
}
|
|
3321
|
+
for (let position = length - 1; position > 0; position--) {
|
|
3322
|
+
const nextConvertValue = convertValue / this._characterSetSizeN;
|
|
3323
|
+
s = this.character(Number(convertValue - nextConvertValue * this._characterSetSizeN)) + s;
|
|
3324
|
+
convertValue = nextConvertValue;
|
|
3325
|
+
}
|
|
3326
|
+
s = this.character(exclusion === Exclusions.FirstZero ? Number(convertValue % this._characterSetSizeMinusOneN) + 1 : Number(convertValue % this._characterSetSizeN)) + s;
|
|
3327
|
+
}
|
|
3328
|
+
return creatorCallback === void 0 ? s : creatorCallback(s, index);
|
|
3329
|
+
});
|
|
3330
|
+
}
|
|
3331
|
+
/**
|
|
3332
|
+
* Determine the value for a string.
|
|
3333
|
+
*
|
|
3334
|
+
* @param s
|
|
3335
|
+
* String.
|
|
3336
|
+
*
|
|
3337
|
+
* @param exclusion
|
|
3338
|
+
* Strings excluded from the range of inputs. See {@linkcode Exclusions} for possible values and their meaning.
|
|
3339
|
+
*
|
|
3340
|
+
* @param tweak
|
|
3341
|
+
* If provided, the numerical value of the string was "tweaked" using an {@link EncryptionTransformer | encryption
|
|
3342
|
+
* transformer}.
|
|
3343
|
+
*
|
|
3344
|
+
* @returns
|
|
3345
|
+
* Numeric value of the string.
|
|
3346
|
+
*/
|
|
3347
|
+
valueFor(s, exclusion = Exclusions.None, tweak) {
|
|
3348
|
+
const length = s.length;
|
|
3349
|
+
this.validateLength(length);
|
|
3350
|
+
this.validateExclusion(exclusion);
|
|
3351
|
+
const characterSetSizeN = BigInt(this.characterSetSize);
|
|
3352
|
+
let value = this.characterIndexes(s).reduce((accumulator, characterIndex, index) => {
|
|
3353
|
+
if (characterIndex === void 0) {
|
|
3354
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.invalidCharacterAtPosition", {
|
|
3355
|
+
c: s.charAt(index),
|
|
3356
|
+
position: index + 1
|
|
3357
|
+
}));
|
|
3358
|
+
}
|
|
3359
|
+
let value2;
|
|
3360
|
+
if (index === 0 && exclusion === Exclusions.FirstZero) {
|
|
3361
|
+
if (characterIndex === 0) {
|
|
3362
|
+
throw new RangeError(i18nextUtility.t("CharacterSetValidator.invalidCharacterAtPosition", {
|
|
3363
|
+
c: "0",
|
|
3364
|
+
position: 1
|
|
3365
|
+
}));
|
|
3366
|
+
}
|
|
3367
|
+
value2 = BigInt(characterIndex - 1);
|
|
3368
|
+
} else {
|
|
3369
|
+
value2 = accumulator * characterSetSizeN + BigInt(characterIndex);
|
|
3370
|
+
}
|
|
3371
|
+
return value2;
|
|
3372
|
+
}, 0n);
|
|
3373
|
+
if (exclusion === Exclusions.AllNumeric) {
|
|
3374
|
+
const allZerosValue = this._allZerosValues[length];
|
|
3375
|
+
if (value >= allZerosValue) {
|
|
3376
|
+
value -= this.allNumericShift(false, length, value - allZerosValue);
|
|
3377
|
+
}
|
|
3378
|
+
}
|
|
3379
|
+
return Transformer.get(this._exclusionDomains[exclusion][length], tweak).reverse(value);
|
|
3380
|
+
}
|
|
3381
|
+
};
|
|
3382
|
+
var NUMERIC_CREATOR = new CharacterSetCreator([
|
|
3383
|
+
"0",
|
|
3384
|
+
"1",
|
|
3385
|
+
"2",
|
|
3386
|
+
"3",
|
|
3387
|
+
"4",
|
|
3388
|
+
"5",
|
|
3389
|
+
"6",
|
|
3390
|
+
"7",
|
|
3391
|
+
"8",
|
|
3392
|
+
"9"
|
|
3393
|
+
], Exclusions.FirstZero);
|
|
3394
|
+
var NUMERIC_VALIDATOR = NUMERIC_CREATOR;
|
|
3395
|
+
var HEXADECIMAL_CREATOR = new CharacterSetCreator([
|
|
3396
|
+
"0",
|
|
3397
|
+
"1",
|
|
3398
|
+
"2",
|
|
3399
|
+
"3",
|
|
3400
|
+
"4",
|
|
3401
|
+
"5",
|
|
3402
|
+
"6",
|
|
3403
|
+
"7",
|
|
3404
|
+
"8",
|
|
3405
|
+
"9",
|
|
3406
|
+
"A",
|
|
3407
|
+
"B",
|
|
3408
|
+
"C",
|
|
3409
|
+
"D",
|
|
3410
|
+
"E",
|
|
3411
|
+
"F"
|
|
3412
|
+
], Exclusions.FirstZero, Exclusions.AllNumeric);
|
|
3413
|
+
var HEXADECIMAL_VALIDATOR = HEXADECIMAL_CREATOR;
|
|
3414
|
+
var ALPHABETIC_CREATOR = new CharacterSetCreator([
|
|
3415
|
+
"A",
|
|
3416
|
+
"B",
|
|
3417
|
+
"C",
|
|
3418
|
+
"D",
|
|
3419
|
+
"E",
|
|
3420
|
+
"F",
|
|
3421
|
+
"G",
|
|
3422
|
+
"H",
|
|
3423
|
+
"I",
|
|
3424
|
+
"J",
|
|
3425
|
+
"K",
|
|
3426
|
+
"L",
|
|
3427
|
+
"M",
|
|
3428
|
+
"N",
|
|
3429
|
+
"O",
|
|
3430
|
+
"P",
|
|
3431
|
+
"Q",
|
|
3432
|
+
"R",
|
|
3433
|
+
"S",
|
|
3434
|
+
"T",
|
|
3435
|
+
"U",
|
|
3436
|
+
"V",
|
|
3437
|
+
"W",
|
|
3438
|
+
"X",
|
|
3439
|
+
"Y",
|
|
3440
|
+
"Z"
|
|
3441
|
+
]);
|
|
3442
|
+
var ALPHABETIC_VALIDATOR = ALPHABETIC_CREATOR;
|
|
3443
|
+
var ALPHANUMERIC_CREATOR = new CharacterSetCreator([
|
|
3444
|
+
"0",
|
|
3445
|
+
"1",
|
|
3446
|
+
"2",
|
|
3447
|
+
"3",
|
|
3448
|
+
"4",
|
|
3449
|
+
"5",
|
|
3450
|
+
"6",
|
|
3451
|
+
"7",
|
|
3452
|
+
"8",
|
|
3453
|
+
"9",
|
|
3454
|
+
"A",
|
|
3455
|
+
"B",
|
|
3456
|
+
"C",
|
|
3457
|
+
"D",
|
|
3458
|
+
"E",
|
|
3459
|
+
"F",
|
|
3460
|
+
"G",
|
|
3461
|
+
"H",
|
|
3462
|
+
"I",
|
|
3463
|
+
"J",
|
|
3464
|
+
"K",
|
|
3465
|
+
"L",
|
|
3466
|
+
"M",
|
|
3467
|
+
"N",
|
|
3468
|
+
"O",
|
|
3469
|
+
"P",
|
|
3470
|
+
"Q",
|
|
3471
|
+
"R",
|
|
3472
|
+
"S",
|
|
3473
|
+
"T",
|
|
3474
|
+
"U",
|
|
3475
|
+
"V",
|
|
3476
|
+
"W",
|
|
3477
|
+
"X",
|
|
3478
|
+
"Y",
|
|
3479
|
+
"Z"
|
|
3480
|
+
], Exclusions.FirstZero, Exclusions.AllNumeric);
|
|
3481
|
+
var ALPHANUMERIC_VALIDATOR = ALPHANUMERIC_CREATOR;
|
|
3482
|
+
export {
|
|
3483
|
+
ALPHABETIC_CREATOR,
|
|
3484
|
+
ALPHABETIC_VALIDATOR,
|
|
3485
|
+
ALPHANUMERIC_CREATOR,
|
|
3486
|
+
ALPHANUMERIC_VALIDATOR,
|
|
3487
|
+
CharacterSetCreator,
|
|
3488
|
+
CharacterSetValidator,
|
|
3489
|
+
EncryptionTransformer,
|
|
3490
|
+
Exclusions,
|
|
3491
|
+
HEXADECIMAL_CREATOR,
|
|
3492
|
+
HEXADECIMAL_VALIDATOR,
|
|
3493
|
+
IdentityTransformer,
|
|
3494
|
+
NUMERIC_CREATOR,
|
|
3495
|
+
NUMERIC_VALIDATOR,
|
|
3496
|
+
RecordValidator,
|
|
3497
|
+
RegExpValidator,
|
|
3498
|
+
Sequence,
|
|
3499
|
+
Transformer,
|
|
3500
|
+
i18nUtilityInit,
|
|
3501
|
+
i18nextUtility,
|
|
3502
|
+
mapIterable,
|
|
3503
|
+
utilityNS,
|
|
3504
|
+
utilityResources
|
|
3505
|
+
};
|
|
1
3506
|
/*!
|
|
2
3507
|
* Copyright © 2024-2025 Dolphin Data Development Ltd. and AIDC Toolkit
|
|
3
3508
|
* contributors
|
|
@@ -14,12 +3519,4 @@
|
|
|
14
3519
|
* See the License for the specific language governing permissions and
|
|
15
3520
|
* limitations under the License.
|
|
16
3521
|
*/
|
|
17
|
-
export * from "./locale/i18n.js";
|
|
18
|
-
export * from "./sequence.js";
|
|
19
|
-
export * from "./iterable-utility.js";
|
|
20
|
-
export * from "./transformer.js";
|
|
21
|
-
export * from "./reg-exp.js";
|
|
22
|
-
export * from "./record.js";
|
|
23
|
-
export * from "./exclusion.js";
|
|
24
|
-
export * from "./character-set.js";
|
|
25
3522
|
//# sourceMappingURL=index.js.map
|