fluent-transpiler 0.1.2 → 0.2.0
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/index.js +46 -39
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -68,8 +68,15 @@ export const compile = (src, opts) => {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
const types = {
|
|
71
|
-
Identifier: (data) => {
|
|
72
|
-
const value =
|
|
71
|
+
Identifier: (data, parent) => {
|
|
72
|
+
const value =
|
|
73
|
+
parent === 'Attribute'
|
|
74
|
+
? data.name
|
|
75
|
+
: variableNotation[options.variableNotation](data.name)
|
|
76
|
+
|
|
77
|
+
if (value.includes('-')) {
|
|
78
|
+
return `'${value}'`
|
|
79
|
+
}
|
|
73
80
|
// Check for reserved words - TODO add in rest
|
|
74
81
|
if (['const', 'default', 'enum', 'if'].includes(value)) {
|
|
75
82
|
return '_' + value
|
|
@@ -77,7 +84,7 @@ export const compile = (src, opts) => {
|
|
|
77
84
|
return value
|
|
78
85
|
},
|
|
79
86
|
Attribute: (data) => {
|
|
80
|
-
const key = compileType(data.id)
|
|
87
|
+
const key = compileType(data.id, data.type)
|
|
81
88
|
const value = compileType(data.value, data.type)
|
|
82
89
|
return ` ${key}: ${value}`
|
|
83
90
|
},
|
|
@@ -360,44 +367,44 @@ const formatTime = (value) => {
|
|
|
360
367
|
*/
|
|
361
368
|
if (functions.__formatRelativeTime) {
|
|
362
369
|
output += `
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
}
|
|
384
|
-
if (Math.abs(elapsed) < msPerMonth) {
|
|
385
|
-
return [Math.round(elapsed / msPerWeek), 'week']
|
|
386
|
-
}
|
|
387
|
-
if (Math.abs(elapsed) < msPerYear) {
|
|
388
|
-
return [Math.round(elapsed / msPerMonth), 'month']
|
|
389
|
-
}
|
|
390
|
-
return [Math.round(elapsed / msPerYear), 'year']
|
|
370
|
+
const __relativeTimeDiff = (d) => {
|
|
371
|
+
const msPerMinute = 60 * 1000
|
|
372
|
+
const msPerHour = msPerMinute * 60
|
|
373
|
+
const msPerDay = msPerHour * 24
|
|
374
|
+
const msPerWeek = msPerDay * 7
|
|
375
|
+
const msPerMonth = msPerDay * 30
|
|
376
|
+
const msPerYear = msPerDay * 365.25
|
|
377
|
+
const elapsed = d - new Date()
|
|
378
|
+
|
|
379
|
+
if (Math.abs(elapsed) < msPerMinute) {
|
|
380
|
+
return [Math.round(elapsed / 1000), 'second']
|
|
381
|
+
}
|
|
382
|
+
if (Math.abs(elapsed) < msPerHour) {
|
|
383
|
+
return [Math.round(elapsed / msPerMinute), 'minute']
|
|
384
|
+
}
|
|
385
|
+
if (Math.abs(elapsed) < msPerDay) {
|
|
386
|
+
return [Math.round(elapsed / msPerHour), 'hour']
|
|
387
|
+
}
|
|
388
|
+
if (Math.abs(elapsed) < msPerWeek * 2) {
|
|
389
|
+
return [Math.round(elapsed / msPerDay), 'day']
|
|
391
390
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
if (isNaN(value.getTime())) return value
|
|
395
|
-
try {
|
|
396
|
-
const [duration, unit] = __relativeTimeDiff(value)
|
|
397
|
-
return new Intl.RelativeTimeFormat(lang, options).format(duration, unit)
|
|
398
|
-
} catch (e) {}
|
|
399
|
-
return new Intl.DateTimeFormat(__locales, options).format(value)
|
|
391
|
+
if (Math.abs(elapsed) < msPerMonth) {
|
|
392
|
+
return [Math.round(elapsed / msPerWeek), 'week']
|
|
400
393
|
}
|
|
394
|
+
if (Math.abs(elapsed) < msPerYear) {
|
|
395
|
+
return [Math.round(elapsed / msPerMonth), 'month']
|
|
396
|
+
}
|
|
397
|
+
return [Math.round(elapsed / msPerYear), 'year']
|
|
398
|
+
}
|
|
399
|
+
const __formatRelativeTime = (value, options) => {
|
|
400
|
+
if (typeof value === 'string') value = new Date(value)
|
|
401
|
+
if (isNaN(value.getTime())) return value
|
|
402
|
+
try {
|
|
403
|
+
const [duration, unit] = __relativeTimeDiff(value)
|
|
404
|
+
return new Intl.RelativeTimeFormat(__locales, options).format(duration, unit)
|
|
405
|
+
} catch (e) {}
|
|
406
|
+
return new Intl.DateTimeFormat(__locales, options).format(value)
|
|
407
|
+
}
|
|
401
408
|
`
|
|
402
409
|
}
|
|
403
410
|
if (functions.__formatDateTime) {
|