Add some jsdoc

This commit is contained in:
Aaron Jensen 2015-08-19 07:29:06 -07:00
parent fe1d881412
commit dea78333ff
3 changed files with 52 additions and 3 deletions

View File

@ -1,6 +1,40 @@
import freeze from './freeze';
export default function constant(object) {
const frozen = freeze(object);
/**
* Returns a function that always returns the supplied value.
*
* Useful for replacing an object outright rather than merging it.
*
* @function
* @sig a -> (* -> a)
* @memberOf u
* @param {*} value what to return from returned function.
* @return {function} a new function that will always return value.
*
* @example
* var alwaysFour = u.constant(4);
* expect(alwaysFour(32)).toEqual(4);
*
* @example
* var user = {
* name: 'Mitch',
* favorites: {
* band: 'Nirvana',
* movie: 'The Matrix'
* }
* };
*
* var newFavorites = {
* band: 'Coldplay'
* };
*
* var result = u({ favorites: u.constant(newFavorites) }, user);
*
* expect(result).toEqual({ name: 'Mitch', favorites: { band: 'Coldplay' } });
*/
function constant(value) {
const frozen = freeze(value);
return () => frozen;
}
export default constant;

View File

@ -22,7 +22,18 @@ function recur(object) {
return object;
}
export default function freeze(object) {
/**
* Deeply freeze a plain javascript object.
*
* If `process.env.NODE_ENV === 'production'`, this returns the original object
* witout freezing.
*
* @function
* @sig a -> a
* @param {object} object Object to freeze.
* @return {object} Frozen object, unless in production, then the same object.
*/
function freeze(object) {
if (process.env.NODE_ENV === 'production') {
return object;
}
@ -33,3 +44,5 @@ export default function freeze(object) {
return object;
}
export default freeze;

View File

@ -49,6 +49,8 @@ function updateArray(updates, object) {
* update({ foo: x => (x + 1) }, { foo: 2 });
* // => { foo: 3 }
*
* @function
* @name update
* @param {Object|Function} updates
* @param {Object|Array} object to update
* @return {Object|Array} new object with modifications