@koine/i18n 2.0.0-beta.28 → 2.0.0-beta.29
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/i18nGenerateTypes.js +98 -31
- package/package.json +15 -3
package/i18nGenerateTypes.js
CHANGED
|
@@ -1,15 +1,72 @@
|
|
|
1
|
-
import { __awaiter, __generator } from "tslib";
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { __awaiter, __generator, __read, __spreadArray } from "tslib";
|
|
2
|
+
import { forin, isNumericLiteral, objectPick, split } from "@koine/utils";
|
|
3
|
+
var pluralSuffixes = ["zero", "one", "two", "few", "many", "other"];
|
|
4
|
+
var requiredPluralSuffix = "other";
|
|
5
|
+
var isPluralSuffix = function (suffix) {
|
|
6
|
+
return (pluralSuffixes.includes(suffix) ||
|
|
7
|
+
isNumericLiteral(suffix));
|
|
8
|
+
};
|
|
9
|
+
var isPluralKey = function (key) {
|
|
10
|
+
var _a = __read(split(key, "_"), 2), suffix = _a[1];
|
|
11
|
+
return isPluralSuffix(suffix);
|
|
12
|
+
};
|
|
13
|
+
var groupPluralsKeysByRoot = function (pluralKeys) {
|
|
14
|
+
var map = {};
|
|
15
|
+
pluralKeys.forEach(function (key) {
|
|
16
|
+
var _a = __read(split(key, "_"), 1), root = _a[0];
|
|
17
|
+
map[root] = map[root] || [];
|
|
18
|
+
map[root].push(key);
|
|
19
|
+
});
|
|
20
|
+
return map;
|
|
21
|
+
};
|
|
22
|
+
var transformKeysForPlurals = function (keys) {
|
|
23
|
+
var pluralKeys = keys.filter(isPluralKey);
|
|
24
|
+
if (pluralKeys.length) {
|
|
25
|
+
var transformedKeys_1 = __spreadArray([], __read(keys), false);
|
|
26
|
+
var groupedPlurals = groupPluralsKeysByRoot(pluralKeys);
|
|
27
|
+
forin(groupedPlurals, function (pluralRoot, pluralKeys) {
|
|
28
|
+
if (!keys.includes(pluralRoot)) {
|
|
29
|
+
transformedKeys_1.push(pluralRoot);
|
|
30
|
+
}
|
|
31
|
+
pluralKeys.forEach(function (pluralKey) {
|
|
32
|
+
if (keys.includes(pluralKey)) {
|
|
33
|
+
transformedKeys_1 = transformedKeys_1.filter(function (k) { return k !== pluralKey; });
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
return transformedKeys_1;
|
|
38
|
+
}
|
|
39
|
+
return keys;
|
|
40
|
+
};
|
|
41
|
+
var analyseObjectPlurals = function (obj) {
|
|
42
|
+
var keys = Object.keys(obj);
|
|
43
|
+
var hasPlurals = keys.includes(requiredPluralSuffix);
|
|
44
|
+
var hasOnlyPluralKeys = false;
|
|
45
|
+
var newValue = obj;
|
|
46
|
+
if (hasPlurals) {
|
|
47
|
+
var nonPluralKeys = keys.filter(function (k) { return !isPluralSuffix(k); });
|
|
48
|
+
hasOnlyPluralKeys = nonPluralKeys.length === 0;
|
|
49
|
+
newValue = objectPick(obj, nonPluralKeys);
|
|
8
50
|
}
|
|
9
|
-
return
|
|
51
|
+
return {
|
|
52
|
+
hasPlurals: hasPlurals,
|
|
53
|
+
hasOnlyPluralKeys: hasOnlyPluralKeys,
|
|
54
|
+
newValue: newValue,
|
|
55
|
+
};
|
|
10
56
|
};
|
|
11
|
-
function
|
|
12
|
-
if (
|
|
57
|
+
var getTypeForObjectEntry = function (key, value) {
|
|
58
|
+
if (typeof value === "object") {
|
|
59
|
+
var _a = analyseObjectPlurals(value), hasPlurals = _a.hasPlurals, hasOnlyPluralKeys = _a.hasOnlyPluralKeys, newValue = _a.newValue;
|
|
60
|
+
if (hasOnlyPluralKeys) {
|
|
61
|
+
return "'".concat(key, "': string;");
|
|
62
|
+
}
|
|
63
|
+
if (hasPlurals) {
|
|
64
|
+
return "'".concat(key, "': string | ").concat(getType(newValue));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return "'".concat(key, "': ").concat(getType(value));
|
|
68
|
+
};
|
|
69
|
+
function getType(value) {
|
|
13
70
|
var out = "";
|
|
14
71
|
var primitiveType = "";
|
|
15
72
|
if (typeof value === "boolean") {
|
|
@@ -26,16 +83,15 @@ function getType(value, options) {
|
|
|
26
83
|
}
|
|
27
84
|
else if (Array.isArray(value)) {
|
|
28
85
|
var firstValue = value[0];
|
|
29
|
-
out += "".concat(getType(firstValue
|
|
86
|
+
out += "".concat(getType(firstValue), "[];");
|
|
30
87
|
}
|
|
31
88
|
else if (typeof value === "object") {
|
|
32
89
|
out += "{";
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
90
|
+
var keys = transformKeysForPlurals(Object.keys(value));
|
|
91
|
+
for (var i = 0; i < keys.length; i++) {
|
|
92
|
+
var key = keys[i];
|
|
93
|
+
var single = value[key] || "";
|
|
94
|
+
out += getTypeForObjectEntry(key, single);
|
|
39
95
|
}
|
|
40
96
|
out += "};";
|
|
41
97
|
}
|
|
@@ -45,22 +101,33 @@ function getType(value, options) {
|
|
|
45
101
|
}
|
|
46
102
|
export function i18nGenerateTypes(options) {
|
|
47
103
|
return __awaiter(this, void 0, void 0, function () {
|
|
48
|
-
var defaultLocale, files, defaultLocaleFiles, header, footer, out, i, _a, path, data, namespace;
|
|
104
|
+
var defaultLocale, files, defaultLocaleFiles, header, footer, out, i, _a, path, data, namespace, format;
|
|
49
105
|
return __generator(this, function (_b) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
106
|
+
switch (_b.label) {
|
|
107
|
+
case 0:
|
|
108
|
+
defaultLocale = options.defaultLocale, files = options.files;
|
|
109
|
+
defaultLocaleFiles = files.filter(function (f) { return f.locale === defaultLocale; });
|
|
110
|
+
header = "\n/* eslint-disable @typescript-eslint/ban-types */\n/* eslint-disable @typescript-eslint/no-namespace */\n\ndeclare namespace Koine {\n interface Translations {\n";
|
|
111
|
+
footer = "\n }\n}\n";
|
|
112
|
+
out = header;
|
|
113
|
+
for (i = 0; i < defaultLocaleFiles.length; i++) {
|
|
114
|
+
_a = defaultLocaleFiles[i], path = _a.path, data = _a.data;
|
|
115
|
+
namespace = path.replace(".json", "");
|
|
116
|
+
out += "\"".concat(namespace, "\": ").concat(getType(data), "\n");
|
|
117
|
+
}
|
|
118
|
+
out += footer;
|
|
119
|
+
if (!!process.env["JEST_WORKER_ID"]) return [3, 3];
|
|
120
|
+
return [4, import("prettier")];
|
|
121
|
+
case 1:
|
|
122
|
+
format = (_b.sent()).format;
|
|
123
|
+
return [4, format(out, {
|
|
124
|
+
parser: "typescript",
|
|
125
|
+
})];
|
|
126
|
+
case 2:
|
|
127
|
+
out = _b.sent();
|
|
128
|
+
_b.label = 3;
|
|
129
|
+
case 3: return [2, out];
|
|
62
130
|
}
|
|
63
|
-
return [2, out];
|
|
64
131
|
});
|
|
65
132
|
});
|
|
66
133
|
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,16 @@
|
|
|
2
2
|
"name": "@koine/i18n",
|
|
3
3
|
"sideEffects": false,
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@koine/node": "2.0.0-beta.
|
|
5
|
+
"@koine/node": "2.0.0-beta.29",
|
|
6
|
+
"@koine/utils": "2.0.0-beta.29"
|
|
7
|
+
},
|
|
8
|
+
"peerDependenciesMeta": {
|
|
9
|
+
"prettier": {
|
|
10
|
+
"optional": true
|
|
11
|
+
},
|
|
12
|
+
"glob": {
|
|
13
|
+
"optional": true
|
|
14
|
+
}
|
|
6
15
|
},
|
|
7
16
|
"module": "./index.js",
|
|
8
17
|
"type": "module",
|
|
@@ -35,6 +44,9 @@
|
|
|
35
44
|
"import": "./types.js"
|
|
36
45
|
}
|
|
37
46
|
},
|
|
38
|
-
"peerDependencies": {
|
|
39
|
-
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"prettier": "^3.1.1",
|
|
49
|
+
"glob": "^10.3.10"
|
|
50
|
+
},
|
|
51
|
+
"version": "2.0.0-beta.29"
|
|
40
52
|
}
|