Skip to content

Commit cdb1dd6

Browse files
authored
Formatted files according to new prettier-config (#67)
1 parent 86b88d1 commit cdb1dd6

File tree

2 files changed

+157
-154
lines changed

2 files changed

+157
-154
lines changed

src/index.js

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import jsonpatch from 'fast-json-patch'
55
import { decamelize, pascalize } from 'humps'
66
import { dropRightWhile, each, map, merge, omit, get, tail } from 'lodash'
77

8-
export const RollbackError = function(message, extra) {
8+
export const RollbackError = function (message, extra) {
99
Error.captureStackTrace(this, this.constructor)
1010
this.name = 'RollbackError'
1111
this.message = message
1212
}
1313

1414
require('util').inherits(RollbackError, Error)
1515

16-
const createPatchModel = options => {
16+
const createPatchModel = (options) => {
1717
const def = {
1818
date: { type: Date, required: true, default: Date.now },
1919
ops: { type: [], required: true },
@@ -49,7 +49,7 @@ const ARRAY_INDEX_WILDCARD = '*'
4949
*
5050
* @param {string} path Path to split
5151
*/
52-
const getArrayFromPath = path => path.replace(/^\//, '').split('/')
52+
const getArrayFromPath = (path) => path.replace(/^\//, '').split('/')
5353

5454
/**
5555
* Checks the provided `json-patch-operation` on `excludePath`. This check joins the `path` and `value` property of the `operation` and removes any hit.
@@ -74,7 +74,7 @@ const deepRemovePath = (patch, excludePath) => {
7474
// start over with each array element and make a fresh check
7575
// Note: it can happen that array elements are rendered to: {}
7676
// we need to keep them to keep the order of array elements consistent
77-
value.forEach(elem => {
77+
value.forEach((elem) => {
7878
deepRemovePath({ path: '/', value: elem }, excludePath.slice(i + 1))
7979
})
8080

@@ -101,7 +101,7 @@ const deepRemovePath = (patch, excludePath) => {
101101
* Sanitizes a path `['']` to be used with `isPathContained()`
102102
* @param {String[]} path
103103
*/
104-
const sanitizeEmptyPath = path =>
104+
const sanitizeEmptyPath = (path) =>
105105
path.length === 1 && path[0] === '' ? [] : path
106106

107107
// Checks if 'fractionPath' is contained in fullPath
@@ -119,15 +119,15 @@ const entryIsIdentical = (entry1, entry2) => entry1 === entry2
119119
const matchesArrayWildcard = (entry1, entry2) =>
120120
isArrayIndexWildcard(entry1) && isIntegerGreaterEqual0(entry2)
121121

122-
const isArrayIndexWildcard = entry => entry === ARRAY_INDEX_WILDCARD
122+
const isArrayIndexWildcard = (entry) => entry === ARRAY_INDEX_WILDCARD
123123

124-
const isIntegerGreaterEqual0 = entry =>
124+
const isIntegerGreaterEqual0 = (entry) =>
125125
Number.isInteger(Number(entry)) && Number(entry) >= 0
126126

127127
// used to convert bson to json - especially ObjectID references need
128128
// to be converted to hex strings so that the jsonpatch `compare` method
129129
// works correctly
130-
const toJSON = obj => JSON.parse(JSON.stringify(obj))
130+
const toJSON = (obj) => JSON.parse(JSON.stringify(obj))
131131

132132
// helper function to merge query conditions after an update has happened
133133
// usefull if a property which was initially defined in _conditions got overwritten
@@ -137,13 +137,13 @@ const mergeQueryConditionsWithUpdate = (_conditions, _update) => {
137137
const conditions = Object.assign({}, conditions, update)
138138

139139
// excluding updates other than $set
140-
Object.keys(conditions).forEach(key => {
140+
Object.keys(conditions).forEach((key) => {
141141
if (key.includes('$')) delete conditions[key]
142142
})
143143
return conditions
144144
}
145145

146-
export default function(schema, opts) {
146+
export default function (schema, opts) {
147147
const options = merge({}, defaultOptions, opts)
148148

149149
// get _id type from schema
@@ -160,7 +160,7 @@ export default function(schema, opts) {
160160

161161
// used to compare instance data snapshots. depopulates instance,
162162
// removes version key and object id
163-
schema.methods.data = function() {
163+
schema.methods.data = function () {
164164
return this.toObject({
165165
depopulate: true,
166166
versionKey: false,
@@ -182,15 +182,18 @@ export default function(schema, opts) {
182182
.sort({ date: 1 })
183183
.exec()
184184
.then(
185-
patches =>
185+
(patches) =>
186186
new Promise((resolve, reject) => {
187187
// patch doesn't exist
188188
if (!~map(patches, 'id').indexOf(patchId)) {
189189
return reject(new RollbackError("patch doesn't exist"))
190190
}
191191

192192
// get all patches that should be applied
193-
const apply = dropRightWhile(patches, patch => patch.id !== patchId)
193+
const apply = dropRightWhile(
194+
patches,
195+
(patch) => patch.id !== patchId
196+
)
194197

195198
// if the patches that are going to be applied are all existing patches,
196199
// the rollback attempts to rollback to the latest patch
@@ -200,7 +203,7 @@ export default function(schema, opts) {
200203

201204
// apply patches to `state`
202205
const state = {}
203-
apply.forEach(patch => {
206+
apply.forEach((patch) => {
204207
jsonpatch.applyPatch(state, patch.ops, true)
205208
})
206209

@@ -223,7 +226,7 @@ export default function(schema, opts) {
223226

224227
// after a document is initialized or saved, fresh snapshots of the
225228
// documents data are created
226-
const snapshot = function() {
229+
const snapshot = function () {
227230
this._original = toJSON(this.data())
228231
}
229232
schema.post('init', snapshot)
@@ -235,10 +238,10 @@ export default function(schema, opts) {
235238
const { _id: ref } = document
236239
return document.patches
237240
.find({ ref: document._id })
238-
.then(patches => join(patches.map(patch => patch.remove())))
241+
.then((patches) => join(patches.map((patch) => patch.remove())))
239242
}
240243

241-
schema.pre('remove', function(next) {
244+
schema.pre('remove', function (next) {
242245
if (!options.removePatches) {
243246
return next()
244247
}
@@ -259,12 +262,12 @@ export default function(schema, opts) {
259262
toJSON(document.data())
260263
)
261264
if (options.excludes.length > 0) {
262-
ops = ops.filter(op => {
265+
ops = ops.filter((op) => {
263266
const pathArray = getArrayFromPath(op.path)
264267
return (
265-
!options.excludes.some(exclude =>
268+
!options.excludes.some((exclude) =>
266269
isPathContained(exclude, pathArray)
267-
) && options.excludes.every(exclude => deepRemovePath(op, exclude))
270+
) && options.excludes.every((exclude) => deepRemovePath(op, exclude))
268271
)
269272
})
270273
}
@@ -276,7 +279,7 @@ export default function(schema, opts) {
276279

277280
// track original values if enabled
278281
if (options.trackOriginalValue) {
279-
ops.map(entry => {
282+
ops.map((entry) => {
280283
const path = tail(entry.path.split('/')).join('.')
281284
entry.originalValue = get(
282285
document.isNew ? {} : document._original,
@@ -295,20 +298,20 @@ export default function(schema, opts) {
295298
return document.patches.create(data)
296299
}
297300

298-
schema.pre('save', function(next) {
301+
schema.pre('save', function (next) {
299302
createPatch(this)
300303
.then(() => next())
301304
.catch(next)
302305
})
303306

304-
schema.pre('findOneAndRemove', function(next) {
307+
schema.pre('findOneAndRemove', function (next) {
305308
if (!options.removePatches) {
306309
return next()
307310
}
308311

309312
this.model
310313
.findOne(this._conditions)
311-
.then(original => deletePatches(original))
314+
.then((original) => deletePatches(original))
312315
.then(() => next())
313316
.catch(next)
314317
})
@@ -318,7 +321,7 @@ export default function(schema, opts) {
318321
function preUpdateOne(next) {
319322
this.model
320323
.findOne(this._conditions)
321-
.then(original => {
324+
.then((original) => {
322325
if (original) this._originalId = original._id
323326
original = original || new this.model({})
324327
this._original = toJSON(original.data())
@@ -327,7 +330,7 @@ export default function(schema, opts) {
327330
.catch(next)
328331
}
329332

330-
schema.post('findOneAndUpdate', function(doc, next) {
333+
schema.post('findOneAndUpdate', function (doc, next) {
331334
if (!this.options.new) {
332335
return postUpdateOne.call(this, {}, next)
333336
}
@@ -351,7 +354,7 @@ export default function(schema, opts) {
351354

352355
this.model
353356
.findOne(conditions)
354-
.then(doc => {
357+
.then((doc) => {
355358
if (!doc) return next()
356359
doc._original = this._original
357360
return createPatch(doc, this.options)
@@ -366,7 +369,7 @@ export default function(schema, opts) {
366369
function preUpdateMany(next) {
367370
this.model
368371
.find(this._conditions)
369-
.then(originals => {
372+
.then((originals) => {
370373
const originalIds = []
371374
const originalData = []
372375
for (const original of originals) {
@@ -393,7 +396,7 @@ export default function(schema, opts) {
393396

394397
this.model
395398
.find(conditions)
396-
.then(docs =>
399+
.then((docs) =>
397400
Promise.all(
398401
docs.map((doc, i) => {
399402
doc._original = this._originals[i]
@@ -408,14 +411,14 @@ export default function(schema, opts) {
408411
schema.pre('updateMany', preUpdateMany)
409412
schema.post('updateMany', postUpdateMany)
410413

411-
schema.pre('update', function(next) {
414+
schema.pre('update', function (next) {
412415
if (this.options.multi) {
413416
preUpdateMany.call(this, next)
414417
} else {
415418
preUpdateOne.call(this, next)
416419
}
417420
})
418-
schema.post('update', function(result, next) {
421+
schema.post('update', function (result, next) {
419422
if (this.options.many) {
420423
postUpdateMany.call(this, result, next)
421424
} else {

0 commit comments

Comments
 (0)