From 2a4059a92c0cf16ec0559367c4ff9bf3e1ff49b0 Mon Sep 17 00:00:00 2001 From: Angelo Tata Date: Sun, 2 Nov 2014 01:26:36 +0100 Subject: [PATCH 1/4] Make casting configurable. Add relevant tests. --- README.md | 13 ++++++++++++- lib/read.js | 33 ++++++++++++++++++++++---------- package.json | 15 +++++++++++---- test/parse.js | 51 +++++++++++++++++++++++++++++++++++++++++++------ test/properties | 9 +-------- 5 files changed, 92 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 924b731..5e8ad68 100644 --- a/README.md +++ b/README.md @@ -383,6 +383,17 @@ Options: ``` - __strict__ - _Boolean_ This option can be used with the `comments` and `separators` options. If true, __only__ the tokens specified in these options are used to parse comments and separators. +- __casting__ - _Object_ + By default, property values will be cast to their JavaScript type equivalent. It is possible to disable this behaviour per type: +```javascript +casting: { + nulls: false, + undefineds: false, + booleans: false, + numbers: false +} +``` +For example, if your property values may contain numbers in octal, hexadecimal or exponential notation, think carefully about their range and their subsequent usage, or the automatic casting might surprise you. - __sections__ - _Boolean_ Parses INI sections. Read the [INI](#ini) section for further details. - __namespaces__ - _Boolean_ @@ -623,4 +634,4 @@ stringifier.section ({ name: "my section", comment: "My Section" }); */ ``` -Look at the [stringify-ini](https://github.com/gagle/node-properties/blob/master/examples/ini/stringify-ini.js) example for further details. \ No newline at end of file +Look at the [stringify-ini](https://github.com/gagle/node-properties/blob/master/examples/ini/stringify-ini.js) example for further details. diff --git a/lib/read.js b/lib/read.js index b9e37df..39211aa 100644 --- a/lib/read.js +++ b/lib/read.js @@ -3,17 +3,29 @@ var fs = require ("fs"); var path = require ("path"); var parse = require ("./parse"); +var merge = require('deepmerge'); var INCLUDE_KEY = "include"; var INDEX_FILE = "index.properties"; +var CASTING_DEFAULTS = { + nulls: true, + undefineds: true, + booleans: true, + numbers: true +}; -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 isNaN (v) ? value : v; +var cast = function (value, options){ + if (options.nulls) { if (value === null || value === "null") return null; } + if (options.undefineds) { if (value === "undefined") return undefined; } + if (options.booleans) { + if (value === "true") return true; + if (value === "false") return false; + } + if (options.numbers) { + var v = Number (value); + return isNaN (v) ? value : v; + } + return value; }; var expand = function (o, str, options, cb){ @@ -365,7 +377,7 @@ var build = function (data, options, dirname, cb){ function (error, value){ if (error) return abort (error); - line (key, cast (value || null)); + line (key, cast (value || null, options._casting)); }); }); }; @@ -381,7 +393,7 @@ var build = function (data, options, dirname, cb){ } }else{ handlers.line = function (key, value){ - line (key, cast (value || null)); + line (key, cast (value || null, options._casting)); }; if (options.sections){ @@ -436,6 +448,7 @@ module.exports = function (data, options, cb){ options = options || {}; options._strict = options.strict && (options.comments || options.separators); options._vars = options.vars || {}; + options._casting = merge(CASTING_DEFAULTS, options.casting); var comments = options.comments || []; if (!Array.isArray (comments)) comments = [comments]; @@ -477,4 +490,4 @@ module.exports = function (data, options, cb){ }else{ return build (data, options, ".", cb); } -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index a741bad..80f81bd 100644 --- a/package.json +++ b/package.json @@ -2,20 +2,27 @@ "name": "properties", "version": "1.2.1", "description": ".properties parser/stringifier", - "keywords": ["properties", "ini", "parser", "stringifier", "config"], + "keywords": [ + "properties", + "ini", + "parser", + "stringifier", + "config" + ], "author": "Gabriel Llamas ", "repository": "git://github.com/gagle/node-properties.git", "engines": { "node": ">=0.10" }, "devDependencies": { + "deepmerge": "^0.2.7", "ini": "1.1.x", - "speedy": "*", - "js-yaml": "2.1.x" + "js-yaml": "2.1.x", + "speedy": "*" }, "scripts": { "test": "node test/parse && node test/stringify" }, "license": "MIT", "main": "lib" -} \ No newline at end of file +} diff --git a/test/parse.js b/test/parse.js index 830c49c..a52ebd6 100644 --- a/test/parse.js +++ b/test/parse.js @@ -43,11 +43,6 @@ var tests = { a19: "É", "←": "→", "→": "→", - a20: true, - a21: false, - a22: 123, - a23: null, - a24: undefined, "[a]": null }); @@ -592,6 +587,50 @@ var tests = { done (); }); + }, + "cast values according to defaults": function(done) { + var options = { path: true }; + + properties.parse ("types", options, + function (error, p){ + assert.ifError (error); + + assert.deepEqual (p, { + a1: true, + a2: false, + a3: 123, + a4: null, + a5: undefined, + a6: 123, + a7: 291, + a8: 1230 + }); + + done (); + } + ); + }, + "cast values according to options": function(done) { + var options = { path: true, casting: { nulls: false, undefineds: false, booleans: false, numbers: false } }; + + properties.parse ("types", options, + function (error, p){ + assert.ifError (error); + + assert.deepEqual (p, { + a1: 'true', + a2: 'false', + a3: '123', + a4: 'null', + a5: 'undefined', + a6: '0123', + a7: '0x123', + a8: '123e1' + }); + + done (); + } + ); } }; @@ -610,4 +649,4 @@ var keysLength = keys.length; again (i + 1); } } -})(0); \ No newline at end of file +})(0); diff --git a/test/properties b/test/properties index 0c70476..80d7064 100644 --- a/test/properties +++ b/test/properties @@ -45,12 +45,5 @@ a19 \u00c9 92=\u\21\ 92 -#Type conversion -a20 true -a21 false -a22 123 -a23 null -a24 undefined - #No sections -[a] \ No newline at end of file +[a] From 6f699376895be812927c95828d059513caaf3b61 Mon Sep 17 00:00:00 2001 From: Angelo Tata Date: Sun, 2 Nov 2014 01:43:11 +0100 Subject: [PATCH 2/4] Fix text alignment --- README.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5e8ad68..11d012b 100644 --- a/README.md +++ b/README.md @@ -385,15 +385,17 @@ Options: This option can be used with the `comments` and `separators` options. If true, __only__ the tokens specified in these options are used to parse comments and separators. - __casting__ - _Object_ By default, property values will be cast to their JavaScript type equivalent. It is possible to disable this behaviour per type: -```javascript -casting: { - nulls: false, - undefineds: false, - booleans: false, - numbers: false -} -``` -For example, if your property values may contain numbers in octal, hexadecimal or exponential notation, think carefully about their range and their subsequent usage, or the automatic casting might surprise you. + + ```javascript + casting: { + nulls: false, + undefineds: false, + booleans: false, + numbers: false + } + ``` + + For example, if your property values may contain numbers in octal, hexadecimal or exponential notation, think carefully about their range and their subsequent usage, or the automatic casting might surprise you. - __sections__ - _Boolean_ Parses INI sections. Read the [INI](#ini) section for further details. - __namespaces__ - _Boolean_ From 39a0e45c67fbebe2ecc440f6c5aada9461e972f0 Mon Sep 17 00:00:00 2001 From: Angelo Tata Date: Sun, 2 Nov 2014 01:54:50 +0100 Subject: [PATCH 3/4] Forgot to add the new properties test file. Shame on me --- test/types | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 test/types diff --git a/test/types b/test/types new file mode 100644 index 0000000..8f5ead5 --- /dev/null +++ b/test/types @@ -0,0 +1,9 @@ +#Type conversion +a1 true +a2 false +a3 123 +a4 null +a5 undefined +a6 0123 +a7 0x123 +a8 123e1 From b569546a8065d68f7ac62c45e975934fb99f7d7f Mon Sep 17 00:00:00 2001 From: Angelo Tata Date: Tue, 24 Mar 2015 11:30:22 +0000 Subject: [PATCH 4/4] Fixed dependency --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 80f81bd..1f71a7d 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,10 @@ "engines": { "node": ">=0.10" }, + "dependencies": { + "deepmerge": "^0.2.7" + }, "devDependencies": { - "deepmerge": "^0.2.7", "ini": "1.1.x", "js-yaml": "2.1.x", "speedy": "*"