properties-file parser & editor

5.0.4 · active · verified Tue Apr 21

The `properties-file` library provides a high-performance, lossless parser, editor, and formatter for Java-style `.properties` files. Currently stable at version 5.0.4, it undergoes active maintenance with frequent releases (multiple patches per month based on recent history). A key differentiator is its lossless data model, preserving comments, blank lines, and duplicate keys, allowing for exact reconstruction or normalized output via `format()` options. It boasts 3–7x faster parsing than alternatives, zero dependencies, and a tiny bundle size (970 B min+gzip for `getProperties`). Compiled to ES5, it offers broad compatibility, running on Node.js versions back to 0.4.0 and any modern browser. It also includes bundler integrations for Webpack, Rollup/Vite, esbuild, and Bun to import `.properties` files directly.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core functionalities: `getProperties` for simple key-value extraction, `Properties` for lossless parsing and node inspection, and `PropertiesEditor` for modifying entries while preserving formatting.

import { readFileSync } from 'node:fs';
import { getProperties, Properties, PropertiesNodeType } from 'properties-file/parser';
import { PropertiesEditor } from 'properties-file/editor';

// Example .properties content
const fileContent = `
# This is a comment
hello=world
foo:bar\n\r
key.with.escapes=value with spaces and \\ backslashes
duplicate=first
duplicate=second
`;

// 1. Basic key-value parsing
const simpleObject = getProperties(fileContent);
console.log('Simple object:', simpleObject);
// Expected: { hello: 'world', 'foo:bar': '', 'key.with.escapes': 'value with spaces and \ backslashes', duplicate: 'second' }

// 2. Lossless parsing with full data model
const properties = new Properties(fileContent);
console.log('\nLossless Nodes:');
for (const node of properties.nodes) {
  switch (node.type) {
    case PropertiesNodeType.PROPERTY:
      console.log(`PROPERTY: ${node.key} = ${node.value}`);
      break;
    case PropertiesNodeType.COMMENT:
      console.log(`COMMENT: ${node.body}`);
      break;
    case PropertiesNodeType.BLANK:
      console.log('BLANK LINE');
      break;
  }
}

// 3. Editing properties
const editor = new PropertiesEditor(fileContent);
editor.upsert('new_key', 'new_value');
editor.delete('duplicate', { occurrence: 'first' }); // Delete the first 'duplicate'
editor.update('hello', 'updated_world');

console.log('\nEdited properties:');
console.log(editor.format());

view raw JSON →