pinafore/src/routes/_utils/formatIntl.js
Nolan Lawson 16e66346d7
fix!: remove esm package, use native Node ES modules (#2064)
BREAKING CHANGE: Node v12.20+, v14.14+, or v16.0+ is required

* fix!: remove esm package, use native Node ES modules

* fix: fix some CJS imports
2021-07-04 20:19:04 -07:00

27 lines
727 B
JavaScript

import interpret from 'format-message-interpret'
import { LOCALE } from '../_static/intl.js'
import { mark, stop } from './marks.js'
function doFormatIntl (ast, values) {
return interpret(ast, LOCALE)(values).trim().replace(/\s+/g, ' ')
}
export function formatIntl (ast, values) {
if (process.env.NODE_ENV !== 'production') {
// useful error debugging for dev mode
if (typeof ast === 'string') {
throw new Error('bad ast: ' + ast)
}
try {
return doFormatIntl(ast, values)
} catch (err) {
console.error(err)
throw new Error('bad ast or values : ' + ast + ' ' + values)
}
}
mark('formatIntl')
const res = doFormatIntl(ast, values)
stop('formatIntl')
return res
}