Skip to main content
Home

@std/ini@0.215.0

latest
It is unknown whether this package works with Node.js, Deno, Browsers, Cloudflare Workers, Bun
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score
76%
Published
a year ago (0.215.0)

parse and stringify for handling INI encoded data, such as the Desktop Entry specification. Values are parsed as strings by default to preserve data parity from the original. Customization is possible in the form of reviver/replacer functions like those in JSON.parse and JSON.stringify. Nested sections, repeated key names within a section, and key/value arrays are not supported, but will be preserved when using IniMap. Multi-line values are not supported and will throw a syntax error. White space padding and lines starting with '#', ';', or '//' will be treated as comments.

Examples

Example 1

import * as ini from "@std/ini";
const iniFile = `# Example configuration file
Global Key=Some data here

[Section #1]
Section Value=42
Section Date=1977-05-25
`;
const parsed = ini.parse(iniFile, {
  reviver: (key, value, section) => {
    if (section === "Section #1") {
      if (key === "Section Value") return Number(value);
      if (key === "Section Date") return new Date(value);
    }
    return value;
  },
});
console.log(parsed);

// =>
// {
//   "Global Key": "Some data here",
//   "Section #1": { "Section Value": 42, "Section Date": 1977-05-25T00:00:00.000Z }
// }

const text = ini.stringify(parsed, {
  replacer: (key, value, section) => {
    if (section === "Section #1" && key === "Section Date") {
      return (value as Date).toISOString().split("T")[0];
    }
    return value;
  },
});
console.log(text);

// =>
// Global Key=Some data here
// [Section #1]
// Section Value=42
// Section Date=1977-05-25

Optionally, IniMap may be used for finer INI handling. Using this class will permit preserving comments, accessing values like a map, iterating over key/value/section entries, and more.

Example 2

import { IniMap } from "@std/ini";
const ini = new IniMap();
ini.set("section1", "keyA", 100)
console.log(ini.toString())

// =>
// [section1]
// keyA=100

ini.set('keyA', 25)
console.log(ini.toObject())

// =>
// {
//   keyA: 25,
//   section1: {
//     keyA: 100
//   }
// }

The reviver and replacer APIs can be used to extend the behavior of IniMap, such as adding support for duplicate keys as if they were arrays of values.

Example 3

import { IniMap } from "@std/ini";
const iniFile = `# Example of key/value arrays
[section1]
key1=This key
key1=is non-standard
key1=but can be captured!
`;
const ini = new IniMap({ assignment: "=", deduplicate: true });
ini.parse(iniFile, (key, value, section) => {
  if (section) {
    if (ini.has(section, key)) {
      const exists = ini.get(section, key);
      if (Array.isArray(exists)) {
        exists.push(value);
        return exists;
      } else {
        return [exists, value];
      }
    }
  }
  return value;
});
console.log(ini.get("section1", "key1"));

// => [ "This key", "is non-standard", "but can be captured!" ]

const result = ini.toString((key, value) => {
  if (Array.isArray(value)) {
    return value.join(
      `${ini.formatting.lineBreak}${key}${ini.formatting.assignment}`,
    );
  }
  return value;
});
console.log(result === iniFile);

// => true

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/ini

Import symbol

import * as ini from "@std/ini";
or

Import directly with a jsr specifier

import * as ini from "jsr:@std/ini";

Add Package

pnpm i jsr:@std/ini
or (using pnpm 10.8 or older)
pnpm dlx jsr add @std/ini

Import symbol

import * as ini from "@std/ini";

Add Package

yarn add jsr:@std/ini
or (using Yarn 4.8 or older)
yarn dlx jsr add @std/ini

Import symbol

import * as ini from "@std/ini";

Add Package

vlt install jsr:@std/ini

Import symbol

import * as ini from "@std/ini";

Add Package

npx jsr add @std/ini

Import symbol

import * as ini from "@std/ini";

Add Package

bunx jsr add @std/ini

Import symbol

import * as ini from "@std/ini";