import { BudgieUtilities } from "../../BudgieUtilities" ; import { LineResults } from "../LineResults" ; import { CommandNames } from "../Names/CommandNames" ; import { Command } from "./Command" ; import { CommandResult } from "./CommandResult" ; import { CommandMetadata } from "./Metadata/CommandMetadata" ; import { RepeatingParameters } from "./Metadata/Parameters/RepeatingParameters" ; import { SingleParameter } from "./Metadata/Parameters/SingleParameter" ; /**
 * Concatenates multiple other values into a single string.
 */ export class StringFormatCommand extends Command { /**
     * Metadata on the command.
     */ private static metadata : CommandMetadata = new CommandMetadata ( CommandNames . StringFormat ) . withDescription ( "Concatenates multiple other values into a single string" ) . withParameters ( [ new SingleParameter ( "format" , "String describing the format." , true ) , new RepeatingParameters ( "Input pairs" , [ new SingleParameter ( "inputName" , "Input pair name" , true ) , new SingleParameter ( "inputType" , "Input pair type" , true ) , ] ) , ] ) ; returns public getMetadata ( ) { return StringFormatCommand . metadata ; } param parameters returns public render ( parameters ) { if ( parameters [ 1 ] [ 0 ] !== '"' ) { throw new Error ( "String formatting must be done with primitives" ) ; } let output = parameters [ 1 ] . substring ( 1 , parameters [ 1 ] . length - 1 ) ; output = this . language . syntax . strings . formatting . formatLeft + output ; const inputsLength = parameters . length / 2 - 1 ; for ( let i = 0 ; i < inputsLength ; i += 1 ) { const replacement = this . formatReplacement ( i , parameters [ i * 2 + 2 ] , parameters [ i * 2 + 3 ] ) ; output = BudgieUtilities . stringReplaceAll ( output , `{${ i }}` , replacement ) ; } if ( ! this . language . syntax . strings . formatting . useInterpolation ) { if ( parameters . length > 2 ) { output += this . language . syntax . strings . formatting . formatMiddle ; for ( let i = 2 ; i < parameters . length - 3 ; i += 2 ) { output += parameters [ i ] += ", " ; } output += parameters [ parameters . length - 2 ] ; } else { output += this . language . syntax . strings . formatting . formatAbbreviated ; } } output += this . language . syntax . strings . formatting . formatRight ; return new LineResults ( [ new CommandResult ( output , 0 ) ] ) . withAddSemicolon ( true ) ; } param i param inputName param inputType returns private formatReplacement ( i , inputName , inputType ) { let output = this . language . syntax . strings . formatting . formatInputLeft ; if ( this . language . syntax . strings . formatting . includeIndexInFormatting ) { output += i ; } if ( this . language . syntax . strings . formatting . inputTypes ) { if ( this . language . syntax . strings . formatting . typeCodes [ inputType ] !== undefined ) { output += this . language . syntax . strings . formatting . typeCodes [ inputType ] ; } else { output += inputType ; } } if ( this . language . syntax . strings . formatting . useInterpolation ) { output += inputName ; } output += this . language . syntax . strings . formatting . formatInputRight ; return output ; } }