Tiny EditorConfig
tiny-editorconfig is an isomorphic JavaScript/TypeScript library designed to parse and resolve EditorConfig configurations. It provides functionality to take a raw `.editorconfig` string and convert it into a structured JavaScript object, as well as to resolve multiple parsed configurations for a given file path, applying the cascading rules of EditorConfig. The current stable version is 1.0.2. Being isomorphic, it can run seamlessly in both Node.js and browser environments without specific environment dependencies. Its primary differentiator is its minimal footprint and focused scope, offering a lightweight alternative to larger configuration processing tools. Release cadence is expected to be infrequent, primarily for bug fixes or minor specification updates, due to the stability of the EditorConfig specification itself.
Common errors
-
Error: Invalid EditorConfig string provided.
cause The input string passed to `parse` is malformed and cannot be interpreted as valid EditorConfig syntax.fixEnsure the input string adheres to the EditorConfig format. Check for missing sections, invalid key-value pairs, or unescaped characters. -
TypeError: Cannot read properties of undefined (reading 'charset')
cause Attempting to access a property (e.g., `charset`) from the resolved configuration that was not defined in any of the provided EditorConfig inputs for the given path.fixEditorConfig properties are only present in the resolved object if explicitly defined. Check if the property is expected or provide a fallback value.
Warnings
- gotcha The library primarily works with string inputs and does not handle file system operations (like reading `.editorconfig` files from disk) itself. Users must provide the content of the `.editorconfig` files as strings.
- gotcha EditorConfig property names and values are typically case-insensitive according to the spec, but `tiny-editorconfig` normalizes keys to camelCase and values to lowercase. Be aware of this transformation when working with the parsed output.
Install
-
npm install tiny-editorconfig -
yarn add tiny-editorconfig -
pnpm add tiny-editorconfig
Imports
- parse
const parse = require('tiny-editorconfig').parse;import { parse } from 'tiny-editorconfig'; - resolve
const resolve = require('tiny-editorconfig').resolve;import { resolve } from 'tiny-editorconfig'; - EditorConfig (namespace)
const EditorConfig = require('tiny-editorconfig');import * as EditorConfig from 'tiny-editorconfig';
Quickstart
import { parse, resolve } from 'tiny-editorconfig';
const editorConfigString1 = `
root=true
[*]
charset = UTF-8
end_of_line = lf
indent_size = 2
indent_style = "space"
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
`;
const editorConfigString2 = `
[*]
end_of_line = crlf
indent_size = 4
`;
const parsed1 = parse(editorConfigString1);
const parsed2 = parse(editorConfigString2);
// Resolve the configurations for a specific path, applying EditorConfig rules
const resolvedConfig = resolve([parsed1, parsed2], '/path/to/my-document.md');
console.log(resolvedConfig);
/* Expected Output:
{
root: true,
charset: 'utf-8',
endOfLine: 'crlf',
indentSize: 4,
indentStyle: 'space',
insertFinalNewline: true,
trimTrailingWhitespace: false
}
*/