UNPKG

office-ui-fabric-react

Version:

Reusable React components for building experiences for Office 365.

34 lines (33 loc) 1.36 kB
define(["require", "exports"], function (require, exports) { "use strict"; // Regex that finds { and } so they can be removed on a lookup for string format var FORMAT_ARGS_REGEX = /[\{\}]/g; // Regex that finds {#} so it can be replaced by the arguments in string format var FORMAT_REGEX = /\{\d+\}/g; /** * String Format is like C# string format. * Usage Example: "hello {0}!".format("mike") will return "hello mike!" * Calling format on a string with less arguments than specified in the format is invalid * Example "I love {0} every {1}".format("CXP") will result in a Debug Exception. */ function format(s) { 'use strict'; var values = []; for (var _i = 1; _i < arguments.length; _i++) { values[_i - 1] = arguments[_i]; } var args = values; // Callback match function function replace_func(match) { // looks up in the args var replacement = args[match.replace(FORMAT_ARGS_REGEX, '')]; // catches undefined in nondebug and null in debug and nondebug if (replacement === null || replacement === undefined) { replacement = ''; } return replacement; } return (s.replace(FORMAT_REGEX, replace_func)); } exports.format = format; });