Skip to content

Instantly share code, notes, and snippets.

@mLuby
Last active February 7, 2023 18:44
Show Gist options
  • Save mLuby/9e999bfbb0fd8e489a26a3b3f91c0eb7 to your computer and use it in GitHub Desktop.
Save mLuby/9e999bfbb0fd8e489a26a3b3f91c0eb7 to your computer and use it in GitHub Desktop.
To underscore the point that you don't need Lodash…
////////
// _.get
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.get(object, 'a[0].b.c'); // => 3
object?.a?.[0]?.b?.c; // => 3
_.get(object, 'a.b.c', 'default'); // => 'default'
object?.a?.b?.c ?? 'default'; // => 'default'
/////////
// _.drop
_.drop([1, 2, 3]); // => [2, 3]
[1, 2, 3].slice(1); // => [2, 3]
_.drop([1, 2, 3], 2); // => [3]
[1, 2, 3].slice(2); // => [3]
_.drop([1, 2, 3], 5); // => []
[1, 2, 3].slice(5); // => []
_.drop([1, 2, 3], 0); // => [1, 2, 3]
[1, 2, 3].slice(0); // => [1, 2, 3]
/////////
// _.pick
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pick(object, ['a', 'c']); // => { 'a': 1, 'c': 3 }
({a: object.a, c: object.c}); // => { 'a': 1, 'c': 3 }
(({a, c}) => ({a, c}))(object); // => { 'a': 1, 'c': 3 }
//////////
// _.curry
var abc = function(a, b, c) { return [a, b, c]; };
var _curried = _.curry(abc);
var curried = a => b => c => abc(a, b, c)
_curried(1)(2)(3); // => [1, 2, 3]
curried(1)(2)(3); // => [1, 2, 3]
// These are harder to do; good luck remembering what the positional arguments refer to though.
_curried(1, 2)(3); // => [1, 2, 3]
_curried(1, 2, 3); // => [1, 2, 3]
_curried(1)(_, 3)(2); // => [1, 2, 3]
////////////
// _.partial
function greet(greeting, name) { return greeting + ' ' + name; }
var sayHelloTo = _.partial(greet, 'hello'); sayHelloTo('fred'); // => 'hello fred'
var sayHelloTo = name => greet('hello', name); sayHelloTo('fred'); // => 'hello fred'
var greetFred = _.partial(greet, _, 'fred'); greetFred('hi'); // => 'hi fred'
var greetFred = greeting => greet(greeting, 'fred'); greetFred('hi'); // => 'hi fred'
@mLuby
Copy link
Author

mLuby commented Feb 7, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment