@uifabric/utilities
Version:
Fluent UI React utilities for building components.
1 lines • 1.94 kB
Source Map (JSON)
{"version":3,"file":"string.js","sourceRoot":"../src/","sources":["string.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,IAAM,iBAAiB,GAAG,SAAS,CAAC;AAEpC,+EAA+E;AAC/E,IAAM,YAAY,GAAG,UAAU,CAAC;AAEhC;;;;;;;;;;;;GAYG;AACH,8DAA8D;AAC9D,MAAM,UAAU,MAAM,CAAC,CAAS;IAAE,gBAAgB;SAAhB,UAAgB,EAAhB,qBAAgB,EAAhB,IAAgB;QAAhB,+BAAgB;;IAChD,IAAI,IAAI,GAAG,MAAM,CAAC;IAClB,0BAA0B;IAC1B,SAAS,WAAW,CAAC,KAAa;QAChC,uBAAuB;QACvB,8DAA8D;QAC9D,IAAI,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,iBAAiB,EAAE,EAAE,CAAQ,CAAC,CAAC;QAEpE,+DAA+D;QAC/D,IAAI,WAAW,KAAK,IAAI,IAAI,WAAW,KAAK,SAAS,EAAE;YACrD,WAAW,GAAG,EAAE,CAAC;SAClB;QAED,OAAO,WAAW,CAAC;IACrB,CAAC;IACD,OAAO,CAAC,CAAC,OAAO,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC;AAC9C,CAAC","sourcesContent":["// Regex that finds { and } so they can be removed on a lookup for string format\nconst FORMAT_ARGS_REGEX = /[\\{\\}]/g;\n\n// Regex that finds {#} so it can be replaced by the arguments in string format\nconst FORMAT_REGEX = /\\{\\d+\\}/g;\n\n/**\n * String format method, used for scenarios where at runtime you\n * need to evaluate a formatted string given a tokenized string. This\n * usually only is needed in localization scenarios.\n\n * @example\n * ```tsx\n * \"I love {0} every {1}\".format(\"CXP\")\n * ```\n * will result in a Debug Exception.\n *\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function format(s: string, ...values: any[]): string {\n let args = values;\n // Callback match function\n function replaceFunc(match: string): string {\n // looks up in the args\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let replacement = args[match.replace(FORMAT_ARGS_REGEX, '') as any];\n\n // catches undefined in nondebug and null in debug and nondebug\n if (replacement === null || replacement === undefined) {\n replacement = '';\n }\n\n return replacement;\n }\n return s.replace(FORMAT_REGEX, replaceFunc);\n}\n"]}