Skip to content

Commit

Permalink
Merge pull request #4 from jillix/registry-ready
Browse files Browse the repository at this point in the history
Registry ready
  • Loading branch information
danandrei authored Mar 14, 2017
2 parents c7bdd05 + 4f1a8f2 commit 447a2c6
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 157 deletions.
45 changes: 15 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
'use strict'

var libob = require('libobject');
var transform = require('./lib/transform');
var translate = require('./lib/translate');
var logic = require('./lib/logic');
var mask = require('./lib/mask');
var json = require('./lib/json');
var slice = require('./lib/slice');
var emit = require('./lib/emit');
//var join = require('./lib/join');

exports.deep = function (scope, inst, args, data, next) {

data = libob.deep(data);

if (data instanceof Error) {
return next(data);
}

next(null, data);
'use strict';

const libob = require('libobject');
const transform = require('./lib/transform');
const translate = require('./lib/translate');
const logic = require('./lib/logic');
const mask = require('./lib/mask');
const json = require('./lib/json');
const slice = require('./lib/slice');
//const join = require('./lib/join');

exports.deep = (data) => {
return libob.deep(data);
};

exports.flat = function (scope, inst, args, data, next) {

data = libob.flat(data);

if (data instanceof Error) {
return next(data);
}

next(null, data);
exports.flat = (data) => {
return libob.flat(data);
};

exports.emit = emit;
Expand Down
11 changes: 0 additions & 11 deletions lib/emit.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/join.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function (scope, inst, args, data, next) {
module.exports = () => {
// TODO how to know, when to call next?
};
};
40 changes: 21 additions & 19 deletions lib/json.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
var libob = require('libobject');
var qs = require('qs');
'use strict';

const libob = require('libobject');
const qs = require('qs');

/**
* Parse data or part of data
*
* @public
* @param {object} The options object.
* @param {object} The data object.
* @param {function} The next handler.
* @param {function} The callback.
*/
exports.parse = function (scope, inst, options, data, next) {
var error;
onKey(options, data, function (key, value, options) {
exports.parse = (options, data, callback) => {
let error;
onKey(options, data, (key, value, options) => {
if (!error) {
try {

Expand Down Expand Up @@ -47,27 +49,27 @@ exports.parse = function (scope, inst, options, data, next) {
}
});

next(error, data);
}
callback(error, data);
};

/**
* Stringify data or part of data
*
* @public
* @param {object} The options object.
* @param {object} The data object.
* @param {function} The next handler.
* @param {function} The callback.
*/
exports.stringify = function (scope, inst, options, data, next) {
exports.stringify = (options, data, callback) => {

var error;
onKey(options, data, function (key, value, options) {
let error;
onKey(options, data, (key, value, options) => {
if (!error) {
options = options || {};
try {
value = JSON.stringify(value, null, options.space);
if (key === null) {
data = value;
data = value;
} else {

// TODO this is a hack!
Expand All @@ -84,9 +86,9 @@ exports.stringify = function (scope, inst, options, data, next) {
}
}
});
next(error, data);
}

callback(error, data);
};

/**
* stringify based on path
Expand All @@ -101,13 +103,13 @@ function onKey (keys, object, handler) {
return handler(null, object);
}

keys.forEach(function (key) {
var options = null;
keys.forEach(key => {
let options = null;
if (key instanceof Array) {
options = key[1];
key = key[0];
}

handler(key, libob.path.get(key, object), options);
});
}
}
14 changes: 8 additions & 6 deletions lib/logic.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
var libob = require('libobject');
var jsonLogic = require('json-logic-js');
'use strict';

module.exports = function (scope, inst, options, data, next) {
const libob = require('libobject');
const jsonLogic = require('json-logic-js');

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

if (!libob.isObject(options) || !libob.isObject(data)) {
return next(new Error('Flow-tools.logic: Options or data is not an object.'));
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);
});

next(null, data);
};
callback(null, data);
};
20 changes: 11 additions & 9 deletions lib/mask.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
var libob = require('libobject');
'use strict';

const libob = require('libobject');

/**
* Add a mask on top of the data chunk.
*
* @public
* @param {object} The options object.
* @param {object} The data object.
* @param {function} The next handler.
* @param {function} The callback function.
*/
module.exports = function (scope, inst, options, data, next) {
module.exports = (options, data, callback) => {

/* TODO create docs
Config example
Expand All @@ -21,11 +23,11 @@ module.exports = function (scope, inst, options, data, next) {
*/

if (!libob.isObject(options) || typeof data !== 'object') {
return next(new Error('Flow-Tools.mask: Config or data is not an object.'));
return callback(new Error('Flow-Tools.mask: Config or data is not an object.'));
}

next(null, build(data, options));
}
callback(null, build(data, options));
};

/**
* Build new object using mask
Expand All @@ -35,9 +37,9 @@ module.exports = function (scope, inst, options, data, next) {
* @param {object} mask The mask used to build the new object.
*/
function build (obj, mask) {
var newObj = {};
let newObj = {};

for (var key in mask) {
for (let key in mask) {
if (!mask.hasOwnProperty(key)) continue;

if (libob.isObject(mask[key]) && obj[key]) {
Expand All @@ -48,4 +50,4 @@ function build (obj, mask) {
}

return newObj;
}
}
12 changes: 6 additions & 6 deletions lib/slice.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module.exports = function (scope, inst, options, data, next, stream) {
module.exports = (data, stream, callback) => {

// data must be an array
if (!(data instanceof Array)) {
return next(new Error('Flow-tool.slice: Data chunk must be an array.'));
return callback(new Error('Flow-tool.slice: Data chunk must be an array.'));
}

if (!data.length) {
return next(new Error('Flow-tool.slice: Data chunk must be a non empty array'));
return callback(new Error('Flow-tool.slice: Data chunk must be a non empty array'));
}

var lastItem = data.pop();
data.forEach(function (item) {
let lastItem = data.pop();
data.forEach(item => {
stream.push(item);
});
next(null, lastItem);
callback(null, lastItem);
};
89 changes: 27 additions & 62 deletions lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,36 @@

const libob = require('libobject');

/* Arguments: {
"flat.key": "{field}"
}*/
exports.transform = function (scope, state, args, data, next) {
libob.change(args, data, data);
next(null, data);
};

/* Arguments: ["dd|ss|ds|sd|ed|es", {
"flat.key": "{field}"
}]*/
exports.transform2 = function (scope, state, args, data, next) {

if (!(args instanceof Array)) {
return next(new Error('Flow-tools.transform2: Invalid arguments.'))
}

switch (args[0]) {
case "dd":
libob.change(args[1], data, data);
break;
case "ss":
libob.change(args[1], state, state);
break;
case "ds":
libob.change(args[1], data, state);
break;
case "sd":
libob.change(args[1], state, data);
break;
case "ed":
libob.change(args[1], scope.env, data);
break;
case "es":
libob.change(args[1], scope.env, state);
break;
default:
return next(new Error('Flow-tools.transform2: Invalid mode "' + args[0] + '"'));
}
exports.transform = (config, source, target) => {

next(null, data);
libob.change(config, source, target);
};

/* Arguments: {
"flat.key": "{field}",
"flat.key": ["{get.path.from.data}"i] ??
}*/
exports.env_transform = function (scope, state, args, data, next) {

let deep;
for (let key in args) {
if (key.indexOf('.') > 0) {
deep = true;
}

if (typeof args[key] === 'string') {
data[key] = libob.path.get(args[key], scope.env);
} else {
data[key] = libob.path.get(libob.path.get(args[key][1], data), libob.path.get(args[key][0], scope.env), true);
}
}

if (deep) {
data = libob.deep(data);
}

next(null, data);
};
// /* Arguments: {
// "flat.key": "{field}",
// "flat.key": ["{get.path.from.data}"i] ??
// }*/
// exports.env_transform = function (scope, state, args, data, stream, next) {

// let deep;
// for (let key in args) {
// if (key.indexOf('.') > 0) {
// deep = true;
// }

// if (typeof args[key] === 'string') {
// data[key] = libob.path.get(args[key], scope.env);
// } else {
// data[key] = libob.path.get(libob.path.get(args[key][1], data), libob.path.get(args[key][0], scope.env), true);
// }
// }

// if (deep) {
// data = libob.deep(data);
// }

// next(null, data);
// };
Loading

0 comments on commit 447a2c6

Please sign in to comment.