Skip to content

Instantly share code, notes, and snippets.

@mbbx6spp
Last active August 19, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbbx6spp/eb30aaab85766efdc1eecaac1defd172 to your computer and use it in GitHub Desktop.
Save mbbx6spp/eb30aaab85766efdc1eecaac1defd172 to your computer and use it in GitHub Desktop.
const fruits = [ 'apples', 'bananas', 'cantelopes', 'durians', false];
const chunked = _.chunk(fruits, 2); // chunks elements of an arrary
const compacted = _.compact(fruits); // removes falsey values
const filled = _.fill(fruits, 'bananas'); // fill with my favorite fuit
const flattened = _.flatten(chunked); // flatten nested arrays
const takeThree = _.take(fruits, 3); // take first three
const takeUptoD = _.takeWhile(fruits, elem => elem && elem.startsWith('a') && elem.startsWith('b') && elem.startsWith('c'));
const object0 = { 'foo': [{ 'bar': { 'baz': 3 } }] };
_.get(object0, 'foo[0].bar.baz'); // returns 3
const object1 = { 'foo': [{ 'bar': { 'qux': 15, 'quux': true } }] };
const object2 = _.merge(object0, object1); // merges object keys together
const bar = _.get(object2, 'foo[0].bar');
_.pick(bar, ['qux', 'baz']); // plucks just qux and baz attributes out of bar object.
const double = (n) => n * 2;
const plusone = (n) => n + 1;
// plain vanilla function calling
const doubleplusone = x => plusone(double(x));
// using pipeline operator in ES2019
const doubleplusone0 = x => x |> double |> plusone;
// using lodash's flow, available with just lodash
const doubleplusone1 = x => _.flow([double, plusone]);
// Using in Promises
const doubleplusone2 = x => Promise.resolve(x).then(double).then(plusone);
console.log(`function calls: ${doubleplusone(3)}`);
console.log(`pipeline operator: ${doubleplusone0(3)}`);
doubleplusone2(3).then(result => {
console.log(`In Promise: ${result}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment