Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(toml): remove Utils object #5342

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 55 additions & 57 deletions toml/_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,66 +140,64 @@ function failure(): Failure {
};
}

export const Utils = {
unflat(
keys: string[],
values: unknown = {},
cObj?: unknown,
): Record<string, unknown> {
const out: Record<string, unknown> = {};
if (keys.length === 0) {
return cObj as Record<string, unknown>;
export function unflat(
keys: string[],
values: unknown = {},
cObj?: unknown,
): Record<string, unknown> {
const out: Record<string, unknown> = {};
if (keys.length === 0) {
return cObj as Record<string, unknown>;
} else {
if (!cObj) {
cObj = values;
}
const key: string | undefined = keys[keys.length - 1];
if (typeof key === "string") {
out[key] = cObj;
}
return unflat(keys.slice(0, -1), values, out);
}
}
export function deepAssignWithTable(target: Record<string, unknown>, table: {
type: "Table" | "TableArray";
key: string[];
value: Record<string, unknown>;
}) {
if (table.key.length === 0 || table.key[0] == null) {
throw new Error("Unexpected key length");
}
const value = target[table.key[0]];

if (typeof value === "undefined") {
Object.assign(
target,
unflat(
table.key,
table.type === "Table" ? table.value : [table.value],
),
);
} else if (Array.isArray(value)) {
if (table.type === "TableArray" && table.key.length === 1) {
value.push(table.value);
} else {
if (!cObj) {
cObj = values;
}
const key: string | undefined = keys[keys.length - 1];
if (typeof key === "string") {
out[key] = cObj;
}
return this.unflat(keys.slice(0, -1), values, out);
}
},
deepAssignWithTable(target: Record<string, unknown>, table: {
type: "Table" | "TableArray";
key: string[];
value: Record<string, unknown>;
}) {
if (table.key.length === 0 || table.key[0] == null) {
throw new Error("Unexpected key length");
}
const value = target[table.key[0]];

if (typeof value === "undefined") {
Object.assign(
target,
this.unflat(
table.key,
table.type === "Table" ? table.value : [table.value],
),
);
} else if (Array.isArray(value)) {
if (table.type === "TableArray" && table.key.length === 1) {
value.push(table.value);
} else {
const last = value[value.length - 1];
Utils.deepAssignWithTable(last, {
type: table.type,
key: table.key.slice(1),
value: table.value,
});
}
} else if (typeof value === "object" && value !== null) {
Utils.deepAssignWithTable(value as Record<string, unknown>, {
const last = value[value.length - 1];
deepAssignWithTable(last, {
type: table.type,
key: table.key.slice(1),
value: table.value,
});
} else {
throw new Error("Unexpected assign");
}
},
};
} else if (typeof value === "object" && value !== null) {
deepAssignWithTable(value as Record<string, unknown>, {
type: table.type,
key: table.key.slice(1),
value: table.value,
});
} else {
throw new Error("Unexpected assign");
}
}

// ---------------------------------
// Parser combinators and generators
Expand Down Expand Up @@ -264,7 +262,7 @@ function kv<T>(
`Value of key/value pair is invalid data format`,
);
}
return success(Utils.unflat(key.body, value.body));
return success(unflat(key.body, value.body));
};
}

Expand Down Expand Up @@ -850,11 +848,11 @@ export function toml(
break;
}
case "Table": {
Utils.deepAssignWithTable(body, block);
deepAssignWithTable(body, block);
break;
}
case "TableArray": {
Utils.deepAssignWithTable(body, block);
deepAssignWithTable(body, block);
break;
}
}
Expand Down
16 changes: 8 additions & 8 deletions toml/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
bareKey,
basicString,
dateTime,
deepAssignWithTable,
dottedKey,
float,
inlineTable,
Expand All @@ -19,7 +20,6 @@ import {
symbols,
table,
TOMLParseError,
Utils,
value,
} from "./_parser.ts";
import { parse, stringify } from "./mod.ts";
Expand Down Expand Up @@ -401,7 +401,7 @@ Deno.test({
});

Deno.test({
name: "parse() handles Utils.deepAssignWithTable",
name: "parse() handles deepAssignWithTable",
fn() {
const source = {
foo: {
Expand All @@ -419,7 +419,7 @@ Deno.test({
},
};

Utils.deepAssignWithTable(
deepAssignWithTable(
source,
{
type: "Table",
Expand Down Expand Up @@ -452,14 +452,14 @@ Deno.test({
});

Deno.test({
name: "parse() handles Utils.deepAssignWithTable / TableArray",
name: "parse() handles deepAssignWithTable / TableArray",
fn() {
const source = {
foo: {},
bar: null,
};

Utils.deepAssignWithTable(
deepAssignWithTable(
source,
{
type: "TableArray",
Expand All @@ -480,7 +480,7 @@ Deno.test({
bar: null,
},
);
Utils.deepAssignWithTable(
deepAssignWithTable(
source,
{
type: "TableArray",
Expand All @@ -507,7 +507,7 @@ Deno.test({

assertThrows(
() =>
Utils.deepAssignWithTable(
deepAssignWithTable(
source,
{
type: "TableArray",
Expand All @@ -521,7 +521,7 @@ Deno.test({

assertThrows(
() =>
Utils.deepAssignWithTable(
deepAssignWithTable(
source,
{
type: "TableArray",
Expand Down