const flowHeader = '// @flow' ; const generatedAutomaticallyMessage = "// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it." ; const id = x => x ; const trimLeft = string => string . replace ( /^\s*/ , '' ) ; const trimRight = string => string . replace ( /\s*$/ , '' ) ; const removeIndent = ( string , indent ) => { return trimLeft ( string . slice ( 0 , indent ) ) + string . slice ( indent ) ; } ; const addIndent = ( string , indent ) => string . length > 0 ? ' ' . repeat ( indent ) + string : string ; const detectIndent = ( string ) => { const matchResult = string . match ( /^\n*(\s+)/ ) ; const indent = matchResult ? matchResult [ 1 ] . length : 0 ; return indent ; } ; const addIndentToMultilineString = ( string , indent , ignoreFirstLine ) => string . split ( '\n' ) . map ( ( line , index ) => ( ignoreFirstLine && index === 0 ) ? line : addIndent ( line , indent ) ) . join ( '\n' ) ; const addIndentToArray = ( array , indent , ignoreFirstElement ) => array . map ( ( element , index ) => addIndentToMultilineString ( element , indent , ignoreFirstElement && index === 0 ) ) ; const removeIndentFromArray = ( array , indent , ignoreFirstElement ) => array . map ( ( element , index ) => ( ignoreFirstElement && index === 0 ) ? element : removeIndent ( element , indent ) ) ; param { String [ ] } stringsArray returns { String [ ] } const addSeparator = ( stringsArray , separator ) => stringsArray . map ( ( string , index ) => index === stringsArray . length - 1 ? string : string + separator ) ; param { String [ ] } rawStrings param { ... String } substitutions returns { String } example const formatBlock = ( rawStrings , ... substitutions ) => { const firstLineIndent = detectIndent ( rawStrings [ 0 ] ) ; let lastLineIndent = 0 ; const result = [ ... substitutions , '' ] . map ( ( substitution , index ) => { const rawString = rawStrings [ index ] ; const maybeTrimLeft = index === 0 ? trimLeft : id ; const maybeTrimRight = index === substitutions . length ? trimRight : id ; const string = maybeTrimLeft ( maybeTrimRight ( rawString ) ) ; const lines = removeIndentFromArray ( string . split ( '\n' ) , firstLineIndent , true ) ; if ( lines . length > 1 ) { const lastLine = lines [ lines . length - 1 ] ; lastLineIndent = detectIndent ( lastLine ) ; } const indentedSubstitution = Array . isArray ( substitution ) ? addIndentToArray ( substitution , lastLineIndent , true ) . join ( '\n' ) : addIndentToMultilineString ( substitution , lastLineIndent , true ) ; return lines . join ( '\n' ) + indentedSubstitution ; } ) . join ( '' ) ; return result ; } ; param { String [ ] } rawStrings param { ... String } substitutions returns { String } const formatFlowFile = ( ... args ) => flowHeader + '\n' + generatedAutomaticallyMessage + '\n\n' + formatBlock ( ... args ) + '\n' ; param { String [ ] } rawStrings param { ... String } substitutions returns { String } const formatTypeScriptFile = ( ... args ) => generatedAutomaticallyMessage + '\n\n' + formatBlock ( ... args ) + '\n' ; module . exports = { addSeparator , formatBlock , formatFlowFile , formatTypeScriptFile } ;