Skip to content

Commit

Permalink
Add boolean formatter
Browse files Browse the repository at this point in the history
This allow developers to make sure they either have true or false.

It supports parsing 'false', '0', 'no' and '' as being false.
  • Loading branch information
nitriques committed Jan 23, 2019
1 parent 6d82cbe commit 4fcfe6d
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ require('tape').test.onFinish(() => process.exit(0));
require('./sitemap');
require('./parse');
require('./process');
require('./types');
18 changes: 1 addition & 17 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ const now = new Date();
const config = {
"selectors": [
{key: "title", selector: "title"}
],
"types": {
"json": "json"
}
]
};

test('Simple parse', (t) => {
Expand Down Expand Up @@ -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 = `<html>
<meta content='{"test":"1","tes2":0.4}'>
</html>`;
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,
Expand Down
179 changes: 179 additions & 0 deletions test/types.js
Original file line number Diff line number Diff line change
@@ -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 = `<html><head>
<json>{"test": true}</json>
<head></html>`;
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 = `<html>
<meta content='{"test":"1","tes2":0.4, "o": {"o":"O"}}'>
</html>`;
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 = `<html><head>
<integer>99</integer>
<head></html>`;
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 = `<html><head>
<integer>uhgd</integer>
<head></html>`;
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 = `<html><head>
<float>99.06</float>
<head></html>`;
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 = `<html><head>
<float>uhgd</float>
<head></html>`;
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 = `<html><head>
<boolean>yes</boolean>
<head></html>`;
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 = `<html><head>
<boolean>no</boolean>
<head></html>`;
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 = `<html><head>
<boolean>false</boolean>
<head></html>`;
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 = `<html><head>
<boolean>uhgd</boolean>
<head></html>`;
parse(rec, data, c);
t.equal(rec.date, now);
t.equal(rec.timestamp, now.getTime());
t.ok(rec.boolean);
t.end();
});

0 comments on commit 4fcfe6d

Please sign in to comment.