Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions ini/_ini_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,32 +269,6 @@ export class IniMap {
return obj;
}

/**
* Convert this `IniMap` to an INI string.
*
* @param replacer The replacer
* @returns Ini string
*/
toString(replacer?: ReplacerFunction): string {
replacer ??= (_key, value, _section) => `${value}`;

const pretty = this.#formatting?.pretty ?? false;
const assignment = pretty ? ` ${ASSIGNMENT_MARK} ` : ASSIGNMENT_MARK;
const lines = this.#lines;

return lines.map((line) => {
switch (line.type) {
case "comment":
return line.val;
case "section":
return `[${line.sec}]`;
case "value":
return line.key + assignment +
replacer(line.key, line.val, line.sec);
}
}).join(this.#formatting?.lineBreak ?? "\n");
}

/**
* Parse an INI string in this `IniMap`.
*
Expand Down
46 changes: 43 additions & 3 deletions ini/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.

import { IniMap, type ReplacerFunction } from "./_ini_map.ts";
import type { ReplacerFunction } from "./_ini_map.ts";

/** Options for {@linkcode stringify}. */
export interface StringifyOptions {
Expand All @@ -28,6 +28,20 @@ export interface StringifyOptions {

export type { ReplacerFunction };

function isPlainObject(object: unknown): object is object {
return Object.prototype.toString.call(object) === "[object Object]";
}

const sort = ([_a, valA]: [string, unknown], [_b, valB]: [string, unknown]) => {
if (isPlainObject(valA)) return 1;
if (isPlainObject(valB)) return -1;
return 0;
};

function defaultReplacer(_key: string, value: unknown, _section?: string) {
return `${value}`;
}

/**
* Compile an object into an INI config string. Provide formatting options to modify the output.
*
Expand Down Expand Up @@ -86,6 +100,32 @@ export type { ReplacerFunction };
* @param options The option to use
* @returns The INI string
*/
export function stringify(object: object, options?: StringifyOptions): string {
return IniMap.from(object, options).toString(options?.replacer);
export function stringify(
object: object,
options: StringifyOptions = {},
): string {
const {
replacer = defaultReplacer,
pretty = false,
lineBreak = "\n",
} = options;
const assignment = pretty ? " = " : "=";

const entries = Object.entries(object).sort(sort);

const lines = [];
for (const [key, value] of entries) {
if (isPlainObject(value)) {
const sectionName = key;
lines.push(`[${sectionName}]`);
for (const [key, val] of Object.entries(value)) {
const line = `${key}${assignment}${replacer(key, val, sectionName)}`;
lines.push(line);
}
} else {
const line = `${key}${assignment}${replacer(key, value)}`;
lines.push(line);
}
}
return lines.join(lineBreak);
}
Loading