UNPKG

ts-comply

Version:

TypeTools is a Typescript library for providing extensible tooling runtime validations and type helpers.

168 lines 6.16 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parsePropertyValue = exports.parseProperties = exports.dedent = void 0; var dedentOriginal = __importStar(require("dedent")); exports.dedent = dedentOriginal; function parseProperties(content) { var buff = Buffer.alloc(content.length * 2); var offset = 0; var withinBackTick = false; for (var i = 0; i < content.length; ++i) { if (!withinBackTick && content[i] === '`') { withinBackTick = true; buff[i + offset] = content.charCodeAt(i); continue; } if (withinBackTick) { if (content[i] === '\n') { buff[i + offset] = 92; ++offset; buff[i + offset] = 110; continue; } if (content[i] === '`' && content[i - 1] !== '\\') { buff[i + offset] = content.charCodeAt(i); withinBackTick = false; continue; } } buff[i + offset] = content.charCodeAt(i); } content = buff.slice(0, content.length + offset).toString('ascii'); var flatMap = content .split('\n') .filter(function (line) { return /(\#|\!)/.test(line.replace(/\s/g, '').slice(0, 1)) ? false : line; }) .reduce(function (obj, line) { var colonifiedLine = line.replace(/(?<!\\)=/, ':'); var key = colonifiedLine .substring(0, colonifiedLine.search(/(?<!\\):/)) .replace(/\\/g, '') .trim(); var value = colonifiedLine .substring(colonifiedLine.search(/(?<!\\):/) + 1) .trim(); obj[key] = value; return obj; }, {}); var tallMap = {}; for (var _i = 0, _a = Object.keys(flatMap); _i < _a.length; _i++) { var fullPath = _a[_i]; if (!fullPath) { continue; } var path = fullPath.split('.'); var pathTraveled = []; var last = path.pop(); var node = tallMap; for (var i = 0; i < path.length; ++i) { var at = path[i]; pathTraveled.push(at); if (!node[at]) { node[at] = {}; } node = node[at]; if (typeof node !== 'object') { throw new Error("".concat(fullPath, " key has conflict with previously defined path ").concat(pathTraveled.join('.'))); } } var value = flatMap[fullPath]; if (!isNaN(+value)) { node[last] = +value; } else { if (value.startsWith('`') && value.endsWith('`')) { value = dedentOriginal.default(value.slice(1, -1).replace(/\\n/g, '\n')); } if (value.startsWith('"') && value.endsWith('"')) { value = value.slice(1, -1).replace(/\\n/g, '\n'); } else if (value.startsWith("'") && value.endsWith("'")) { value = value.slice(1, -1).replace(/\\n/g, '\n').replace(/\\\'/g, "'"); } node[last] = value.trim(); } } return tallMap; } exports.parseProperties = parseProperties; ; function parsePropertyValue(entry) { var lines = entry.split('\n'); var result = {}; for (var i = 0; i < lines.length; ++i) { var line = lines[i].trim(); while (line.endsWith(';')) { line = line.slice(0, -1); } var lit = line.split('='); var propName = trimQuote(lit[0].trim()); var value = lit.slice(1).join('=').trim(); if (propName.startsWith('[') && propName.endsWith(']')) { var lit2 = propName.slice(1, -1).split(':').map(function (a) { return trimQuote(a.trim()); }); var baseKey = lit2[0]; var childKey = lit2[1]; if (!result[baseKey]) { result[baseKey] = {}; } if (value.indexOf(',') >= 0) { result[baseKey][childKey] = value.split(',').map(function (a) { return trimQuote(a.trim()); }).filter(function (a) { return a; }); } else { result[baseKey][childKey] = trimQuote(value); } } else { if (value.indexOf(',') >= 0) { result[propName] = value.split(',').map(function (a) { return trimQuote(a.trim()); }).filter(function (a) { return a; }); } else { result[propName] = trimQuote(value); } } } for (var _i = 0, _a = Object.keys(result); _i < _a.length; _i++) { var prop = _a[_i]; if (!isNaN(+result[prop])) { result[prop] = +result[prop]; } } return result; } exports.parsePropertyValue = parsePropertyValue; function trimQuote(str) { while (str.startsWith("'") && str.endsWith("'")) { str = str.slice(1, -1).trim(); } while (str.startsWith('"') && str.endsWith('"')) { str = str.slice(1, -1).trim(); } return str; } //# sourceMappingURL=properties.parse.js.map