Created
July 16, 2015 21:22
-
-
Save kentcdodds/6d68220a2a7247f12f35 to your computer and use it in GitHub Desktop.
A few helpful lodash mixins
This file contains 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
import _ from 'lodash'; | |
/* eslint no-underscore-dangle:0 */ | |
const _mixins = {pickMultiple, defaultsDeep: getDefaultsDeep(), randomBool, guid, repeat}; | |
export default _mixins; | |
if (process.env.NODE_ENV === 'test') { | |
require('./_mixins.test')(_mixins); | |
} | |
function pickMultiple(array, thingsToPick) { | |
return _.map(array, _.partial(_.ary(_.pick, thingsToPick.length), _, thingsToPick)); | |
} | |
function getDefaultsDeep() { | |
return _.partialRight(_.merge, function defaults(objVal, srcVal) { | |
return objVal === undefined ? srcVal : _.merge(objVal, srcVal, defaults); | |
}); | |
} | |
function randomBool() { | |
return Math.random() >= 0.5; | |
} | |
function guid() { | |
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + | |
s4() + '-' + s4() + s4() + s4(); | |
} | |
function s4() { | |
return Math.floor((1 + Math.random()) * 0x10000) | |
.toString(16) | |
.substring(1); | |
} | |
function repeat(fn, times) { | |
return _.map(new Array(times), (item, index) => { | |
return fn(index); | |
}); | |
} |
This file contains 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
/* eslint no-underscore-dangle:0 */ | |
export default _mixins => { | |
describe('_mixins', () => { | |
describe(`pickMultiple`, () => { | |
const {pickMultiple} = _mixins; | |
it(`be able to pick multiple items from an array of objects`, () => { | |
const foos = [ | |
{x: 1, y: 2, z: 3}, | |
{x: 4, y: 5, z: 6}, | |
{x: 7, y: 8, z: 9} | |
]; | |
const result = pickMultiple(foos, ['x', 'y']); | |
expect(result).to.eql([ | |
{x: 1, y: 2}, | |
{x: 4, y: 5}, | |
{x: 7, y: 8} | |
]); | |
}); | |
}); | |
describe(`defaultsDeep`, () => { | |
const {defaultsDeep} = _mixins; | |
it(`should should deep merge with defaults`, () => { | |
const defaults = { | |
foo: '(-o-)', | |
bar: { | |
baz: '|-o-|', | |
foobar: [ | |
{spam: 'eggs'} | |
] | |
}, | |
oof: { | |
rab: 32 | |
}, | |
raboof: 'maps' | |
}; | |
const defaults2 = { | |
whatever: 'tired of making things up' | |
}; | |
const obj = { | |
foo: 'ᕕ( ᐛ )ᕗ', | |
bar: { | |
foobar: [ | |
{eggs: 'spam'}, | |
{cool: 'beans'} | |
] | |
}, | |
oof: { | |
sgge: 'maps' | |
} | |
}; | |
defaultsDeep(obj, defaults, defaults2); | |
expect(obj).to.eql({ | |
foo: 'ᕕ( ᐛ )ᕗ', | |
bar: { | |
baz: '|-o-|', | |
foobar: [ | |
{eggs: 'spam', spam: 'eggs'}, | |
{cool: 'beans'} | |
] | |
}, | |
oof: { | |
rab: 32, | |
sgge: 'maps' | |
}, | |
raboof: 'maps', | |
whatever: 'tired of making things up' | |
}); | |
}); | |
}); | |
describe(`randomBool`, () => { | |
const {randomBool} = _mixins; | |
it(`should return a boolean`, () => { | |
expect(randomBool()).to.be.a('boolean'); | |
}); | |
}); | |
describe(`guid`, () => { | |
const {guid} = _mixins; | |
it(`should create a random number with hyphens`, () => { | |
expect(guid()).to.match(/^\w{8}(-\w{4}){3}-\w{12}$/); | |
}); | |
}); | |
describe(`repeat`, () => { | |
const {repeat} = _mixins; | |
it(`should call the given function with the index and give back an array of what is called`, () => { | |
const spy = sinon.spy(index => index + 'hi'); | |
const result = repeat(spy, 3); | |
expect(spy).to.have.been.calledThrice; | |
expect(result).to.eql(['0hi', '1hi', '2hi']); | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: https://twitter.com/jdalton/status/621796316218142720