stringify

import { stringify } from "jsr:@ende/ini";

function

function stringify(object: object, options: StringifyOptions = [UNSUPPORTED]): string

Compile an object into an INI config string. Provide formatting options to modify the output.

object: object

The object to stringify

options: StringifyOptions = [UNSUPPORTED]

The option to use

Returns

The INI string

Example: Usage

import { stringify } from "@ende/ini/stringify";
import { assertEquals } from "@ende/assert";

const str = stringify({
  key1: "value1",
  key2: "value2",
  section1: {
    foo: "bar",
  },
  section2: {
    hello: "world",
  },
});

assertEquals(str, `key1=value1
key2=value2
[section1]
foo=bar
[section2]
hello=world`);

Example: Using replacer option

import { stringify } from "@ende/ini/stringify";
import { assertEquals } from "@ende/assert";

const str = stringify({
  "section X": {
    date: new Date("2024-06-10"),
  },
  "section Y": {
    name: "John"
  }
}, {
  replacer(key, value, section) {
    if (section === "section X" && key === "date") {
      return value.toISOString().slice(0, 10);
    }
    return value;
  },
});

assertEquals(str, `[section X]
date=2024-06-10
[section Y]
name=John`);

Source