UNPKG

plist2

Version:

Converts between .tmLanguage(.plist), .json, .cson and .yaml formats

66 lines (63 loc) 2.91 kB
/* eslint-disable */ /* dester builds: js2plist.ts */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var lib = require('../lib'); /* filename: js2plist.ts timestamp: 2024-12-13T15:18:07.574Z */ var normalizeString = value => (lib.__EMPTY__ + value).trim().replace(/</g, '&lt;').replace(/>/g, '&gt;'); // .replace(/"/gi, '&quot;') // .replace(/'/gi, '&apos;') var wrapValue = (tag, value, concise) => concise ? "<" + value + "/>" : "<" + tag + ">" + value + "</" + tag + ">"; // prettier-ignore var writeComments = (comment, indent, newline) => !comment ? '' : (lib.isArray(comment) ? comment : [comment]).map(v => indent + ("<!--" + v + "-->") + newline).join(lib.__EMPTY__); var __js2plist__ = (source, indent, deep) => { deep++; var INDENT = lib.setIndent(indent, deep); var NEW_LINE = indent ? '\n' : lib.__EMPTY__; var INDENT2 = lib.setIndent(indent, deep + 1); var res = lib.__EMPTY__; if (lib.isArray(source)) { var comments = { ...(source[lib.__COMMENTS_KEY__] || {}) }; source.forEach((v, k) => { res += writeComments(comments[k], INDENT2, NEW_LINE); res += __js2plist__(v, indent, deep); }); res += writeComments(comments[lib.__EMPTY__], INDENT2, NEW_LINE); res = '<' + lib.__ARRAY__ + '>' + NEW_LINE + res + INDENT + '</' + lib.__ARRAY__ + '>'; } else if (typeof source === 'number') { res = wrapValue(source === parseInt('' + source, 10) ? lib.__INTEGER__ : lib.__REAL__, source); } else if (typeof source === 'boolean') { res = wrapValue(lib.__EMPTY__, source, true); } else if (source instanceof Date) { res = wrapValue(lib.__DATE__, source.toISOString()); } else if (source && source[lib.__BINARY64_KEY__]) { res = wrapValue(lib.__DATA__, source[lib.__BINARY64_KEY__]); } else if (lib.isObject(source)) { var _comments = { ...(source[lib.__COMMENTS_KEY__] || {}) }; lib.keys(source).forEach(k => { if (k !== lib.__COMMENTS_KEY__) { res += writeComments(_comments[k], INDENT2, NEW_LINE); res += INDENT2 + wrapValue(lib.__KEY__, normalizeString(k)) + NEW_LINE; res += __js2plist__(source[k], indent, deep); } }); res += writeComments(_comments[lib.__EMPTY__], INDENT2, NEW_LINE); res = '<' + lib.__DICT__ + '>' + NEW_LINE + res + INDENT + '</' + lib.__DICT__ + '>'; } else { res = wrapValue(lib.__STRING__, normalizeString(source)); } return INDENT + res + NEW_LINE; }; // prettier-ignore var js2plist = (source, indent = 2) => ['<?xml version="1.0" encoding="UTF-8"?>', '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">', '<plist version="1.0">', __js2plist__(source, !indent ? lib.__EMPTY__ : indent === +indent ? lib.repeat(' ', indent) : lib.__EMPTY__ + indent, 0) + '</plist>'].join(indent ? '\n' : lib.__EMPTY__); exports["default"] = js2plist;