UNPKG

sprint

Version:

Complex sprintf() implementation

225 lines (217 loc) 8.29 kB
// Generated by CoffeeScript 1.3.3 (function() { var errorMessage, format, intSizeTable, sprint, _ref, __slice = [].slice, __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; format = /%(?:%|(\d+[$])?((?:[+\x20\-#0])*)((?:[*](?:\d+[$])?)?v)?(\d*|[*](?:\d+[$])?)(?:[.](\d+|[*](?:\d+[$])?))?(hh?|ll?|[Lzjtq]|I(?:32|64)?)?([diuDUfFeEgGxXoOscpnbBr]))/g; errorMessage = "64-bit numbers aren't supported by sprint()!"; intSizeTable = { h: 2, hh: 1, l: 4, ll: new RangeError(errorMessage), L: 4, z: 4, j: 4, t: 4, I: 4, I32: 4, I64: new RangeError(errorMessage), q: new RangeError(errorMessage) }; sprint = function() { var arrayObjects, dearrayified, i, padString, string, toString, values, _ref; string = arguments[0], values = 2 <= arguments.length ? __slice.call(arguments, 1) : []; arrayObjects = ['[object Array]', '[object Arguments]']; toString = Object.prototype.toString; if ((_ref = toString.call(values[0]), __indexOf.call(arrayObjects, _ref) >= 0) && values.length === 1) { values = dearrayified = values[0]; } i = -1; padString = function(string, length, joiner, leftPad) { string = "" + string; if (string.length > length) { return string; } else if (leftPad) { return "" + string + (new Array(length - string.length + 1).join(joiner)); } else { return "" + (new Array(length - string.length + 1).join(joiner)) + string; } }; return ("" + string).replace(format, function(string, argument, flags, vector, length, precision, intSize, type) { var abs, alignCharacter, arg, args, character, defaultPrecision, leftPad, letter, padInteger, prefix, result, special, toExponential; if (intSize == null) { intSize = 'L'; } if (string === '%%') { return '%'; } leftPad = __indexOf.call(flags, '-') >= 0; alignCharacter = __indexOf.call(flags, '0') >= 0 && !leftPad ? '0' : ' '; abs = function(number, signed) { var bits, entry, highValue; if (signed == null) { signed = false; } if (intSize === 'L' && (number >= 0 || signed)) { return parseInt(number, 10); } entry = intSizeTable[intSize]; if (entry instanceof Error) { throw entry; } bits = entry * 8; number = parseInt(number, 10) % Math.pow(2, bits); highValue = Math.pow(2, bits) - 1; if (signed && number >= Math.pow(2, bits - 1)) { number = -Math.pow(2, bits) + number; } if (signed) { return number; } else { return number >>> 0; } }; toExponential = function() { argument = (+argument).toExponential(precision); if (special && __indexOf.call(argument, '.') < 0) { argument = argument.replace('e', '.e'); } return argument.toLowerCase().replace(/\d+$/, function(string) { return padString(string, 3, 0); }); }; padInteger = function(string) { if (+string === 0 && +precision === 0) { return ''; } else if (defaultPrecision) { return string; } else { alignCharacter = ' '; return padString(string, precision, '0'); } }; if (vector) { character = vector[0] === '*' ? vector.length > 2 ? values[parseInt(vector.slice(1), 10) - 1] : values[++i] : '.'; } length = length[0] === '*' ? length.length === 1 ? values[++i] : values[parseInt(length.slice(1)) - 1] : !length ? 0 : length; precision = precision && precision[0] === '*' ? precision.length === 1 ? values[++i] : values[parseInt(precision.slice(1), 10) - 1] : !precision ? (defaultPrecision = true, 6) : precision; argument = values[(parseInt(argument, 10) || ++i + 1) - 1]; if (argument === 0) { if (1 / argument === -Infinity) { argument = '-0'; } } argument = argument != null ? argument : ''; special = __indexOf.call(flags, '#') >= 0; args = (function() { var _i, _len, _results; if (vector) { _results = []; for (_i = 0, _len = argument.length; _i < _len; _i++) { letter = argument[_i]; _results.push(letter.charCodeAt(0)); } return _results; } else { return [argument]; } })(); result = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = args.length; _i < _len; _i++) { argument = args[_i]; argument = (function() { var _ref1; switch (type) { case 'd': case 'i': case 'D': return padInteger(abs(argument, true)); case 'u': case 'U': return padInteger(abs(argument)); case 'f': case 'F': argument = (+argument).toFixed(precision).toLowerCase(); if (special && __indexOf.call(argument, '.') < 0 && !/^-?[a-z]+$/.test(argument)) { argument += '.'; } return argument; case 'e': case 'E': return toExponential(); case 'g': case 'G': arg = Math.abs(argument); if (+arg === 0 || (0.0001 <= (_ref1 = Math.abs(arg)) && _ref1 < Math.pow(10, precision))) { argument = ("" + argument).substring(0, +precision + 1); if (special) { return argument.replace(/[.]?$/, '.'); } else { return argument.replace(/[.]$/, ''); } } else { return toExponential().replace(/[.]?0+e/, 'e'); } break; case 'x': case 'X': prefix = special && +argument !== 0 ? '0x' : ''; return "" + prefix + (padInteger(abs(argument).toString(16))); case 'b': case 'B': prefix = special && +argument !== 0 ? '0b' : ''; return "" + prefix + (padInteger(abs(argument).toString(2))); case 'o': case 'O': prefix = special ? '0' : ''; return "" + prefix + (padInteger(abs(argument).toString(8))); case 'r': if (dearrayified && i === 0) { argument = dearrayified; } argument = JSON.stringify(argument); if (defaultPrecision) { return argument; } else { return argument.substring(0, precision); } break; case 's': if (defaultPrecision) { return argument; } else { return argument.substring(0, precision); } break; case 'c': return String.fromCharCode(argument); default: throw new Exception("Unrecognized %type (?). Shouldn't happen."); } })(); argument = "" + argument; if (type === type.toUpperCase()) { argument = argument.toUpperCase(); } if (argument[0] !== '-') { if (__indexOf.call(flags, '+') >= 0) { argument = "+" + argument; } else if (__indexOf.call(flags, ' ') >= 0) { argument = " " + argument; } } _results.push(padString(argument, length, alignCharacter, leftPad)); } return _results; })(); return result.join(character); }); }; if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) { module.exports = sprint; } ((_ref = typeof module !== "undefined" && module !== null ? module.exports : void 0) != null ? _ref : this).sprint = sprint; }).call(this);