nl_tokens: === Define the Main Function ================================================= A function for converting lists into human - friendly joined stings e . g . [ A B C ] into A B & C .
pl_tokens: function ( list , options ) { // short-circuit non-arrays if ( ! Array . isArray ( list ) || Object . prototype . toString . call ( list ) === '[object Arguments]' ) { return String ( list ) ; } // short-circuit empty lists if ( list . length === 0 ) { return '' ; } // make sure we have a sane options object if ( typeof options === 'string' ) { var newOptions = { } ; newOptions [ options ] = true ; <mask> = newOptions ; } if ( typeof options !== 'object' ) { options = { } ; } // set up configuration var def = humanJoin . optionDefaults ; // a local reference to make the code more readable var separator = typeof def . separator === 'string' || typeof def . separator === 'number' ? def . separator : ', ' ; if ( typeof options . separator === 'string' || typeof options . separator === 'number' ) { separator = options . separator ; } separator = '' + separator ; // force to string var conjunction = typeof def . conjunction === 'string' || typeof def . conjunction === 'number' || typeof def . conjunction === 'boolean' ? def . conjunction : ' & ' ; if ( typeof options . conjunction === 'string' || typeof options . conjunction === 'number' || typeof options . conjunction === 'boolean' ) { conjunction = options . conjunction ; } if ( typeof conjunction === 'number' ) { conjunction = '' + conjunction ; // force to string } if ( options . noConjunction ) { conjunction = false ; } var quoteWith = typeof def . quoteWith === 'string' || typeof def . quoteWith === 'number' || typeof def . quoteWith === 'boolean' ? def . quoteWith : false ; if ( typeof options . quoteWith === 'string' || typeof options . quoteWith === 'number' || typeof options . quoteWith === 'boolean' ) { quoteWith = options . quoteWith ; } if ( typeof quoteWith === 'number' ) { quoteWith = '' + quoteWith ; // force to string } var mirrorQuote = typeof def . mirrorQuote === 'boolean' ? def . mirrorQuote : true ; if ( typeof options . mirrorQuote !== 'undefined' ) { mirrorQuote = options . mirrorQuote ? true : false ; } // apply any shortcuts specified if ( options . and ) { conjunction = ' and ' ; } if ( options . or ) { conjunction = ' or ' ; } if ( options . oxford || options . oxfordAnd ) { conjunction = ', and ' ; } if ( options . oxfordOr ) { conjunction = ', or ' ; } // process the array to quote and stringify as needed var stringList = [ ] ; for ( var i = 0 ; i < list . length ; i ++ ) { // for loop rather than forEach to support Arguments objects // force to string stringList [ i ] = '' + list [ i ] ; // quote if needed if ( quoteWith ) { if ( mirrorQuote ) { stringList [ i ] = quoteWith + stringList [ i ] + humanJoin . mirrorString ( quoteWith ) ; } else { stringList [ i ] = quoteWith + stringList [ i ] + quoteWith ; } } } // generate the human-friendly string var ans = stringList . shift ( ) ; while ( stringList . length ) { if ( stringList . length === 1 && conjunction !== false ) { ans += conjunction ; } else { ans += separator ; } ans += stringList . shift ( ) ; } // return the generated string return ans ; }