From d2056177407eda4cc486e011670b6d92611c5ee5 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Fri, 2 Aug 2024 19:19:02 +1000 Subject: [PATCH] feat(ini): add type param for value type (#5588) --- ini/_ini_map.ts | 23 ++++++++++++----------- ini/parse.ts | 18 +++++++++++------- ini/parse_test.ts | 2 +- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/ini/_ini_map.ts b/ini/_ini_map.ts index 4ef44b3b4..1be7277b6 100644 --- a/ini/_ini_map.ts +++ b/ini/_ini_map.ts @@ -18,9 +18,10 @@ export interface FormattingOptions { } /** Options for parsing INI strings. */ -interface ParseOptions { +// deno-lint-ignore no-explicit-any +interface ParseOptions { /** Provide custom parsing of the value in a key/value pair. */ - reviver?: ReviverFunction; + reviver?: ReviverFunction; } /** Function for replacing JavaScript values with INI string values. */ @@ -32,13 +33,12 @@ export type ReplacerFunction = ( ) => string; /** Function for replacing INI values with JavaScript values. */ -export type ReviverFunction = ( +// deno-lint-ignore no-explicit-any +export type ReviverFunction = ( key: string, - // deno-lint-ignore no-explicit-any - value: any, + value: string, section?: string, - // deno-lint-ignore no-explicit-any -) => any; +) => T; const ASSIGNMENT_MARK = "="; @@ -52,7 +52,8 @@ function trimQuotes(value: string): string { /** * Class implementation for fine control of INI data structures. */ -export class IniMap { +// deno-lint-ignore no-explicit-any +export class IniMap { #global = new Map(); #sections = new Map(); #lines: Line[] = []; @@ -227,8 +228,8 @@ export class IniMap { * * @returns The object equivalent to this {@code IniMap} */ - toObject(): Record> { - const obj: Record> = {}; + toObject(): Record> { + const obj: Record> = {}; for (const { key, val } of this.#global.values()) { Object.defineProperty(obj, key, { @@ -239,7 +240,7 @@ export class IniMap { }); } for (const { sec, map } of this.#sections.values()) { - const section: Record = {}; + const section: Record = {}; Object.defineProperty(obj, sec, { value: section, writable: true, diff --git a/ini/parse.ts b/ini/parse.ts index b74faf0e8..3366dee12 100644 --- a/ini/parse.ts +++ b/ini/parse.ts @@ -2,23 +2,25 @@ // This module is browser compatible. import { IniMap, type ReviverFunction } from "./_ini_map.ts"; -export type { ParseOptions, ReviverFunction }; +export type { ReviverFunction }; /** Options for {@linkcode parse}. */ -interface ParseOptions { +// deno-lint-ignore no-explicit-any +export interface ParseOptions { /** * Provide custom parsing of the value in a key/value pair. Similar to the * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#reviver | reviver} * function in {@linkcode JSON.parse}. */ - reviver?: ReviverFunction; + reviver?: ReviverFunction; } /** * Parse an INI config string into an object. * * Values are parsed as strings by default to preserve data parity from the - * original. + * original. To parse values as other types besides strings, use + * {@linkcode ParseOptions.reviver}. * * Nested sections, repeated key names within a section, and key/value arrays * are not supported. White space padding and lines starting with `#`, `;`, or @@ -75,11 +77,13 @@ interface ParseOptions { * * @param text The text to parse * @param options The options to use + * @typeParam T The type of the value * @return The parsed object */ -export function parse( +// deno-lint-ignore no-explicit-any +export function parse( text: string, - options?: ParseOptions, -): Record> { + options?: ParseOptions, +): Record> { return IniMap.from(text, options).toObject(); } diff --git a/ini/parse_test.ts b/ini/parse_test.ts index 33f6a2420..914be2c49 100644 --- a/ini/parse_test.ts +++ b/ini/parse_test.ts @@ -104,7 +104,7 @@ Deno.test({ }); assertEquals(ini, json); assertEquals((ini as Record).__proto__, 100); - assertEquals((ini as Record).__proto__, json.__proto__); + assertEquals((ini as Record).__proto__, json.__proto__); assertStrictEquals(Object.getPrototypeOf(ini), Object.prototype); assertStrictEquals( Object.getPrototypeOf(ini),