Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <script src="https://github.com/typicaljoe/taffydb/raw/master/taffy.js"></script>
- <script>
- (function () {
- innerJoinFunction = (function () {
- function _mode(rightTable, arg1, arg2) {
- if (typeof rightTable.filter !== "function") {
- if (rightTable.TAFFY) {
- rightTable = rightTable(); //"Normalize" rightTable so we know we can call filter on it
- } else {
- console.log(rightTable);
- throw "TAFFY DB or result not supplied";
- }
- }
- if (typeof arg1 === "function") {
- return _functionMode.call(this, rightTable, arg1);
- }
- if (typeof arg1 === "object" && arg1.length) {
- return _arrayMode.apply(this, arguments);
- }
- if (typeof arg1 === "string" && typeof arg2 === "string") {
- return _equiJoinMode.call(this, rightTable, arg1, arg2);
- }
- throw "Not sure how to join.";
- }
- function _combine(l, r) {
- var out = {},
- i,
- prefix;
- for (i in l) {
- if (l.hasOwnProperty(i)) {
- out[i] = l[i];
- }
- }
- for (i in r) {
- if (r.hasOwnProperty(i)) {
- prefix = "";
- if (out[i]) {
- prefix = "right_";
- }
- out[prefix + i] = r[i];
- }
- }
- return out;
- }
- function _functionMode(rightTable, comparison) {
- this.context({
- results: this.getDBI().query(this.context())
- });
- var toReturn = [];
- TAFFY.each(this.context().results, function (l) {
- rightTable.each(function (r) {
- if (comparison(l, r)) {
- toReturn.push(_combine(l, r));
- }
- });
- });
- return TAFFY(toReturn)();
- }
- function _arrayMode(rightTable) {
- var args = arguments;
- function _compareFunction(l, r) {
- var i,
- passed = true,
- leftCol,
- rightCol,
- operator;
- //Note: Start i at 1 since the first argument is rightTable!
- for (i = 1; i < args.length; i += 1) {
- leftCol = l[args[i][0]];
- operator = args[i][1];
- rightCol = r[args[i][2]];
- switch (operator) {
- case "==":
- passed = passed && (leftCol == rightCol);
- break;
- case "===":
- passed = passed && (leftCol === rightCol);
- break;
- case "!=":
- passed = passed && (leftCol != rightCol);
- break;
- case "!==":
- passed = passed && (leftCol !== rightCol);
- break;
- case "<":
- passed = passed && (leftCol < rightCol);
- break;
- case ">":
- passed = passed && (leftCol > rightCol);
- break;
- case "<=":
- passed = passed && (leftCol <= rightCol);
- break;
- case ">=":
- passed = passed && (leftCol >= rightCol);
- break;
- default:
- throw "Unrecognized operator: " + operator;
- }
- }
- return passed;
- }
- return _functionMode.call(this, rightTable, _compareFunction);
- }
- function _equiJoinMode(rightTable, leftCol, rightCol) {
- return _functionMode.call(this, rightTable, function (l, r) {
- return l[leftCol] === r[rightCol];
- });
- }
- return _mode;
- }());
- TAFFY.extend("innerJoin", innerJoinFunction);
- }());
- var cities = TAFFY([
- {name:"New York",state:"NY"},
- {name:"Las Vegas",state:"NV"},
- {name:"Boston",state:"MA"}
- ]);
- var states = TAFFY([
- {name: "New York", abbreviation: "NY"},
- {name: "Nevada", abbreviation: "NV"},
- {name: "Massachusetts", abbreviation: "MA"}
- ]);
- console.log("Equijoin:");
- cities().innerJoin(states, "state", "abbreviation").each(function (r) { console.log(r)});
- console.log("Equijoin w/pre-filtered DB:");
- cities().innerJoin(states({abbreviation: "NV"}), "state", "abbreviation").each(function (r) { console.log(r)});
- console.log("Array Join, 'Not Equal':");
- cities().innerJoin(states, ["state", "!==", "abbreviation"]).each(function (r) { console.log(r)});
- console.log("Function join, state name contains abbreviation:");
- cities().innerJoin(states, function (l, r) {
- return (r.name.toLowerCase().indexOf(l.state.toLowerCase()) >= 0);
- }).each(function (r) { console.log(r)});
- </script>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement