Skip to content

Instantly share code, notes, and snippets.

@jrencz
Created June 1, 2016 14:04
Show Gist options
  • Save jrencz/0390f4c0ba51a271e5db894bc7dc0234 to your computer and use it in GitHub Desktop.
Save jrencz/0390f4c0ba51a271e5db894bc7dc0234 to your computer and use it in GitHub Desktop.
Lodash: merge property paths given as strings or arrays into one
/**
* @param {...(string|Array)} paths
*
* @example
* > mergePaths('a.b.c', 'd');
* // ['a', 'b', 'c', 'd']
*
* > mergePaths('a.b.c', ['d']);
* // ['a', 'b', 'c', 'd']
*
* > mergePaths(['a', 'b', 'c'], ['d']);
* // ['a', 'b', 'c', 'd']
*
* > mergePaths(['a', 'b', 'c'], 'd');
* // ['a', 'b', 'c', 'd']
*
* @returns {Array}
*/
const mergePaths = (...paths) => _.flatMap(paths, _.toPath);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment