UNPKG

microsoft-cognitiveservices-speech-sdk

Version:
1 lines 3.01 kB
{"version":3,"sources":["src/common/StringUtils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD;;GAEG;AACH,qBAAa,WAAW;IAEpB;;;;;;OAMG;WACW,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,iBAAiB,CAAC,MAAM,CAAC,GAAG,MAAM;CAwD9F","file":"StringUtils.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\nimport { IStringDictionary } from \"./IDictionary.js\";\r\n\r\n/**\r\n * String helper functions\r\n */\r\nexport class StringUtils {\r\n\r\n /**\r\n * Formats a string by replacing the named {keys} in the string with the values contained in the replacement dictionary.\r\n * @param format The format string that contains the parts to replace surrounded by {}. For example: \"wss://{region}.cts.speech.microsoft.com\".\r\n * If your string needs to contain a { or } you can use the {{ and }} escape sequences respectively.\r\n * @param replacements The dictionary of replacements. If a replacement is not found, it is replaced with an empty string\r\n * @returns The formatted string. If you pass in a null or undefined format string, an empty string will be returned\r\n */\r\n public static formatString(format: string, replacements: IStringDictionary<string>): string {\r\n if (!format) {\r\n return \"\";\r\n }\r\n\r\n if (!replacements) {\r\n return format;\r\n }\r\n\r\n let formatted: string = \"\";\r\n let key: string = \"\";\r\n\r\n const appendToFormatted = (str: string): void => {\r\n formatted += str;\r\n };\r\n const appendToKey = (str: string): void => {\r\n key += str;\r\n };\r\n let appendFunc: (str: string) => void = appendToFormatted;\r\n\r\n for (let i = 0; i < format.length; i++) {\r\n const c: string = format[i];\r\n const next: string = i + 1 < format.length ? format[i + 1] : \"\";\r\n\r\n switch (c) {\r\n case \"{\":\r\n if (next === \"{\") {\r\n appendFunc(\"{\");\r\n i++;\r\n } else {\r\n appendFunc = appendToKey;\r\n }\r\n break;\r\n\r\n case \"}\":\r\n if (next === \"}\") {\r\n appendFunc(\"}\");\r\n i++;\r\n } else {\r\n if (replacements.hasOwnProperty(key)) {\r\n formatted += replacements[key];\r\n }\r\n\r\n appendFunc = appendToFormatted;\r\n key = \"\";\r\n }\r\n break;\r\n\r\n default:\r\n appendFunc(c);\r\n break;\r\n }\r\n }\r\n\r\n return formatted;\r\n }\r\n}\r\n"]}