fluent-transpiler 0.3.0 → 0.3.2
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 -43
- 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,13 +24,13 @@ 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
35
|
|
|
36
36
|
const metadata = {}
|
|
@@ -118,23 +118,23 @@ export const compile = (src, opts) => {
|
|
|
118
118
|
const assignment = compileAssignment(data.id)
|
|
119
119
|
|
|
120
120
|
if (
|
|
121
|
-
options.
|
|
122
|
-
!options.
|
|
121
|
+
options.includeKey.length &&
|
|
122
|
+
!options.includeKey.includes(assignment)
|
|
123
123
|
) {
|
|
124
124
|
return ''
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
if (
|
|
128
|
-
options.
|
|
129
|
-
options.
|
|
128
|
+
options.excludeKey.length &&
|
|
129
|
+
options.excludeKey.includes(assignment)
|
|
130
130
|
) {
|
|
131
131
|
return ''
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
|
|
134
|
+
let templateStringLiteral =
|
|
135
135
|
data.value && compileType(data.value, data.type)
|
|
136
136
|
|
|
137
|
-
if (options.
|
|
137
|
+
if (options.excludeValue === templateStringLiteral) {
|
|
138
138
|
templateStringLiteral = '``'
|
|
139
139
|
}
|
|
140
140
|
|
|
@@ -183,12 +183,12 @@ export const compile = (src, opts) => {
|
|
|
183
183
|
}
|
|
184
184
|
return `export const ${assignment} = ${message}`
|
|
185
185
|
/*} else {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
186
|
+
if (assignment === metadata[assignment].id) {
|
|
187
|
+
exports.push(`${assignment}: ${message}`)
|
|
188
|
+
} else {
|
|
189
|
+
exports.push(`'${metadata[assignment].id}': ${message}`)
|
|
190
|
+
}
|
|
191
|
+
}*/
|
|
192
192
|
return ''
|
|
193
193
|
},
|
|
194
194
|
Comment: (data) => {
|
|
@@ -369,10 +369,10 @@ const formatTime = (value) => {
|
|
|
369
369
|
value = new Date(value)
|
|
370
370
|
if (isNaN(value.getTime())) return value
|
|
371
371
|
try {
|
|
372
|
-
|
|
373
|
-
|
|
372
|
+
const [duration, unit] = relativeTimeDiff(value)
|
|
373
|
+
return relativeTimeFormat.format(duration, unit)
|
|
374
374
|
} catch (e) {
|
|
375
|
-
|
|
375
|
+
return dateTimeFormat.format(value)
|
|
376
376
|
}
|
|
377
377
|
}
|
|
378
378
|
*/
|
|
@@ -386,24 +386,24 @@ const __relativeTimeDiff = (d) => {
|
|
|
386
386
|
const msPerMonth = msPerDay * 30
|
|
387
387
|
const msPerYear = msPerDay * 365.25
|
|
388
388
|
const elapsed = d - new Date()
|
|
389
|
-
|
|
389
|
+
|
|
390
390
|
if (Math.abs(elapsed) < msPerMinute) {
|
|
391
391
|
return [Math.round(elapsed / 1000), 'second']
|
|
392
392
|
}
|
|
393
393
|
if (Math.abs(elapsed) < msPerHour) {
|
|
394
|
-
|
|
394
|
+
return [Math.round(elapsed / msPerMinute), 'minute']
|
|
395
395
|
}
|
|
396
396
|
if (Math.abs(elapsed) < msPerDay) {
|
|
397
|
-
|
|
397
|
+
return [Math.round(elapsed / msPerHour), 'hour']
|
|
398
398
|
}
|
|
399
399
|
if (Math.abs(elapsed) < msPerWeek * 2) {
|
|
400
|
-
|
|
400
|
+
return [Math.round(elapsed / msPerDay), 'day']
|
|
401
401
|
}
|
|
402
402
|
if (Math.abs(elapsed) < msPerMonth) {
|
|
403
|
-
|
|
403
|
+
return [Math.round(elapsed / msPerWeek), 'week']
|
|
404
404
|
}
|
|
405
405
|
if (Math.abs(elapsed) < msPerYear) {
|
|
406
|
-
|
|
406
|
+
return [Math.round(elapsed / msPerMonth), 'month']
|
|
407
407
|
}
|
|
408
408
|
return [Math.round(elapsed / msPerYear), 'year']
|
|
409
409
|
}
|
|
@@ -411,8 +411,8 @@ const __formatRelativeTime = (value, options) => {
|
|
|
411
411
|
if (typeof value === 'string') value = new Date(value)
|
|
412
412
|
if (isNaN(value.getTime())) return value
|
|
413
413
|
try {
|
|
414
|
-
|
|
415
|
-
|
|
414
|
+
const [duration, unit] = __relativeTimeDiff(value)
|
|
415
|
+
return new Intl.RelativeTimeFormat(__locales, options).format(duration, unit)
|
|
416
416
|
} catch (e) {}
|
|
417
417
|
return new Intl.DateTimeFormat(__locales, options).format(value)
|
|
418
418
|
}
|
|
@@ -421,16 +421,16 @@ const __formatRelativeTime = (value, options) => {
|
|
|
421
421
|
if (functions.__formatDateTime) {
|
|
422
422
|
output += `
|
|
423
423
|
const __formatDateTime = (value, options) => {
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
424
|
+
if (typeof value === 'string') value = new Date(value)
|
|
425
|
+
if (isNaN(value.getTime())) return value
|
|
426
|
+
return new Intl.DateTimeFormat(__locales, options).format(value)
|
|
427
427
|
}
|
|
428
428
|
`
|
|
429
429
|
}
|
|
430
430
|
if (functions.__formatVariable || functions.__formatNumber) {
|
|
431
431
|
output += `
|
|
432
432
|
const __formatNumber = (value, options) => {
|
|
433
|
-
|
|
433
|
+
return new Intl.NumberFormat(__locales, options).format(value)
|
|
434
434
|
}
|
|
435
435
|
`
|
|
436
436
|
}
|
|
@@ -447,9 +447,9 @@ const __formatVariable = (value) => {
|
|
|
447
447
|
if (functions.__select) {
|
|
448
448
|
output += `
|
|
449
449
|
const __select = (value, cases, fallback, options) => {
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
450
|
+
const pluralRules = new Intl.PluralRules(__locales, options)
|
|
451
|
+
const rule = pluralRules.select(value)
|
|
452
|
+
return cases[value] ?? cases[rule] ?? fallback
|
|
453
453
|
}
|
|
454
454
|
`
|
|
455
455
|
}
|