BREAKING(ini): parse understands booleans, undefined, null and numbers (#6121)

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
Lucas Wasilewski 2024-11-06 01:45:40 -03:00 committed by GitHub
parent e7c6d36abf
commit bc20dbe21b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 4 deletions

View File

@ -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;
@ -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 = {

View File

@ -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",
* },
* });

View File

@ -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" },
});
@ -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 });
},
});

View File

@ -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`);
},
});