Skip to content

Commit 5472681

Browse files
author
Christoph Werner
committed
Add .editorconfig
1 parent 75d3899 commit 5472681

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
charset = utf-8
7+
indent_style = space
8+
indent_size = 2

lib/index.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6+
exports.PatchError = undefined;
67

78
exports.default = function (schema, opts) {
89
var options = (0, _lodash.merge)({}, defaultOptions, opts);
@@ -31,13 +32,13 @@ exports.default = function (schema, opts) {
3132

3233
// roll the document back to the state of a given patch id
3334
schema.methods.rollback = function (patchId, data) {
34-
var _this = this;
35+
var _this2 = this;
3536

3637
return this.patches.find({ ref: this.id }).sort({ date: 1 }).exec().then(function (patches) {
3738
return new _bluebird2.default(function (resolve, reject) {
3839
// patch doesn't exist
3940
if (! ~(0, _lodash.map)(patches, 'id').indexOf(patchId)) {
40-
return reject(new Error('patch doesn\'t exist'));
41+
return reject(new PatchError('patch doesn\'t exist'));
4142
}
4243

4344
// get all patches that should be applied
@@ -48,7 +49,7 @@ exports.default = function (schema, opts) {
4849
// if the patches that are going to be applied are all existing patches,
4950
// the rollback attempts to rollback to the latest patch
5051
if (patches.length === apply.length) {
51-
return reject(new Error('rollback to latest patch'));
52+
return reject(new PatchError('rollback to latest patch'));
5253
}
5354

5455
// apply patches to `state`
@@ -58,7 +59,7 @@ exports.default = function (schema, opts) {
5859
});
5960

6061
// save new state and resolve with the resulting document
61-
_this.set((0, _lodash.merge)(data, state)).save().then(resolve).catch(reject);
62+
_this2.set((0, _lodash.merge)(data, state)).save().then(resolve).catch(reject);
6263
});
6364
});
6465
};
@@ -100,7 +101,7 @@ exports.default = function (schema, opts) {
100101
// document has changed), a new patch document reflecting the changes is
101102
// added to the associated patch collection
102103
schema.pre('save', function (next) {
103-
var _this2 = this;
104+
var _this3 = this;
104105

105106
var ref = this._id;
106107

@@ -114,7 +115,7 @@ exports.default = function (schema, opts) {
114115
// assemble patch data
115116
var data = { ops: ops, ref: ref };
116117
(0, _lodash.each)(options.includes, function (type, name) {
117-
data[name] = _this2[type.from || name];
118+
data[name] = _this3[type.from || name];
118119
});
119120

120121
this.patches.create(data).then(next).catch(next);
@@ -141,6 +142,29 @@ var _lodash = require('lodash');
141142

142143
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
143144

145+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
146+
147+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
148+
149+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
150+
151+
var PatchError = exports.PatchError = function (_Error) {
152+
_inherits(PatchError, _Error);
153+
154+
function PatchError(message) {
155+
_classCallCheck(this, PatchError);
156+
157+
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(PatchError).call(this));
158+
159+
Error.captureStackTrace(_this, _this.constructor);
160+
_this.name = _this.constructor.name;
161+
_this.message = message;
162+
return _this;
163+
}
164+
165+
return PatchError;
166+
}(Error);
167+
144168
var createPatchModel = function createPatchModel(options) {
145169
var def = {
146170
date: { type: Date, required: true, default: Date.now },

0 commit comments

Comments
 (0)