Last active
May 19, 2020 22:32
-
-
Save homeslicesolutions-zz/89a7bfc555c32d8ff6d65b8baf95d57b to your computer and use it in GitHub Desktop.
Underscore/lodash => native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require('lodash'); | |
// COLLECTIONS | |
// _.each => Array.prototype.forEach | |
// _.map => Array.prototype.map | |
// _.reduce => Array.prototype.reduce | |
// _.reduceRight => Array.prototype.reduceRight | |
// _.find => Array.prototype.find | |
// _.filter => Array.prototype.filter | |
// ------------------------- | |
// _.where | |
var listOfPlays = [ | |
{title: "Cymbeline", author: "Shakespeare", year: 1611}, | |
{title: "The Tempest", author: "Shakespeare", year: 1611}, | |
{title: "Another book", author: "Shakespeare", year: 1613} | |
]; | |
// lodash | |
_.where(listOfPlays, {author: "Shakespeare", year: 1611}); | |
// native | |
listOfPlays.filter(play => play.author === 'Shakespeare' && play.year === 1611); | |
// => [{title: "Cymbeline", author: "Shakespeare", year: 1611}, {title: "The Tempest", author: "Shakespeare", year: 1611}] | |
// ------------------------- | |
// _.findWhere | |
var listOfBooks = [ | |
{title: "Cymbeline", author: "Shakespeare", year: 1611}, | |
{title: "I love donald trump", author: "Another idiot", year: 1611}, | |
{title: "I hate dogs", author: "Another idiot", year: 1611}, | |
{title: "Another book", author: "Some idiot", year: 2016} | |
]; | |
// lodash | |
_.findWhere(listOfBooks, {author: "Another idiot", year: 1611}); | |
// native | |
listOfBooks.find(book => book.author === "Another idiot" && book.year === 1611); | |
// => {title: "I love donald trump", author: "Another idiot", year: 1611} | |
// ------------------------- | |
// _.reject | |
// lodash | |
_.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); | |
// native | |
[1, 2, 3, 4, 5, 6].filter(num => num % 2 != 0 ); | |
// => [1, 3, 5] | |
// TODO: other methods... | |
// ========================== | |
// ARRAYS | |
// ------------------------- | |
// _.uniq | |
// -- Simple Unique (strings, numbers) | |
// lodash | |
_.uniq([1, 2, 1, 4, 1, 3]); | |
// native | |
[1, 2, 1, 4, 1, 3].reduce((arr, num) => !~arr.indexOf(num) ? arr.concat(num) : arr, []); | |
// => [1, 2, 4, 3] | |
// -- Simple Unique on Collections based on one property | |
var listOfBooks = [ | |
{title: "Cymbeline", author: "Shakespeare", year: 1611}, | |
{title: "I love donald trump", author: "Another idiot", year: 1611}, | |
{title: "I hate dogs", author: "Another idiot", year: 1611}, | |
{title: "Another book", author: "Some idiot", year: 2016} | |
]; | |
// lodash | |
_.uniq(listOfBooks, false, 'year'); | |
// native | |
listOfBooks.reduce((bucket, book) => ( | |
!bucket.find(bucketed => bucketed.year === book.year) ? bucket.concat(book) : bucket | |
), []); | |
// => [{author: "Shakespeare", title: "Cymbeline", year: 1611},{author: "Some idiot", title: "Another book", year: 2016}] | |
// ------------------------- | |
// _.object | |
var listOne = ['moe', 'larry', 'curly']; | |
var listTwo = [30, 40, 50]; | |
// lodash | |
_.object(listOne, listTwo); | |
// native | |
listOne.reduce((obj, itemKey, index) => { | |
obj[itemKey] = listTwo[index]; | |
return obj; | |
}, {}); | |
// => {moe: 30, larry: 40, curly: 50} | |
// ------------------------- | |
// _.zip | |
var listOne = ['moe', 'larry', 'curly']; | |
var listTwo = [30, 40, 50]; | |
var listThree = [true, false, false]; | |
// lodash | |
_.zip(listOne, listTwo, listThree); | |
// native | |
[listOne, listTwo, listThree].reduce((zipped, list, listIndex) => { | |
list.forEach((value, index) => { | |
zipped[index] = zipped[index] || []; | |
zipped[index][listIndex] = value; | |
}); | |
return zipped; | |
}, []); | |
// => [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]] | |
// ------------------------- | |
// _.unzip | |
var zipped = [["moe", 30, true], ["larry", 40, false], ["curly", 50, false], ["shermp", 60, true]]; | |
// lodash | |
_.unzip(zipped); | |
// native | |
zipped.reduce((arr, item, zipIndex) => { | |
item.forEach((attr, index) => { | |
arr[index] = arr[index] || []; | |
arr[index][zipIndex] = attr; | |
}); | |
return arr; | |
}, []); | |
// => [['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]] | |
// ------------------------- | |
// _.isEqual | |
// -- Deep Equal | |
var stooge = {name: 'moe', luckyNumbers: [13, 27, 34]}; | |
var clone = {name: 'moe', luckyNumbers: [13, 27, 34]}; | |
// lodash | |
_.isEqual(stooge, clone); | |
// native | |
function isEqual(obj1, obj2) { | |
var keys = Object.keys(obj1).concat(Object.keys(obj2)); | |
keys = keys.reduce((arr, key) => !~arr.indexOf(key) ? arr.concat(key) : arr, []); | |
try { | |
for (var i in keys) { | |
if (typeof obj1[keys[i]] == 'object') { | |
return isEqual(obj1[keys[i]], obj2[keys[i]]); | |
} else if (obj1[keys[i]] !== obj2[keys[i]]) { | |
return false; | |
} | |
} | |
} catch (e) { | |
return false; | |
} | |
return true; | |
} | |
isEqual(stooge, clone); | |
// => true | |
// ------------------------- | |
// _.isEmpty (object, collection, map, or set) | |
var emptyObject = {}; | |
var filledObject = { 'a': 1 }; | |
var emptyString = ""; | |
var filledString = "something"; | |
var emptyArray = []; | |
var filledArray = [1,2,3]; | |
// isEmpty | |
_.isEmpty(emptyObject); // ==> true | |
_.isEmpty(filledObject); // ==> false | |
_.isEmpty(emptyString); // ==> true | |
_.isEmpty(filledString); // ==> false | |
_.isEmpty(emptyArray); // ==> true | |
_.isEmpty(filledArray); // ==> false | |
// native | |
function isEmpty(obj) { | |
if (obj.hasOwnProperty('length')) return obj.length < 1; | |
return Object.keys(obj).length < 1; | |
} | |
isEmpty(emptyObject); // ==> true | |
isEmpty(filledObject); // ==> false | |
isEmpty(emptyString); // ==> true | |
isEmpty(filledString); // ==> false | |
isEmpty(emptyArray); // ==> true | |
isEmpty(filledArray); // ==> false | |
// ------------------------- | |
// Object.assign | |
// native | |
function objectAssign() { | |
var args = [].slice.call(arguments); | |
var object; | |
args.forEach(function(arg) { | |
if (arg) { | |
if (!object) { | |
object = arg; | |
} | |
Object.keys(arg).forEach(function(key) { | |
object[key] = arg[key]; | |
}); | |
} | |
}); | |
return object; | |
} | |
/** | |
* _.omit() | |
* Return a new object without specified attributes | |
* @param {Object<T>} obj | |
* @param {Array} [...args] | |
* @return {Object<T>} | |
*/ | |
export default function omit<T>(obj: { [key: string]: any }, ...args: string[]): T { | |
return Object.keys(obj).reduce((mem, key) => { | |
if (!args.includes(key)) { | |
return { | |
...mem, | |
[key]: obj[key], | |
}; | |
} | |
return mem; | |
}, {}) as T; | |
} | |
// TODO: other methods... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment