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

BREAKING(ini): remove internal Formatting type #4818

Merged
merged 1 commit into from
May 23, 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
40 changes: 13 additions & 27 deletions ini/ini_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface FormattingOptions {
/** The character used to assign a value to a key; defaults to '='. */
assignment?: string;
/** Character(s) used to break lines in the config file; defaults to '\n'. Ignored on parse. */
lineBreak?: "\n" | "\r\n";
lineBreak?: "\n" | "\r\n" | "\r";
/** Mark to use for setting comments; expects '#', ';', '//', defaults to '#' unless another mark is found. */
commentChar?: "#" | ";" | "//";
/** Use a plain assignment char or pad with spaces; defaults to false. Ignored on parse. */
Expand All @@ -15,11 +15,6 @@ export interface FormattingOptions {
deduplicate?: boolean;
}

type Formatting = Omit<FormattingOptions, "lineBreak" | "commentChar"> & {
lineBreak?: string;
commentChar?: string;
};

/** Options for parsing INI strings. */
export interface ParseOptions {
/** The character used to assign a value to a key; defaults to '='. */
Expand Down Expand Up @@ -170,7 +165,7 @@ export class IniMap {
return this.comments;
},
};
#formatting: Formatting;
#formatting: FormattingOptions;

constructor(formatting?: FormattingOptions) {
this.#formatting = this.#cleanFormatting(formatting);
Expand All @@ -185,7 +180,7 @@ export class IniMap {
return size;
}

get formatting(): Formatting {
get formatting(): FormattingOptions {
return this.#formatting;
}

Expand Down Expand Up @@ -388,34 +383,23 @@ export class IniMap {
}

*#readTextLines(text: string): Generator<string> {
const lineBreak = "\r\n";
const { length } = text;
let lineBreakLength = -1;
let line = "";

for (let i = 0; i < length; i += 1) {
const char = text[i]!;

if (lineBreak.includes(char)) {
if (char === "\n" || char === "\r") {
yield line;
line = "";
if (lineBreakLength === -1) {
const ahead = text[i + 1];
if (
ahead !== undefined && ahead !== char && lineBreak.includes(ahead)
) {
if (!this.#formatting.lineBreak) {
this.#formatting.lineBreak = char + ahead;
}
lineBreakLength = 1;
} else {
if (!this.#formatting.lineBreak) {
this.#formatting.lineBreak = char;
}
lineBreakLength = 0;
if (char === "\r" && text[i + 1] === "\n") {
i++;
if (!this.#formatting.lineBreak) {
this.#formatting.lineBreak = "\r\n";
}
} else if (!this.#formatting.lineBreak) {
this.#formatting.lineBreak = char;
}
i += lineBreakLength;
} else {
line += char;
}
Expand Down Expand Up @@ -524,7 +508,9 @@ export class IniMap {
const mark = trimmed[0];
if (mark) {
// if mark is truthy, use the character.
this.#formatting.commentChar = mark === "/" ? "//" : mark;
this.#formatting.commentChar = mark === "/"
? "//"
: mark as "#" | ";";
}
}
this.#lines.push({
Expand Down
2 changes: 1 addition & 1 deletion ini/ini_map_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Deno.test({
});
assertObjectMatch(IniMap.from("# comment\n\ra =b").formatting, {
commentChar: "#",
lineBreak: "\n\r",
lineBreak: "\n",
pretty: false,
});
},
Expand Down