Skip to content

Commit

Permalink
Merge pull request #3 from elasticio/new_modules
Browse files Browse the repository at this point in the history
added crypt, formats, heartbeat-stream, ObjectUtilities
  • Loading branch information
Igor Drobiazko committed Sep 22, 2015
2 parents 54b1339 + d26dd27 commit 2cf47b0
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 43 deletions.
8 changes: 6 additions & 2 deletions elasticio.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
exports.HttpComponent = require("./lib/httpComponent.js").HttpComponent;
exports.messages = require("./lib/messages.js");
exports.HttpComponent = require("./lib/httpComponent").HttpComponent;
exports.messages = require("./lib/messages");
exports.crypt = require("./lib/crypt");
exports.formats = require("./lib/formats");
exports.HeartBeatStream = require("./lib/heartbeat-stream");
exports.ObjectUtilities = require("./lib/ObjectUtilities");
92 changes: 92 additions & 0 deletions lib/ObjectUtilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module.exports = {
flatten: makeObjectFlatten,
unflatten: makeObjectUnflatten,
getRefWithPath: getRefWithPath,
removeEmptyValues: removeEmptyValues
};

/**
* Remove all null, empty string (''), empty object ({}), empty array ([]) properties from an object
*/
function removeEmptyValues(obj){

for (var key in obj) {
if (obj[key] === null || obj[key] === '') {
delete obj[key];
}
if (typeof obj[key] === 'object') {
// recursive clean of child object
obj[key] = removeEmptyValues(obj[key]);
// delete child object if empty
if (Object.keys(obj[key]).length === 0) {
delete obj[key];
}
}
}
return obj;
}

/**
* Return value of field with given path in given object
* If path couldn't be resolved return null
* @param {Object} obj
* @param {String} path
* @returns {Object|null}
*/
function getRefWithPath(obj, path) {
var keys = path.split(".");
var curr = obj;
for (var i = 0; i < keys.length; i++) {
if (!curr[keys[i]]) {
return null;
} else {
curr = curr[keys[i]];
}
}
return curr;
}

function makeObjectFlatten(data) {
var result = {};
function recurse (cur, prop) {
if (Object(cur) !== cur) {
result[prop] = cur;
} else if (Array.isArray(cur)) {
for (var i=0, l=cur.length; i<l; i++) {
recurse(cur[i], prop + "[" + i + "]");
}
if (l == 0){
result[prop] = [];
}
} else {
var isEmpty = true;
for (var p in cur) {
isEmpty = false;
recurse(cur[p], prop ? prop+"."+p : p);
}
if (isEmpty && prop) {
result[prop] = {};
}
}
}
recurse(data, "");
return result;
}

function makeObjectUnflatten(data) {
if (Object(data) !== data || Array.isArray(data))
return data;
var regex = /\.?([^.\[\]]+)|\[(\d+)\]/g,
resultholder = {};
for (var p in data) {
var cur = resultholder,
prop = "",
m;
while (m = regex.exec(p)) {
cur = cur[prop] || (cur[prop] = (m[2] ? [] : {}));
prop = m[2] || m[1];
}
cur[prop] = data[p];
}
return resultholder[""] || resultholder;
}
18 changes: 18 additions & 0 deletions lib/crypt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var crypto = require('crypto');
var crc = require('crc');

exports.getHash = getHash;
exports.getCRC = getCRC;

function getHash (entry) {
return crypto.createHash('md5').update(JSON.stringify(entry)).digest('hex');
}

/**
* Works much faster as hash but with similar results. Has higher probability of clashes.
*
* @param entry
*/
function getCRC(entry) {
return crc.crc32(JSON.stringify(entry)).toString(16);
}
62 changes: 62 additions & 0 deletions lib/formats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var moment = require('moment');
var accounting = require('accounting');


const DATE_TIME_FORMAT = "YYYY-MM-DDTHH:mm:ssZ";

exports.toDateTime = function (value, formats) {
return moment(value, formats).format(DATE_TIME_FORMAT);
};

exports.toTargetDateTime = function (value, format) {
return moment(value, DATE_TIME_FORMAT).format(format);
};

exports.utc = function (date) {
return moment(date, DATE_TIME_FORMAT).utc();
};

