Skip to content

Commit 3232ec9

Browse files
author
Oliver Rumbelow
committed
Clone all objects passing in-to and out-of the in memory store
1 parent efa3d62 commit 3232ec9

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

lib/MemoryHandler.js

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ MemoryStore.prototype.search = function(request, callback) {
3636
if (request.params.page) {
3737
results = results.slice(request.params.page.offset, request.params.page.offset + request.params.page.limit);
3838
}
39-
return callback(null, this._clone(results), resultCount);
39+
return callback(null, MemoryStore._clone(results), resultCount);
4040
};
4141

4242
/**
@@ -59,18 +59,16 @@ MemoryStore.prototype.find = function(request, callback) {
5959
}
6060

6161
// Return the requested resource
62-
return callback(null, this._clone(theResource));
62+
return callback(null, MemoryStore._clone(theResource));
6363
};
6464

6565
/**
6666
Create (store) a new resource given a resource type and an object.
6767
*/
6868
MemoryStore.prototype.create = function(request, newResource, callback) {
6969
// Check to see if the ID already exists
70-
var matches = resources[request.params.type].filter(function(resource) {
71-
return resource.id === newResource.id;
72-
}).length;
73-
if (matches > 0) {
70+
var index = MemoryStore._indexOf(resources[request.params.type], newResource);
71+
if (index !== -1) {
7472
return callback({
7573
status: "403",
7674
code: "EFORBIDDEN",
@@ -81,7 +79,7 @@ MemoryStore.prototype.create = function(request, newResource, callback) {
8179
// Push the newResource into our in-memory store.
8280
resources[request.params.type].push(newResource);
8381
// Return the newly created resource
84-
return callback(null, this._clone(newResource));
82+
return callback(null, MemoryStore._clone(newResource));
8583
};
8684

8785
/**
@@ -93,13 +91,8 @@ MemoryStore.prototype.delete = function(request, callback) {
9391
if (err) return callback(err);
9492

9593
// Remove the resource from the in-memory store.
96-
resources[request.params.type].some(function(resource, index) {
97-
var match = resource.id === theResource.id;
98-
if (match) {
99-
resources[request.params.type].splice(index, 1);
100-
}
101-
return match;
102-
});
94+
var index = MemoryStore._indexOf(resources[request.params.type], theResource);
95+
resources[request.params.type].splice(index, 1);
10396

10497
// Return with no error
10598
return callback();
@@ -120,16 +113,11 @@ MemoryStore.prototype.update = function(request, partialResource, callback) {
120113
theResource = _.assign(theResource, partialResource);
121114

122115
// Push the newly updated resource back into the in-memory store
123-
resources[request.params.type].some(function(resource, index) {
124-
var match = resource.id === theResource.id;
125-
if (match) {
126-
resources[request.params.type][index] = theResource;
127-
}
128-
return match;
129-
});
116+
var index = MemoryStore._indexOf(resources[request.params.type], theResource);
117+
resources[request.params.type][index] = theResource;
130118

131119
// Return the newly updated resource
132-
return callback(null, self._clone(theResource));
120+
return callback(null, MemoryStore._clone(theResource));
133121
});
134122
};
135123

@@ -158,6 +146,13 @@ MemoryStore.prototype._sortList = function(request, list) {
158146
});
159147
};
160148

161-
MemoryStore.prototype._clone = function(obj) {
149+
MemoryStore._clone = function(obj) {
162150
return JSON.parse(JSON.stringify(obj));
163151
};
152+
153+
MemoryStore._indexOf = function(list, obj) {
154+
for (var i in list) {
155+
if (list[i].id === obj.id) return i;
156+
}
157+
return -1;
158+
};

0 commit comments

Comments
 (0)