All files index.js

100% Statements 40/40
92% Branches 23/25
100% Functions 11/11
100% Lines 37/37
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73    1x   1x   1x 7x   1x 2x 1x   2x   2x 64x     2x     1x 6x   6x 5x   6x     1x 3x 3x     1x 12x   1x 5x   5x 1x   5x   5x     1x 5x     1x 8x 3x     5x 5x 5x         1x         1x  
'use strict'
 
const lodashRange = require( 'lodash.range' )
 
const clone = ( obj = {} ) => JSON.parse( JSON.stringify( obj ) )
 
const matches = ( obj = {}, source = {} ) =>
  Object.keys( source ).every( key => obj[ key ] === source[ key ] )
 
const id = ( prefix = '', length = 32 ) => {
  if( prefix )
    prefix = identifier( prefix ) + '-'
 
  let str = prefix
 
  for( let i = 0; i < length; i++ ) {
    str += Math.floor( Math.random() * 16 ).toString( 16 )
  }
 
  return str
}
 
const identifier = ( value = '', caseSensitive = false ) => {
  let id = value.replace( /[^a-z0-9]/gi, '-' ).replace( /-{2,}/g, '-' ).replace( /^-/i, '' ).replace( /-$/i, '' )
 
  if( !caseSensitive )
    id = id.toLowerCase()
 
  return id
}
 
const escapeHtml = ( str = '' ) => {
  const result = str.replace( /</g, '&lt;' )
  return result
}
 
const capitalizeFirstLetter = ( str = '' ) =>
  str.charAt( 0 ).toUpperCase() + str.slice( 1 )
 
const hyphenatedToCamelCase = ( str = '', capitalizeFirst = false ) => {
  let [ head, ...rest ] = str.split( '-' )
 
  if( capitalizeFirst )
    head = capitalizeFirstLetter( head )
 
  const capitalized = rest.map( capitalizeFirstLetter )
 
  return [ head, ...capitalized ].join( '' )
}
 
const camelCaseToHyphenated = ( str = '' ) =>
  str.replace( /([A-Z])/g, matches => `-${matches[ 0 ].toLowerCase()}` )
 
 
const range = ( start = 0, end ) => {
  if( typeof end === 'undefined' ) {
    return lodashRange( start )
  }
  else {
    const step = start <= end ? 1 : -1
    const normEnd = end >= 0 ? end + 1 : end - 1
      return lodashRange( start, normEnd, step )
  }
}
 
 
const utils = {
  id, identifier, matches, clone, escapeHtml, capitalizeFirstLetter,
  hyphenatedToCamelCase, camelCaseToHyphenated, range
}
 
module.exports = utils