mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
feat(ini): add type param for value type (#5588)
This commit is contained in:
parent
2accb2b1b8
commit
d205617740
@ -18,9 +18,10 @@ export interface FormattingOptions {
|
||||
}
|
||||
|
||||
/** Options for parsing INI strings. */
|
||||
interface ParseOptions {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
interface ParseOptions<T = any> {
|
||||
/** Provide custom parsing of the value in a key/value pair. */
|
||||
reviver?: ReviverFunction;
|
||||
reviver?: ReviverFunction<T>;
|
||||
}
|
||||
|
||||
/** 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<T = any> = (
|
||||
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<T = any> {
|
||||
#global = new Map<string, LineValue>();
|
||||
#sections = new Map<string, LineSection>();
|
||||
#lines: Line[] = [];
|
||||
@ -227,8 +228,8 @@ export class IniMap {
|
||||
*
|
||||
* @returns The object equivalent to this {@code IniMap}
|
||||
*/
|
||||
toObject(): Record<string, unknown | Record<string, unknown>> {
|
||||
const obj: Record<string, unknown | Record<string, unknown>> = {};
|
||||
toObject(): Record<string, T | Record<string, T>> {
|
||||
const obj: Record<string, T | Record<string, T>> = {};
|
||||
|
||||
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<string, unknown> = {};
|
||||
const section: Record<string, T> = {};
|
||||
Object.defineProperty(obj, sec, {
|
||||
value: section,
|
||||
writable: true,
|
||||
|
18
ini/parse.ts
18
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<T = any> {
|
||||
/**
|
||||
* 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<T>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<T = any>(
|
||||
text: string,
|
||||
options?: ParseOptions,
|
||||
): Record<string, unknown | Record<string, unknown>> {
|
||||
options?: ParseOptions<T>,
|
||||
): Record<string, T | Record<string, T>> {
|
||||
return IniMap.from(text, options).toObject();
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ Deno.test({
|
||||
});
|
||||
assertEquals(ini, json);
|
||||
assertEquals((ini as Record<string, number>).__proto__, 100);
|
||||
assertEquals((ini as Record<string, string>).__proto__, json.__proto__);
|
||||
assertEquals((ini as Record<string, undefined>).__proto__, json.__proto__);
|
||||
assertStrictEquals(Object.getPrototypeOf(ini), Object.prototype);
|
||||
assertStrictEquals(
|
||||
Object.getPrototypeOf(ini),
|
||||
|
Loading…
Reference in New Issue
Block a user