Remove lodash isNaN, isNumber, isString, bind, uniqBy

(re: 6087)
This commit is contained in:
Bryan Housel
2019-03-27 23:11:08 -04:00
parent 6fb8fcb86b
commit 3896b2282f
12 changed files with 130 additions and 74 deletions

View File

@@ -45,6 +45,7 @@ export function utilArrayUniq(a) {
return Array.from(new Set(a));
}
// Splits array into chunks of given chunk size
// var a = [1,2,3,4,5,6,7];
// utilArrayChunk(a, 3);
@@ -89,3 +90,39 @@ export function utilArrayGroupBy(a, key) {
}, {});
}
// Returns an Array with all the duplicates removed
// where uniqueness determined by the given key
// `key` can be passed as a property or as a key function
//
// var pets = [
// { type: 'Dog', name: 'Spot' },
// { type: 'Cat', name: 'Tiger' },
// { type: 'Dog', name: 'Rover' },
// { type: 'Cat', name: 'Leo' }
// ];
//
// utilArrayUniqBy(pets, 'type')
// [
// { type: 'Dog', name: 'Spot' },
// { type: 'Cat', name: 'Tiger' }
// ]
//
// utilArrayUniqBy(pets, function(item) { return item.name.length; })
// [
// { type: 'Dog', name: 'Spot' },
// { type: 'Cat', name: 'Tiger' },
// { type: 'Cat', name: 'Leo' }
// }
export function utilArrayUniqBy(a, key) {
var seen = new Set();
return a.reduce(function(acc, item) {
var val = (typeof key === 'function') ? key(item) : item[key];
if (val && !seen.has(val)) {
seen.add(val);
acc.push(item);
}
return acc;
}, []);
}