Skip to content

Commit

Permalink
Added logic and array handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
danandrei committed May 3, 2017
1 parent 076347a commit fc632ba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const logic = require('./lib/logic');
const mask = require('./lib/mask');
const json = require('./lib/json');
const slice = require('./lib/slice');
const array = require('./lib/array');
//const join = require('./lib/join');

exports.deep = (data) => {
Expand All @@ -27,4 +28,5 @@ exports.translate = translate;
exports.logic = logic;
exports.mask = mask;
exports.slice = slice;
exports.array = array;
//exports.join = join;
14 changes: 14 additions & 0 deletions lib/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const libob = require('libobject');

exports.length = (path, data, callback) => {

let array = libob.path.get(path, data);

if (typeof array !== 'object' || !(array instanceof Array)) {
return callback(new Error('Flow-tools.array.length: Data is not an array.'));
}

callback(null, array.length);
};
11 changes: 4 additions & 7 deletions lib/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
const libob = require('libobject');
const jsonLogic = require('json-logic-js');

module.exports = (options, data, callback) => {
module.exports = (rules, data, callback) => {

if (!libob.isObject(options) || !libob.isObject(data)) {
if (!libob.isObject(rules) || !libob.isObject(data)) {
return callback(new Error('Flow-tools.logic: Options or data is not an object.'));
}

// do logic for multiple keys
Object.keys(options).forEach(function (key) {
data[key] = jsonLogic.apply(options[key], data);
});
let result = jsonLogic.apply(rules, data);

callback(null, data);
callback(null, result);
};

0 comments on commit fc632ba

Please sign in to comment.