editorconfig-parser

parse and serialize editorconfog files

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
editorconfig-parser
0.0.27 years ago7 years agoMinified + gzip package size for editorconfig-parser in KB

Readme

editorconfig parser
!Build12 !Test Coverage34 !Code Climate56 !Downloads78 !Version98 !Dependency Status1011
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'
    }]
]