-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathhelpers.js
361 lines (324 loc) · 10.1 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import { pathToFileURL } from 'url'
import { build } from 'esbuild'
import { rmSync } from 'fs'
import yaml from 'js-yaml'
import { builtinModules } from 'module'
/**
* Take an entry for the Parser and turn it into a hash,
* turning the key path 'foo.bar' into an hash {foo: {bar: ""}}
* The generated hash can be merged with an optional `target`.
* @returns An `{ target, duplicate, conflict }` object. `target` is the hash
* that was passed as an argument or a new hash if none was passed. `duplicate`
* indicates whether the entry already existed in the `target` hash. `conflict`
* is `"key"` if a parent of the key was already mapped to a string (e.g. when
* merging entry {one: {two: "bla"}} with target {one: "bla"}) or the key was
* already mapped to a map (e.g. when merging entry {one: "bla"} with target
* {one: {two: "bla"}}), `"value"` if the same key already exists with a
* different value, or `false`.
*/
function dotPathToHash(entry, target = {}, options = {}) {
let conflict = false
let duplicate = false
let path = entry.keyWithNamespace
if (options.suffix) {
path += options.suffix
}
const separator = options.separator || '.'
const key = entry.keyWithNamespace.substring(
entry.keyWithNamespace.indexOf(separator) + separator.length,
entry.keyWithNamespace.length
)
// There is no key to process so we return an empty object
if (!key) {
if (!target[entry.namespace]) {
target[entry.namespace] = {}
}
return { target, duplicate, conflict }
}
const defaultValue =
entry[`defaultValue${options.suffix}`] || entry.defaultValue || ''
let newValue =
typeof options.value === 'function'
? options.value(options.locale, entry.namespace, key, defaultValue)
: options.value || defaultValue
if (path.endsWith(separator)) {
path = path.slice(0, -separator.length)
}
const segments = path.split(separator)
let inner = target
for (let i = 0; i < segments.length - 1; i += 1) {
const segment = segments[i]
if (segment) {
if (typeof inner[segment] === 'string') {
conflict = 'key'
}
if (inner[segment] === undefined || conflict) {
inner[segment] = {}
}
inner = inner[segment]
}
}
const lastSegment = segments[segments.length - 1]
const oldValue = inner[lastSegment]
if (oldValue !== undefined && oldValue !== newValue) {
if (typeof oldValue !== typeof newValue) {
conflict = 'key'
} else if (oldValue !== '') {
if (newValue === '') {
newValue = oldValue
} else {
conflict = 'value'
}
}
}
duplicate = oldValue !== undefined || conflict !== false
if (options.customValueTemplate) {
inner[lastSegment] = {}
const entries = Object.entries(options.customValueTemplate)
entries.forEach((valueEntry) => {
if (valueEntry[1] === '${defaultValue}') {
inner[lastSegment][valueEntry[0]] = newValue
} else if (valueEntry[1] === '${filePaths}') {
inner[lastSegment][valueEntry[0]] = entry.filePaths
} else {
inner[lastSegment][valueEntry[0]] =
entry[valueEntry[1].replace(/\${(\w+)}/, '$1')] || ''
}
})
} else {
inner[lastSegment] = newValue
}
return { target, duplicate, conflict }
}
/**
* Takes a `source` hash and makes sure its value
* is pasted in the `target` hash, if the target
* hash has the corresponding key (or if `options.keepRemoved` is true).
* @returns An `{ old, new, mergeCount, pullCount, oldCount, reset, resetCount }` object.
* `old` is a hash of values that have not been merged into `target`.
* `new` is `target`. `mergeCount` is the number of keys merged into
* `new`, `pullCount` is the number of context and plural keys added to
* `new` and `oldCount` is the number of keys that were either added to `old` or
* `new` (if `options.keepRemoved` is true and `target` didn't have the corresponding
* key) and `reset` is the keys that were reset due to not matching default values,
* and `resetCount` which is the number of keys reset.
*/
function mergeHashes(source, target, options = {}, resetValues = {}) {
let old = {}
let reset = {}
let mergeCount = 0
let pullCount = 0
let oldCount = 0
let resetCount = 0
const keepRemoved =
(typeof options.keepRemoved === 'boolean' && options.keepRemoved) || false
const keepRemovedPatterns =
(typeof options.keepRemoved !== 'boolean' && options.keepRemoved) || []
const fullKeyPrefix = options.fullKeyPrefix || ''
const keySeparator = options.keySeparator || '.'
const pluralSeparator = options.pluralSeparator || '_'
const contextSeparator = options.contextSeparator || '_'
for (const key in source) {
const hasNestedEntries =
typeof target[key] === 'object' && !Array.isArray(target[key])
if (hasNestedEntries) {
const nested = mergeHashes(
source[key],
target[key],
{ ...options, fullKeyPrefix: fullKeyPrefix + key + keySeparator },
resetValues[key]
)
mergeCount += nested.mergeCount
pullCount += nested.pullCount
oldCount += nested.oldCount
resetCount += nested.resetCount
if (Object.keys(nested.old).length) {
old[key] = nested.old
}
if (Object.keys(nested.reset).length) {
reset[key] = nested.reset
}
} else if (target[key] !== undefined) {
if (typeof source[key] !== 'string' && !Array.isArray(source[key])) {
old[key] = source[key]
oldCount += 1
} else {
if (
(options.resetAndFlag &&
!isPlural(key) &&
typeof source[key] === 'string' &&
source[key] !== target[key]) ||
resetValues[key]
) {
old[key] = source[key]
oldCount += 1
reset[key] = true
resetCount += 1
} else {
target[key] = source[key]
mergeCount += 1
}
}
} else {
// support for plural in keys
const singularKey = getSingularForm(key, pluralSeparator)
const pluralMatch = key !== singularKey
// support for context in keys
const contextRegex = new RegExp(
`\\${contextSeparator}([^\\${contextSeparator}]+)?$`
)
const contextMatch = contextRegex.test(singularKey)
const rawKey = singularKey.replace(contextRegex, '')
if (
(contextMatch && target[rawKey] !== undefined) ||
(pluralMatch &&
hasRelatedPluralKey(`${singularKey}${pluralSeparator}`, target))
) {
target[key] = source[key]
pullCount += 1
} else {
const keepKey =
keepRemoved ||
keepRemovedPatterns.some((pattern) =>
pattern.test(fullKeyPrefix + key)
)
if (keepKey) {
target[key] = source[key]
} else {
old[key] = source[key]
}
oldCount += 1
}
}
}
return {
old,
new: target,
mergeCount,
pullCount,
oldCount,
reset,
resetCount,
}
}
/**
* Merge `source` into `target` by merging nested dictionaries.
*/
function transferValues(source, target) {
for (const key in source) {
const sourceValue = source[key]
const targetValue = target[key]
if (
typeof sourceValue === 'object' &&
typeof targetValue === 'object' &&
!Array.isArray(sourceValue)
) {
transferValues(sourceValue, targetValue)
} else {
target[key] = sourceValue
}
}
}
const pluralSuffixes = ['zero', 'one', 'two', 'few', 'many', 'other']
function isPlural(key) {
return pluralSuffixes.some((suffix) => key.endsWith(suffix))
}
function hasRelatedPluralKey(rawKey, source) {
return pluralSuffixes.some(
(suffix) => source[`${rawKey}${suffix}`] !== undefined
)
}
function getSingularForm(key, pluralSeparator) {
const pluralRegex = new RegExp(
`(\\${pluralSeparator}(?:zero|one|two|few|many|other))$`
)
return key.replace(pluralRegex, '')
}
function getPluralSuffixPosition(key) {
for (let i = 0, len = pluralSuffixes.length; i < len; i++) {
if (key.endsWith(pluralSuffixes[i])) return i
}
return -1
}
function makeDefaultSort(pluralSeparator) {
return function defaultSort(key1, key2) {
const singularKey1 = getSingularForm(key1, pluralSeparator)
const singularKey2 = getSingularForm(key2, pluralSeparator)
if (singularKey1 === singularKey2) {
return getPluralSuffixPosition(key1) - getPluralSuffixPosition(key2)
}
return singularKey1.localeCompare(singularKey2, 'en')
}
}
async function esConfigLoader(filepath) {
return (await import(pathToFileURL(filepath))).default
}
async function tsConfigLoader(filepath) {
const outfile = filepath + '.bundle.mjs'
await build({
absWorkingDir: process.cwd(),
entryPoints: [filepath],
outfile,
write: true,
target: ['node14.18', 'node16'],
platform: 'node',
bundle: true,
format: 'esm',
sourcemap: 'inline',
external: [
...builtinModules,
...builtinModules.map((mod) => 'node:' + mod),
],
})
const config = await esConfigLoader(outfile)
rmSync(outfile)
return config
}
function yamlConfigLoader(filepath, content) {
return yaml.load(content)
}
// unescape common html entities
// code from react-18next taken from
// https://github.com/i18next/react-i18next/blob/d3247b5c232f5d8c1a154fe5dd0090ca88c82dcf/src/unescape.js
function unescape(text) {
const matchHtmlEntity =
/&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34|nbsp|#160|copy|#169|reg|#174|hellip|#8230|#x2F|#47);/g
const htmlEntities = {
'&': '&',
'&': '&',
'<': '<',
'<': '<',
'>': '>',
'>': '>',
''': "'",
''': "'",
'"': '"',
'"': '"',
' ': ' ',
' ': ' ',
'©': '©',
'©': '©',
'®': '®',
'®': '®',
'…': '…',
'…': '…',
'/': '/',
'/': '/',
}
const unescapeHtmlEntity = (m) => htmlEntities[m]
return text.replace(matchHtmlEntity, unescapeHtmlEntity)
}
export {
dotPathToHash,
mergeHashes,
transferValues,
hasRelatedPluralKey,
getSingularForm,
getPluralSuffixPosition,
makeDefaultSort,
esConfigLoader,
tsConfigLoader,
yamlConfigLoader,
unescape,
isPlural,
}