Skip to content

Commit

Permalink
Merge pull request #21 from evangelion1204/es6-improvements
Browse files Browse the repository at this point in the history
Further improvements
  • Loading branch information
evangelion1204 committed Nov 14, 2016
2 parents 2853d4c + 066ec37 commit c69593f
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 51 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ini.write(file, content);
Following options are available:
* encoding \[*'utf8'*\] - directly passed to readFileSync
* keep_quotes \[*false*\] - does not strip quotes around values
* filters - predefined *lowercase*, *uppercase*, *trim*
* filters - predefined *lowercase*, *uppercase*, *trim*, *constants*

### Examples

Expand Down Expand Up @@ -83,8 +83,35 @@ content = ini.read(file, {line_breaks: 'windows'});
content.section.key = value;
```

#### Parser

It's also possible to parse a ini file from an array of strings.

```js
ini = require('multi-ini');
parser = new ini.Parser();
content = parser.parse(lines);
```

#### Serializer

Like parsing it's also possible to serialize an ini object to a string.

```js
ini = require('multi-ini');
serializer = new ini.Serializer();
content = serializer.serialize({
production: {
base_url: 'https://google.com'
}
});
```

## Changelog

### 1.0.0
* First full release keeping backwards compatibility

### 0.5.2
* Introduced option for line breaks

Expand All @@ -109,4 +136,4 @@ to the value "**example**" instead of "** example**"
Implemented support for constants and removed a lot of bugs and the options **ignore_invalid** and **oninvalid**, this may be introduced again but are currently not necessary.

### 0.2.3
Fixed a bug that the module was not recognized as a module by Node.
Fixed a bug that the module was not recognized as a module by Node.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multi-ini",
"version": "0.5.2",
"version": "1.0.0",
"description": "An ini-file parser which supports multi line, multiple levels and arrays to get a maximum of compatibility with Zend config files.",
"main": "lib/index.js",
"scripts": {
Expand Down
34 changes: 34 additions & 0 deletions src/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const _ = require('lodash');

export function lowercase(value) {
return _.isString(value) ? value.toLowerCase() : value;
}

export function uppercase(value) {
return _.isString(value) ? value.toUpperCase() : value;
}

export function trim(value) {
return _.isString(value) ? value.trim() : value;
}

export function constants(value, options) {
if (!_.isString(value) || _.isEmpty(options.constants)) {
return value;
}

_.forIn(options.constants, (replacement, constant) => {
let matcher = new RegExp(`" ${constant} "`, 'g');
value = value.replace(matcher, `${replacement}`);

matcher = new RegExp(`" ${constant}$`, 'g');
value = value.replace(matcher, `${replacement}"`);

matcher = new RegExp(`^${constant} "`, 'g');
value = value.replace(matcher, `"${replacement}`);
});

return value;
}
58 changes: 18 additions & 40 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,23 @@
'use strict';

const _ = require('lodash');
const MultiIni = require('./multi-ini-class');
const Parser = require('./parser');
const Serializer = require('./serializer');
const filters = require('./filters');

export {
filters,
MultiIni as Class,
Parser,
Serializer,
};

module.exports = {
Class: MultiIni,

filters: {
lowercase: (value) => _.isString(value) ? value.toLowerCase() : value,

uppercase: (value) => _.isString(value) ? value.toUpperCase() : value,

trim: (value) => _.isString(value) ? value.trim() : value,

constants: (value, options) => {
if (!_.isString(value) || _.isEmpty(options.constants)) {
return value;
}

_.forIn(options.constants, (replacement, constant) => {
let matcher = new RegExp(`" ${constant} "`, 'g');
value = value.replace(matcher, `${replacement}`);

matcher = new RegExp(`" ${constant}$`, 'g');
value = value.replace(matcher, `${replacement}"`);

matcher = new RegExp(`^${constant} "`, 'g');
value = value.replace(matcher, `"${replacement}`);
});

return value;
}
},

read: (filename, options = {}) => {
const instance = new MultiIni(options);
return instance.read(filename);
},
export function read(filename, options = {}) {
const instance = new MultiIni(options);
return instance.read(filename);
}

write: (filename, content, options = {}) => {
const instance = new MultiIni(options);
return instance.write(filename, content);
},
};
export function write(filename, content, options = {}) {
const instance = new MultiIni(options);
return instance.write(filename, content);
}
7 changes: 1 addition & 6 deletions src/multi-ini-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@ const Constants = require('./constants');

const defaults = {
encoding: 'utf8',
ignore_invalid: true,
keep_quotes: false,
oninvalid: () => true,
filters: [],
line_breaks: 'unix',
constants: {},
};

class MultiIni {
constructor(options = {}) {
this.options = _.extend(_.clone(defaults), options);
this.options = Object.assign({}, defaults, options);

this.parser = new Parser(this.options);
this.serializer = new Serializer(this.options);
Expand Down
10 changes: 9 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ const REGEXP_ARRAY = /^(.*?)\[\]$/;
const STATUS_OK = 0;
const STATUS_INVALID = 1;

const defaults = {
ignore_invalid: true,
keep_quotes: false,
oninvalid: () => true,
filters: [],
constants: {},
};

class Parser {
constructor(options = {}) {
this.options = options;
this.options = Object.assign({}, defaults, options);

this.handlers = [
this.handleMultiLineStart,
Expand Down
7 changes: 6 additions & 1 deletion src/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
const _ = require('lodash');
const Constants = require('./constants');

const defaults = {
line_breaks: 'unix',
};


class Serializer {
constructor(options = {}) {
this.options = options
this.options = Object.assign({}, defaults, options);
}

needToBeQuoted(value) {
Expand Down

0 comments on commit c69593f

Please sign in to comment.