Skip to content

Commit

Permalink
Improve style
Browse files Browse the repository at this point in the history
  • Loading branch information
erikolson186 committed Aug 24, 2017
1 parent 596d571 commit 3d15946
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 31 deletions.
45 changes: 16 additions & 29 deletions src/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ const {
getIDBError
} = require('./util.js');

const $set = (path_pieces, value) => (doc) => {
const ops = {};

ops.$set = (path_pieces, value) => (doc) => {
set(doc, path_pieces, value);
};

const $unset = path_pieces => doc => remove1(doc, path_pieces);
ops.$unset = path_pieces => doc => remove1(doc, path_pieces);

const $rename = (path_pieces, new_name) => (doc) => {
ops.$rename = (path_pieces, new_name) => (doc) => {
rename(doc, path_pieces, new_name);
};

Expand All @@ -38,8 +40,8 @@ const arithOp = (fn) => (path_pieces, value1) => {
return modifyOp(path_pieces, update, init);
};

const $inc = arithOp((a, b) => a + b);
const $mul = arithOp((a, b) => a * b);
ops.$inc = arithOp((a, b) => a + b);
ops.$mul = arithOp((a, b) => a * b);

const compareOp = (fn) => (path_pieces, value) => {
const update = (obj, field) => {
Expand All @@ -51,10 +53,10 @@ const compareOp = (fn) => (path_pieces, value) => {
return modifyOp(path_pieces, update, init);
};

const $min = compareOp((a, b) => a < b);
const $max = compareOp((a, b) => a > b);
ops.$min = compareOp((a, b) => a < b);
ops.$max = compareOp((a, b) => a > b);

const $push = (path_pieces, value) => {
ops.$push = (path_pieces, value) => {
const update = (obj, field) => {
const elements = obj[field];

Expand All @@ -68,7 +70,7 @@ const $push = (path_pieces, value) => {
return modifyOp(path_pieces, update, init);
};

const $pop = (path_pieces, direction) => {
ops.$pop = (path_pieces, direction) => {
let pop;

if (direction < 1) {
Expand All @@ -86,7 +88,7 @@ const $pop = (path_pieces, direction) => {
};
};

const $pullAll = (path_pieces, values) => (doc) => {
ops.$pullAll = (path_pieces, values) => (doc) => {
get(doc, path_pieces, (obj, field) => {
const elements = obj[field];
if (!Array.isArray(elements)) { return; }
Expand All @@ -109,11 +111,11 @@ const $pullAll = (path_pieces, values) => (doc) => {
});
};

const $pull = (path_pieces, value) => {
return $pullAll(path_pieces, [value]);
ops.$pull = (path_pieces, value) => {
return ops.$pullAll(path_pieces, [value]);
};

const $addToSet = (path_pieces, value) => (doc) => {
ops.$addToSet = (path_pieces, value) => (doc) => {
get(doc, path_pieces, (obj, field) => {
const elements = obj[field];
if (!Array.isArray(elements)) { return; }
Expand All @@ -126,24 +128,9 @@ const $addToSet = (path_pieces, value) => (doc) => {
});
};

const ops = {
$set,
$unset,
$rename,
$inc,
$mul,
$min,
$max,
$push,
$pop,
$pullAll,
$pull,
$addToSet
};

const build = (steps, field, value) => {
if (field[0] !== '$') {
return steps.push($set(toPathPieces(field), value));
return steps.push(ops.$set(toPathPieces(field), value));
}

const op = ops[field];
Expand Down
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const isObject = (obj) => {
return typeof obj === 'object' && obj !== null;
};

// Update a value or create it and it's path if it doesn't exist.
// Update a value or create it and its path if it doesn't exist.
const modify = (obj, path_pieces, update, init) => {
const last = path_pieces[path_pieces.length - 1];

Expand Down
2 changes: 1 addition & 1 deletion test/lang/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ describe('arithmetic', () => {
expect(build({ y: { $trunc: 4 } }).has_refs).to.be.false;
});

it("should truncate a number to it's integer", () => {
it("should truncate a number to its integer", () => {
expect(evalExpr({ $trunc: 0 })).to.equal(0);
expect(evalExpr({ $trunc: 7.80 })).to.equal(7);
expect(evalExpr({ $trunc: '$x' }, { x: -2.3 })).to.equal(-2);
Expand Down

0 comments on commit 3d15946

Please sign in to comment.