properties
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -8,6 +8,5 @@ "use strict"; | ||
stringify: require ("./write"), | ||
stringifier: function (o){ | ||
if (o) return Stringifier.from (o); | ||
createStringifier: function (){ | ||
return new Stringifier (); | ||
} | ||
}; |
@@ -11,25 +11,11 @@ "use strict"; | ||
var convertType = function (value, cb){ | ||
if (value === null || value === "null") return cb (null, null); | ||
if (value === "undefined") return cb (null, undefined); | ||
if (value === "true") return cb (null, true); | ||
if (value === "false") return cb (null, false); | ||
var cast = function (value){ | ||
if (value === null || value === "null") return null; | ||
if (value === "undefined") return undefined; | ||
if (value === "true") return true; | ||
if (value === "false") return false; | ||
var v = Number (value); | ||
return cb (null, isNaN (v) ? value : v); | ||
return isNaN (v) ? value : v; | ||
}; | ||
var convertJson = function (value, cb){ | ||
if (value === null) return cb (null, null); | ||
if (value[0] === "{" || value[0] === "["){ | ||
try{ | ||
cb (null, JSON.parse (value)); | ||
}catch (error){ | ||
cb (new PropertiesError (error)); | ||
} | ||
}else{ | ||
convertType (value, cb); | ||
} | ||
}; | ||
var expand = function (o, str, options, cb){ | ||
@@ -87,6 +73,2 @@ if (!options.variables || !str) return cb (null, str); | ||
//If json is enabled, arrays and objects must be stringified | ||
if (options.json && typeof v === "object"){ | ||
v = JSON.stringify (v); | ||
} | ||
t = stack.pop (); | ||
@@ -203,3 +185,2 @@ section = t.section; | ||
var convert = options.json ? convertJson : convertType; | ||
var currentSection = null; | ||
@@ -337,7 +318,3 @@ | ||
convert (value || null, function (error, value){ | ||
if (error) return abort (error); | ||
line (key, value); | ||
}); | ||
line (key, cast (value || null)); | ||
}); | ||
@@ -358,7 +335,3 @@ }); | ||
handlers.line = function (key, value){ | ||
convert (value || null, function (error, value){ | ||
if (error) return abort (error); | ||
line (key, value); | ||
}); | ||
line (key, cast (value || null)); | ||
}; | ||
@@ -365,0 +338,0 @@ |
@@ -8,14 +8,2 @@ "use strict"; | ||
Stringifier.from = function (o){ | ||
var stringifier = new Stringifier (); | ||
//Cannot contains sections | ||
//Warning! Numeric keys are iterated before any other key | ||
for (var p in o){ | ||
stringifier.property ({ key: p, value: o[p] }); | ||
} | ||
return stringifier; | ||
}; | ||
Stringifier.prototype.header = function (comment){ | ||
@@ -22,0 +10,0 @@ this._header = comment; |
"use strict"; | ||
var escape = require ("./escape"); | ||
var stringifier = require ("./stringifier"); | ||
var Stringifier = require ("./stringifier"); | ||
@@ -70,13 +70,51 @@ //The data doesn't need to be buffered because .properties files typically | ||
var stringifySection = stringifyKey; | ||
var stringifyObject = function (obj, options){ | ||
var str = ""; | ||
var meta = { | ||
separator: options._separator.charCodeAt (0) | ||
}; | ||
var value; | ||
var first = true; | ||
if (options.replacer){ | ||
var o = { | ||
assert: function (){ | ||
return replace.property ? replace.value : true; | ||
}, | ||
isProperty: true, | ||
isSection: false | ||
}; | ||
} | ||
var toString = function (o){ | ||
if (typeof o === "object"){ | ||
return JSON.stringify (o); | ||
}else{ | ||
return o + ""; | ||
for (var p in obj){ | ||
value = obj[p]; | ||
if (options.replacer){ | ||
value = options.replacer.call (o, p, value, null); | ||
if (value === undefined) continue; | ||
} | ||
if (!first) str += EOL; | ||
meta.whitespace = true; | ||
meta.key = true; | ||
str += stringifyKey (p, meta, options); | ||
meta.key = false; | ||
str += options._separator; | ||
if (value !== null && value !== undefined){ | ||
str += stringifyValue (value + "", meta, options); | ||
} | ||
meta.whitespace = false; | ||
first = false; | ||
} | ||
return str; | ||
}; | ||
module.exports = function (stringifier, options){ | ||
var stringifyStringifier = function (stringifier, options){ | ||
var str = ""; | ||
@@ -143,3 +181,3 @@ var meta = { | ||
meta.key = true; | ||
str += stringifyKey (toString (line.key), meta, options); | ||
str += stringifyKey (line.key + "", meta, options); | ||
meta.key = false; | ||
@@ -151,3 +189,3 @@ } | ||
if (line.value !== null && line.value !== undefined){ | ||
str += stringifyValue (toString (line.value), meta, options); | ||
str += stringifyValue (line.value + "", meta, options); | ||
} | ||
@@ -158,3 +196,3 @@ | ||
if (line.name){ | ||
str += "[" + stringifySection (toString (line.name), meta, options) + | ||
str += "[" + stringifyKey (line.name + "", meta, options) + | ||
"]"; | ||
@@ -170,2 +208,10 @@ }else{ | ||
return str; | ||
}; | ||
module.exports = function (stringifier, options){ | ||
if (!(stringifier instanceof Stringifier)){ | ||
return stringifyObject (stringifier, options); | ||
}else{ | ||
return stringifyStringifier (stringifier, options); | ||
} | ||
}; |
@@ -5,3 +5,2 @@ "use strict"; | ||
var stringify = require ("./stringify"); | ||
var Stringifier = require ("./stringifier"); | ||
@@ -43,6 +42,2 @@ module.exports = function (stringifier, options, cb){ | ||
if (!(stringifier instanceof Stringifier)){ | ||
stringifier = Stringifier.from (stringifier); | ||
} | ||
var data = stringify (stringifier, options); | ||
@@ -57,4 +52,2 @@ | ||
}); | ||
}else if (cb){ | ||
cb (null, data); | ||
}else{ | ||
@@ -61,0 +54,0 @@ return data; |
{ | ||
"name": "properties", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": ".properties parser/stringifier", | ||
@@ -12,5 +12,5 @@ "keywords": ["properties", "ini", "parser", "stringifier", "config"], | ||
"devDependencies": { | ||
"ini": "*", | ||
"ini": "1.1.x", | ||
"speedy": "*", | ||
"js-yaml": "*" | ||
"js-yaml": "2.1.x" | ||
}, | ||
@@ -17,0 +17,0 @@ "license": "MIT", |
183
README.md
@@ -8,10 +8,8 @@ properties | ||
Version: 1.0.4 | ||
Version: 1.1.0 | ||
[Specification](http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load%28java.io.Reader%29) | ||
This module implements the Java .properties specification and adds additional features like ini sections, variables (key referencing), namespaces, importing files and much more. | ||
This module implements the Java .properties specification and adds additional features like [ini](#ini) sections, variables (key referencing), namespaces, importing files and much more. | ||
This is a .properties file parser/stringifier but it can also parse/stringify [ini](#ini) files. | ||
#### Installation #### | ||
@@ -25,3 +23,2 @@ | ||
- [JSON](#json) | ||
- [Sections](#sections) | ||
@@ -38,3 +35,3 @@ - [Variables](#variables) | ||
- [_module_.parse(data[, options][, callback]) : undefined | Object](#parse) | ||
- [_module_.stringifier([obj]) : Stringifier](#stringifier) | ||
- [_module_.createStringifier() : Stringifier](#createStringifier) | ||
- [_module_.stringify(obj[, options][, callback]) : undefined | String](#stringify) | ||
@@ -55,44 +52,2 @@ | ||
<a name="json"></a> | ||
__JSON__ | ||
By default the value of a property is converted to a Number, Boolean or String. When the `json` option is enabled the value can be also parsed to Array or Object. The value must be a valid json data. | ||
This is a very powerful feature because you can parse arrays. You can also parse objects but I recommend to use [namespaces](#namespaces) because with objects you have to surround with double quotes each key and if you want to write a multiline object you need to escape the line break. | ||
``` | ||
a = ["string", 1, true] | ||
``` | ||
If the namespaces are enabled the following two properties create the same object: | ||
``` | ||
a = {\ | ||
"b": 1\ | ||
} | ||
a.b = 1 | ||
``` | ||
Creates: | ||
```javascript | ||
{ | ||
a: { | ||
b: 1 | ||
} | ||
} | ||
``` | ||
Therefore, is much more easier and clear to write `a.b = 1` instead of `a = { "b": 1 }`. | ||
You can also use [variables](#variables). Remember that the value must be a valid json data, the strings must be double quoted. | ||
``` | ||
a = string | ||
b = 1 | ||
c = ["${a}", ${b}] | ||
``` | ||
--- | ||
<a name="sections"></a> | ||
@@ -401,4 +356,2 @@ __Sections__ | ||
This option can be used with the comments and separators options. If is set to true __only__ the tokens specified in these options are used to parse comments and separators. | ||
- __json__ - _Boolean_ | ||
Tries to parse the property value as an array or object. See the [json](#json) section for further details. | ||
- __sections__ - _Boolean_ | ||
@@ -417,3 +370,3 @@ Parses INI sections. See the [ini](#ini) section for further details. | ||
The reviver it's exatcly the same as the replacer from [stringify()](#stringify). The same function can be reused. | ||
The reviver it's exactly the same as the replacer from [stringify()](#stringify). The same function can be reused. | ||
@@ -424,30 +377,30 @@ The callback gets 3 parameters: key, value and section. | ||
For your convenience, to know if the line is a property or is a section, you can access to `this.isProperty` and `this.isSection` from inside the replacer function. Also, `this.assert()` can be used to return the _default_ value, the unmodified value that will be used to parse the line. | ||
For your convenience, to know if the line is a property or section you can access to `this.isProperty` and `this.isSection` from inside the replacer function. Also, `this.assert()` can be used to return the _default_ value, the unmodified value that will be used to parse the line. | ||
`this.assert()` it's the same as: | ||
```javascript | ||
if (this.isProperty){ | ||
return value; | ||
}else{ | ||
//isSection | ||
return true; | ||
} | ||
``` | ||
```javascript | ||
if (this.isProperty){ | ||
return value; | ||
}else{ | ||
//isSection | ||
return true; | ||
} | ||
``` | ||
For example, a reviver that does nothing and a reviver that removes all the lines: | ||
```javascript | ||
function (key, value, section){ | ||
//Returns all the lines | ||
return this.assert (); | ||
} | ||
``` | ||
```javascript | ||
function (key, value, section){ | ||
//Returns all the lines | ||
return this.assert (); | ||
} | ||
``` | ||
```javascript | ||
function (key, value, section){ | ||
//Removes all the lines | ||
} | ||
``` | ||
```javascript | ||
function (key, value, section){ | ||
//Removes all the lines | ||
} | ||
``` | ||
Look at the [reviver](https://github.com/gagle/node-properties/blob/master/examples/reviver/reviver.js) example for further details. | ||
@@ -457,17 +410,7 @@ | ||
<a name="stringifier"></a> | ||
___module_.stringifier([obj]) : Stringifier__ | ||
<a name="createStringifier"></a> | ||
___module_.createStringifier() : Stringifier__ | ||
Creates a new `Stringifier`. This class helps to stringify data when you want to add sections or comments. | ||
Returns a new [Stringifier](#Stringifier) instance. | ||
The function accepts a parameter. If you pass an object it will be converted to a Stringifier. It's not very useful because [stringify()](#stringify) already converts automatically the object to a Stringifier. | ||
```javascript | ||
var obj = { ... }; | ||
var stringifier = properties.stringifier (obj); | ||
properties.stringify (stringifier); | ||
//The same as: | ||
properties.stringify ({ ... }); | ||
``` | ||
--- | ||
@@ -480,18 +423,26 @@ | ||
If you don't need to add sections nor comments simply pass an object, otherwise use a Stringifier. | ||
If you don't need to add sections nor comments simply pass an object, otherwise use a [Stringifier](#Stringifier). | ||
If a callback is given, the result is returned as the second parameter. | ||
The callback is only needed when the `path` option is used. | ||
Nested objects and arrays cannot be stringified like in JSON.stringify: | ||
```javascript | ||
str = properties.stringify ({ ... }); | ||
properties.stringify ({ | ||
a: [1, "a"], | ||
b: {} | ||
}); | ||
properties.stringify ({ ... }, function (error, str){ | ||
//The "error" can be ignored, it is always null if the "path" option is not used | ||
}); | ||
/* | ||
a = 1,a | ||
b = [object Object] | ||
*/ | ||
``` | ||
This also applies to the [Stringifier](#Stringifier) keys and values. | ||
Options: | ||
- __path__ - _String_ | ||
By default `stringify()` returns a String and you decide what to do with it. If you want to write the final string to a file, give the path of a file. If this option is used the callback is mandatory. It gets two parameters, a possible error and the final string. | ||
By default `stringify()` returns a string. If you want to write it to a file, use this option and pass the path of a file. If this option is used the callback is mandatory. It gets two parameters, a possible error and the string. | ||
- __comment__ - _String_ | ||
@@ -516,30 +467,30 @@ The token to use to write comments. It must be a single printable non-whitespace ascii character. Default is `#`. | ||
For your convenience, to know if the line is a property or is a section, you can access to `this.isProperty` and `this.isSection` from inside the replacer function. Also, `this.assert()` can be used to return the _default_ value, the unmodified value that will be used to stringify the line. | ||
For your convenience, to know if the line is a property or section you can access to `this.isProperty` and `this.isSection` from inside the replacer function. Also, `this.assert()` can be used to return the _default_ value, the unmodified value that will be used to stringify the line. | ||
`this.assert()` it's the same as: | ||
```javascript | ||
if (this.isProperty){ | ||
return value; | ||
}else{ | ||
//isSection | ||
return true; | ||
} | ||
``` | ||
```javascript | ||
if (this.isProperty){ | ||
return value; | ||
}else{ | ||
//isSection | ||
return true; | ||
} | ||
``` | ||
For example, a replacer that does nothing and a replacer that removes all the lines: | ||
```javascript | ||
function (key, value, section){ | ||
//Returns all the lines | ||
return this.assert (); | ||
} | ||
``` | ||
```javascript | ||
function (key, value, section){ | ||
//Removes all the lines | ||
} | ||
``` | ||
```javascript | ||
function (key, value, section){ | ||
//Returns all the lines | ||
return this.assert (); | ||
} | ||
``` | ||
```javascript | ||
function (key, value, section){ | ||
//Removes all the lines | ||
} | ||
``` | ||
Look at the [replacer](https://github.com/gagle/node-properties/blob/master/examples/replacer.js) example for further details. | ||
@@ -554,3 +505,3 @@ | ||
To create a Stringifier use the [stringifier()](#stringifier) function. | ||
To create a Stringifier use the [createStringifier()](#createStringifier) function. | ||
@@ -583,3 +534,3 @@ __Methods__ | ||
a = | ||
b = [1,2,3] | ||
b = 1,2,3 | ||
# empty | ||
@@ -586,0 +537,0 @@ = |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43478
888
549