Skip to content

Commit

Permalink
Moving transform() out of the class and calling it depending upon i…
Browse files Browse the repository at this point in the history
…nput, more prep work for a `Worker`
  • Loading branch information
avoidwork committed Sep 28, 2015
1 parent 1cc7b26 commit e112d3f
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 121 deletions.
86 changes: 48 additions & 38 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,41 @@ function toObjekt (arg) {
return result;
}

function transform (input) {
let result;

switch (true) {
case input instanceof Map:
result = {};
input.forEach((value, key) => {
result[key] = transform(value);
});
break;
case input instanceof Set:
result = [];
input.forEach(i => {
result.push(transform(i));
});
break;
case input instanceof Array:
result = new Set();
input.forEach(i => {
result.add(transform(i));
});
break;
case input instanceof Object:
result = new Map();
Object.keys(input).forEach(i => {
result.set(i, transform(input[i]));
});
break;
default:
result = input;
}

return result;
}

function uuid () {
return (s() + s() + "-" + s() + "-4" + s().substr(0, 3) + "-" + r[Math.floor(Math.random() * 4)] + s().substr(0, 3) + "-" + s() + s() + s());
}
Expand Down Expand Up @@ -914,42 +949,7 @@ class Haro {
}

transform (input, fn) {
let result;

if (typeof fn === "function") {
result = fn(input);
} else {
switch (true) {
case input instanceof Map:
result = {};
input.forEach((value, key) => {
result[key] = this.transform(value);
});
break;
case input instanceof Set:
result = [];
input.forEach(i => {
result.push(this.transform(i));
});
break;
case input instanceof Array:
result = new Set();
input.forEach(i => {
result.add(this.transform(i));
});
break;
case input instanceof Object:
result = new Map();
Object.keys(input).forEach(i => {
result.set(i, this.transform(input[i]));
});
break;
default:
result = input;
}
}

return result;
return typeof fn === "function" ? fn(input) : transform(input);
}

unload (type = "mongo", key = undefined) {
Expand Down Expand Up @@ -981,11 +981,21 @@ class Haro {

function factory (data = null, config = {}, indexes = []) {
let obj = new Haro(data, config, indexes),
fns = [];
functions;

if (webWorker) {
functions = [
clone.toString(),
keyIndex.toString(),
iterate.toString(),
merge.toString(),
setIndexValue.toString(),
setIndex.toString(),
transform.toString()
];

try {
obj.worker = global.URL.createObjectURL(blob(fns.join("\n")));
obj.worker = global.URL.createObjectURL(blob(functions.join("\n")));
} catch (e) {
obj.worker = null;
}
Expand Down
86 changes: 43 additions & 43 deletions lib/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,41 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return result;
}

function _transform(input) {
var result = undefined;

switch (true) {
case input instanceof Map:
result = {};
input.forEach(function (value, key) {
result[key] = _transform(value);
});
break;
case input instanceof Set:
result = [];
input.forEach(function (i) {
result.push(_transform(i));
});
break;
case input instanceof Array:
result = new Set();
input.forEach(function (i) {
result.add(_transform(i));
});
break;
case input instanceof Object:
result = new Map();
Object.keys(input).forEach(function (i) {
result.set(i, _transform(input[i]));
});
break;
default:
result = input;
}

return result;
}

function uuid() {
return s() + s() + "-" + s() + "-4" + s().substr(0, 3) + "-" + r[Math.floor(Math.random() * 4)] + s().substr(0, 3) + "-" + s() + s() + s();
}
Expand Down Expand Up @@ -1033,63 +1068,26 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}, {
key: "transform",
value: function transform(input, fn) {
var _this14 = this;

var result = undefined;

if (typeof fn === "function") {
result = fn(input);
} else {
switch (true) {
case input instanceof Map:
result = {};
input.forEach(function (value, key) {
result[key] = _this14.transform(value);
});
break;
case input instanceof Set:
result = [];
input.forEach(function (i) {
result.push(_this14.transform(i));
});
break;
case input instanceof Array:
result = new Set();
input.forEach(function (i) {
result.add(_this14.transform(i));
});
break;
case input instanceof Object:
result = new Map();
Object.keys(input).forEach(function (i) {
result.set(i, _this14.transform(input[i]));
});
break;
default:
result = input;
}
}

return result;
return typeof fn === "function" ? fn(input) : _transform(input);
}
}, {
key: "unload",
value: function unload() {
var _this15 = this;
var _this14 = this;

var type = arguments.length <= 0 || arguments[0] === undefined ? "mongo" : arguments[0];
var key = arguments.length <= 1 || arguments[1] === undefined ? undefined : arguments[1];

var id = key !== undefined ? key : this.id;

return this.cmd(type, "remove", key).then(function (arg) {
if (_this15.logging) {
if (_this14.logging) {
console.log("Unloaded", id, "from", type, "persistent storage");
}

return arg;
}, function (e) {
if (_this15.logging) {
if (_this14.logging) {
console.error("Error unloading", id, "from", type, "persistent storage:", e.message || e.stack || e);
}

Expand Down Expand Up @@ -1117,11 +1115,13 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var indexes = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2];

var obj = new Haro(data, config, indexes),
fns = [];
functions = undefined;

if (webWorker) {
functions = [clone.toString(), keyIndex.toString(), iterate.toString(), merge.toString(), setIndexValue.toString(), setIndex.toString(), _transform.toString()];

try {
obj.worker = global.URL.createObjectURL(blob(fns.join("\n")));
obj.worker = global.URL.createObjectURL(blob(functions.join("\n")));
} catch (e) {
obj.worker = null;
}
Expand Down
Loading

0 comments on commit e112d3f

Please sign in to comment.