Skip to content

Commit

Permalink
improved the arrays example
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpopdev committed Aug 31, 2015
1 parent d29ad80 commit 3257dae
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 261 deletions.
38 changes: 29 additions & 9 deletions arrays-objects-functions/arrays/clientRetriever.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,57 @@ var clientRetriever = (function() {
var clients = dataProvider.getClients();
return {
getNewestClients: function(count) {
return _.last(_.sortBy(clients, 'registered'), count);
return _.last(_.sortBy(clients, function(client) {
return new Date(client.registered);
}), count);
},
getOldestClients: function(count) {
return _.first(_.sortBy(clients, 'registered'), count);
return _.first(_.sortBy(clients, function(client) {
return new Date(client.registered);
}), count);
},
getOlderClients: function(skipLastCount) {
return _.initial(_.sortBy(clients, 'registered'), skipLastCount);
return _.initial(_.sortBy(clients, function(client) {
return new Date(client.registered);
}), skipLastCount);
},
getNewerClients: function(skipFirstCount) {
return _.rest(_.sortBy(clients, 'registered'), skipFirstCount);
return _.rest(_.sortBy(clients, function(client) {
return new Date(client.registered);
}), skipFirstCount);
},
getOldestOrBestClients: function(count) {
var oldestClients = _.first(_.sortBy(clients, 'registered'), count);
var oldestClients = _.first(_.sortBy(clients, function(client) {
return new Date(client.registered);
}),
count);
var bestClients = _.last(_.sortBy(clients, 'bikePoints'), count);
var oldestOrBestClients = _.union(oldestClients, bestClients);
return oldestOrBestClients;
},
getOldestOrBestClientsWithuniq: function(count) {
var oldestClients = _.first(_.sortBy(clients, 'registered'), count);
var oldestClients = _.first(_.sortBy(clients, function(client) {
return new Date(client.registered);
}),
count);
var bestClients = _.last(_.sortBy(clients, 'bikePoints'), count);
var oldestOrBestClientsWithDuplicates = oldestClients.concat(bestClients);
return _.uniq(oldestOrBestClientsWithDuplicates);
},
getOldestAndBestClients: function(count) {
var oldestClients = _.first(_.sortBy(clients, 'registered'), count);
var oldestClients = _.first(_.sortBy(clients, function(client) {
return new Date(client.registered);
}),
count);
var bestClients = _.last(_.sortBy(clients, 'bikePoints'), count);
var oldestAndBestClients = _.intersection(oldestClients, bestClients);
return oldestAndBestClients;
},
getOldestOrBestClientsThatAreNotBoth: function(count) {
var oldestClients = _.first(_.sortBy(clients, 'registered'), count);
var oldestClients = _.first(_.sortBy(clients, function(client) {
return new Date(client.registered);
}),
count);
var bestClients = _.last(_.sortBy(clients, 'bikePoints'), count);
var oldestOrBestClients = _.union(oldestClients, bestClients);
var oldestAndBestClients = _.intersection(oldestClients, bestClients);
Expand All @@ -53,4 +73,4 @@ var clientRetriever = (function() {
return clientAndOrders;
}
};
}());
}());
2 changes: 1 addition & 1 deletion arrays-objects-functions/arrays/clients.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
email: '{{email()}}',
phone: '+1 {{phone()}}',
address: '{{integer(100, 999)}} {{street()}}, {{city()}}, {{state()}}, {{integer(100, 10000)}}',
registered: '{{date(new Date(2014, 0, 1), new Date(), "YYYY-MM-ddThh:mm:ss Z")}}',
registered: '{{date(new Date(2014, 0, 1), new Date(), "YYYY-MM-ddThh:mm:ssZ")}}',
preferredBike: '{{random("A fast bike", "An even faster bike", "A springy bike", "A springier bike", "An all-terain bike", "A classy bike", "A modern bike", "A commuter bike", "A blue bike", "A pink bike", "A noisy bike", "A clown bike")}}',
bikePoints: '{{integer(1000, 7000)}}',
isActive: '{{bool()}}',
Expand Down
Loading

0 comments on commit 3257dae

Please sign in to comment.