All files utils.js

100% Statements 23/23
100% Branches 19/19
100% Functions 9/9
100% Lines 17/17

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 58 59              9x           8x             1x 2x 2x         2x 22x 22x 5x 17x 1x     2x             1x 22x 22x             2x           3x  
import { PREFIX, ZERO_SEC } from './constants';
import { SETTINGS_DEFAULTS } from './defaults';
 
/**
 * @param {string} value value
 * @returns {string} time
 */
export const parseTime = value => ! /^-?\d*\.?\d+m?s$/.test( value ) || /^-?0?\.?0+m?s$/.test( value ) ? ZERO_SEC : value;
 
/**
 * @param {string|number} value value
 * @returns {number} thickness
 */
export const parseStripeThickness = value => isNaN( parseInt( value, 10 ) ) ? SETTINGS_DEFAULTS.stripe_thickness : parseInt( value );
 
/**
 * @param {object} target target
 * @param {Array} args args
 * @returns {object} options
 */
export const getTargetOptions = ( target, args ) => {
	const options = Object.assign( {}, args.length && typeof args[ 0 ] === 'object' ? args[ 0 ] : {} );
	const filters = {
		'delay': parseTime,
		'duration': parseTime,
		'stripe_thickness': parseStripeThickness,
	};
	Object.keys( SETTINGS_DEFAULTS ).forEach( key => {
		const data = target.data( PREFIX + key );
		if ( isScalar( data ) && key in filters ) {
			options[ key ] = filters[ key ]( data );
		} else if ( undefined !== data ) {
			options[ key ] = data;
		}
	} );
	return Object.assign( {}, SETTINGS_DEFAULTS, options );
};
 
/**
 * @param {*} value value
 * @returns {boolean} is scalar?
 */
export const isScalar = value => {
	const type = typeof value;
	return 'string' === type || 'number' === type || 'boolean' === type;
};
 
/**
 * @param {string} string target string
 * @returns {string} snake case
 */
export const toSnakeCase = string => string.replace( /[\w]([A-Z])/g, matches => `${ matches[ 0 ] }_${ matches[ 1 ] }` ).toLowerCase();
 
/**
 * @param {string} string target string
 * @returns {string} camel case
 */
export const toCamelCase = string => string.replace( '-', '_' ).replace( /(_\w)/g, matches => matches[ 1 ].toUpperCase() );