From bc20dbe21ba35a09edb82c563d2d98ffa32c143e Mon Sep 17 00:00:00 2001 From: Lucas Wasilewski <66091116+WasixXD@users.noreply.github.com> Date: Wed, 6 Nov 2024 01:45:40 -0300 Subject: [PATCH] BREAKING(ini): parse understands booleans, undefined, null and numbers (#6121) Co-authored-by: Yoshiya Hinosawa --- ini/_ini_map.ts | 9 +++++++-- ini/mod.ts | 2 +- ini/parse_test.ts | 33 ++++++++++++++++++++++++++++++++- ini/stringify_test.ts | 9 +++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/ini/_ini_map.ts b/ini/_ini_map.ts index 6eb7931fc..edb95d870 100644 --- a/ini/_ini_map.ts +++ b/ini/_ini_map.ts @@ -302,7 +302,12 @@ export class IniMap { } 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 { } const key = leftHand.trim(); - const value = trimQuotes(rightHand.trim()); + const value = rightHand.trim(); if (currentSection) { const lineValue: LineValue = { diff --git a/ini/mod.ts b/ini/mod.ts index 40dff49d1..41d1dbedf 100644 --- a/ini/mod.ts +++ b/ini/mod.ts @@ -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", * }, * }); diff --git a/ini/parse_test.ts b/ini/parse_test.ts index 914be2c49..a9aa13780 100644 --- a/ini/parse_test.ts +++ b/ini/parse_test.ts @@ -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 }); + }, +}); diff --git a/ini/stringify_test.ts b/ini/stringify_test.ts index 1e2bca261..8d704f4e6 100644 --- a/ini/stringify_test.ts +++ b/ini/stringify_test.ts @@ -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`); }, });