import type { Falsy , Token } from './types' ; import * as is from './is' ; export const join = ( parts , separator = '-' ) : string => parts . join ( separator ) ; export const joinTruthy = ( parts , separator ? ) : string => join ( parts . filter ( Boolean ) as string [ ] , separator ) ; export const tail = < T extends string | readonly unknown [ ] > ( array , startIndex = 1 ) : T => array . slice ( startIndex ) as T ; export const identity = < T > ( value ) : T => value ; export const capitalize = ( value ) : string => value [ 0 ] . toUpperCase ( ) + tail ( value ) ; const uppercasePattern = /([A-Z])/g ; const prefixAndLowerCase = ( char ) : string => `-${ char . toLowerCase ( ) }` ; export const hyphenate = ( value ) : string => value . replace ( uppercasePattern , prefixAndLowerCase ) ; export interface Cache < V > { has ( key ) : boolean ; get ( key ) : V | undefined ; set ( key , value ) : void ; } export const createCache = < V > ( ) : Cache < V > => { if ( typeof Map === 'function' ) { return new Map ( ) ; } const store = Object . create ( null ) as Record < string , V > ; return { has ( key ) { return key in store ; } , get ( key ) { return store [ key ] ; } , set ( key , value ) { store [ key ] = value ; } , } ; } ; const interleave = ( strings , interpolations ) : Token [ ] => { const result = [ strings [ 0 ] ] ; for ( let index = 0 ; index < interpolations . length ; ) { if ( is . string ( interpolations [ index ] ) ) { ; ( result [ result . length - 1 ] as string ) += ( interpolations [ index ] as string ) + strings [ ++ index ] ; } else { if ( interpolations [ index ] ) { result . push ( interpolations [ index ] ) ; } result . push ( strings [ ++ index ] ) ; } } return result ; } ; export const asTokens = ( tokens ) : Token [ ] => is . array ( tokens [ 0 ] ) && is . array ( ( ( tokens [ 0 ] as unknown ) as TemplateStringsArray ) . raw ) ? interleave ( ( tokens [ 0 ] as unknown ) as TemplateStringsArray , tail ( tokens ) as Token [ ] ) : ( tokens as Token [ ] ) ; returns export const sortedInsertionIndex = ( array , element ) : number => { let high = array . length ; if ( high === 0 ) return 0 ; for ( let low = 0 ; low < high ; ) { const pivot = ( high + low ) >> 1 ; if ( array [ pivot ] <= element ) { low = pivot + 1 ; } else { high = pivot ; } } return high ; } ; export const merge = < A , B > ( a , b ) : A & B => ( a && b ? { ... a , ... b } : a || b || { } ) as A & B ;