From 3232ec96bf7f1c8d284555c288078eea2988de68 Mon Sep 17 00:00:00 2001 From: Oliver Rumbelow Date: Wed, 8 Jun 2016 07:17:38 +0100 Subject: [PATCH] Clone all objects passing in-to and out-of the in memory store --- lib/MemoryHandler.js | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/lib/MemoryHandler.js b/lib/MemoryHandler.js index 16595338..da8bd0de 100644 --- a/lib/MemoryHandler.js +++ b/lib/MemoryHandler.js @@ -36,7 +36,7 @@ MemoryStore.prototype.search = function(request, callback) { if (request.params.page) { results = results.slice(request.params.page.offset, request.params.page.offset + request.params.page.limit); } - return callback(null, this._clone(results), resultCount); + return callback(null, MemoryStore._clone(results), resultCount); }; /** @@ -59,7 +59,7 @@ MemoryStore.prototype.find = function(request, callback) { } // Return the requested resource - return callback(null, this._clone(theResource)); + return callback(null, MemoryStore._clone(theResource)); }; /** @@ -67,10 +67,8 @@ MemoryStore.prototype.find = function(request, callback) { */ MemoryStore.prototype.create = function(request, newResource, callback) { // Check to see if the ID already exists - var matches = resources[request.params.type].filter(function(resource) { - return resource.id === newResource.id; - }).length; - if (matches > 0) { + var index = MemoryStore._indexOf(resources[request.params.type], newResource); + if (index !== -1) { return callback({ status: "403", code: "EFORBIDDEN", @@ -81,7 +79,7 @@ MemoryStore.prototype.create = function(request, newResource, callback) { // Push the newResource into our in-memory store. resources[request.params.type].push(newResource); // Return the newly created resource - return callback(null, this._clone(newResource)); + return callback(null, MemoryStore._clone(newResource)); }; /** @@ -93,13 +91,8 @@ MemoryStore.prototype.delete = function(request, callback) { if (err) return callback(err); // Remove the resource from the in-memory store. - resources[request.params.type].some(function(resource, index) { - var match = resource.id === theResource.id; - if (match) { - resources[request.params.type].splice(index, 1); - } - return match; - }); + var index = MemoryStore._indexOf(resources[request.params.type], theResource); + resources[request.params.type].splice(index, 1); // Return with no error return callback(); @@ -120,16 +113,11 @@ MemoryStore.prototype.update = function(request, partialResource, callback) { theResource = _.assign(theResource, partialResource); // Push the newly updated resource back into the in-memory store - resources[request.params.type].some(function(resource, index) { - var match = resource.id === theResource.id; - if (match) { - resources[request.params.type][index] = theResource; - } - return match; - }); + var index = MemoryStore._indexOf(resources[request.params.type], theResource); + resources[request.params.type][index] = theResource; // Return the newly updated resource - return callback(null, self._clone(theResource)); + return callback(null, MemoryStore._clone(theResource)); }); }; @@ -158,6 +146,13 @@ MemoryStore.prototype._sortList = function(request, list) { }); }; -MemoryStore.prototype._clone = function(obj) { +MemoryStore._clone = function(obj) { return JSON.parse(JSON.stringify(obj)); }; + +MemoryStore._indexOf = function(list, obj) { + for (var i in list) { + if (list[i].id === obj.id) return i; + } + return -1; +};