fluent-transpiler 0.3.0 → 0.3.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/cli.js +3 -3
- package/index.js +43 -42
- package/package.json +1 -1
package/cli.js
CHANGED
|
@@ -26,19 +26,19 @@ new Command()
|
|
|
26
26
|
)
|
|
27
27
|
.addOption(
|
|
28
28
|
new Option(
|
|
29
|
-
'--include <
|
|
29
|
+
'--include-key <includeMessageKey...>',
|
|
30
30
|
'Allowed messages to be included. Default to include all.'
|
|
31
31
|
)
|
|
32
32
|
)
|
|
33
33
|
.addOption(
|
|
34
34
|
new Option(
|
|
35
|
-
'--exclude <
|
|
35
|
+
'--exclude-key <excludeMessageKey...>',
|
|
36
36
|
'Ignored messages to be excluded. Default to exclude none.'
|
|
37
37
|
)
|
|
38
38
|
)
|
|
39
39
|
.addOption(
|
|
40
40
|
new Option(
|
|
41
|
-
'--exclude-value <excludeMessageValue
|
|
41
|
+
'--exclude-value <excludeMessageValue>',
|
|
42
42
|
'Set message to an empty string when it contains this value. Default to not allowing empty strings.'
|
|
43
43
|
)
|
|
44
44
|
)
|
package/index.js
CHANGED
|
@@ -2,19 +2,19 @@ import { parse } from '@fluent/syntax'
|
|
|
2
2
|
import { camelCase, pascalCase, constantCase, snakeCase } from 'change-case'
|
|
3
3
|
|
|
4
4
|
const exportDefault = `(id, params) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
const source = __exports[id] ?? __exports['_'+id]
|
|
6
|
+
if (typeof source === 'undefined') return '*** '+id+' ***'
|
|
7
|
+
if (typeof source === 'function') return source(params)
|
|
8
|
+
return source
|
|
9
9
|
}
|
|
10
10
|
`
|
|
11
11
|
export const compile = (src, opts) => {
|
|
12
12
|
const options = {
|
|
13
13
|
comments: true,
|
|
14
14
|
errorOnJunk: true,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
includeKey: [],
|
|
16
|
+
excludeKey: [],
|
|
17
|
+
excludeValue: undefined,
|
|
18
18
|
//treeShaking: false,
|
|
19
19
|
variableNotation: 'camelCase',
|
|
20
20
|
disableMinify: false, // TODO needs better name strictInterface?
|
|
@@ -24,14 +24,15 @@ export const compile = (src, opts) => {
|
|
|
24
24
|
...opts
|
|
25
25
|
}
|
|
26
26
|
if (!Array.isArray(options.locale)) options.locale = [options.locale]
|
|
27
|
-
if (!Array.isArray(options.
|
|
28
|
-
options.
|
|
29
|
-
if (!Array.isArray(options.
|
|
30
|
-
options.
|
|
31
|
-
if (options.
|
|
27
|
+
if (!Array.isArray(options.includeKey))
|
|
28
|
+
options.includeKey = [options.includeKey]
|
|
29
|
+
if (!Array.isArray(options.excludeKey))
|
|
30
|
+
options.excludeKey = [options.excludeKey]
|
|
31
|
+
if (options.excludeValue) {
|
|
32
32
|
// cast to template literal
|
|
33
|
-
options.
|
|
33
|
+
options.excludeValue = '`' + options.excludeValue + '`'
|
|
34
34
|
}
|
|
35
|
+
console.log({ options })
|
|
35
36
|
|
|
36
37
|
const metadata = {}
|
|
37
38
|
const exports = []
|
|
@@ -118,15 +119,15 @@ export const compile = (src, opts) => {
|
|
|
118
119
|
const assignment = compileAssignment(data.id)
|
|
119
120
|
|
|
120
121
|
if (
|
|
121
|
-
options.
|
|
122
|
-
!options.
|
|
122
|
+
options.includeKey.length &&
|
|
123
|
+
!options.includeKey.includes(assignment)
|
|
123
124
|
) {
|
|
124
125
|
return ''
|
|
125
126
|
}
|
|
126
127
|
|
|
127
128
|
if (
|
|
128
|
-
options.
|
|
129
|
-
options.
|
|
129
|
+
options.excludeKey.length &&
|
|
130
|
+
options.excludeKey.includes(assignment)
|
|
130
131
|
) {
|
|
131
132
|
return ''
|
|
132
133
|
}
|
|
@@ -134,7 +135,7 @@ export const compile = (src, opts) => {
|
|
|
134
135
|
const templateStringLiteral =
|
|
135
136
|
data.value && compileType(data.value, data.type)
|
|
136
137
|
|
|
137
|
-
if (options.
|
|
138
|
+
if (options.excludeValue === templateStringLiteral) {
|
|
138
139
|
templateStringLiteral = '``'
|
|
139
140
|
}
|
|
140
141
|
|
|
@@ -183,12 +184,12 @@ export const compile = (src, opts) => {
|
|
|
183
184
|
}
|
|
184
185
|
return `export const ${assignment} = ${message}`
|
|
185
186
|
/*} else {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
187
|
+
if (assignment === metadata[assignment].id) {
|
|
188
|
+
exports.push(`${assignment}: ${message}`)
|
|
189
|
+
} else {
|
|
190
|
+
exports.push(`'${metadata[assignment].id}': ${message}`)
|
|
191
|
+
}
|
|
192
|
+
}*/
|
|
192
193
|
return ''
|
|
193
194
|
},
|
|
194
195
|
Comment: (data) => {
|
|
@@ -369,10 +370,10 @@ const formatTime = (value) => {
|
|
|
369
370
|
value = new Date(value)
|
|
370
371
|
if (isNaN(value.getTime())) return value
|
|
371
372
|
try {
|
|
372
|
-
|
|
373
|
-
|
|
373
|
+
const [duration, unit] = relativeTimeDiff(value)
|
|
374
|
+
return relativeTimeFormat.format(duration, unit)
|
|
374
375
|
} catch (e) {
|
|
375
|
-
|
|
376
|
+
return dateTimeFormat.format(value)
|
|
376
377
|
}
|
|
377
378
|
}
|
|
378
379
|
*/
|
|
@@ -386,24 +387,24 @@ const __relativeTimeDiff = (d) => {
|
|
|
386
387
|
const msPerMonth = msPerDay * 30
|
|
387
388
|
const msPerYear = msPerDay * 365.25
|
|
388
389
|
const elapsed = d - new Date()
|
|
389
|
-
|
|
390
|
+
|
|
390
391
|
if (Math.abs(elapsed) < msPerMinute) {
|
|
391
392
|
return [Math.round(elapsed / 1000), 'second']
|
|
392
393
|
}
|
|
393
394
|
if (Math.abs(elapsed) < msPerHour) {
|
|
394
|
-
|
|
395
|
+
return [Math.round(elapsed / msPerMinute), 'minute']
|
|
395
396
|
}
|
|
396
397
|
if (Math.abs(elapsed) < msPerDay) {
|
|
397
|
-
|
|
398
|
+
return [Math.round(elapsed / msPerHour), 'hour']
|
|
398
399
|
}
|
|
399
400
|
if (Math.abs(elapsed) < msPerWeek * 2) {
|
|
400
|
-
|
|
401
|
+
return [Math.round(elapsed / msPerDay), 'day']
|
|
401
402
|
}
|
|
402
403
|
if (Math.abs(elapsed) < msPerMonth) {
|
|
403
|
-
|
|
404
|
+
return [Math.round(elapsed / msPerWeek), 'week']
|
|
404
405
|
}
|
|
405
406
|
if (Math.abs(elapsed) < msPerYear) {
|
|
406
|
-
|
|
407
|
+
return [Math.round(elapsed / msPerMonth), 'month']
|
|
407
408
|
}
|
|
408
409
|
return [Math.round(elapsed / msPerYear), 'year']
|
|
409
410
|
}
|
|
@@ -411,8 +412,8 @@ const __formatRelativeTime = (value, options) => {
|
|
|
411
412
|
if (typeof value === 'string') value = new Date(value)
|
|
412
413
|
if (isNaN(value.getTime())) return value
|
|
413
414
|
try {
|
|
414
|
-
|
|
415
|
-
|
|
415
|
+
const [duration, unit] = __relativeTimeDiff(value)
|
|
416
|
+
return new Intl.RelativeTimeFormat(__locales, options).format(duration, unit)
|
|
416
417
|
} catch (e) {}
|
|
417
418
|
return new Intl.DateTimeFormat(__locales, options).format(value)
|
|
418
419
|
}
|
|
@@ -421,16 +422,16 @@ const __formatRelativeTime = (value, options) => {
|
|
|
421
422
|
if (functions.__formatDateTime) {
|
|
422
423
|
output += `
|
|
423
424
|
const __formatDateTime = (value, options) => {
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
425
|
+
if (typeof value === 'string') value = new Date(value)
|
|
426
|
+
if (isNaN(value.getTime())) return value
|
|
427
|
+
return new Intl.DateTimeFormat(__locales, options).format(value)
|
|
427
428
|
}
|
|
428
429
|
`
|
|
429
430
|
}
|
|
430
431
|
if (functions.__formatVariable || functions.__formatNumber) {
|
|
431
432
|
output += `
|
|
432
433
|
const __formatNumber = (value, options) => {
|
|
433
|
-
|
|
434
|
+
return new Intl.NumberFormat(__locales, options).format(value)
|
|
434
435
|
}
|
|
435
436
|
`
|
|
436
437
|
}
|
|
@@ -447,9 +448,9 @@ const __formatVariable = (value) => {
|
|
|
447
448
|
if (functions.__select) {
|
|
448
449
|
output += `
|
|
449
450
|
const __select = (value, cases, fallback, options) => {
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
451
|
+
const pluralRules = new Intl.PluralRules(__locales, options)
|
|
452
|
+
const rule = pluralRules.select(value)
|
|
453
|
+
return cases[value] ?? cases[rule] ?? fallback
|
|
453
454
|
}
|
|
454
455
|
`
|
|
455
456
|
}
|