How to convert some common lodash methods to ES6, will update as I find other lodash stuff on the code I'm working on. Feel free to correct or shorten my golf game.
noop is a simple function that always returns undefined
// lodash & lodash/fp
_.noop; // => 'undefined'
// ES6
() => undefined;
// Codegolf
x;f=>x
constant & always return what you specify
// lodash
_.constant(x); // returns x
// lodash/fp
_.always(x); // returns x
// ES6
() => x;
// Codegolf
f=>!!1 // true
f=>!!0 // false
f=>x // whatever you want
return the given value
// lodash & lodash/fp
_.identity(x); // returns x
// ES6
(x) => x;
// Codegolf
x=>x
check if the value is undefined
// lodash & lodash/fp
_.isUndefined(x); // true or false
// ES6
typeof x === 'undefined';
// or
x === void 0;
// Codegolf
f;x==f
check if value is an array
// lodash & lodash/fp
_.isArray(x); // true or false
// ES6
Array.isArray(x);
// Codegolf
// .push is unique to arrays so if you don't have objects with a 'push' key, this is safe
x.push&&!!1 // true or undefined
x.push?!!1:!!0 // true or false
check if value is a string
// lodash & lodash/fp
_.isString(x); // true or false
// ES6
typeof x === 'string';
// for checking 'new String()' add the following
x instanceof String;
// Codegolf
x===x+''
check if value is Object (arrays, functions, objects, regexes, stuff with new
)
// lodash & lodash/fp
_.isObject(x); // true or false
// ES6
x === Object(x);
// Codegolf
x===Object(x) // no idea how to optimize this
check if value is empty (arrays, objects, strings)
// lodash & lodash/fp
_.isEmpty(x); // true or false
// ES6
x.length === 0;
// Codegolf
!x.length
convert string to lowercase
// lodash & lodash/fp
_.toLower(x);
// ES6
x.toLowerCase();
// Codegolf
// if you are not allowed to use .toLowerCase()
x.replace(/[A-Z]/g,$=>String.fromCharCode($.charCodeAt()+32)))
check if value is a function
// lodash & lodash/fp
_.isFunction(x); // true or false
// ES6
typeof x === 'function';
// Codegolf
// .call is a unique property of functions
!!x.call