Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jul 5, 2021
1 parent 2988754 commit 8b70aa0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
39 changes: 21 additions & 18 deletions encoding/toml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class Parser {
!this.context.currentGroup ||
(this.context.currentGroup &&
this.context.currentGroup.name !==
line.replace(/(\[|\])/g, ""))
line.replace(/(\[|\])/g, ""))
) {
this._createGroup(line);
continue;
Expand Down Expand Up @@ -603,7 +603,7 @@ class Dumper {
const arrayType = this._getTypeOfArray(value);
if (arrayType === ArrayType.ONLY_PRIMITIVE) {
out.push(this._arrayDeclaration([prop], value));
} else if(arrayType === ArrayType.ONLY_OBJECT_EXCLUDING_ARRAY) {
} else if (arrayType === ArrayType.ONLY_OBJECT_EXCLUDING_ARRAY) {
// array of objects
for (let i = 0; i < value.length; i++) {
out.push("");
Expand All @@ -612,10 +612,9 @@ class Dumper {
}
} else {
// this is a complex array, use the inline format.
const str = value.map(x => this._printAsInlineValue(x)).join(',')
out.push(`${prop} = [${str}]`)
const str = value.map((x) => this._printAsInlineValue(x)).join(",");
out.push(`${prop} = [${str}]`);
}

} else if (typeof value === "object") {
out.push("");
out.push(this._header([...keys, prop]));
Expand All @@ -632,24 +631,28 @@ class Dumper {
_isPrimitive(value: unknown): boolean {
return value instanceof Date ||
value instanceof RegExp ||
["string", "number", "boolean"].includes(typeof value)
["string", "number", "boolean"].includes(typeof value);
}
_getTypeOfArray(arr: unknown[]): ArrayType {
if (!arr.length) {
// any type should be fine
return ArrayType.ONLY_PRIMITIVE
return ArrayType.ONLY_PRIMITIVE;
}

const onlyPrimitive = this._isPrimitive(arr[0]);
if (arr[0] instanceof Array) {
return ArrayType.MIXED;
}
for (let i = 1; i < arr.length; i++) {
if (onlyPrimitive !== this._isPrimitive(arr[i]) || arr[i] instanceof Array) {
return ArrayType.MIXED
if (
onlyPrimitive !== this._isPrimitive(arr[i]) || arr[i] instanceof Array
) {
return ArrayType.MIXED;
}
}
return onlyPrimitive ? ArrayType.ONLY_PRIMITIVE : ArrayType.ONLY_OBJECT_EXCLUDING_ARRAY;
return onlyPrimitive
? ArrayType.ONLY_PRIMITIVE
: ArrayType.ONLY_OBJECT_EXCLUDING_ARRAY;
}
_printAsInlineValue(value: unknown): string | number {
if (value instanceof Date) {
Expand All @@ -663,20 +666,20 @@ class Dumper {
} else if (
value instanceof Array
) {
const str = value.map(x => this._printAsInlineValue(x)).join(",")
return `[${str}]`
const str = value.map((x) => this._printAsInlineValue(x)).join(",");
return `[${str}]`;
} else if (typeof value === "object") {
if (!value) {
throw new Error("should never reach")
throw new Error("should never reach");
}
const str = Object.keys(value).map(key => {
const str = Object.keys(value).map((key) => {
// deno-lint-ignore no-explicit-any
return `${key} = ${this._printAsInlineValue((value as any)[key])}`
}).join(",")
return `{${str}}`
return `${key} = ${this._printAsInlineValue((value as any)[key])}`;
}).join(",");
return `{${str}}`;
}

throw new Error("should never reach")
throw new Error("should never reach");
}
_isSimplySerializable(value: unknown): boolean {
return (
Expand Down
14 changes: 7 additions & 7 deletions encoding/toml_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,15 @@ Deno.test({
name: "[TOML] Mixed Array",
fn(): void {
const src = {
mixedArray1: [1, {b: 2}],
mixedArray2: [{b: 2}, 1],
nestedArray1: [[{b: 1}]],
nestedArray2: [[[{b: 1}]]],
mixedArray1: [1, { b: 2 }],
mixedArray2: [{ b: 2 }, 1],
nestedArray1: [[{ b: 1 }]],
nestedArray2: [[[{ b: 1 }]]],
deepNested: {
a: {
b: [1, { c: 2, d: [{ e: 3}, true] }]
}
}
b: [1, { c: 2, d: [{ e: 3 }, true] }],
},
},
};
const expected = `mixedArray1 = [1,{b = 2}]
mixedArray2 = [{b = 2},1]
Expand Down

0 comments on commit 8b70aa0

Please sign in to comment.