From fc632ba4d84570aed985a6364d8c998c75a7d839 Mon Sep 17 00:00:00 2001 From: Dan Andrei Date: Wed, 3 May 2017 18:07:32 +0300 Subject: [PATCH] Added logic and array handlers. --- index.js | 2 ++ lib/array.js | 14 ++++++++++++++ lib/logic.js | 11 ++++------- 3 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 lib/array.js diff --git a/index.js b/index.js index a43ed7f..d54d42c 100644 --- a/index.js +++ b/index.js @@ -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) => { @@ -27,4 +28,5 @@ exports.translate = translate; exports.logic = logic; exports.mask = mask; exports.slice = slice; +exports.array = array; //exports.join = join; diff --git a/lib/array.js b/lib/array.js new file mode 100644 index 0000000..63f544f --- /dev/null +++ b/lib/array.js @@ -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); +}; \ No newline at end of file diff --git a/lib/logic.js b/lib/logic.js index 60e51d5..37bd781 100644 --- a/lib/logic.js +++ b/lib/logic.js @@ -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); }; \ No newline at end of file