From 9e7d997aabea9b234a884cf5bffb89ab41e83671 Mon Sep 17 00:00:00 2001 From: John Lomond Date: Wed, 4 Mar 2020 12:09:20 -0500 Subject: [PATCH 1/2] Refactor sorting --- underscore.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/underscore.js b/underscore.js index 014ebfa..7601ac4 100644 --- a/underscore.js +++ b/underscore.js @@ -14,6 +14,29 @@ _.min = function(obj, iteratee, context) { result = value; } } + _.sortBy = function(obj, iteratee, context) { + var index = 0; + iteratee = cb(iteratee, context); + return _.pluck( + _.map(obj, function(value, key, list) { + return { + value: value, + index: index++, + criteria: iteratee(value, key, list) + }; + }).sort(function(left, right) { + var a = left.criteria; + var b = right.criteria; + if (a !== b) { + if (a > b || a === void 0) return 1; + if (a < b || b === void 0) return -1; + } + return left.index - right.index; + }), + "value" + ); + }; + } else { iteratee = cb(iteratee, context); _.each(obj, function(v, index, list) { From c12f980f3a8a44e343e9a68e2f4968811a6f2bd3 Mon Sep 17 00:00:00 2001 From: David Hersh Date: Fri, 4 Dec 2020 11:27:38 -0500 Subject: [PATCH 2/2] Refactored stuff --- shapes.js | 6 ++++++ underscore.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/shapes.js b/shapes.js index 20df403..9288fc4 100644 --- a/shapes.js +++ b/shapes.js @@ -9,6 +9,12 @@ class Rectangle { } } +class CircleGetsTheSquare { + constructor(circumference, radius) { + this.circumference = circumference + this.radius = radius + } + class Square extends Rectangle { constructor(x) { super(x, x) diff --git a/underscore.js b/underscore.js index 7601ac4..1ac0249 100644 --- a/underscore.js +++ b/underscore.js @@ -50,6 +50,24 @@ _.min = function(obj, iteratee, context) { return result; }; +_.sampleThis = function(obj, n, guard) { + if (n == null || guard) { + if (!isArrayLike(obj)) obj = _.values(obj); + return obj[_.random(obj.length - 1)]; + } + var sample = isArrayLike(obj) ? _.clone(obj) : _.values(obj); + var length = getLength(sample); + n = Math.max(Math.min(n, length), 0); + var last = length - 1; + for (var index = 0; index < n; index++) { + var rand = _.random(index, last); + var temp = sample[index]; + sample[index] = sample[rand]; + sample[rand] = temp; + } + return sample.slice(0, n); +}; + _.shuffle = function(obj) { return _.sample(obj, Infinity); };