diff --git a/README.md b/README.md index 94abf67..7370f65 100644 --- a/README.md +++ b/README.md @@ -204,7 +204,7 @@ The string to remove from the specified key. Can also be an array of strings. #### types[key]: String -The parse function used to format the value. Supported types are "integer", "float" and "json". +The parse function used to format the value. Supported types are "integer", "float", "boolean" and "json". #### defaults[key]: String diff --git a/lib/process.js b/lib/process.js index b7f619d..aacd415 100644 --- a/lib/process.js +++ b/lib/process.js @@ -76,7 +76,13 @@ const poll = () => { const types = { 'integer': (value) => parseInt(value, 10), 'float': (value) => parseFloat(value), - 'json': (value) => JSON.parse(value) + 'json': (value) => JSON.parse(value), + 'boolean': (value) => { + if (value === 'false' || value === '0' || value === 'no' || !value) { + return false; + } + return !!value; + } }; const parse = (record, data, config) => { diff --git a/test/index.js b/test/index.js index d036224..9ca451a 100644 --- a/test/index.js +++ b/test/index.js @@ -5,3 +5,4 @@ require('tape').test.onFinish(() => process.exit(0)); require('./sitemap'); require('./parse'); require('./process'); +require('./types'); diff --git a/test/parse.js b/test/parse.js index 0eeb77a..7eae14e 100644 --- a/test/parse.js +++ b/test/parse.js @@ -8,10 +8,7 @@ const now = new Date(); const config = { "selectors": [ {key: "title", selector: "title"} - ], - "types": { - "json": "json" - } + ] }; test('Simple parse', (t) => { @@ -90,19 +87,6 @@ test('Selector exclusion by data-attribute parse', (t) => { t.end(); }); -test('JSON formatter', (t) => { - const rec = {}; - const c = _.cloneDeep(config); - const data = ` - -`; - c.selectors.push({key: 'json', selector: 'meta'}); - parse(rec, data, c); - t.equal(rec.json.test, '1'); - t.equal(rec.json.tes2, 0.4); - t.end(); -}); - test('Simple parse no spaces', (t) => { const rec = { date: now, diff --git a/test/types.js b/test/types.js new file mode 100644 index 0000000..b0c69ac --- /dev/null +++ b/test/types.js @@ -0,0 +1,179 @@ +'use strict'; + +const _ = require('lodash'); +const parse = require('../lib/process').parse; +const test = require('tape').test; + +const now = new Date(); +const config = { + "selectors": [ + {key: "json", selector: "json"}, + {key: "integer", selector: "integer"}, + {key: "boolean", selector: "boolean"}, + {key: "float", selector: "float"}, + ], + "types": { + "json": "json", + "integer": "integer", + "boolean": "boolean", + "float": "float" + } +}; + +test('JSON formatter simple', (t) => { + const rec = { + date: now, + timestamp: now.getTime() + }; + const c = _.cloneDeep(config); + const data = ` + {"test": true} +`; + parse(rec, data, c); + t.equal(rec.date, now); + t.equal(rec.timestamp, now.getTime()); + t.equal(rec.json.test, true); + t.end(); +}); + +test('JSON formatter', (t) => { + const rec = {}; + const c = _.cloneDeep(config); + const data = ` + +`; + c.selectors[0] = {key: 'json', selector: 'meta'}; + parse(rec, data, c); + t.equal(rec.json.test, '1'); + t.equal(rec.json.tes2, 0.4); + t.equal(rec.json.o.o, "O"); + t.end(); +}); + +test('Integer formatter', (t) => { + const rec = { + date: now, + timestamp: now.getTime() + }; + const c = _.cloneDeep(config); + const data = ` + 99 +`; + parse(rec, data, c); + t.equal(rec.date, now); + t.equal(rec.timestamp, now.getTime()); + t.equal(rec.integer, 99); + t.end(); +}); + +test('Integer formatter wrong', (t) => { + const rec = { + date: now, + timestamp: now.getTime() + }; + const c = _.cloneDeep(config); + const data = ` + uhgd +`; + parse(rec, data, c); + t.equal(rec.date, now); + t.equal(rec.timestamp, now.getTime()); + t.ok(isNaN(rec.integer)); + t.end(); +}); + +test('Float formatter', (t) => { + const rec = { + date: now, + timestamp: now.getTime() + }; + const c = _.cloneDeep(config); + const data = ` + 99.06 +`; + parse(rec, data, c); + t.equal(rec.date, now); + t.equal(rec.timestamp, now.getTime()); + t.equal(rec.float, 99.06); + t.end(); +}); + +test('Float formatter wrong', (t) => { + const rec = { + date: now, + timestamp: now.getTime() + }; + const c = _.cloneDeep(config); + const data = ` + uhgd +`; + parse(rec, data, c); + t.equal(rec.date, now); + t.equal(rec.timestamp, now.getTime()); + t.ok(isNaN(rec.float)); + t.end(); +}); + +test('Boolean formatter yes', (t) => { + const rec = { + date: now, + timestamp: now.getTime() + }; + const c = _.cloneDeep(config); + const data = ` + yes +`; + parse(rec, data, c); + t.equal(rec.date, now); + t.equal(rec.timestamp, now.getTime()); + t.equal(rec.boolean, true); + t.end(); +}); + +test('Boolean formatter no', (t) => { + const rec = { + date: now, + timestamp: now.getTime() + }; + const c = _.cloneDeep(config); + const data = ` + no +`; + parse(rec, data, c); + t.equal(rec.date, now); + t.equal(rec.timestamp, now.getTime()); + t.equal(rec.boolean, false); + t.end(); +}); + +test('Boolean formatter false', (t) => { + const rec = { + date: now, + timestamp: now.getTime() + }; + const c = _.cloneDeep(config); + const data = ` + false +`; + parse(rec, data, c); + t.equal(rec.date, now); + t.equal(rec.timestamp, now.getTime()); + t.equal(rec.boolean, false); + t.end(); +}); + +test('Boolean formatter wrong', (t) => { + const rec = { + date: now, + timestamp: now.getTime() + }; + const c = _.cloneDeep(config); + const data = ` + uhgd +`; + parse(rec, data, c); + t.equal(rec.date, now); + t.equal(rec.timestamp, now.getTime()); + t.ok(rec.boolean); + t.end(); +}); \ No newline at end of file