2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-05-09 08:34:47 -04:00
|
|
|
import { deepAssign } from "../_util/deep_assign.ts";
|
2020-06-07 09:20:33 -04:00
|
|
|
import { assert } from "../_util/assert.ts";
|
2019-03-28 12:31:15 -04:00
|
|
|
|
|
|
|
class KeyValuePair {
|
2019-05-30 08:59:30 -04:00
|
|
|
constructor(public key: string, public value: unknown) {}
|
2019-03-28 12:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class ParserGroup {
|
|
|
|
arrValues: unknown[] = [];
|
2019-05-30 08:59:30 -04:00
|
|
|
objValues: Record<string, unknown> = {};
|
|
|
|
|
|
|
|
constructor(public type: string, public name: string) {}
|
2019-03-28 12:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class ParserContext {
|
|
|
|
currentGroup?: ParserGroup;
|
2019-05-30 08:59:30 -04:00
|
|
|
output: Record<string, unknown> = {};
|
2019-03-28 12:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class Parser {
|
|
|
|
tomlLines: string[];
|
|
|
|
context: ParserContext;
|
|
|
|
constructor(tomlString: string) {
|
|
|
|
this.tomlLines = this._split(tomlString);
|
|
|
|
this.context = new ParserContext();
|
|
|
|
}
|
|
|
|
_sanitize(): void {
|
2019-05-30 08:59:30 -04:00
|
|
|
const out: string[] = [];
|
2019-03-28 12:31:15 -04:00
|
|
|
for (let i = 0; i < this.tomlLines.length; i++) {
|
2019-03-29 10:51:23 -04:00
|
|
|
const s = this.tomlLines[i];
|
|
|
|
const trimmed = s.trim();
|
|
|
|
if (trimmed !== "" && trimmed[0] !== "#") {
|
2019-03-28 12:31:15 -04:00
|
|
|
out.push(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.tomlLines = out;
|
|
|
|
this._mergeMultilines();
|
|
|
|
}
|
|
|
|
|
|
|
|
_mergeMultilines(): void {
|
|
|
|
function arrayStart(line: string): boolean {
|
|
|
|
const reg = /.*=\s*\[/g;
|
|
|
|
return reg.test(line) && !(line[line.length - 1] === "]");
|
|
|
|
}
|
|
|
|
|
|
|
|
function arrayEnd(line: string): boolean {
|
|
|
|
return line[line.length - 1] === "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
function stringStart(line: string): boolean {
|
|
|
|
const m = line.match(/.*=\s*(?:\"\"\"|''')/);
|
|
|
|
if (!m) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !line.endsWith(`"""`) || !line.endsWith(`'''`);
|
|
|
|
}
|
|
|
|
|
|
|
|
function stringEnd(line: string): boolean {
|
|
|
|
return line.endsWith(`'''`) || line.endsWith(`"""`);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isLiteralString(line: string): boolean {
|
|
|
|
return line.match(/'''/) ? true : false;
|
|
|
|
}
|
|
|
|
|
2019-10-05 12:02:34 -04:00
|
|
|
const merged = [];
|
|
|
|
let acc = [],
|
2019-03-28 12:31:15 -04:00
|
|
|
isLiteral = false,
|
|
|
|
capture = false,
|
|
|
|
captureType = "",
|
|
|
|
merge = false;
|
|
|
|
|
|
|
|
for (let i = 0; i < this.tomlLines.length; i++) {
|
|
|
|
const line = this.tomlLines[i];
|
|
|
|
const trimmed = line.trim();
|
|
|
|
if (!capture && arrayStart(trimmed)) {
|
|
|
|
capture = true;
|
|
|
|
captureType = "array";
|
|
|
|
} else if (!capture && stringStart(trimmed)) {
|
|
|
|
isLiteral = isLiteralString(trimmed);
|
|
|
|
capture = true;
|
|
|
|
captureType = "string";
|
|
|
|
} else if (capture && arrayEnd(trimmed)) {
|
|
|
|
merge = true;
|
|
|
|
} else if (capture && stringEnd(trimmed)) {
|
|
|
|
merge = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (capture) {
|
|
|
|
if (isLiteral) {
|
|
|
|
acc.push(line);
|
|
|
|
} else {
|
|
|
|
acc.push(trimmed);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isLiteral) {
|
|
|
|
merged.push(line);
|
|
|
|
} else {
|
|
|
|
merged.push(trimmed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (merge) {
|
|
|
|
capture = false;
|
|
|
|
merge = false;
|
|
|
|
if (captureType === "string") {
|
|
|
|
merged.push(
|
|
|
|
acc
|
|
|
|
.join("\n")
|
|
|
|
.replace(/"""/g, '"')
|
|
|
|
.replace(/'''/g, `'`)
|
|
|
|
.replace(/\n/g, "\\n")
|
|
|
|
);
|
|
|
|
isLiteral = false;
|
|
|
|
} else {
|
|
|
|
merged.push(acc.join(""));
|
|
|
|
}
|
|
|
|
captureType = "";
|
|
|
|
acc = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.tomlLines = merged;
|
|
|
|
}
|
|
|
|
_unflat(keys: string[], values: object = {}, cObj: object = {}): object {
|
2019-10-05 12:02:34 -04:00
|
|
|
const out: Record<string, unknown> = {};
|
2019-03-28 12:31:15 -04:00
|
|
|
if (keys.length === 0) {
|
|
|
|
return cObj;
|
|
|
|
} else {
|
|
|
|
if (Object.keys(cObj).length === 0) {
|
|
|
|
cObj = values;
|
|
|
|
}
|
2019-10-05 12:02:34 -04:00
|
|
|
const key: string | undefined = keys.pop();
|
2019-05-30 08:59:30 -04:00
|
|
|
if (key) {
|
|
|
|
out[key] = cObj;
|
|
|
|
}
|
2019-03-28 12:31:15 -04:00
|
|
|
return this._unflat(keys, values, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_groupToOutput(): void {
|
2020-02-08 15:15:59 -05:00
|
|
|
assert(this.context.currentGroup != null, "currentGroup must be set");
|
|
|
|
const arrProperty = this.context.currentGroup.name
|
|
|
|
.replace(/"/g, "")
|
2019-03-28 12:31:15 -04:00
|
|
|
.replace(/'/g, "")
|
|
|
|
.split(".");
|
|
|
|
let u = {};
|
2020-02-08 15:15:59 -05:00
|
|
|
if (this.context.currentGroup.type === "array") {
|
|
|
|
u = this._unflat(arrProperty, this.context.currentGroup.arrValues);
|
2019-03-28 12:31:15 -04:00
|
|
|
} else {
|
2020-02-08 15:15:59 -05:00
|
|
|
u = this._unflat(arrProperty, this.context.currentGroup.objValues);
|
2019-03-28 12:31:15 -04:00
|
|
|
}
|
|
|
|
deepAssign(this.context.output, u);
|
|
|
|
delete this.context.currentGroup;
|
|
|
|
}
|
|
|
|
_split(str: string): string[] {
|
2019-10-05 12:02:34 -04:00
|
|
|
const out = [];
|
2019-03-28 12:31:15 -04:00
|
|
|
out.push(...str.split("\n"));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
_isGroup(line: string): boolean {
|
|
|
|
const t = line.trim();
|
2019-03-29 10:51:23 -04:00
|
|
|
return t[0] === "[" && /\[(.*)\]/.exec(t) ? true : false;
|
2019-03-28 12:31:15 -04:00
|
|
|
}
|
|
|
|
_isDeclaration(line: string): boolean {
|
|
|
|
return line.split("=").length > 1;
|
|
|
|
}
|
|
|
|
_createGroup(line: string): void {
|
|
|
|
const captureReg = /\[(.*)\]/;
|
|
|
|
if (this.context.currentGroup) {
|
|
|
|
this._groupToOutput();
|
|
|
|
}
|
2019-05-30 08:59:30 -04:00
|
|
|
|
|
|
|
let type;
|
2020-02-08 15:15:59 -05:00
|
|
|
let m = line.match(captureReg);
|
|
|
|
assert(m != null, "line mut be matched");
|
|
|
|
let name = m[1];
|
2019-05-30 08:59:30 -04:00
|
|
|
if (name.match(/\[.*\]/)) {
|
|
|
|
type = "array";
|
2020-02-08 15:15:59 -05:00
|
|
|
m = name.match(captureReg);
|
|
|
|
assert(m != null, "name must be matched");
|
|
|
|
name = m[1];
|
2019-03-28 12:31:15 -04:00
|
|
|
} else {
|
2019-05-30 08:59:30 -04:00
|
|
|
type = "object";
|
2019-03-28 12:31:15 -04:00
|
|
|
}
|
2019-05-30 08:59:30 -04:00
|
|
|
this.context.currentGroup = new ParserGroup(type, name);
|
2019-03-28 12:31:15 -04:00
|
|
|
}
|
|
|
|
_processDeclaration(line: string): KeyValuePair {
|
|
|
|
const idx = line.indexOf("=");
|
2019-05-30 08:59:30 -04:00
|
|
|
const key = line.substring(0, idx).trim();
|
|
|
|
const value = this._parseData(line.slice(idx + 1));
|
|
|
|
return new KeyValuePair(key, value);
|
2019-03-28 12:31:15 -04:00
|
|
|
}
|
2019-03-29 10:51:23 -04:00
|
|
|
// TODO (zekth) Need refactor using ACC
|
2019-03-28 12:31:15 -04:00
|
|
|
_parseData(dataString: string): unknown {
|
|
|
|
dataString = dataString.trim();
|
2019-03-29 10:51:23 -04:00
|
|
|
|
2019-03-28 12:31:15 -04:00
|
|
|
if (this._isDate(dataString)) {
|
2019-03-29 10:51:23 -04:00
|
|
|
return new Date(dataString.split("#")[0].trim());
|
2019-03-28 12:31:15 -04:00
|
|
|
}
|
2019-03-29 10:51:23 -04:00
|
|
|
|
2019-03-28 12:31:15 -04:00
|
|
|
if (this._isLocalTime(dataString)) {
|
2019-03-29 10:51:23 -04:00
|
|
|
return eval(`"${dataString.split("#")[0].trim()}"`);
|
2019-03-28 12:31:15 -04:00
|
|
|
}
|
2019-03-29 10:51:23 -04:00
|
|
|
|
|
|
|
const cut3 = dataString.substring(0, 3).toLowerCase();
|
|
|
|
const cut4 = dataString.substring(0, 4).toLowerCase();
|
|
|
|
if (cut3 === "inf" || cut4 === "+inf") {
|
2019-03-28 12:31:15 -04:00
|
|
|
return Infinity;
|
|
|
|
}
|
2019-03-29 10:51:23 -04:00
|
|
|
if (cut4 === "-inf") {
|
2019-03-28 12:31:15 -04:00
|
|
|
return -Infinity;
|
|
|
|
}
|
2019-03-29 10:51:23 -04:00
|
|
|
|
|
|
|
if (cut3 === "nan" || cut4 === "+nan" || cut4 === "-nan") {
|
2019-03-28 12:31:15 -04:00
|
|
|
return NaN;
|
|
|
|
}
|
2019-03-29 10:51:23 -04:00
|
|
|
|
|
|
|
// If binary / octal / hex
|
|
|
|
const hex = /(0(?:x|o|b)[0-9a-f_]*)[^#]/gi.exec(dataString);
|
|
|
|
if (hex && hex[0]) {
|
|
|
|
return hex[0].trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
const testNumber = this._isParsableNumber(dataString);
|
|
|
|
if (testNumber && !isNaN(testNumber as number)) {
|
|
|
|
return testNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
const invalidArr = /,\]/g.exec(dataString);
|
|
|
|
if (invalidArr) {
|
|
|
|
dataString = dataString.replace(/,]/g, "]");
|
|
|
|
}
|
|
|
|
const m = /(?:\'|\[|{|\").*(?:\'|\]|\"|})\s*[^#]/g.exec(dataString);
|
|
|
|
if (m) {
|
|
|
|
dataString = m[0].trim();
|
|
|
|
}
|
2019-03-28 12:31:15 -04:00
|
|
|
if (dataString[0] === "{" && dataString[dataString.length - 1] === "}") {
|
|
|
|
const reg = /([a-zA-Z0-9-_\.]*) (=)/gi;
|
|
|
|
let result;
|
|
|
|
while ((result = reg.exec(dataString))) {
|
2019-10-05 12:02:34 -04:00
|
|
|
const ogVal = result[0];
|
|
|
|
const newVal = ogVal
|
2019-03-28 12:31:15 -04:00
|
|
|
.replace(result[1], `"${result[1]}"`)
|
|
|
|
.replace(result[2], ":");
|
|
|
|
dataString = dataString.replace(ogVal, newVal);
|
|
|
|
}
|
|
|
|
return JSON.parse(dataString);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle First and last EOL for multiline strings
|
|
|
|
if (dataString.startsWith(`"\\n`)) {
|
|
|
|
dataString = dataString.replace(`"\\n`, `"`);
|
|
|
|
} else if (dataString.startsWith(`'\\n`)) {
|
|
|
|
dataString = dataString.replace(`'\\n`, `'`);
|
|
|
|
}
|
|
|
|
if (dataString.endsWith(`\\n"`)) {
|
|
|
|
dataString = dataString.replace(`\\n"`, `"`);
|
|
|
|
} else if (dataString.endsWith(`\\n'`)) {
|
|
|
|
dataString = dataString.replace(`\\n'`, `'`);
|
|
|
|
}
|
|
|
|
return eval(dataString);
|
|
|
|
}
|
|
|
|
_isLocalTime(str: string): boolean {
|
|
|
|
const reg = /(\d{2}):(\d{2}):(\d{2})/;
|
|
|
|
return reg.test(str);
|
|
|
|
}
|
2019-03-29 10:51:23 -04:00
|
|
|
_isParsableNumber(dataString: string): number | boolean {
|
|
|
|
const m = /((?:\+|-|)[0-9_\.e+\-]*)[^#]/i.exec(dataString.trim());
|
|
|
|
if (!m) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return parseFloat(m[0].replace(/_/g, ""));
|
|
|
|
}
|
2019-03-28 12:31:15 -04:00
|
|
|
}
|
|
|
|
_isDate(dateStr: string): boolean {
|
|
|
|
const reg = /\d{4}-\d{2}-\d{2}/;
|
|
|
|
return reg.test(dateStr);
|
|
|
|
}
|
2019-04-04 06:00:24 -04:00
|
|
|
_parseDeclarationName(declaration: string): string[] {
|
|
|
|
const out = [];
|
|
|
|
let acc = [];
|
2019-04-25 18:47:18 -04:00
|
|
|
let inLiteral = false;
|
2019-04-04 06:00:24 -04:00
|
|
|
for (let i = 0; i < declaration.length; i++) {
|
|
|
|
const c = declaration[i];
|
|
|
|
switch (c) {
|
|
|
|
case ".":
|
2019-04-25 18:47:18 -04:00
|
|
|
if (!inLiteral) {
|
2019-04-04 06:00:24 -04:00
|
|
|
out.push(acc.join(""));
|
|
|
|
acc = [];
|
|
|
|
} else {
|
|
|
|
acc.push(c);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case `"`:
|
2019-04-25 18:47:18 -04:00
|
|
|
if (inLiteral) {
|
|
|
|
inLiteral = false;
|
2019-04-04 06:00:24 -04:00
|
|
|
} else {
|
2019-04-25 18:47:18 -04:00
|
|
|
inLiteral = true;
|
2019-04-04 06:00:24 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
acc.push(c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (acc.length !== 0) {
|
|
|
|
out.push(acc.join(""));
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2019-03-28 12:31:15 -04:00
|
|
|
_parseLines(): void {
|
|
|
|
for (let i = 0; i < this.tomlLines.length; i++) {
|
|
|
|
const line = this.tomlLines[i];
|
|
|
|
|
|
|
|
// TODO (zekth) Handle unflat of array of tables
|
|
|
|
if (this._isGroup(line)) {
|
|
|
|
// if the current group is an array we push the
|
|
|
|
// parsed objects in it.
|
|
|
|
if (
|
|
|
|
this.context.currentGroup &&
|
|
|
|
this.context.currentGroup.type === "array"
|
|
|
|
) {
|
|
|
|
this.context.currentGroup.arrValues.push(
|
|
|
|
this.context.currentGroup.objValues
|
|
|
|
);
|
|
|
|
this.context.currentGroup.objValues = {};
|
|
|
|
}
|
|
|
|
// If we need to create a group or to change group
|
|
|
|
if (
|
|
|
|
!this.context.currentGroup ||
|
|
|
|
(this.context.currentGroup &&
|
|
|
|
this.context.currentGroup.name !==
|
|
|
|
line.replace(/\[/g, "").replace(/\]/g, ""))
|
|
|
|
) {
|
|
|
|
this._createGroup(line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this._isDeclaration(line)) {
|
2019-10-05 12:02:34 -04:00
|
|
|
const kv = this._processDeclaration(line);
|
|
|
|
const key = kv.key;
|
|
|
|
const value = kv.value;
|
2019-03-28 12:31:15 -04:00
|
|
|
if (!this.context.currentGroup) {
|
2019-04-04 06:00:24 -04:00
|
|
|
this.context.output[key] = value;
|
2019-03-28 12:31:15 -04:00
|
|
|
} else {
|
2019-04-04 06:00:24 -04:00
|
|
|
this.context.currentGroup.objValues[key] = value;
|
2019-03-28 12:31:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.context.currentGroup) {
|
|
|
|
if (this.context.currentGroup.type === "array") {
|
|
|
|
this.context.currentGroup.arrValues.push(
|
|
|
|
this.context.currentGroup.objValues
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this._groupToOutput();
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 06:00:24 -04:00
|
|
|
_cleanOutput(): void {
|
|
|
|
this._propertyClean(this.context.output);
|
|
|
|
}
|
2019-05-30 08:59:30 -04:00
|
|
|
_propertyClean(obj: Record<string, unknown>): void {
|
2019-04-04 06:00:24 -04:00
|
|
|
const keys = Object.keys(obj);
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
let k = keys[i];
|
2019-05-30 08:59:30 -04:00
|
|
|
if (k) {
|
|
|
|
let v = obj[k];
|
2019-10-05 12:02:34 -04:00
|
|
|
const pathDeclaration = this._parseDeclarationName(k);
|
2019-05-30 08:59:30 -04:00
|
|
|
delete obj[k];
|
|
|
|
if (pathDeclaration.length > 1) {
|
|
|
|
const shift = pathDeclaration.shift();
|
|
|
|
if (shift) {
|
|
|
|
k = shift.replace(/"/g, "");
|
|
|
|
v = this._unflat(pathDeclaration, v as object);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
k = k.replace(/"/g, "");
|
|
|
|
}
|
|
|
|
obj[k] = v;
|
|
|
|
if (v instanceof Object) {
|
2019-06-09 21:42:06 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
this._propertyClean(v as any);
|
2019-05-30 08:59:30 -04:00
|
|
|
}
|
2019-04-04 06:00:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-28 12:31:15 -04:00
|
|
|
parse(): object {
|
|
|
|
this._sanitize();
|
|
|
|
this._parseLines();
|
2019-04-04 06:00:24 -04:00
|
|
|
this._cleanOutput();
|
2019-03-28 12:31:15 -04:00
|
|
|
return this.context.output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 11:08:57 -04:00
|
|
|
// Bare keys may only contain ASCII letters,
|
|
|
|
// ASCII digits, underscores, and dashes (A-Za-z0-9_-).
|
|
|
|
function joinKeys(keys: string[]): string {
|
|
|
|
// Dotted keys are a sequence of bare or quoted keys joined with a dot.
|
|
|
|
// This allows for grouping similar properties together:
|
|
|
|
return keys
|
2019-11-13 13:42:34 -05:00
|
|
|
.map((str: string): string => {
|
|
|
|
return str.match(/[^A-Za-z0-9_-]/) ? `"${str}"` : str;
|
|
|
|
})
|
2019-09-23 11:08:57 -04:00
|
|
|
.join(".");
|
|
|
|
}
|
|
|
|
|
2019-04-05 00:23:05 -04:00
|
|
|
class Dumper {
|
2019-10-05 12:02:34 -04:00
|
|
|
maxPad = 0;
|
2019-04-05 00:23:05 -04:00
|
|
|
srcObject: object;
|
|
|
|
output: string[] = [];
|
|
|
|
constructor(srcObjc: object) {
|
|
|
|
this.srcObject = srcObjc;
|
|
|
|
}
|
|
|
|
dump(): string[] {
|
2019-06-09 21:42:06 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
this.output = this._parse(this.srcObject as any);
|
2019-04-05 00:23:05 -04:00
|
|
|
this.output = this._format();
|
|
|
|
return this.output;
|
|
|
|
}
|
2019-09-23 11:08:57 -04:00
|
|
|
_parse(obj: Record<string, unknown>, keys: string[] = []): string[] {
|
2019-04-05 00:23:05 -04:00
|
|
|
const out = [];
|
|
|
|
const props = Object.keys(obj);
|
2019-11-13 13:42:34 -05:00
|
|
|
const propObj = props.filter((e: string): boolean => {
|
|
|
|
if (obj[e] instanceof Array) {
|
|
|
|
const d: unknown[] = obj[e] as unknown[];
|
|
|
|
return !this._isSimplySerializable(d[0]);
|
2019-05-30 08:59:30 -04:00
|
|
|
}
|
2019-11-13 13:42:34 -05:00
|
|
|
return !this._isSimplySerializable(obj[e]);
|
|
|
|
});
|
|
|
|
const propPrim = props.filter((e: string): boolean => {
|
|
|
|
if (obj[e] instanceof Array) {
|
|
|
|
const d: unknown[] = obj[e] as unknown[];
|
|
|
|
return this._isSimplySerializable(d[0]);
|
2019-05-30 08:59:30 -04:00
|
|
|
}
|
2019-11-13 13:42:34 -05:00
|
|
|
return this._isSimplySerializable(obj[e]);
|
|
|
|
});
|
2019-04-05 00:23:05 -04:00
|
|
|
const k = propPrim.concat(propObj);
|
|
|
|
for (let i = 0; i < k.length; i++) {
|
|
|
|
const prop = k[i];
|
|
|
|
const value = obj[prop];
|
|
|
|
if (value instanceof Date) {
|
2019-09-23 11:08:57 -04:00
|
|
|
out.push(this._dateDeclaration([prop], value));
|
2019-04-05 00:23:05 -04:00
|
|
|
} else if (typeof value === "string" || value instanceof RegExp) {
|
2019-09-23 11:08:57 -04:00
|
|
|
out.push(this._strDeclaration([prop], value.toString()));
|
2019-04-05 00:23:05 -04:00
|
|
|
} else if (typeof value === "number") {
|
2019-09-23 11:08:57 -04:00
|
|
|
out.push(this._numberDeclaration([prop], value));
|
2019-04-05 00:23:05 -04:00
|
|
|
} else if (
|
|
|
|
value instanceof Array &&
|
|
|
|
this._isSimplySerializable(value[0])
|
|
|
|
) {
|
|
|
|
// only if primitives types in the array
|
2019-09-23 11:08:57 -04:00
|
|
|
out.push(this._arrayDeclaration([prop], value));
|
2019-04-05 00:23:05 -04:00
|
|
|
} else if (
|
|
|
|
value instanceof Array &&
|
|
|
|
!this._isSimplySerializable(value[0])
|
|
|
|
) {
|
|
|
|
// array of objects
|
|
|
|
for (let i = 0; i < value.length; i++) {
|
|
|
|
out.push("");
|
2019-09-23 11:08:57 -04:00
|
|
|
out.push(this._headerGroup([...keys, prop]));
|
|
|
|
out.push(...this._parse(value[i], [...keys, prop]));
|
2019-04-05 00:23:05 -04:00
|
|
|
}
|
|
|
|
} else if (typeof value === "object") {
|
|
|
|
out.push("");
|
2019-09-23 11:08:57 -04:00
|
|
|
out.push(this._header([...keys, prop]));
|
2019-05-30 08:59:30 -04:00
|
|
|
if (value) {
|
2019-06-09 21:42:06 -04:00
|
|
|
const toParse = value as Record<string, unknown>;
|
2019-09-23 11:08:57 -04:00
|
|
|
out.push(...this._parse(toParse, [...keys, prop]));
|
2019-05-30 08:59:30 -04:00
|
|
|
}
|
|
|
|
// out.push(...this._parse(value, `${path}${prop}.`));
|
2019-04-05 00:23:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out.push("");
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
_isSimplySerializable(value: unknown): boolean {
|
|
|
|
return (
|
|
|
|
typeof value === "string" ||
|
|
|
|
typeof value === "number" ||
|
|
|
|
value instanceof RegExp ||
|
|
|
|
value instanceof Date ||
|
|
|
|
value instanceof Array
|
|
|
|
);
|
|
|
|
}
|
2019-09-23 11:08:57 -04:00
|
|
|
_header(keys: string[]): string {
|
|
|
|
return `[${joinKeys(keys)}]`;
|
2019-04-05 00:23:05 -04:00
|
|
|
}
|
2019-09-23 11:08:57 -04:00
|
|
|
_headerGroup(keys: string[]): string {
|
|
|
|
return `[[${joinKeys(keys)}]]`;
|
2019-04-05 00:23:05 -04:00
|
|
|
}
|
2019-09-23 11:08:57 -04:00
|
|
|
_declaration(keys: string[]): string {
|
|
|
|
const title = joinKeys(keys);
|
2019-04-05 00:23:05 -04:00
|
|
|
if (title.length > this.maxPad) {
|
|
|
|
this.maxPad = title.length;
|
|
|
|
}
|
|
|
|
return `${title} = `;
|
|
|
|
}
|
2019-09-23 11:08:57 -04:00
|
|
|
_arrayDeclaration(keys: string[], value: unknown[]): string {
|
|
|
|
return `${this._declaration(keys)}${JSON.stringify(value)}`;
|
2019-04-05 00:23:05 -04:00
|
|
|
}
|
2019-09-23 11:08:57 -04:00
|
|
|
_strDeclaration(keys: string[], value: string): string {
|
|
|
|
return `${this._declaration(keys)}"${value}"`;
|
2019-04-05 00:23:05 -04:00
|
|
|
}
|
2019-09-23 11:08:57 -04:00
|
|
|
_numberDeclaration(keys: string[], value: number): string {
|
2019-04-05 00:23:05 -04:00
|
|
|
switch (value) {
|
|
|
|
case Infinity:
|
2019-09-23 11:08:57 -04:00
|
|
|
return `${this._declaration(keys)}inf`;
|
2019-04-05 00:23:05 -04:00
|
|
|
case -Infinity:
|
2019-09-23 11:08:57 -04:00
|
|
|
return `${this._declaration(keys)}-inf`;
|
2019-04-05 00:23:05 -04:00
|
|
|
default:
|
2019-09-23 11:08:57 -04:00
|
|
|
return `${this._declaration(keys)}${value}`;
|
2019-04-05 00:23:05 -04:00
|
|
|
}
|
|
|
|
}
|
2019-09-23 11:08:57 -04:00
|
|
|
_dateDeclaration(keys: string[], value: Date): string {
|
2019-10-05 12:02:34 -04:00
|
|
|
function dtPad(v: string, lPad = 2): string {
|
2020-03-31 06:34:13 -04:00
|
|
|
return v.padStart(lPad, "0");
|
2019-04-05 00:23:05 -04:00
|
|
|
}
|
2019-10-05 12:02:34 -04:00
|
|
|
const m = dtPad((value.getUTCMonth() + 1).toString());
|
|
|
|
const d = dtPad(value.getUTCDate().toString());
|
2019-04-05 00:23:05 -04:00
|
|
|
const h = dtPad(value.getUTCHours().toString());
|
|
|
|
const min = dtPad(value.getUTCMinutes().toString());
|
|
|
|
const s = dtPad(value.getUTCSeconds().toString());
|
|
|
|
const ms = dtPad(value.getUTCMilliseconds().toString(), 3);
|
2019-06-19 00:22:01 -04:00
|
|
|
// formated date
|
|
|
|
const fData = `${value.getUTCFullYear()}-${m}-${d}T${h}:${min}:${s}.${ms}`;
|
2019-09-23 11:08:57 -04:00
|
|
|
return `${this._declaration(keys)}${fData}`;
|
2019-04-05 00:23:05 -04:00
|
|
|
}
|
|
|
|
_format(): string[] {
|
|
|
|
const rDeclaration = /(.*)\s=/;
|
|
|
|
const out = [];
|
|
|
|
for (let i = 0; i < this.output.length; i++) {
|
|
|
|
const l = this.output[i];
|
|
|
|
// we keep empty entry for array of objects
|
|
|
|
if (l[0] === "[" && l[1] !== "[") {
|
|
|
|
// empty object
|
|
|
|
if (this.output[i + 1] === "") {
|
|
|
|
i += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
out.push(l);
|
|
|
|
} else {
|
|
|
|
const m = rDeclaration.exec(l);
|
|
|
|
if (m) {
|
2020-03-31 06:34:13 -04:00
|
|
|
out.push(l.replace(m[1], m[1].padEnd(this.maxPad)));
|
2019-04-05 00:23:05 -04:00
|
|
|
} else {
|
|
|
|
out.push(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Cleaning multiple spaces
|
|
|
|
const cleanedOutput = [];
|
|
|
|
for (let i = 0; i < out.length; i++) {
|
|
|
|
const l = out[i];
|
|
|
|
if (!(l === "" && out[i + 1] === "")) {
|
|
|
|
cleanedOutput.push(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cleanedOutput;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function stringify(srcObj: object): string {
|
2019-09-23 11:08:57 -04:00
|
|
|
return new Dumper(srcObj).dump().join("\n");
|
2019-04-05 00:23:05 -04:00
|
|
|
}
|
|
|
|
|
2019-03-28 12:31:15 -04:00
|
|
|
export function parse(tomlString: string): object {
|
|
|
|
// File is potentially using EOL CRLF
|
|
|
|
tomlString = tomlString.replace(/\r\n/g, "\n").replace(/\\\n/g, "\n");
|
|
|
|
return new Parser(tomlString).parse();
|
|
|
|
}
|