Skip to content

Commit

Permalink
feat(mvc.Collection): update collection methods (#2461)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorgewilliams authored Jan 3, 2024
1 parent b38fffb commit 8a3e121
Show file tree
Hide file tree
Showing 4 changed files with 1,481 additions and 703 deletions.
62 changes: 43 additions & 19 deletions packages/joint-core/src/mvc/Collection.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ assign(Collection.prototype, Events, {
// The JSON representation of a Collection is an array of the
// models' attributes.
toJSON: function(options) {
return Array.from(this).map(function(model) { return model.toJSON(options); });
return this.map(function(model) { return model.toJSON(options); });
},

// Add a model, or list of models to the set. `models` may be
Expand Down Expand Up @@ -290,11 +290,6 @@ assign(Collection.prototype, Events, {
return this;
},

// Pluck an attribute from each model in the collection.
pluck: function(attr) {
return Array.from(this).map((model) => model.get(attr + ''));
},

// Create a new collection with an identical list of models as this one.
clone: function() {
return new this.constructor(this.models, {
Expand Down Expand Up @@ -323,6 +318,46 @@ assign(Collection.prototype, Events, {
return new CollectionIterator(this, ITERATOR_KEYSVALUES);
},

// Iterate over elements of the collection, and invoke fn for each element
each: function(fn, context) {
this.models.forEach(fn, context);
},

// Iterate over elements of collection, and return an array of all elements fn returns truthy for
filter: function(fn, context) {
return this.models.filter(fn, context);
},

// Return the first model of the collection
first: function() {
return this.models[0];
},

// Return true if value is in the collection
includes: function(value) {
return this.models.includes(value);
},

// Return the last model of the collection
last: function() {
return this.models[this.models.length - 1];
},

// Return true if collection has no elements
isEmpty: function() {
return !this.models.length;
},

// Create an array of values by running each element in the collection through fn
map: function(fn, context) {
return this.models.map(fn, context);
},

// Runs "reducer" fn over all elements in the collection, in ascending-index order, and accumulates them into a single value
reduce: function(fn, initAcc, context) {
return this.models.reduce(fn, initAcc, context);
},

// Private method to reset all internal state. Called when the collection
// is first initialized or reset.
_reset: function() {
Expand Down Expand Up @@ -361,7 +396,7 @@ assign(Collection.prototype, Events, {
var model = this.get(models[i]);
if (!model) continue;

var index = Array.from(this).indexOf(model);
var index = this.models.indexOf(model);
this.models.splice(index, 1);
this.length--;

Expand Down Expand Up @@ -491,7 +526,7 @@ CollectionIterator.prototype.next = function() {
};

// Methods that we want to implement on the Collection.
var collectionMethods = { toArray: 1, first: 3, last: 3, sortBy: 3 };
var collectionMethods = { toArray: 1, sortBy: 3 };


// Mix in each method as a proxy to `Collection#models`.
Expand All @@ -503,19 +538,8 @@ function addMethods(config) {
methods = config[1],
attribute = config[2];

function first(array) {
return (array && array.length) ? array[0] : undefined;
}

function last(array) {
var length = array == null ? 0 : array.length;
return length ? array[length - 1] : undefined;
}

const methodsToAdd = {
sortBy,
first,
last,
toArray
};

Expand Down
1 change: 0 additions & 1 deletion packages/joint-core/src/mvc/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export * from './Events.mjs';
export * from './Collection.mjs';
export * from './Model.mjs';
export * from './ViewBase.mjs';
export * from './mvcUtils.mjs';
export { default as Data } from './Data.mjs';
export { default as $ } from './Dom/index.mjs';
export * from './Dom/Event.mjs';
Loading

0 comments on commit 8a3e121

Please sign in to comment.