Skip to content

Commit

Permalink
Functional inner join, need to audit resulting object
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Oct 14, 2015
1 parent 368dbc0 commit 1264f22
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 40 deletions.
15 changes: 14 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,20 @@ module.exports = function (grunt) {
sourceMap: true,
sourceMapIncludeSources: true,
mangle: {
except: ["Map", "Set", "Haro", "cast", "createIndexes", "keyIndex", "setIndexValue", "setIndex"]
except: [
"Map",
"Set",
"Haro",
"cast",
"clone",
"createIndexes",
"each",
"keyIndex",
"joinData",
"merge",
"setIndexValue",
"setIndex"
]
}
},
target: {
Expand Down
30 changes: 18 additions & 12 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ function onmessage (ev) {
}

if (cmd === "join") {
result = joinData(data.records[0], data.records[1], data.on, data.type);
result = joinData(data.records[0], data.records[1], data.key, data.on, data.type);
}

postMessage(JSON.stringify(result));
}

function joinData (a, b, on, type = "inner") {
function joinData (a, b, key, on, type = "inner") {
let error = false,
errorMsg, result;

Expand All @@ -214,7 +214,7 @@ function joinData (a, b, on, type = "inner") {

each(a, function (i) {
let c = b.filter(function (x) {
return x[on] === i[on];
return x[on] === i[key];
});

if (c.length > 1) {
Expand Down Expand Up @@ -554,13 +554,16 @@ class Haro {
return this.data.has(key);
}

join (other = [], on = this.key, type = "inner", where = {}) {
join (other, on = this.key, type = "inner", where = {}) {
let defer = deferred();

if (other.length > 0) {
if (other.total > 0) {
if (Object.keys(where).length > 0) {
this.find(where, true).then(data => {
return data.length > 0 ? this.offload([data, other, on, type], "join") : [];
Promise.all([
this.find(where, true),
other.find(where, true)
]).then(data => {
return data[0].length > 0 && data[1].length > 1 ? this.offload([data[0], data[1], this.key, on, type], "join") : [];
}, function (e) {
throw e;
}).then(function (result) {
Expand All @@ -571,7 +574,7 @@ class Haro {
}
}, defer.reject);
} else {
this.offload([this.toArray(null, true), other, on, type], "join").then(function (result) {
this.offload([this.toArray(null, true), other.toArray(null, true), this.key, on, type], "join").then(function (result) {
if (typeof result === "string") {
defer.reject(new Error(result));
} else {
Expand Down Expand Up @@ -674,8 +677,9 @@ class Haro {
payload = {
cmd: cmd,
records: [data[0], data[1]],
on: data[2],
type: data[3]
key: data[2],
on: data[3],
type: data[4]
};
}

Expand Down Expand Up @@ -1168,13 +1172,15 @@ function factory (data = null, config = {}, indexes = []) {

if (webWorker) {
functions = [
cast.toString(),
clone.toString(),
createIndexes.toString(),
each.toString(),
joinData.toString(),
keyIndex.toString(),
merge.toString(),
setIndexValue.toString(),
setIndex.toString(),
cast.toString(),
joinData.toString(),
(!server ? "" : "self.") + "onmessage = " + onmessage.toString() + ";"
];

Expand Down
26 changes: 13 additions & 13 deletions lib/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,14 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}

if (cmd === "join") {
result = joinData(data.records[0], data.records[1], data.on, data.type);
result = joinData(data.records[0], data.records[1], data.key, data.on, data.type);
}

postMessage(JSON.stringify(result));
}

function joinData(a, b, on) {
var type = arguments.length <= 3 || arguments[3] === undefined ? "inner" : arguments[3];
function joinData(a, b, key, on) {
var type = arguments.length <= 4 || arguments[4] === undefined ? "inner" : arguments[4];

var error = false,
errorMsg = undefined,
Expand All @@ -221,7 +221,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

each(a, function (i) {
var c = b.filter(function (x) {
return x[on] === i[on];
return x[on] === i[key];
});

if (c.length > 1) {
Expand Down Expand Up @@ -607,8 +607,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}
}, {
key: "join",
value: function join() {
var other = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
value: function join(other) {
var on = arguments.length <= 1 || arguments[1] === undefined ? this.key : arguments[1];

var _this5 = this;
Expand All @@ -618,10 +617,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

var defer = deferred();

if (other.length > 0) {
if (other.total > 0) {
if (Object.keys(where).length > 0) {
this.find(where, true).then(function (data) {
return data.length > 0 ? _this5.offload([data, other, on, type], "join") : [];
Promise.all([this.find(where, true), other.find(where, true)]).then(function (data) {
return data[0].length > 0 && data[1].length > 1 ? _this5.offload([data[0], data[1], _this5.key, on, type], "join") : [];
}, function (e) {
throw e;
}).then(function (result) {
Expand All @@ -632,7 +631,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}
}, defer.reject);
} else {
this.offload([this.toArray(null, true), other, on, type], "join").then(function (result) {
this.offload([this.toArray(null, true), other.toArray(null, true), this.key, on, type], "join").then(function (result) {
if (typeof result === "string") {
defer.reject(new Error(result));
} else {
Expand Down Expand Up @@ -753,8 +752,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
payload = {
cmd: cmd,
records: [data[0], data[1]],
on: data[2],
type: data[3]
key: data[2],
on: data[3],
type: data[4]
};
}

Expand Down Expand Up @@ -1323,7 +1323,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
functions = undefined;

if (webWorker) {
functions = [createIndexes.toString(), each.toString(), keyIndex.toString(), setIndexValue.toString(), setIndex.toString(), cast.toString(), joinData.toString(), (!server ? "" : "self.") + "onmessage = " + onmessage.toString() + ";"];
functions = [cast.toString(), clone.toString(), createIndexes.toString(), each.toString(), joinData.toString(), keyIndex.toString(), merge.toString(), setIndexValue.toString(), setIndex.toString(), (!server ? "" : "self.") + "onmessage = " + onmessage.toString() + ";"];

try {
obj.worker = !server ? global.URL.createObjectURL(blob(functions.join("\n"))) : new Function(functions.join("\n"));
Expand Down
2 changes: 1 addition & 1 deletion lib/haro.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/haro.min.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ function factory (data = null, config = {}, indexes = []) {

if (webWorker) {
functions = [
cast.toString(),
clone.toString(),
createIndexes.toString(),
each.toString(),
joinData.toString(),
keyIndex.toString(),
merge.toString(),
setIndexValue.toString(),
setIndex.toString(),
cast.toString(),
joinData.toString(),
(!server ? "" : "self.") + "onmessage = " + onmessage.toString() + ";"
];

Expand Down
18 changes: 11 additions & 7 deletions src/haro.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,16 @@ class Haro {
return this.data.has(key);
}

join (other = [], on = this.key, type = "inner", where = {}) {
join (other, on = this.key, type = "inner", where = {}) {
let defer = deferred();

if (other.length > 0) {
if (other.total > 0) {
if (Object.keys(where).length > 0) {
this.find(where, true).then(data => {
return data.length > 0 ? this.offload([data, other, on, type], "join") : [];
Promise.all([
this.find(where, true),
other.find(where, true)
]).then(data => {
return data[0].length > 0 && data[1].length > 1 ? this.offload([data[0], data[1], this.key, on, type], "join") : [];
}, function (e) {
throw e;
}).then(function (result) {
Expand All @@ -269,7 +272,7 @@ class Haro {
}
}, defer.reject);
} else {
this.offload([this.toArray(null, true), other, on, type], "join").then(function (result) {
this.offload([this.toArray(null, true), other.toArray(null, true), this.key, on, type], "join").then(function (result) {
if (typeof result === "string") {
defer.reject(new Error(result));
} else {
Expand Down Expand Up @@ -372,8 +375,9 @@ class Haro {
payload = {
cmd: cmd,
records: [data[0], data[1]],
on: data[2],
type: data[3]
key: data[2],
on: data[3],
type: data[4]
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ function onmessage (ev) {
}

if (cmd === "join") {
result = joinData(data.records[0], data.records[1], data.on, data.type);
result = joinData(data.records[0], data.records[1], data.key, data.on, data.type);
}

postMessage(JSON.stringify(result));
}

function joinData (a, b, on, type = "inner") {
function joinData (a, b, key, on, type = "inner") {
let error = false,
errorMsg, result;

Expand All @@ -184,7 +184,7 @@ function joinData (a, b, on, type = "inner") {

each(a, function (i) {
let c = b.filter(function (x) {
return x[on] === i[on];
return x[on] === i[key];
});

if (c.length > 1) {
Expand Down

0 comments on commit 1264f22

Please sign in to comment.