Skip to content

Commit

Permalink
chaining to methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ivank committed Jul 25, 2017
1 parent 3daac07 commit 864775e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-originals",
"version": "1.3.1",
"version": "1.3.2",
"description": "Get original value of mongoose fields",
"main": "src/index.js",
"repository": "[email protected]:enhancv/mongoose-originals.git",
Expand Down
22 changes: 10 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,29 @@ function mongooseOriginals(schema, userOptions) {
throw new Error("No fields specified for mongoose originals on schema");
}

function isChanged() {
schema.method("isChanged", function isChanged() {
return (
!this.original ||
!isEqual(
this.original,
pick(options.fields, this.toObject({ getters: false, transform: false }))
)
);
}
});

function setSnapshotOriginal() {
schema.method("setSnapshotOriginal", function setSnapshotOriginal() {
eachMongooseOriginalsPath(this, item => {
item.snapshotOriginal = item.original;
});
}
return this;
});

function clearSnapshotOriginal() {
schema.method("clearSnapshotOriginal", function clearSnapshotOriginal() {
eachMongooseOriginalsPath(this, item => {
delete item.snapshotOriginal;
});
}
return this;
});

function saveOriginalNamed() {
this.original = {};
Expand All @@ -53,17 +55,13 @@ function mongooseOriginals(schema, userOptions) {
});
}

function initOriginals() {
schema.method("initOriginals", function initOriginals() {
if (this.original === undefined) {
saveOriginalNamed.bind(this)();
}
}
});

schema.mongooseOriginals = true;
schema.method("setSnapshotOriginal", setSnapshotOriginal);
schema.method("clearSnapshotOriginal", clearSnapshotOriginal);
schema.method("initOriginals", initOriginals);
schema.method("isChanged", isChanged);
schema.post("init", saveOriginalNamed);
schema.post("save", saveOriginalNamed);

Expand Down
4 changes: 2 additions & 2 deletions test/indexTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe(
return nested
.save()
.then(nested => {
nested.setSnapshotOriginal();
assert.equal(nested.setSnapshotOriginal(), nested);

assert.deepEqual(nested.snapshotOriginal, nested.original);
assert.deepEqual(nested.embedded.snapshotOriginal, nested.embedded.original);
Expand Down Expand Up @@ -104,7 +104,7 @@ describe(
assert.deepEqual(nested.children[1].snapshotOriginal, { category: "77" });
assert.deepEqual(nested.children[1].nested.snapshotOriginal, { type: "88" });

nested.clearSnapshotOriginal();
assert.equal(nested.clearSnapshotOriginal(), nested);

assert.equal(nested.snapshotOriginal, undefined);
assert.equal(nested.embedded.snapshotOriginal, undefined);
Expand Down

0 comments on commit 864775e

Please sign in to comment.