editorconfig-parser

0.0.2 • Public • Published

editorconfig parser

Build Test Coverage Code Climate Downloads Version Dependency Status

Parse and serialize .editorconfig files.

Install

npm install editorconfig-parser

Use

The following fuctions are available for general use:

  • parse({String} str){Object}: parse an editorconfig file to an object.
  • serialize({Object} obj){String}: serialize an object to an editorconfig file.
var fs = require('fs');
var ec = require('editorconfig-parser');
 
// read a file from disk
var file = fs.readFileSync('.editorconfig', 'utf8');
 
var obj = ec.parse(file);
// modify object
var str = ec.serialize(obj);
 
// write the new file
fs.writeFileSync('.editorconfig', str);

Example of a parsed editorconfig object:

{
    root: 'true',
    '*': {
        indent_style: 'space',
        indent_size: '4',
        end_of_line: 'lf',
        charset: 'utf-8',
        insert_final_newline: 'true'
    },
    '{package.json,*.yml}': {
        indent_style: 'space',
        indent_size: '2',
        insert_final_newline: 'true'
    }
};

Editorconfig itself uses a modified parser that provides the content in an array instead of an object. Theoretically, this is safer, since it absolutely guaranteed order. Unless you are doing something crazy though, the object above will do just fine and will be much easier to manipulate than the array. However, this module will support parsing and serializing the array-based version as well, though the following methods:

  • parseRaw({String} str){Array}: parse an editorconfig file to an array.
  • serializeRaw({Array} arr){String}: serialize an array to an editorconfig file.
var fs = require('fs');
var ec = require('editorconfig-parser');
 
// read a file from disk
var file = fs.readFileSync('.editorconfig', 'utf8');
 
var arr = ec.parseRaw(file);
// modify array
var str = ec.serializeRaw(arr);
 
// write the new file
fs.writeFileSync('.editorconfig', str);

Example of a parsed editorconfig array:

[ 
    [ null, { root: 'true' } ],
    [ '*', {
        indent_style: 'space',
        indent_size: '4',
        end_of_line: 'lf',
        charset: 'utf-8',
        insert_final_newline: 'true'
    }],
    [ '{package.json,*.yml}', {
        indent_style: 'space',
        indent_size: '2',
        insert_final_newline: 'true'
    }]
]

Package Sidebar

Install

npm i editorconfig-parser

Weekly Downloads

24

Version

0.0.2

License

ISC

Last publish

Collaborators

  • kirilv