All files alias.js

100% Statements 13/13
100% Branches 4/4
100% Functions 6/6
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                  7x                                   7x 1x 5x 5x 5x                 7x 371x               8x 2x       7x 7x  
import { curry, merge } from "f-utility"
 
/**
@method alias
@param {Object} object - an object to store aliases in
@param {string} original - a string
@param {string} alt - the alias
@returns null
*/
export const alias = curry((object, original, alt) => {
  /* istanbul ignore next */
  if (!object[alt]) {
    /* istanbul ignore next */
    object[alt] = original // eslint-disable-line fp/no-mutation
  }
  /* istanbul ignore next */
  if (!object[original]) {
    /* istanbul ignore next */
    object[original] = object[alt] // eslint-disable-line fp/no-mutation
  }
})
 
/**
 @method pureAliasedListeners
 @param {Function} subscriber - a callback function
 @returns {Object} current
 */
export const pureAliasedListeners = subscriber =>
  curry((original, alt, seed) => {
    const emitted = merge(seed, { [alt]: original, [original]: original })
    subscriber(emitted)
    return emitted
  })
 
/**
@method getAliasFrom
@param {Object} object - an object to pull aliases from
@param {string} key - a key to look up
@returns {*} whatever the lookup resulted in or the key itself
*/
export const getAliasFrom = curry(
  (object, key) => (object && object[key]) || key
)
 
/**
@method canonicalize
@param {Object} object - an object to store aliases in and pull aliases from
@returns {Object} an object with canonize and getCanon on it
*/
export const canonicalize = object => ({
  canonize: (a, b = a) => alias(object, a, b),
  getCanon: getAliasFrom(object)
})
 
const authors = {}
export const { getCanon, canonize } = canonicalize(authors)