Skip to content

Commit

Permalink
BREAKING(ini): parse understands booleans, undefined, null and numbers (
Browse files Browse the repository at this point in the history
#6121)

Co-authored-by: Yoshiya Hinosawa <[email protected]>
  • Loading branch information
WasixXD and kt3k authored Nov 6, 2024
1 parent e7c6d36 commit bc20dbe
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
9 changes: 7 additions & 2 deletions ini/_ini_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,12 @@ export class IniMap<T = any> {
}
const reviverFunc: ReviverFunction = typeof reviver === "function"
? reviver
: (_key, value, _section) => value;
: (_key, value, _section) => {
if (!isNaN(+value) && !value.includes('"')) return +value;
if (value === "null") return null;
if (value === "true" || value === "false") return value === "true";
return trimQuotes(value);
};
let lineNumber = 1;
let currentSection: LineSection | undefined;

Expand Down Expand Up @@ -355,7 +360,7 @@ export class IniMap<T = any> {
}

const key = leftHand.trim();
const value = trimQuotes(rightHand.trim());
const value = rightHand.trim();

if (currentSection) {
const lineValue: LineValue = {
Expand Down
2 changes: 1 addition & 1 deletion ini/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* assertEquals(parse(text), {
* "Global Key": "Some data here",
* "Section #1": {
* "Section Value": "42",
* "Section Value": 42,
* "Section Date": "1977-05-25",
* },
* });
Expand Down
33 changes: 32 additions & 1 deletion ini/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Deno.test({
});
assertValidParse(`a=b\n[section]\nc=d`, { a: "b", section: { c: "d" } });
assertValidParse('value="value"', { value: "value" });
assertValidParse("#comment\nkeyA=1977-05-25\n[section1]\nkeyA=100", {
assertValidParse('#comment\nkeyA=1977-05-25\n[section1]\nkeyA="100"', {
keyA: "1977-05-25",
section1: { keyA: "100" },
});
Expand Down Expand Up @@ -171,3 +171,34 @@ Deno.test({
assert(success);
},
});

Deno.test({
name: "parse() return value as number",
fn() {
assertEquals(parse("value=123"), { value: 123 });
assertEquals(parse("value=1e3"), { value: 1000 });
},
});

Deno.test({
name: "parse() correctly parse number with special characters ",
fn() {
assertEquals(parse("value=123foo"), { value: "123foo" });
assertEquals(parse('value="1e3"'), { value: "1e3" });
},
});

Deno.test({
name: "parse() return value as null",
fn() {
assertEquals(parse("value=null"), { value: null });
},
});

Deno.test({
name: "parse() correctly parse booleans",
fn() {
assertEquals(parse("value=true"), { value: true });
assertEquals(parse("value=false"), { value: false });
},
});
9 changes: 9 additions & 0 deletions ini/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,14 @@ Deno.test({
keyA: "1977-05-25",
section1: { keyA: 100 },
}, `keyA=1977-05-25\n[section1]\nkeyA=100`);

assertValidStringify({ a: 100 }, `a=100`);
assertValidStringify({ a: 100 }, `a = 100`, { pretty: true });
assertValidStringify({ a: "123foo" }, `a=123foo`);
assertValidStringify({ a: "foo" }, `a=foo`);
assertValidStringify({ a: true }, `a=true`);
assertValidStringify({ a: false }, `a=false`);
assertValidStringify({ a: null }, `a=null`);
assertValidStringify({ a: undefined }, `a=undefined`);
},
});

0 comments on commit bc20dbe

Please sign in to comment.