Last active
January 23, 2017 15:55
-
-
Save jhyland87/378ca238be640b90b5f2f8d68217cde3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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' ) | |
let aliases = [ | |
"alias", | |
[ | |
" aliases ", | |
[ | |
"idk", | |
"TEST ", | |
123 | |
], | |
{ | |
"a": 12 | |
} | |
], | |
false, | |
null, | |
null, | |
true, | |
[ | |
"IDK", | |
function(){}, | |
zomg => false | |
] | |
] | |
const _aliases = _.chain( aliases ) | |
.thru( _.castArray ) | |
.flattenDeep() | |
.map( val => _.isString( val ) ? val : null ) | |
.map( _.trim ) | |
.map( _.toLower ) | |
.compact() | |
.sortedUniq() | |
.value() | |
// Result: ["alias", "aliases", "idk", "test", "idk"] | |
// _.map() needs to be executed three times to execute 3 functions.. | |
const _aliases = _.chain( aliases ) | |
.thru( _.castArray ) | |
.flattenDeep() | |
.map( val => _.isString( val ) ? val : null ) | |
.map( _.trim ) | |
.map( _.toLower ) | |
.compact() | |
.sortedUniq() | |
.value() | |
// Or a single map, with a lot more functionality | |
const _aliases = _.chain( aliases ) | |
.thru( _.castArray ) | |
.flattenDeep() | |
.map( val => _.chain( val ) | |
.map(val => _.isString( val ) ? val : null ) | |
.trim() | |
.toLower() | |
.value() ) | |
.compact() | |
.sortedUniq() | |
.value() | |
// Something like a _.multiMap() would be much easier, like so: | |
const _aliases = _.chain( aliases ) | |
.thru( _.castArray ) | |
.flattenDeep() | |
.multiMap( | |
val => _.isString( val ) ? val : null, | |
_.trim, | |
_.toLower | |
) | |
.compact() | |
.sortedUniq() | |
.value() | |
// Or an array of functions to map | |
const _aliases = _.chain( aliases ) | |
.thru( _.castArray ) | |
.flattenDeep() | |
.multiMap([ | |
val => _.isString( val ) ? val : null, | |
_.trim, | |
_.toLower | |
]) | |
.compact() | |
.sortedUniq() | |
.value() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment