Skip to content

Commit

Permalink
Fix merge/apply being flipped
Browse files Browse the repository at this point in the history
  • Loading branch information
aravindet committed May 19, 2016
1 parent 10e79a2 commit 4c28b2e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonop",
"version": "3.0.1",
"version": "3.0.2",
"description": "JSON-encoded operations on JSON documents",
"main": "src/index",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var jsonop = require("./jsonop");

exports.merge = function (left, right) { return jsonop(left, right, false); };
exports.apply = function (left, right) { return jsonop(left, right, true); };
exports.merge = function (left, right) { return jsonop(left, right, true); };
exports.apply = function (left, right) { return jsonop(left, right, false); };
2 changes: 1 addition & 1 deletion src/jsonop.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function (left, right, doMerge) {
$mul: updater(function (a, b) { return a * b; }),
$min: updater(function (a, b) { return Math.min(a, b); }),
$max: updater(function (a, b) { return Math.max(a, b); }),
$mod: updater(function (a, b) { return a % b; }),
$mod: function (a, b) { return typeof a === "undefined" ? 0 : a % b; },

$union: updater(function (a, b) {
var result = a.slice(0), i = 0, l = b.length;
Expand Down
58 changes: 32 additions & 26 deletions tests/jsonop.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
var jsonop = require('../src/jsonop.js'),
var jsonop = require('../src/index.js'),
test = require('ava');

test('kitchen sink', (t) => {
let result = jsonop.apply(
{
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: { foo: 6 },
g: [7, 8, 9],
h: [8, 9, 10],
i: { foo: 9 },
},
{
a: 3,
b: [2, "$add"],
d: [4, "$mul"],
e: "$delete",
f: { bar: 7 },
g: [10, 11],
h: [ [11, 9, 4], "$union" ],
i: [ { bar: 10 }, "$replace" ],
j: [ 3, "$add" ],
k: { foo: [7, "$mod"] }
}
);

console.log("Result is", result);

t.deepEqual(
jsonop.apply(
{
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: { foo: 6 },
g: [7, 8, 9],
h: [8, 9, 10],
i: { foo: 9 },
},
{
a: 3,
b: [2, "$add"],
d: [4, "$mul"],
e: "$delete",
f: { bar: 7 },
g: [10, 11],
h: [ [11, 9, 4], "$union" ],
i: [ { bar: 10 }, "$replace" ],
j: [ 3, "$add" ],
}
),
result,
{
a: 3,
b: 4,
Expand All @@ -36,7 +41,8 @@ test('kitchen sink', (t) => {
g: [10, 11],
h: [8, 9, 10, 11, 4],
i: { bar: 10 },
j: 3
j: 3,
k: { foo: 0 }
}
);

Expand Down

0 comments on commit 4c28b2e

Please sign in to comment.