import escapeRegexStr = require ( '@jsbits/escape-regex-str' ) ; import getPackageVersion = require ( '@jsbits/get-package-version' ) ; import pathRelative = require ( './lib/path-relative' ) ; import R = require ( './regexes' ) ; import Jscc from '../index' ; interface RawValues { [ k ] : any ; } /**
 * Default prefixes, equivalent to `['//', '/*', '<!--']`
 */ const S_DEF_PREFIXES = /\/[/*]|<!--/ . source ; param error const errorHandler = ( error ) => { if ( typeof error === 'string' ) { error = new Error ( error ) ; } throw error ; } ; const enum QUOTES { Single = 1 , Double = 2 } param opts const getEscapeQuotes = ( opts ) => { switch ( opts . escapeQuotes ) { case 'single' : return QUOTES . Single ; case 'double' : return QUOTES . Double ; case 'both' : return QUOTES . Single | QUOTES . Double ; } return 0 ; } ; param prefix const parsePrefix = ( prefix ) => { if ( prefix instanceof RegExp ) { return prefix . source ; } if ( typeof prefix === 'string' ) { return escapeRegexStr ( prefix ) ; } return errorHandler ( 'jscc `prefixes` must be an array of strings or regexes' ) ; } ; param opts const getPrefixes = ( opts ) => { let prefixes = opts . prefixes || '' ; if ( prefixes ) { const list = Array . isArray ( prefixes ) ? prefixes : [ prefixes ] ; prefixes = list . filter ( Boolean ) . map ( parsePrefix ) . join ( '|' ) ; } return prefixes || S_DEF_PREFIXES ; } ; param src const copyValues = function ( src ) { const keys = Object . keys ( src ) ; const dest = Object . create ( null ) as any ; for ( let v , i = 0 ; i < keys . length ; i ++ ) { v = keys [ i ] ; if ( R . VARNAME . test ( v ) ) { dest [ v ] = src [ v ] ; } else { errorHandler ( `Invalid memvar name: ${ v }` ) ; } } return dest as Jscc . Values ; } ; param filename param srcValues const getValues = ( filename , srcValues ) => { if ( typeof srcValues !== 'object' ) { return errorHandler ( 'jscc values must be a plain object' ) ; } const values = copyValues ( srcValues ) ; if ( typeof values . _VERSION !== 'string' ) { values . _VERSION = getPackageVersion ( ) ; } values . _FILE = pathRelative ( filename ) ; return values ; } ; param opts const parseOptions = function _parseOptions ( filename , opts ) : JsccProps { const values = getValues ( filename , opts . values || { } ) ; const prefixes = getPrefixes ( opts ) ; const escapeQuotes = getEscapeQuotes ( opts ) ; return { magicStr : { } as any , errorHandler , escapeQuotes , keepLines : ! ! opts . keepLines , mapContent : ! ! opts . mapContent , mapHires : opts . mapHires !== false , sourceMap : opts . sourceMap !== false , prefixes , values , } ; } ; export = parseOptions ;