UNPKG

@paydirt/fmt

Version:

String formating using commonly used standards

49 lines (48 loc) 1.45 kB
import object from './replacers/object'; import type from './replacers/type'; import boolean from './replacers/bool'; import integer from './replacers/integer'; import binary from './replacers/binary'; import char from './replacers/char'; import hex from './replacers/hex'; import float from './replacers/float'; import string from './replacers/string'; var flagMap = new Map([ ['v', object], ['T', type], ['t', boolean], ['d', integer], ['b', binary], ['c', char], ['X', hex], ['x', hex], ['f', float], ['Q', string], ['q', string], ['S', string], ['s', string] ]); export default function sprintf(format) { var a = []; for (var _i = 1; _i < arguments.length; _i++) { a[_i - 1] = arguments[_i]; } var i = -1; return format.replace(/(%%)|(?:%(?:(\+)?([\^_])?(\-)?(\d+)?(?:\.(\d+))?)?([vdsfbecxtq]))/gi, function (_, literal, sign, transform, negative, padding, precision, flag) { if (literal) { return '%'; } var mods = { sign: Boolean(sign), transform: transform, negative: Boolean(negative), padding: parseInt(padding, 10), precision: parseInt(precision, 10) }; var method = flagMap.get(flag); if (method) { return method(flag, mods, a[++i]); } throw new SyntaxError("Unrecognized flag \"".concat(flag, "\".")); }); }