dataToEsm.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import makeLegalIdentifier from './makeLegalIdentifier';
  2. function serializeArray (arr, indent, baseIndent) {
  3. let output = '[';
  4. const separator = indent ? '\n' + baseIndent + indent : '';
  5. for (let i = 0; i < arr.length; i++) {
  6. const key = arr[i];
  7. output += `${ i > 0 ? ',' : '' }${ separator }${ serialize(key, indent, baseIndent + indent) }`;
  8. }
  9. return output + `${ indent ? '\n' + baseIndent : '' }]`;
  10. }
  11. function serializeObject (obj, indent, baseIndent) {
  12. let output = '{';
  13. const separator = indent ? '\n' + baseIndent + indent : '';
  14. const keys = Object.keys(obj);
  15. for (let i = 0; i < keys.length; i++) {
  16. const key = keys[i];
  17. const stringKey = makeLegalIdentifier(key) === key ? key : JSON.stringify(key);
  18. output += `${ i > 0 ? ',' : '' }${ separator }${ stringKey }:${ indent ? ' ' : '' }${ serialize(obj[key], indent, baseIndent + indent) }`;
  19. }
  20. return output + `${ indent ? '\n' + baseIndent : '' }}`;
  21. }
  22. function serialize (obj, indent, baseIndent) {
  23. if (obj === Infinity)
  24. return 'Infinity';
  25. if (obj instanceof Date)
  26. return 'new Date(' + obj.getTime() + ')';
  27. if (obj instanceof RegExp)
  28. return obj.toString();
  29. if (typeof obj === 'number' && isNaN(obj))
  30. return 'NaN';
  31. if (Array.isArray(obj))
  32. return serializeArray(obj, indent, baseIndent);
  33. if (obj === null)
  34. return 'null';
  35. if (typeof obj === 'object')
  36. return serializeObject(obj, indent, baseIndent);
  37. return JSON.stringify(obj);
  38. }
  39. // convert data object into separate named exports (and default)
  40. export default function dataToNamedExports (data, options = {}) {
  41. const t = options.compact ? '' : 'indent' in options ? options.indent : '\t';
  42. const _ = options.compact ? '' : ' ';
  43. const n = options.compact ? '' : '\n';
  44. const declarationType = options.preferConst ? 'const' : 'var';
  45. if (options.namedExports === false || typeof data !== 'object' || Array.isArray(data) || data === null)
  46. return `export default${ _ }${ serialize( data, options.compact ? null : t, '' ) };`;
  47. let namedExportCode = '';
  48. const defaultExportRows = [];
  49. const dataKeys = Object.keys(data);
  50. for (let i = 0; i < dataKeys.length; i++) {
  51. const key = dataKeys[i];
  52. if (key === makeLegalIdentifier(key)) {
  53. if (options.objectShorthand)
  54. defaultExportRows.push(key);
  55. else
  56. defaultExportRows.push(`${ key }:${ _ }${ key }`);
  57. namedExportCode += `export ${declarationType} ${key}${ _ }=${ _ }${ serialize(data[key], options.compact ? null : t, '') };${ n }`;
  58. } else {
  59. defaultExportRows.push(`${ JSON.stringify(key) }: ${ serialize(data[key], options.compact ? null : t, '')}`);
  60. }
  61. }
  62. return namedExportCode + `export default${ _ }{${ n }${ t }${ defaultExportRows.join(`,${ n }${ t }`) }${ n }};${ n }`;
  63. }