/**
* This method converts money string to number, e.g. "1.234,56 €" to 1234.56
* as well as "$ 1,234.56" to same value as above
*
* WARNING: Works only well for money where we have at max 100 cent in € or $
*
* @param string
*/
exports.moneyToNumber = function (string, decimal) {
// This regexp will match a string that starts
// with 0, 1 or 2 numbers followed by any number of non-number chars
var expr = /^[0-9]{0,2}[^0-9]*$/;
var expr2 = /^[0-9]{3}[^0-9]*$/;
if (!string || !string.length) {
return string;
}
if (string.length == 0) {
return undefined;
}
if (!decimal) {
// First we need to decide what's the decimal separator
// as we don't have an access to encoding or locales
// and it's not set
var lastComma = string.lastIndexOf(',');
var lastPoint = string.lastIndexOf('.');
if (lastComma > 0 && lastPoint > 0) {
// if we have both, then the last wins
if (lastComma > lastPoint) {
decimal = ','
} else {
decimal = '.'
}
} else {
if (lastComma >= 0 && expr.test(string.substr(lastComma + 1))) {
decimal = ',';
} else if (lastPoint >= 0 && expr.test(string.substr(lastPoint + 1))) {
decimal = '.'
} else if (lastPoint >= 0 && expr2.test(string.substr(lastPoint + 1))) {
decimal = ',';
}
}
}
return accounting.unformat(string, decimal);
};
45 changes: 45 additions & 0 deletions lib/heartbeat-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var events = require("events");
var StreamCounter = require('stream-counter');
var util = require("util");

var HEARTBEAT_PERIOD = 10 * 1000; // 10 secs

var HeartBeatStream = function(heartBeatEmitter) {
if (heartBeatEmitter instanceof events.EventEmitter) {
this.emitter = heartBeatEmitter;
}
events.EventEmitter.call(this);
};

util.inherits(HeartBeatStream, events.EventEmitter);

HeartBeatStream.prototype.start = function start(stream) {

var lastHeartbeatTime = 0;

var that = this;

var counter = new StreamCounter();

counter.on('progress', function() {

var now = new Date().getTime();

if ((now - lastHeartbeatTime) > HEARTBEAT_PERIOD) {
console.log(counter.bytes + " bytes read so far");

lastHeartbeatTime = now;

if (that.emitter) {
that.emitter.emit('heartbeat')
}

that.emit('heartbeat');
}

});

stream.pipe(counter);
};

exports.HeartBeatStream = HeartBeatStream;
86 changes: 45 additions & 41 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
{
"name": "elasticio-node",
"description": "Node.js API for the elastic.io integration platform",
"version": "0.0.6",
"author": {
"name": "elastic.io GmbH",
"email": "info@elastic.io"
},
"keywords": [
"elasticio",
"integration",
"restful",
"webservice",
"http"
],
"main": "./elasticio.js",
"repository": {
"type": "git",
"url": "git://github.com/elasticio/nodejs-api"
},
"bugs": {
"url": "https://github.com/elasticio/nodejs-api/issues"
},
"homepage": "http://www.elastic.io",
"dependencies": {
"request": "2.9.x",
"node-uuid": "1.3.3",
"q": "1.0.0",
"underscore":"1.5.1"
},
"devDependencies": {
"nock": "0.27.2",
"grunt": "~0.4.5",
"grunt-cli": "~0.1.9",
"grunt-contrib-clean": "~0.5.0",
"grunt-jasmine-node": "0.2.1",
"grunt-istanbul": "0.3.0",
"grunt-env": "~0.4.1",
"grunt-coveralls": "0.3.0",
"grunt-contrib-jshint": "~0.7.2",
"grunt-contrib-copy": "~0.5.0"
}
"name": "elasticio-node",
"description": "Node.js API for the elastic.io integration platform",
"version": "0.0.7",
"author": {
"name": "elastic.io GmbH",
"email": "info@elastic.io"
},
"keywords": [
"elasticio",
"integration",
"restful",
"webservice",
"http"
],
"main": "./elasticio.js",
"repository": {
"type": "git",
"url": "git://github.com/elasticio/nodejs-api"
},
"bugs": {
"url": "https://github.com/elasticio/nodejs-api/issues"
},
"homepage": "http://www.elastic.io",
"dependencies": {
"accounting": "0.4.1",
"crc": "3.3.0",
"moment": "2.10.6",
"node-uuid": "1.3.3",
"q": "1.0.0",
"request": "2.9.x",
"stream-counter": "1.0.0",
"underscore": "1.5.1"
},
"devDependencies": {
"nock": "0.27.2",
"grunt": "~0.4.5",
"grunt-cli": "~0.1.9",
"grunt-contrib-clean": "~0.5.0",
"grunt-jasmine-node": "0.2.1",
"grunt-istanbul": "0.3.0",
"grunt-env": "~0.4.1",
"grunt-coveralls": "0.3.0",
"grunt-contrib-jshint": "~0.7.2",
"grunt-contrib-copy": "~0.5.0"
}
}

0 comments on commit 2cf47b0

Please sign in to comment.