Skip to content

Commit

Permalink
Assume immutability of provided fixtures for faster uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejewski committed May 22, 2017
1 parent 0c5d0d4 commit ffd0b74
Showing 1 changed file with 39 additions and 44 deletions.
83 changes: 39 additions & 44 deletions helpers/legacyStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,25 @@ var isArrayLike = require("can-util/js/is-array-like/is-array-like");
var each = require("can-util/js/each/each");
var assign = require("can-util/js/assign/assign");

module.exports = function (count, make, filter) {
/*jshint eqeqeq:false */
var nextUniqueId = (function () {
var i = 0;
return function () {
return i++;
}
})();

// Check to see if an existing item has the ID we want to assign
var isIdUnique = function (items, id) {
for (var key in items) {
if (items[key].id === id) {
return false;
}
/*
Returns the initial ID all new item IDs will start from.
Assumption: the list items passed will not have their IDs modified.
*/
function getStartingId(items) {
var startingId = 0;
each(items, function (item) {
if (typeof item.id === 'number') {
startingId = Math.max(startingId, item.id + 1);
}
return true;
}
});
return startingId;
}

var getUniqueId = function (items) {
var id = nextUniqueId();
while (!isIdUnique(items, id)) {
id = nextUniqueId();
}
return id;
module.exports = function (count, make, filter) {
/*jshint eqeqeq:false */
var nextItemId;
var getNextItemId = function () {
return nextItemId++;
}

var items,
Expand All @@ -43,20 +37,20 @@ module.exports = function (count, make, filter) {
types,
reset;

if(isArrayLike(count) && typeof count[0] === "string" ){
if (isArrayLike(count) && typeof count[0] === "string") {
types = count;
count = make;
make= filter;
make = filter;
filter = arguments[3];
} else if(typeof count === "string") {
} else if (typeof count === "string") {
types = [count + "s", count];
count = make;
make= filter;
make = filter;
filter = arguments[3];
}


if(typeof count === "number") {
if (typeof count === "number") {
nextItemId = 0;
items = [];
reset = function () {
items = [];
Expand All @@ -65,15 +59,16 @@ module.exports = function (count, make, filter) {
var item = make(i, items);

if (!item.id) {
item.id = getUniqueId(items);
item.id = getNextItemId();
}
items.push(item);
}
};
} else {
filter = make;
var initialItems = count;
reset = function(){
nextItemId = getStartingId(initialItems);
reset = function () {
items = initialItems.slice(0);
};
}
Expand Down Expand Up @@ -144,7 +139,7 @@ module.exports = function (count, make, filter) {
}
}

if ( typeof filter === "function" ) {
if (typeof filter === "function") {
i = 0;
while (i < retArr.length) {
if (!filter(retArr[i], request)) {
Expand All @@ -153,11 +148,11 @@ module.exports = function (count, make, filter) {
i++;
}
}
} else if( typeof filter === "object" ) {
} else if (typeof filter === "object") {
i = 0;
while (i < retArr.length) {
var subset = canSet.subset(retArr[i], request.data, filter);
if ( !subset ) {
if (!subset) {
retArr.splice(i, 1);
} else {
i++;
Expand All @@ -170,8 +165,8 @@ module.exports = function (count, make, filter) {
"count": retArr.length,
"data": retArr.slice(offset, offset + limit)
};
each(["limit","offset"], function(prop){
if(prop in request.data) {
each(["limit", "offset"], function (prop) {
if (prop in request.data) {
responseData[prop] = request.data[prop];
}
});
Expand Down Expand Up @@ -202,7 +197,7 @@ module.exports = function (count, make, filter) {
getData: function (request, response) {
var item = findOne(getId(request));

if(typeof item === "undefined") {
if (typeof item === "undefined") {
return response(404, 'Requested resource not found');
}

Expand All @@ -214,7 +209,7 @@ module.exports = function (count, make, filter) {
var id = getId(request),
item = findOne(id);

if(typeof item === "undefined") {
if (typeof item === "undefined") {
return response(404, 'Requested resource not found');
}

Expand All @@ -223,8 +218,8 @@ module.exports = function (count, make, filter) {
response({
id: id
}, {
location: request.url || "/" + getId(request)
});
location: request.url || "/" + getId(request)
});
},

/**
Expand All @@ -249,7 +244,7 @@ module.exports = function (count, make, filter) {
var id = getId(request),
item = findOne(id);

if(typeof item === "undefined") {
if (typeof item === "undefined") {
return response(404, 'Requested resource not found');
}

Expand All @@ -274,16 +269,16 @@ module.exports = function (count, make, filter) {
// If an ID wasn't passed into the request, we give the item
// a unique ID.
if (!item.id) {
item.id = getUniqueId(items);
item.id = getNextItemId();
}

// Push the new item into the store.
items.push(item);
response({
id: item.id
}, {
location: settings.url + "/" + item.id
});
location: settings.url + "/" + item.id
});
}
});
reset();
Expand Down

0 comments on commit ffd0b74

Please sign in to comment.