From 6f5beacfc4a971df9673fb24fff4f8c719448df4 Mon Sep 17 00:00:00 2001 From: Simon Legner Date: Sat, 6 Jan 2024 15:48:09 +0100 Subject: [PATCH] Sanity-check decaffeinate app.Collection --- assets/javascripts/collections/collection.js | 29 ++++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/assets/javascripts/collections/collection.js b/assets/javascripts/collections/collection.js index fedaf358..79bca0be 100644 --- a/assets/javascripts/collections/collection.js +++ b/assets/javascripts/collections/collection.js @@ -1,12 +1,3 @@ -// TODO: This file was created by bulk-decaffeinate. -// Sanity-check the conversion and remove this comment. -/* - * decaffeinate suggestions: - * DS101: Remove unnecessary use of Array.from - * DS102: Remove unnecessary code created because of implicit returns - * DS207: Consider shorter variations of null checks - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md - */ app.Collection = class Collection { constructor(objects) { if (objects == null) { @@ -24,7 +15,7 @@ app.Collection = class Collection { objects = []; } this.models = []; - for (var object of Array.from(objects)) { + for (var object of objects) { this.add(object); } } @@ -33,11 +24,11 @@ app.Collection = class Collection { if (object instanceof app.Model) { this.models.push(object); } else if (object instanceof Array) { - for (var obj of Array.from(object)) { + for (var obj of object) { this.add(obj); } } else if (object instanceof app.Collection) { - this.models.push(...Array.from(object.all() || [])); + this.models.push(...(object.all() || [])); } else { this.models.push(new (this.model())(object)); } @@ -56,7 +47,7 @@ app.Collection = class Collection { } each(fn) { - for (var model of Array.from(this.models)) { + for (var model of this.models) { fn(model); } } @@ -66,24 +57,20 @@ app.Collection = class Collection { } contains(model) { - return this.models.indexOf(model) >= 0; + return this.models.includes(model); } findBy(attr, value) { - for (var model of Array.from(this.models)) { - if (model[attr] === value) { - return model; - } - } + return this.models.find((model) => model[attr] === value); } findAllBy(attr, value) { - return Array.from(this.models).filter((model) => model[attr] === value); + return this.models.filter((model) => model[attr] === value); } countAllBy(attr, value) { let i = 0; - for (var model of Array.from(this.models)) { + for (var model of this.models) { if (model[attr] === value) { i += 1; }