| 1 |
import makeLegalIdentifier from './makeLegalIdentifier'; |
| 2 |
import { DataToEsm } from './pluginutils'; |
| 3 |
|
| 4 |
export type Indent = string | null | undefined; |
| 5 |
|
| 6 |
function stringify(obj: any): string { |
| 7 |
return (JSON.stringify(obj) || 'undefined').replace(/[\u2028\u2029]/g, char => `\\u${('000' + char.charCodeAt(0).toString(16)).slice(-4)}`); |
| 8 |
} |
| 9 |
|
| 10 |
function serializeArray<T>(arr: Array<T>, indent: Indent, baseIndent: string): string { |
| 11 |
let output = '['; |
| 12 |
const separator = indent ? '\n' + baseIndent + indent : ''; |
| 13 |
for (let i = 0; i < arr.length; i++) { |
| 14 |
const key = arr[i]; |
| 15 |
output += `${i > 0 ? ',' : ''}${separator}${serialize(key, indent, baseIndent + indent)}`; |
| 16 |
} |
| 17 |
return output + `${indent ? '\n' + baseIndent : ''}]`; |
| 18 |
} |
| 19 |
|
| 20 |
function serializeObject<T>(obj: { [key: string]: T }, indent: Indent, baseIndent: string): string { |
| 21 |
let output = '{'; |
| 22 |
const separator = indent ? '\n' + baseIndent + indent : ''; |
| 23 |
const keys = Object.keys(obj); |
| 24 |
for (let i = 0; i < keys.length; i++) { |
| 25 |
const key = keys[i]; |
| 26 |
const stringKey = makeLegalIdentifier(key) === key ? key : stringify(key); |
| 27 |
output += `${i > 0 ? ',' : ''}${separator}${stringKey}:${indent ? ' ' : ''}${serialize( |
| 28 |
obj[key], |
| 29 |
indent, |
| 30 |
baseIndent + indent |
| 31 |
)}`; |
| 32 |
} |
| 33 |
return output + `${indent ? '\n' + baseIndent : ''}}`; |
| 34 |
} |
| 35 |
|
| 36 |
function serialize(obj: any, indent: Indent, baseIndent: string): string { |
| 37 |
if (obj === Infinity) return 'Infinity'; |
| 38 |
if (obj === -Infinity) return '-Infinity'; |
| 39 |
if (obj === 0 && 1/obj === -Infinity) return '-0'; |
| 40 |
if (obj instanceof Date) return 'new Date(' + obj.getTime() + ')'; |
| 41 |
if (obj instanceof RegExp) return obj.toString(); |
| 42 |
if (obj !== obj) return 'NaN'; |
| 43 |
if (Array.isArray(obj)) return serializeArray(obj, indent, baseIndent); |
| 44 |
if (obj === null) return 'null'; |
| 45 |
if (typeof obj === 'object') return serializeObject(obj, indent, baseIndent); |
| 46 |
return stringify(obj); |
| 47 |
} |
| 48 |
|
| 49 |
const dataToEsm: DataToEsm = function dataToEsm(data, options = {}) { |
| 50 |
const t = options.compact ? '' : 'indent' in options ? options.indent : '\t'; |
| 51 |
const _ = options.compact ? '' : ' '; |
| 52 |
const n = options.compact ? '' : '\n'; |
| 53 |
const declarationType = options.preferConst ? 'const' : 'var'; |
| 54 |
|
| 55 |
if ( |
| 56 |
options.namedExports === false || |
| 57 |
typeof data !== 'object' || |
| 58 |
Array.isArray(data) || |
| 59 |
data instanceof Date || |
| 60 |
data instanceof RegExp || |
| 61 |
data === null |
| 62 |
) { |
| 63 |
const code = serialize(data, options.compact ? null : t, ''); |
| 64 |
const __ = _ || (/^[{[\-\/]/.test(code) ? '' : ' '); |
| 65 |
return `export default${__}${code};`; |
| 66 |
} |
| 67 |
|
| 68 |
let namedExportCode = ''; |
| 69 |
const defaultExportRows = []; |
| 70 |
const dataKeys = Object.keys(data); |
| 71 |
for (let i = 0; i < dataKeys.length; i++) { |
| 72 |
const key = dataKeys[i]; |
| 73 |
if (key === makeLegalIdentifier(key)) { |
| 74 |
if (options.objectShorthand) defaultExportRows.push(key); |
| 75 |
else defaultExportRows.push(`${key}:${_}${key}`); |
| 76 |
namedExportCode += `export ${declarationType} ${key}${_}=${_}${serialize( |
| 77 |
data[key], |
| 78 |
options.compact ? null : t, |
| 79 |
'' |
| 80 |
)};${n}`; |
| 81 |
} else { |
| 82 |
defaultExportRows.push( |
| 83 |
`${stringify(key)}:${_}${serialize(data[key], options.compact ? null : t, '')}` |
| 84 |
); |
| 85 |
} |
| 86 |
} |
| 87 |
return ( |
| 88 |
namedExportCode + `export default${_}{${n}${t}${defaultExportRows.join(`,${n}${t}`)}${n}};${n}` |
| 89 |
); |
| 90 |
}; |
| 91 |
|
| 92 |
export { dataToEsm as default }; |