@miot-rn/common-component 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/dist/index.d.ts +0 -0
- package/dist/index.js +0 -0
- package/dist/resources/i18n.d.ts +55 -0
- package/dist/resources/i18n.js +212 -0
- package/dist/service/specs/error-code.d.ts +19 -0
- package/dist/service/specs/error-code.js +41 -0
- package/dist/service/specs/index.d.ts +95 -0
- package/dist/service/specs/index.js +412 -0
- package/dist/service/specs/spec.d.ts +10 -0
- package/dist/service/specs/spec.js +109 -0
- package/dist/service/specs/specCache.d.ts +85 -0
- package/dist/service/specs/specCache.js +421 -0
- package/dist/service/specs/std-spec.d.ts +5 -0
- package/dist/service/specs/std-spec.js +228 -0
- package/dist/service/specs/subject.d.ts +20 -0
- package/dist/service/specs/subject.js +145 -0
- package/dist/service/specs/utils.d.ts +28 -0
- package/dist/service/specs/utils.js +46 -0
- package/dist/specs/i18n-parser.d.ts +10 -0
- package/dist/specs/i18n-parser.js +300 -0
- package/dist/specs/instance-parser.d.ts +26 -0
- package/dist/specs/instance-parser.js +706 -0
- package/dist/store/useGlobalSpecManager.d.ts +320 -0
- package/dist/store/useGlobalSpecManager.js +867 -0
- package/dist/types/index.d.ts +23 -0
- package/dist/types/index.js +1 -0
- package/dist/utils/fns.d.ts +12 -0
- package/dist/utils/fns.js +308 -0
- package/dist/utils/get-value-or-default.d.ts +1 -0
- package/dist/utils/get-value-or-default.js +4 -0
- package/dist/utils/i18n.d.ts +1 -0
- package/dist/utils/i18n.js +141 -0
- package/dist/utils/objects.d.ts +2 -0
- package/dist/utils/objects.js +222 -0
- package/dist/utils/spec.d.ts +8 -0
- package/dist/utils/spec.js +171 -0
- package/dist/utils/types.d.ts +3 -0
- package/dist/utils/types.js +25 -0
- package/package.json +27 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface Spec {
|
|
2
|
+
did?: string;
|
|
3
|
+
siid: number;
|
|
4
|
+
piid?: number;
|
|
5
|
+
aiid?: number;
|
|
6
|
+
miid?: number;
|
|
7
|
+
value?: any;
|
|
8
|
+
in?: any[];
|
|
9
|
+
dataSource?: number;
|
|
10
|
+
code?: number;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
}
|
|
13
|
+
export interface QuerySpecParams {
|
|
14
|
+
mkey?: string;
|
|
15
|
+
mDescription?: string;
|
|
16
|
+
skey?: string;
|
|
17
|
+
sDescription?: string;
|
|
18
|
+
pkey?: string;
|
|
19
|
+
akey?: string;
|
|
20
|
+
ekey?: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
dataSource?: unknown;
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function NOOP(): void;
|
|
2
|
+
export declare function safeTrim(value: any): any;
|
|
3
|
+
export declare function log(...args: any[]): void;
|
|
4
|
+
export declare function isSameArrayElements(a: any[], b: any[]): boolean;
|
|
5
|
+
export declare function getType(o: any): string;
|
|
6
|
+
export declare function getStringGenerator(template: any, witch?: number): any;
|
|
7
|
+
export declare function getTemplateValueOneByOne(values?: any[], template?: any): any;
|
|
8
|
+
export declare function getTemplateValueCompare(values?: any[], template?: any): any;
|
|
9
|
+
export declare function getTemplateValueCompareEach(values?: any[], template?: any): any;
|
|
10
|
+
export declare function fixNum(n: any): string;
|
|
11
|
+
export declare function removeDuplicateIf(array: any[], keyOf: (ele: any) => any): any[];
|
|
12
|
+
export declare function removeAllIf(array: any[], fn: (ele: any, idx: number) => boolean): any[];
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
// @native begin
|
|
2
|
+
/* eslint-disable func-names */
|
|
3
|
+
|
|
4
|
+
// import Resources, { Language } from '../resources';
|
|
5
|
+
// const LANGUAGE = Resources.getLanguage();
|
|
6
|
+
|
|
7
|
+
// 空函数
|
|
8
|
+
export function NOOP() {}
|
|
9
|
+
|
|
10
|
+
// 安全去除字符串首尾空格
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
+
export function safeTrim(value) {
|
|
13
|
+
return typeof value === 'string' ? value.trim() : value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// 打印参数
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
export function log(...args) {
|
|
19
|
+
// eslint-disable-next-line no-console
|
|
20
|
+
console.log(...args);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 判断两个数组的元素相同
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
|
+
export function isSameArrayElements(a, b) {
|
|
26
|
+
if (a === b) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
if (!a && b || a && !b) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (a && b && a.length !== b.length) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
for (let i = 0, l = a.length; i < l; i++) {
|
|
36
|
+
const item = a[i];
|
|
37
|
+
if (b.find(n => {
|
|
38
|
+
return n === item;
|
|
39
|
+
}) === undefined) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 获取数据类型
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
48
|
+
export function getType(o) {
|
|
49
|
+
return Object.prototype.toString.call(o).slice(8, -1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 利用多语言模版进行字符串求值
|
|
53
|
+
// 不分语种
|
|
54
|
+
// export function getStringGenerator(template) {
|
|
55
|
+
// let i18n = (Resources.createI18n({
|
|
56
|
+
// [LANGUAGE]: {
|
|
57
|
+
// text: template
|
|
58
|
+
// }
|
|
59
|
+
// }, LANGUAGE) || {}).strings;
|
|
60
|
+
// return i18n.text;
|
|
61
|
+
// }
|
|
62
|
+
const StringGeneratorTypes = {
|
|
63
|
+
normal: 'StringGeneratorTypes:normal',
|
|
64
|
+
oneByOne: 'StringGeneratorTypes:oneByOne',
|
|
65
|
+
// 比较两个属性值
|
|
66
|
+
compare: 'StringGeneratorTypes:compare',
|
|
67
|
+
// 比较单个属性值与预设值
|
|
68
|
+
compareEach: 'StringGeneratorTypes:compareEach'
|
|
69
|
+
};
|
|
70
|
+
function formatStringRet(template) {
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
+
return function (...args) {
|
|
73
|
+
let ret = template;
|
|
74
|
+
args.forEach((a, index) => {
|
|
75
|
+
ret = ret.replace(new RegExp(`\\{${index + 1}\\}`, 'g'), a);
|
|
76
|
+
});
|
|
77
|
+
return ret;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */
|
|
82
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
83
|
+
function formatArrayRet(template, witch = 0) {
|
|
84
|
+
const [first, ...rest] = template;
|
|
85
|
+
if (witch) {
|
|
86
|
+
return formatArrayNormal(template, witch);
|
|
87
|
+
}
|
|
88
|
+
switch (first) {
|
|
89
|
+
case StringGeneratorTypes.oneByOne:
|
|
90
|
+
return formatArrayOnebyOne(rest);
|
|
91
|
+
case StringGeneratorTypes.compare:
|
|
92
|
+
return formatArrayCompare(rest);
|
|
93
|
+
case StringGeneratorTypes.compareEach:
|
|
94
|
+
return formatArrayCompareEach(rest);
|
|
95
|
+
case StringGeneratorTypes.normal:
|
|
96
|
+
default:
|
|
97
|
+
return formatArrayNormal(template, witch);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
102
|
+
function formatArrayNormal(template, witch = 0) {
|
|
103
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, func-names
|
|
104
|
+
return function (...args) {
|
|
105
|
+
const target = args[witch];
|
|
106
|
+
for (let i = 1, l = template.length; i < l; i++) {
|
|
107
|
+
const t = template[i];
|
|
108
|
+
const type = getType(t);
|
|
109
|
+
if (type !== 'Array') {
|
|
110
|
+
return getStringGenerator([template[0], [target, t]], witch + 1)(...args);
|
|
111
|
+
}
|
|
112
|
+
if (type === 'Array' && target === t[0]) {
|
|
113
|
+
const ttype = getType(t[1]);
|
|
114
|
+
return getStringGenerator(ttype === 'Array' ? [template[0], ...t.slice(1)] : t[1], witch + 1)(...args);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return getStringGenerator(template[0])(...args);
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
122
|
+
function formatArrayOnebyOne(template) {
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, func-names
|
|
124
|
+
return function (...values) {
|
|
125
|
+
// @ts-ignore
|
|
126
|
+
// eslint-disable-next-line no-undef
|
|
127
|
+
return getTemplateValueOneByOne(values, template);
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
132
|
+
function formatArrayCompare(template) {
|
|
133
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, func-names
|
|
134
|
+
return function (...values) {
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
// eslint-disable-next-line no-undef
|
|
137
|
+
return getTemplateValueCompare(values, template);
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
142
|
+
function formatArrayCompareEach(template) {
|
|
143
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, func-names
|
|
144
|
+
return function (...values) {
|
|
145
|
+
// @ts-ignore
|
|
146
|
+
// eslint-disable-next-line no-undef
|
|
147
|
+
return getTemplateValueCompareEach(values, template);
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
152
|
+
export function getStringGenerator(template, witch = 0) {
|
|
153
|
+
if (!template) {
|
|
154
|
+
// eslint-disable-next-line func-names
|
|
155
|
+
return function () {
|
|
156
|
+
return '';
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
const type = getType(template);
|
|
160
|
+
switch (type) {
|
|
161
|
+
case 'String':
|
|
162
|
+
return formatStringRet(template);
|
|
163
|
+
case 'Array':
|
|
164
|
+
return formatArrayRet(template, witch);
|
|
165
|
+
case 'Function':
|
|
166
|
+
return template;
|
|
167
|
+
default:
|
|
168
|
+
return function () {
|
|
169
|
+
return template;
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// console.log('getStringGeneratorTest', getStringGenerator('{1}---{2}')(3, 'oooooooo') === '3---oooooooo');
|
|
175
|
+
// console.log('getStringGeneratorTest', getStringGenerator(['{1}---{2}', [5, '{2}']])(3, 'oooooooo') === '3---oooooooo');
|
|
176
|
+
// console.log('getStringGeneratorTest', getStringGenerator(['{1}---{2}', [5, '{2}']])(5, 'oooooooo') === 'oooooooo');
|
|
177
|
+
// console.log('getStringGeneratorTest', getStringGenerator(['{1}---{2}', [5, [7, '-{1}--{2}']]])(5, 7) === '-5--7');
|
|
178
|
+
// console.log('getStringGeneratorTest', getStringGenerator(['{1}---{2}', [5, [7, [9, '-{2}-{1}']]]])(5, 7, 9) === '-7-5');
|
|
179
|
+
// console.log('getStringGeneratorTest', getStringGenerator(['{1}---{2}', [5, [7, [9, [true, 'true in 4'], [false, 'false in 4']]]]])(5, 7, 9, true) === 'true in 4');
|
|
180
|
+
// console.log('getStringGeneratorTest', getStringGenerator(['{1}---{2}', [5, [7, [9, [true, 'true in 4'], [false, 'false in 4']]]]])(5, 7, 9, false) === 'false in 4');
|
|
181
|
+
// console.log('getStringGeneratorTest', getStringGenerator(['{1}---{2}', [5, [7, [9, [true, 'true in 4'], [false, 'false in 4 with {2}-{3}']]]]])(5, 7, 9, false) === 'false in 4 with 7-9');
|
|
182
|
+
|
|
183
|
+
// 根据参数列表和模版,返回满足参数的值
|
|
184
|
+
// template格式为result 或[defaultValue, [result0, [indexX, valueX], [indexY, [valueY1, valueY2, ...]], ...], ...]
|
|
185
|
+
export function getTemplateValueOneByOne(values = [], template = []) {
|
|
186
|
+
if (getType(template) !== 'Array') {
|
|
187
|
+
return template;
|
|
188
|
+
}
|
|
189
|
+
const [defaultValue, ...rest] = template;
|
|
190
|
+
let ret = defaultValue;
|
|
191
|
+
for (let i = 0, l = rest.length; i < l; i++) {
|
|
192
|
+
const [r, ...rs] = rest[i] || [];
|
|
193
|
+
const notFitIndex = rs.findIndex(([index, result]) => {
|
|
194
|
+
const v = values[index];
|
|
195
|
+
const typeResult = getType(result);
|
|
196
|
+
// 未定义时。抹平JSON把undefined转成null的情况
|
|
197
|
+
if ((v === undefined || v === null) && (result === undefined || result === null)) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
// 为数组时,等于其中一个值即可
|
|
201
|
+
if (typeResult === 'Array') {
|
|
202
|
+
return result.findIndex(res => {
|
|
203
|
+
return v === res || (v === undefined || v === null) && (res === undefined || res === null);
|
|
204
|
+
}) === -1;
|
|
205
|
+
}
|
|
206
|
+
return v !== result;
|
|
207
|
+
});
|
|
208
|
+
if (notFitIndex === -1 && rs.length !== 0) {
|
|
209
|
+
ret = r;
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return getType(ret) === 'String' ? formatStringRet(ret)(...values) : ret;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// 根据参数列表和模版,进行对比,返回值
|
|
217
|
+
// template格式为result 或[defaultValue, [result0, [[indexX1, indexY1], [defaultValueX1, defaultValueY1], flag1], [[indexX2, indexY2], [defaultValueX2, defaultValueY2], flag2]]]
|
|
218
|
+
// flag取值为lt, lte, e, gte, gt
|
|
219
|
+
export function getTemplateValueCompare(values = [], template = []) {
|
|
220
|
+
if (getType(template) !== 'Array') {
|
|
221
|
+
return template;
|
|
222
|
+
}
|
|
223
|
+
const [defaultValue, ...rest] = template;
|
|
224
|
+
let ret = defaultValue;
|
|
225
|
+
for (let i = 0, l = rest.length; i < l; i++) {
|
|
226
|
+
const [r, ...rs] = rest[i] || [];
|
|
227
|
+
const notFitIndex = rs.findIndex(([[index1, index2], [defaultValue1 = undefined, defaultValue2 = undefined], flag = 'e']) => {
|
|
228
|
+
let v1 = values[index1];
|
|
229
|
+
if (v1 === undefined || v1 === null || v1 === '') {
|
|
230
|
+
v1 = defaultValue1;
|
|
231
|
+
}
|
|
232
|
+
let v2 = values[index2];
|
|
233
|
+
if (v2 === undefined || v2 === null || v2 === '') {
|
|
234
|
+
v2 = defaultValue2;
|
|
235
|
+
}
|
|
236
|
+
if (v1 === undefined || v1 === null || v2 === undefined || v2 === null) {
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
if (['lt', 'lte', 'e', 'ne', 'gte', 'gt'].indexOf(flag) === -1) {
|
|
240
|
+
flag = 'e';
|
|
241
|
+
}
|
|
242
|
+
return !(flag === 'lt' && v1 < v2 || flag === 'lte' && v1 <= v2 || flag === 'e' && v1 === v2 || flag === 'gte' && v1 >= v2 || flag === 'gt' && v1 > v2 || flag === 'ne' && v1 !== v2);
|
|
243
|
+
});
|
|
244
|
+
if (notFitIndex === -1 && rs.length !== 0) {
|
|
245
|
+
ret = r;
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return getType(ret) === 'String' ? formatStringRet(ret)(...values) : ret;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// 根据参数列表和模板,对每一项的值和预设值进行对比,返回值
|
|
253
|
+
// template格式为result 或[defaultValue, [result0, [index0, value0, flag0, defaultValue0], [index1, value1, flag1, defaultValue1]...], ...]
|
|
254
|
+
// flag取值为lt, lte, e, gte, gt
|
|
255
|
+
export function getTemplateValueCompareEach(values = [], template = []) {
|
|
256
|
+
if (getType(template) !== 'Array') {
|
|
257
|
+
return template;
|
|
258
|
+
}
|
|
259
|
+
const [defaultValue, ...rest] = template;
|
|
260
|
+
let ret = defaultValue;
|
|
261
|
+
for (let i = 0, l = rest.length; i < l; i++) {
|
|
262
|
+
const [r, ...rs] = rest[i] || [];
|
|
263
|
+
const notFitIndex = rs.findIndex(([index, value, flag = 'e', defaultValue]) => {
|
|
264
|
+
let v = values[index];
|
|
265
|
+
if (v === undefined || v === null || v === '') {
|
|
266
|
+
v = defaultValue;
|
|
267
|
+
}
|
|
268
|
+
if (['lt', 'lte', 'e', 'ne', 'gte', 'gt'].indexOf(flag) === -1) {
|
|
269
|
+
flag = 'e';
|
|
270
|
+
}
|
|
271
|
+
return !(flag === 'lt' && v < value || flag === 'lte' && v <= value || flag === 'e' && v === value || flag === 'gte' && v >= value || flag === 'gt' && v > value || flag === 'ne' && v !== value);
|
|
272
|
+
});
|
|
273
|
+
if (notFitIndex === -1 && rs.length !== 0) {
|
|
274
|
+
ret = r;
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return getType(ret) === 'String' ? formatStringRet(ret)(...values) : ret;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// 补齐两位
|
|
282
|
+
export function fixNum(n) {
|
|
283
|
+
return `00${n === undefined || n === null ? 0 : n}`.slice(-2);
|
|
284
|
+
}
|
|
285
|
+
export function removeDuplicateIf(array, keyOf) {
|
|
286
|
+
const set = new Set();
|
|
287
|
+
return removeAllIf(array, ele => {
|
|
288
|
+
const k = keyOf(ele);
|
|
289
|
+
if (!set.has(k)) {
|
|
290
|
+
set.add(k);
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
return true;
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
export function removeAllIf(array, fn) {
|
|
297
|
+
const ret = [];
|
|
298
|
+
const idxArr = array.reduceRight((acc, cur, idx) => {
|
|
299
|
+
fn(cur, idx) && acc.push(idx);
|
|
300
|
+
return acc;
|
|
301
|
+
}, []);
|
|
302
|
+
idxArr.forEach(i => {
|
|
303
|
+
ret.push(array.splice(i, 1)[0]);
|
|
304
|
+
});
|
|
305
|
+
return ret;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// @native end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function getValueOrDefault(host: any, p: string, d: any): any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getPluralRules(locale: string, n: number, ord?: boolean): string;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// 标准 https://unicode-org.github.io/cldr-staging/charts/latest/supplemental/language_plural_rules.html
|
|
2
|
+
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
// import { Host } from 'miot';
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
const Quantity = {
|
|
8
|
+
zero: 'zero', // 语言需要对数字0进行特殊处理。(比如阿拉伯语)
|
|
9
|
+
one: 'one', // 语言需要对类似1的数字进行特殊处理。(比如英语和其它大多数语言里的1;在俄语里,任何以1结尾但不以11结尾的数也属于此类型。)
|
|
10
|
+
two: 'two', // 语言需要对类似2的数字进行特殊处理。(比如威尔士语)
|
|
11
|
+
few: 'few', // 语言需要对较小数字进行特殊处理(比如捷克语里的2、3、4;或者波兰语里以2、3、4结尾但不是12、13、14的数。)
|
|
12
|
+
many: 'many', // 语言需要对较大数字进行特殊处理(比如马耳他语里以11-99结尾的数)
|
|
13
|
+
other: 'other', // 语言不需要对数字进行特殊处理。
|
|
14
|
+
};
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 获取复数规则,实现参考 https://formatjs.io/docs/polyfills/intl-pluralrules/
|
|
19
|
+
* @param {string} locale
|
|
20
|
+
* @param {number} number
|
|
21
|
+
* @param {string} type cardinal / ordinal
|
|
22
|
+
* @returns {string} Quantity
|
|
23
|
+
*/
|
|
24
|
+
/* eslint-disable eqeqeq */
|
|
25
|
+
export function getPluralRules(locale, n, ord = false) {
|
|
26
|
+
if (locale === 'zh' || locale === 'zh_tw' || locale === 'zh_hk') {
|
|
27
|
+
return 'other';
|
|
28
|
+
}
|
|
29
|
+
if (locale === 'de') {
|
|
30
|
+
const s = String(n).split('.');
|
|
31
|
+
const v0 = !s[1];
|
|
32
|
+
if (ord) return 'other';
|
|
33
|
+
return n == 1 && v0 ? 'one' : 'other';
|
|
34
|
+
}
|
|
35
|
+
if (locale === 'en') {
|
|
36
|
+
const s = String(n).split('.');
|
|
37
|
+
const v0 = !s[1];
|
|
38
|
+
const t0 = Number(s[0]) == n;
|
|
39
|
+
const n10 = t0 && s[0].slice(-1);
|
|
40
|
+
const n100 = t0 && s[0].slice(-2);
|
|
41
|
+
if (ord)
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
return n10 == 1 && n100 != 11 ? 'one' : n10 == 2 && n100 != 12 ? 'two' : n10 == 3 && n100 != 13 ? 'few' : 'other';
|
|
44
|
+
return n == 1 && v0 ? 'one' : 'other';
|
|
45
|
+
}
|
|
46
|
+
if (locale === 'es') {
|
|
47
|
+
const _n = String(n);
|
|
48
|
+
const se = _n.split(/[ce]/);
|
|
49
|
+
const e = Number(se[1]) || 0;
|
|
50
|
+
// const c = e;
|
|
51
|
+
const s = String(e ? Number(se[0]) * 10 ** e : _n).split('.');
|
|
52
|
+
const i = s[0];
|
|
53
|
+
const v0 = !s[1];
|
|
54
|
+
const i1000000 = i.slice(-6);
|
|
55
|
+
if (ord) return 'other';
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
return n == 1 ? 'one' : e == 0 && i != 0 && i1000000 == 0 && v0 || e < 0 || e > 5 ? 'many' : 'other';
|
|
58
|
+
}
|
|
59
|
+
if (locale === 'fr') {
|
|
60
|
+
const _n = String(n);
|
|
61
|
+
const se = _n.split(/[ce]/);
|
|
62
|
+
const e = Number(se[1]) || 0;
|
|
63
|
+
// const c = e;
|
|
64
|
+
const s = String(e ? Number(se[0]) * 10 ** e : _n).split('.');
|
|
65
|
+
const i = s[0];
|
|
66
|
+
const v0 = !s[1];
|
|
67
|
+
const i1000000 = i.slice(-6);
|
|
68
|
+
if (ord) return n == 1 ? 'one' : 'other';
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
return n >= 0 && n < 2 ? 'one' : e == 0 && i != 0 && i1000000 == 0 && v0 || e < 0 || e > 5 ? 'many' : 'other';
|
|
71
|
+
}
|
|
72
|
+
if (locale === 'id') {
|
|
73
|
+
return 'other';
|
|
74
|
+
}
|
|
75
|
+
if (locale === 'it') {
|
|
76
|
+
const _n = String(n);
|
|
77
|
+
const se = _n.split(/[ce]/);
|
|
78
|
+
const e = Number(se[1]) || 0;
|
|
79
|
+
// const c = e;
|
|
80
|
+
const s = String(e ? Number(se[0]) * 10 ** e : _n).split('.');
|
|
81
|
+
const i = s[0];
|
|
82
|
+
const v0 = !s[1];
|
|
83
|
+
const i1000000 = i.slice(-6);
|
|
84
|
+
if (ord) return n == 11 || n == 8 || n == 80 || n == 800 ? 'many' : 'other';
|
|
85
|
+
// @ts-ignore
|
|
86
|
+
return n == 1 && v0 ? 'one' : e == 0 && i != 0 && i1000000 == 0 && v0 || e < 0 || e > 5 ? 'many' : 'other';
|
|
87
|
+
}
|
|
88
|
+
if (locale === 'ja') {
|
|
89
|
+
return 'other';
|
|
90
|
+
}
|
|
91
|
+
if (locale === 'ko') {
|
|
92
|
+
return 'other';
|
|
93
|
+
}
|
|
94
|
+
if (locale === 'nl') {
|
|
95
|
+
const s = String(n).split('.');
|
|
96
|
+
const v0 = !s[1];
|
|
97
|
+
if (ord) return 'other';
|
|
98
|
+
return n == 1 && v0 ? 'one' : 'other';
|
|
99
|
+
}
|
|
100
|
+
if (locale === 'pl') {
|
|
101
|
+
const s = String(n).split('.');
|
|
102
|
+
const i = s[0];
|
|
103
|
+
const v0 = !s[1];
|
|
104
|
+
const i10 = Number(i.slice(-1));
|
|
105
|
+
const i100 = Number(i.slice(-2));
|
|
106
|
+
if (ord) return 'other';
|
|
107
|
+
return n == 1 && v0 ? 'one' : v0 && i10 >= 2 && i10 <= 4 && (i100 < 12 || i100 > 14) ? 'few' : v0 && i != '1' && (i10 == 0 || i10 == 1) || v0 && i10 >= 5 && i10 <= 9 || v0 && i100 >= 12 && i100 <= 14 ? 'many' : 'other';
|
|
108
|
+
}
|
|
109
|
+
if (locale === 'pt') {
|
|
110
|
+
const _n = String(n);
|
|
111
|
+
const se = _n.split(/[ce]/);
|
|
112
|
+
const e = Number(se[1]) || 0;
|
|
113
|
+
// const c = e;
|
|
114
|
+
const s = String(e ? Number(se[0]) * 10 ** e : _n).split('.');
|
|
115
|
+
const i = s[0];
|
|
116
|
+
const v0 = !s[1];
|
|
117
|
+
const i1000000 = i.slice(-6);
|
|
118
|
+
if (ord) return 'other';
|
|
119
|
+
// @ts-ignore
|
|
120
|
+
return i == '0' || i == '1' ? 'one' : e == 0 && i != '0' && i1000000 == '0' && v0 || e < 0 || e > 5 ? 'many' : 'other';
|
|
121
|
+
}
|
|
122
|
+
if (locale === 'ru') {
|
|
123
|
+
const s = String(n).split('.');
|
|
124
|
+
const i = s[0];
|
|
125
|
+
const v0 = !s[1];
|
|
126
|
+
const i10 = Number(i.slice(-1));
|
|
127
|
+
const i100 = Number(i.slice(-2));
|
|
128
|
+
if (ord) return 'other';
|
|
129
|
+
return v0 && i10 == 1 && i100 != 11 ? 'one' : v0 && i10 >= 2 && i10 <= 4 && (i100 < 12 || i100 > 14) ? 'few' : v0 && i10 == 0 || v0 && i10 >= 5 && i10 <= 9 || v0 && i100 >= 11 && i100 <= 14 ? 'many' : 'other';
|
|
130
|
+
}
|
|
131
|
+
if (locale === 'th') {
|
|
132
|
+
if (ord) return 'other';
|
|
133
|
+
return n == 1 ? 'one' : 'other';
|
|
134
|
+
}
|
|
135
|
+
if (locale === 'tr') {
|
|
136
|
+
if (ord) return 'other';
|
|
137
|
+
return n == 1 ? 'one' : 'other';
|
|
138
|
+
}
|
|
139
|
+
return 'other';
|
|
140
|
+
}
|
|
141
|
+
/* eslint-enable eqeqeq */
|