UNPKG

plist2

Version:

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

61 lines (59 loc) 2.88 kB
/* eslint-disable */ /* dester builds: js2plist.ts */ import { __EMPTY__, repeat, setIndent, isArray, __COMMENTS_KEY__, __ARRAY__, __DATE__, __BINARY64_KEY__, __DATA__, isObject, keys, __KEY__, __DICT__, __STRING__, __INTEGER__, __REAL__ } from '../lib'; /* filename: js2plist.ts timestamp: 2024-12-13T15:18:07.574Z */ var normalizeString = value => (__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 ? '' : (isArray(comment) ? comment : [comment]).map(v => indent + ("<!--" + v + "-->") + newline).join(__EMPTY__); var __js2plist__ = (source, indent, deep) => { deep++; var INDENT = setIndent(indent, deep); var NEW_LINE = indent ? '\n' : __EMPTY__; var INDENT2 = setIndent(indent, deep + 1); var res = __EMPTY__; if (isArray(source)) { var comments = { ...(source[__COMMENTS_KEY__] || {}) }; source.forEach((v, k) => { res += writeComments(comments[k], INDENT2, NEW_LINE); res += __js2plist__(v, indent, deep); }); res += writeComments(comments[__EMPTY__], INDENT2, NEW_LINE); res = '<' + __ARRAY__ + '>' + NEW_LINE + res + INDENT + '</' + __ARRAY__ + '>'; } else if (typeof source === 'number') { res = wrapValue(source === parseInt('' + source, 10) ? __INTEGER__ : __REAL__, source); } else if (typeof source === 'boolean') { res = wrapValue(__EMPTY__, source, true); } else if (source instanceof Date) { res = wrapValue(__DATE__, source.toISOString()); } else if (source && source[__BINARY64_KEY__]) { res = wrapValue(__DATA__, source[__BINARY64_KEY__]); } else if (isObject(source)) { var _comments = { ...(source[__COMMENTS_KEY__] || {}) }; keys(source).forEach(k => { if (k !== __COMMENTS_KEY__) { res += writeComments(_comments[k], INDENT2, NEW_LINE); res += INDENT2 + wrapValue(__KEY__, normalizeString(k)) + NEW_LINE; res += __js2plist__(source[k], indent, deep); } }); res += writeComments(_comments[__EMPTY__], INDENT2, NEW_LINE); res = '<' + __DICT__ + '>' + NEW_LINE + res + INDENT + '</' + __DICT__ + '>'; } else { res = wrapValue(__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 ? __EMPTY__ : indent === +indent ? repeat(' ', indent) : __EMPTY__ + indent, 0) + '</plist>'].join(indent ? '\n' : __EMPTY__); export { js2plist as default };