clr-format
Version:
A lightweight, modular and stand-alone JavaScript implementation of a string formatting function that supports composite format strings, globalization and customization
1,055 lines (1,030 loc) • 171 kB
JavaScript
"use strict";
var Format, __extends = this && this.__extends || function(d, b) {
function __() {
this.constructor = d;
}
for (var p in b) {
if (b.hasOwnProperty(p)) {
d[p] = b[p];
}
}
d.prototype = null === b ? Object.create(b) : (__.prototype = b.prototype, new __());
};
(function(Format) {
var Errors;
(function(Errors) {
Format.Errors.ErrorClass = Error;
var SystemError = function(_super) {
function SystemError(message, innerError) {
_super.call(this, message);
this.name = "SystemError";
this.message = message;
if (innerError) {
this.innerError = innerError;
this.stack = innerError.stack;
}
}
__extends(SystemError, _super);
return SystemError;
}(Errors.ErrorClass);
Errors.SystemError = SystemError;
})(Errors = Format.Errors || (Format.Errors = {}));
})(Format || (Format = {}));
(function(Format) {
var Errors;
(function(Errors) {
var ArgumentError = function(_super) {
function ArgumentError(message, innerError) {
_super.call(this, message, innerError);
this.name = "ArgumentError";
}
__extends(ArgumentError, _super);
return ArgumentError;
}(Errors.SystemError);
Errors.ArgumentError = ArgumentError;
})(Errors = Format.Errors || (Format.Errors = {}));
})(Format || (Format = {}));
(function(Format) {
var Errors;
(function(Errors) {
var ArgumentNullError = function(_super) {
function ArgumentNullError(argumentName) {
_super.call(this, "Argument '" + argumentName + "' cannot be undefined or null");
this.name = "ArgumentNullError";
}
__extends(ArgumentNullError, _super);
return ArgumentNullError;
}(Errors.ArgumentError);
Errors.ArgumentNullError = ArgumentNullError;
})(Errors = Format.Errors || (Format.Errors = {}));
})(Format || (Format = {}));
(function(Format) {
var Utils;
(function(Utils) {
var Text;
(function(Text) {
function isNullOrWhitespace(value) {
return !(value && Utils.Polyfill.trim(value).length > 0);
}
function insert(value, startIndex, insertValue) {
if (null == value) {
throw new Format.Errors.ArgumentNullError("value");
}
if (null == startIndex) {
throw new Format.Errors.ArgumentNullError("startIndex");
}
if (null == insertValue) {
throw new Format.Errors.ArgumentNullError("insertValue");
}
if (startIndex < 0 || startIndex > value.length || isNaN(startIndex)) {
throw RangeError("Argument 'startIndex=" + startIndex + "' is not an index inside of 'value=\"" + value + "\"'");
}
return value.substring(0, startIndex) + insertValue + value.substring(startIndex);
}
Text.isNullOrWhitespace = isNullOrWhitespace;
Text.insert = insert;
})(Text = Utils.Text || (Utils.Text = {}));
})(Utils = Format.Utils || (Format.Utils = {}));
})(Format || (Format = {}));
(function(Format) {
var Utils;
(function(Utils) {
function isType(type, object) {
return getType(object) === getTypeString(type);
}
function getType(object) {
if (null === object) {
return Utils.Types.Null;
}
if (void 0 === object) {
return Utils.Types.Undefined;
} else {
return Object.prototype.toString.call(object);
}
}
var fillTypes, getTypeString;
Utils.isType = isType;
Utils.getType = getType;
fillTypes = function(types) {
for (var key in types) {
if (types.hasOwnProperty(key)) {
types[key] = getTypeString(key);
}
}
return types;
};
getTypeString = function(type) {
return "[object " + type + "]";
};
Utils.Types = fillTypes({
Array: "",
Boolean: "",
Date: "",
Function: "",
Null: "",
Number: "",
Object: "",
RegExp: "",
String: "",
Undefined: ""
});
})(Utils = Format.Utils || (Format.Utils = {}));
})(Format || (Format = {}));
(function(Format) {
var Utils;
(function(Utils) {
var Numeric;
(function(Numeric) {
function isCounting(value) {
return value > 0 && Numeric.isInteger(value);
}
function isWhole(value) {
return value >= 0 && Numeric.isInteger(value);
}
function isEven(value) {
if (!Numeric.isInteger(value)) {
throw new Format.Errors.ArgumentError("Argument 'value' must be an integer");
}
return 0 === (1 & value);
}
function toFixedMinMax(value, minDigits, maxDigits) {
return toMinMax(getToFixedHandler(value), minDigits, maxDigits);
}
function toExponentialMinMax(value, minDigits, maxDigits) {
return toMinMax(getToExponentialHandler(value), minDigits, maxDigits);
}
function toPrecisionMinMax(value, minDigits, maxDigits) {
return toMinMax(getToPrecisionHandler(value), minDigits, maxDigits);
}
var toMinMax, validateToMinMaxDigitsArguments, applyMinMax, iterateMinMax, getToFixedHandler, getToExponentialHandler, getToPrecisionHandler, validateValueArgument;
Numeric.isCounting = isCounting;
Numeric.isWhole = isWhole;
Numeric.isEven = isEven;
Numeric.isInteger = Number.isInteger || function(value) {
return null !== value && +value === value >> 0;
};
Numeric.toFixedMinMax = toFixedMinMax;
Numeric.toExponentialMinMax = toExponentialMinMax;
Numeric.toPrecisionMinMax = toPrecisionMinMax;
toMinMax = function(numberHandler, minDigits, maxDigits) {
if (null == minDigits) {
minDigits = void 0;
}
if (null == maxDigits) {
maxDigits = void 0;
}
if (void 0 === minDigits && void 0 !== maxDigits) {
minDigits = numberHandler.defaultMinDigits;
}
validateToMinMaxDigitsArguments(numberHandler, minDigits, maxDigits);
return applyMinMax(numberHandler, minDigits, maxDigits);
};
validateToMinMaxDigitsArguments = function(numberHandler, minDigits, maxDigits) {
if (void 0 !== minDigits && !isFinite(minDigits)) {
throw new Format.Errors.ArgumentError("Argument 'minDigits' cannot be NaN or infinite");
}
if (void 0 !== maxDigits && !isFinite(maxDigits)) {
throw new Format.Errors.ArgumentError("Argument 'maxDigits' cannot be NaN or infinite");
}
if (minDigits > maxDigits) {
throw new RangeError("Argument 'minDigits=" + minDigits + "' cannot be greater than argument 'maxDigits=" + maxDigits + "'");
}
if (maxDigits - minDigits > 20) {
throw new RangeError("The difference between arguments 'minDigits=" + minDigits + "' and 'maxDigits=" + maxDigits + "' cannot exceed 20");
}
};
applyMinMax = function(numberHandler, minDigits, maxDigits) {
var targetValue, minValue, maxValue = numberHandler.delegate(maxDigits);
if (minDigits === maxDigits) {
return maxValue;
}
targetValue = +maxValue, minValue = numberHandler.delegate(minDigits);
if (targetValue === +minValue) {
return minValue;
} else {
return iterateMinMax(numberHandler, {
minDigits: minDigits,
maxDigits: maxDigits,
targetValue: targetValue
}) || maxValue;
}
};
iterateMinMax = function(numberHandler, options) {
var i, minValue;
for (i = options.minDigits + 1; i < options.maxDigits; i += 1) {
minValue = numberHandler.delegate(i);
if (options.targetValue === +minValue) {
return minValue;
}
}
};
getToFixedHandler = function(value) {
validateValueArgument(value);
return {
defaultMinDigits: 0,
delegate: function(digits) {
return null != digits ? value.toFixed(digits) : value.toString();
}
};
};
getToExponentialHandler = function(value) {
validateValueArgument(value);
return {
defaultMinDigits: 0,
delegate: function(digits) {
return void 0 !== digits ? value.toExponential(digits) : value.toExponential();
}
};
};
getToPrecisionHandler = function(value) {
validateValueArgument(value);
return {
defaultMinDigits: 1,
delegate: function(digits) {
return null !== digits ? value.toPrecision(digits) : value.toPrecision();
}
};
};
validateValueArgument = function(value) {
if (null == value) {
throw new Format.Errors.ArgumentNullError("value");
}
if (!isFinite(value)) {
throw new Format.Errors.ArgumentError("Argument 'value' cannot be NaN or infinite");
}
};
})(Numeric = Utils.Numeric || (Utils.Numeric = {}));
})(Utils = Format.Utils || (Format.Utils = {}));
})(Format || (Format = {}));
(function(Format) {
var Utils;
(function(Utils) {
var Padding;
(function(Padding) {
function pad(value, options) {
if (null == value) {
throw new Format.Errors.ArgumentNullError("value");
}
setDefaultOptions(options);
validateOptions(options);
if (isPaddingRequired(value, options)) {
value = directionStrategies[options.direction](value, options);
}
return value;
}
var Direction, setDefaultOptions, validateOptions, isPaddingRequired, getPadWidth, getPadding, directionStrategies;
(function(Direction) {
Direction[Direction.Left = 1] = "Left";
Direction[Direction.Right = 2] = "Right";
Direction[Direction.Both = 3] = "Both";
})(Padding.Direction || (Padding.Direction = {}));
Direction = Padding.Direction;
Padding.pad = pad;
setDefaultOptions = function(options) {
options.direction = options.direction || Direction.Right;
options.paddingChar = options.paddingChar || " ";
};
validateOptions = function(options) {
if (!Utils.Numeric.isCounting(options.totalWidth)) {
throw new Format.Errors.ArgumentError("Option 'totalWidth' with value '" + options.totalWidth + "' must be a positive non-zero integer (counting) number");
}
if ("string" !== typeof options.paddingChar || options.paddingChar.length > 1) {
throw new Format.Errors.ArgumentError("Option 'paddingChar' with value '" + options.paddingChar + "' must be a single character string");
}
if (null == Direction[options.direction]) {
throw new Format.Errors.ArgumentError("Option 'direction' with value '" + options.direction + "' must be one of Padding.Direction enum values");
}
};
isPaddingRequired = function(value, options) {
return options.totalWidth > value.length;
};
getPadWidth = function(value, options) {
return options.totalWidth - value.length;
};
getPadding = function(padWidth, options) {
return new Array(padWidth + 1).join(options.paddingChar);
};
directionStrategies = {};
directionStrategies[Direction.Left] = function(value, options) {
return getPadding(getPadWidth(value, options), options) + value;
};
directionStrategies[Direction.Right] = function(value, options) {
return value + getPadding(getPadWidth(value, options), options);
};
directionStrategies[Direction.Both] = function(value, options) {
var padWidth = getPadWidth(value, options), right = Math.ceil(padWidth / 2), left = padWidth - right;
return getPadding(left, options) + value + getPadding(right, options);
};
})(Padding = Utils.Padding || (Utils.Padding = {}));
})(Utils = Format.Utils || (Format.Utils = {}));
})(Format || (Format = {}));
(function(Format) {
var Utils;
(function(Utils) {
var Enumerable;
(function(Enumerable) {
function takeWhile(array, predicate) {
var result, i, len;
if (null == array) {
throw new Format.Errors.ArgumentNullError("array");
}
if ("function" !== typeof predicate) {
throw new TypeError("Cannot call method 'takeWhile' without a predicate function");
}
result = [];
for (i = 0, len = array.length; i < len && predicate(array[i], i); i += 1) {
result.push(array[i]);
}
return result;
}
function compact(array) {
var j, i, len;
if (null == array) {
throw new Format.Errors.ArgumentNullError("array");
}
j = 0;
for (i = 0, len = array.length; i < len; i += 1) {
if (void 0 !== array[i]) {
array[j] = array[i];
j += 1;
}
}
array.length = j;
return array;
}
Enumerable.takeWhile = takeWhile;
Enumerable.compact = compact;
})(Enumerable = Utils.Enumerable || (Utils.Enumerable = {}));
})(Utils = Format.Utils || (Format.Utils = {}));
})(Format || (Format = {}));
(function(Format) {
var Utils;
(function(Utils) {
var Polyfill;
(function(Polyfill) {
function indexOf(array, searchElement, fromIndex) {
if (null == array) {
throw new Format.Errors.ArgumentNullError("array");
}
fromIndex = +fromIndex || 0;
if (!Utils.Numeric.isInteger(fromIndex)) {
throw new Format.Errors.ArgumentError("Argument 'fromIndex' with value '" + fromIndex + "' must be an integer");
}
if (array.indexOf) {
return array.indexOf(searchElement, fromIndex);
} else {
return indexOfPolyfill(array, searchElement, fromIndex);
}
}
function round(value, exponent) {
if (null == value) {
throw new Format.Errors.ArgumentNullError("value");
}
exponent >>= 0;
if (!exponent || !isFinite(value)) {
return Math.round(value);
}
var sign = value >= 0 ? 1 : -1;
value = Math.round(shiftValue(value * sign, -exponent));
return shiftValue(value, exponent) * sign;
}
var shiftValue, originalToFixed, indexOfPolyfill, getIndexOfStartIndex, validateValueArgument, trimWhitespaceRegExp;
Polyfill.indexOf = indexOf;
Polyfill.round = round;
shiftValue = function(value, exponent) {
var valueParts = (value + "").split("e");
if (valueParts[1]) {
exponent += +valueParts[1];
}
return +(valueParts[0] + "e" + exponent);
};
if (0 === +.005.toFixed(2)) {
originalToFixed = Number.prototype.toFixed;
Number.prototype.toFixed = function(fractionDigits) {
return originalToFixed.call(round(this, -fractionDigits), fractionDigits) + "";
};
}
indexOfPolyfill = function(array, searchElement, fromIndex) {
var index, arrayObject = Object(array), length = arrayObject.length >>> 0;
if (0 === length || fromIndex >= length) {
return -1;
}
for (index = getIndexOfStartIndex(fromIndex, length); index < length; index += 1) {
if (index in arrayObject && arrayObject[index] === searchElement) {
return index;
}
}
return -1;
};
getIndexOfStartIndex = function(fromIndex, length) {
if (fromIndex < 0) {
fromIndex += length;
}
return Math.max(0, fromIndex);
};
Polyfill.isArray = Array.isArray || function(object) {
return Utils.getType(object) === Utils.Types.Array;
};
Polyfill.trim = "".trim ? function(value) {
validateValueArgument(value);
return value.trim();
} : function(value) {
validateValueArgument(value);
return value.replace(trimWhitespaceRegExp, "");
};
validateValueArgument = function(value) {
if (null == value) {
throw new Format.Errors.ArgumentNullError("value");
}
};
trimWhitespaceRegExp = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
})(Polyfill = Utils.Polyfill || (Utils.Polyfill = {}));
})(Utils = Format.Utils || (Format.Utils = {}));
})(Format || (Format = {}));
(function(Format) {
var Utils;
(function(Utils) {
function isObject(object) {
return Utils.getType(object) === Utils.Types.Object;
}
function isEmpty(object) {
if (!isEnumerable(object)) {
throw TypeError("Cannot call method 'isEmpty' on non-enumerable objects");
}
for (var key in object) {
if (object.hasOwnProperty(key)) {
return false;
}
}
return true;
}
var isEnumerable, validateValueAsKey, innerExtend, getDeepTarget, isArray, merge, canDeepMerge, deepMerge, getDeepMergeSource;
Utils.isObject = isObject;
Utils.isEmpty = isEmpty;
isEnumerable = function(object) {
return ("object" === typeof object || "function" === typeof object) && null !== object;
};
Utils.mapValuesAsKeys = function(object) {
var objectIsArray, result, key, value;
if (null == object) {
throw new Format.Errors.ArgumentNullError("object");
}
if ("string" === typeof object) {
throw new Format.Errors.ArgumentError("Cannot call method 'enumerateValues' on immutable string objects");
}
objectIsArray = Utils.Polyfill.isArray(object), result = objectIsArray ? [] : {};
for (key in object) {
if (object.hasOwnProperty(key)) {
value = object[key];
validateValueAsKey(object, result, value);
result[value] = objectIsArray ? +key : key;
result[key] = value;
}
}
return result;
};
validateValueAsKey = function(object, result, value) {
if (null == value) {
throw new Format.Errors.ArgumentError("Cannot call method 'enumerateValues' on objects that contain undefined or null values");
}
if (object.hasOwnProperty(value) || result.hasOwnProperty(value)) {
throw new Format.Errors.ArgumentError("Cannot enumerate value '" + value + "' because such a key already exists in " + object);
}
};
Utils.extend = function(target) {
var _i, objects = [];
for (_i = 1; _i < arguments.length; _i++) {
objects[_i - 1] = arguments[_i];
}
return innerExtend(target, objects, {
deep: false,
seen: []
});
};
Utils.deepExtend = function(target) {
var _i, objects = [];
for (_i = 1; _i < arguments.length; _i++) {
objects[_i - 1] = arguments[_i];
}
return innerExtend(target, objects, {
deep: true,
seen: []
});
};
innerExtend = function(target, objects, context) {
target = getDeepTarget(target, context);
if (!objects.length) {
throw new Format.Errors.ArgumentError("Arguments' list 'options' must contain at least one element");
}
for (var i = 0, len = objects.length; i < len; i += 1) {
if (null != objects[i]) {
merge(target, objects[i], context);
} else {}
}
return target;
};
getDeepTarget = function(target, context) {
if (!isEnumerable(target)) {
if (!context.deep) {
throw new Format.Errors.ArgumentError("Argument 'target' with value '" + target + "' must be an enumerable object instance");
}
return {};
}
return target;
};
isArray = Utils.Polyfill.isArray;
merge = function(target, object, context) {
var key, copy, objectIsArray = isArray(object);
context.seen.push(object);
for (key in object) {
context.key = key;
copy = object[key];
if (copy !== target && (!objectIsArray || object.hasOwnProperty(key))) {
if (canDeepMerge(copy, context)) {
deepMerge(target, copy, context);
} else {
if (void 0 !== copy) {
target[key] = copy;
}
}
} else {}
}
};
canDeepMerge = function(copy, context) {
return context.deep && Utils.Polyfill.indexOf(context.seen, copy) === -1 && (isObject(copy) && copy.constructor === Object || isArray(copy));
};
deepMerge = function(target, copy, context) {
var source = getDeepMergeSource(target[context.key], copy);
target[context.key] = innerExtend(source, [ copy ], context);
};
getDeepMergeSource = function(source, copy) {
if (isArray(copy)) {
return isArray(source) ? source : [];
} else {
return isObject(source) ? source : {};
}
};
})(Utils = Format.Utils || (Format.Utils = {}));
})(Format || (Format = {}));
(function(Format) {
var Globalization;
(function(Globalization) {
var DateTime;
(function(DateTime) {
var Specifiers;
(function(Specifiers) {
Specifiers.MaxSubSecondPrecision = 3;
Specifiers.Custom = Format.Utils.mapValuesAsKeys({
dayPlaceholder: "d",
zeroSubSecondPlaceholder: "f",
digitSubSecondPlaceholder: "F",
eraPlaceholder: "g",
hour12Placeholder: "h",
hour24Placeholdr: "H",
timeZonePlaceholder: "K",
minutePlaceholder: "m",
monthPlaceholder: "M",
secondPlaceholder: "s",
amPmPlaceholder: "t",
yearPlaceholder: "y",
hoursOffsetPlaceholder: "z",
timeSeparator: ":",
dateSeparator: "/",
literalStringDelimeterSingle: "'",
literalStringDelimeterDouble: '"',
singleCharFormatSpecifier: "%",
escapeChar: "\\"
});
var specifiers = Specifiers.Custom, getEscapePattern = function(escapeChar) {
return "\\" + escapeChar + ".";
}, getLiteralPattern = function(literalStringDelimeter) {
return literalStringDelimeter + "[^" + literalStringDelimeter + "]*?" + literalStringDelimeter;
};
Specifiers.CustomSpecifiersRegExp = new RegExp([ getLiteralPattern(specifiers.literalStringDelimeterSingle), getLiteralPattern(specifiers.literalStringDelimeterDouble), getEscapePattern(specifiers.singleCharFormatSpecifier), getEscapePattern(specifiers.escapeChar), specifiers.timeZonePlaceholder, specifiers.timeSeparator, specifiers.dateSeparator, specifiers.literalStringDelimeterSingle, specifiers.literalStringDelimeterDouble, "[" + [ specifiers.dayPlaceholder, specifiers.zeroSubSecondPlaceholder, specifiers.digitSubSecondPlaceholder, specifiers.eraPlaceholder, specifiers.hour12Placeholder, specifiers.hour24Placeholdr, specifiers.minutePlaceholder, specifiers.monthPlaceholder, specifiers.secondPlaceholder, specifiers.amPmPlaceholder, specifiers.yearPlaceholder, specifiers.hoursOffsetPlaceholder ].join("") + "]+" ].join("|"), "g");
})(Specifiers = DateTime.Specifiers || (DateTime.Specifiers = {}));
})(DateTime = Globalization.DateTime || (Globalization.DateTime = {}));
})(Globalization = Format.Globalization || (Format.Globalization = {}));
})(Format || (Format = {}));
(function(Format) {
var Errors;
(function(Errors) {
var FormatError = function(_super) {
function FormatError(message, innerError) {
_super.call(this, message, innerError);
this.name = "FormatError";
}
__extends(FormatError, _super);
return FormatError;
}(Errors.SystemError);
Errors.FormatError = FormatError;
})(Errors = Format.Errors || (Format.Errors = {}));
})(Format || (Format = {}));
(function(Format) {
var Globalization;
(function(Globalization) {
var DateTime;
(function(DateTime) {
var padding, pad, pad2, customSpecifiers, InfoSpecifierFormatter = function() {
function InfoSpecifierFormatter(formatInfo) {
var _this = this;
this.formatSpecifier_ = function(specifierMatch) {
return _this.formatters[customSpecifiers[specifierMatch[0]]](specifierMatch.length, specifierMatch) + "";
};
this.formatters = {
dayPlaceholder: function(specifierCount) {
switch (specifierCount) {
case 1:
case 2:
return pad(_this.value.getDate(), specifierCount);
case 3:
return _this.formatInfo_.AbbreviatedDayNames[_this.value.getDay()];
default:
return _this.formatInfo_.DayNames[_this.value.getDay()];
}
},
zeroSubSecondPlaceholder: function(specifierCount) {
return pad(_this.getSubSecond_(specifierCount, customSpecifiers.digitSubSecondPlaceholder), specifierCount);
},
digitSubSecondPlaceholder: function(specifierCount) {
var subSecond = _this.getSubSecond_(specifierCount, customSpecifiers.zeroSubSecondPlaceholder);
return subSecond ? pad(subSecond, specifierCount) : "";
},
eraPlaceholder: function() {
return _this.value.getFullYear() < 0 ? "B.C." : "A.D.";
},
hour12Placeholder: function(specifierCount) {
return pad2(_this.value.getHours() % 12 || 12, specifierCount);
},
hour24Placeholdr: function(specifierCount) {
return pad2(_this.value.getHours(), specifierCount);
},
timeZonePlaceholder: function() {
return _this.getHoursOffset_() + ":" + pad2(Math.abs(_this.value.getTimezoneOffset() % 60));
},
minutePlaceholder: function(specifierCount) {
return pad2(_this.value.getMinutes(), specifierCount);
},
monthPlaceholder: function(specifierCount) {
switch (specifierCount) {
case 1:
case 2:
return pad(_this.value.getMonth() + 1, specifierCount);
case 3:
return _this.formatInfo_.AbbreviatedMonthNames[_this.value.getMonth()];
default:
return _this.formatInfo_.MonthNames[_this.value.getMonth()];
}
},
secondPlaceholder: function(specifierCount) {
return pad2(_this.value.getSeconds(), specifierCount);
},
amPmPlaceholder: function(specifierCount) {
return (_this.value.getHours() < 12 ? _this.formatInfo_.AMDesignator : _this.formatInfo_.PMDesignator).substr(0, 1 === specifierCount ? 1 : void 0);
},
yearPlaceholder: function(specifierCount) {
var year = _this.value.getFullYear();
if (specifierCount <= 2) {
year %= 100;
}
return pad(year, specifierCount);
},
hoursOffsetPlaceholder: function(specifierCount) {
switch (specifierCount) {
case 1:
case 2:
return _this.getHoursOffset_(specifierCount);
default:
return _this.formatters.timeZonePlaceholder();
}
},
timeSeparator: function() {
return _this.formatInfo_.TimeSeparator;
},
dateSeparator: function() {
return _this.formatInfo_.DateSeparator;
},
literalStringDelimeterSingle: function(matchLength, specifierMatch) {
if (1 === matchLength) {
throw new Format.Errors.FormatError("Cannot find a matching quote character for the character '" + specifierMatch + "'");
}
return specifierMatch.substring(1, matchLength - 1);
},
literalStringDelimeterDouble: function(matchLength, specifierMatch) {
return _this.formatters.literalStringDelimeterSingle(matchLength, specifierMatch);
},
singleCharFormatSpecifier: function(matchLength, specifierMatch) {
var specifier = specifierMatch[1];
if (specifier === customSpecifiers.singleCharFormatSpecifier) {
throw new Format.Errors.FormatError("Specifier combination '" + specifier + specifier + "' is not valid");
}
return customSpecifiers[specifier] ? _this.formatSpecifier_(specifier) : specifier;
},
escapeChar: function(matchLength, specifierMatch) {
return specifierMatch[1];
}
};
if (null == formatInfo) {
throw new Format.Errors.ArgumentNullError("formatInfo");
}
this.formatInfo_ = formatInfo;
}
InfoSpecifierFormatter.prototype.format = function(format, value) {
this.value = value;
return format.replace(DateTime.Specifiers.CustomSpecifiersRegExp, this.formatSpecifier_);
};
InfoSpecifierFormatter.prototype.getSubSecond_ = function(precision, subSecondPlaceholder) {
var maxPrecision = DateTime.Specifiers.MaxSubSecondPrecision;
if (precision > maxPrecision) {
throw new Format.Errors.FormatError("Date and time format specifier '" + subSecondPlaceholder + "' cannot be used more than " + maxPrecision + " times");
}
return Math.floor(this.value.getMilliseconds() / Math.pow(10, maxPrecision - precision));
};
InfoSpecifierFormatter.prototype.getHoursOffset_ = function(totalWidth) {
if (totalWidth === void 0) {
totalWidth = 2;
}
return Format.innerComponentFormat("+" + pad(0, totalWidth) + ";-" + pad(0, totalWidth), Math.floor(-this.value.getTimezoneOffset() / 60));
};
return InfoSpecifierFormatter;
}();
DateTime.InfoSpecifierFormatter = InfoSpecifierFormatter;
padding = Format.Utils.Padding, pad = function(value, totalWidth) {
return padding.pad(value + "", {
totalWidth: totalWidth,
paddingChar: "0",
direction: padding.Direction.Left
});
}, pad2 = function(value, totalWidth) {
if (totalWidth === void 0) {
totalWidth = 2;
}
return pad(value, Math.min(totalWidth, 2));
}, customSpecifiers = DateTime.Specifiers.Custom;
})(DateTime = Globalization.DateTime || (Globalization.DateTime = {}));
})(Globalization = Format.Globalization || (Format.Globalization = {}));
})(Format || (Format = {}));
(function(Format) {
var Errors;
(function(Errors) {
var InvalidOperationError = function(_super) {
function InvalidOperationError(message, innerError) {
_super.call(this, message, innerError);
this.name = "InvalidOperationError";
}
__extends(InvalidOperationError, _super);
return InvalidOperationError;
}(Errors.SystemError);
Errors.InvalidOperationError = InvalidOperationError;
})(Errors = Format.Errors || (Format.Errors = {}));
})(Format || (Format = {}));
(function(Format) {
var Globalization;
(function(Globalization) {
var DateTime;
(function(DateTime) {
var InfoFormatter = function() {
function InfoFormatter(optionsProviderConstructor, formatInfo, options) {
var _this = this;
this.formatters_ = {
shortDate: function() {
return _this.specifiersFormatter_.format("MM/dd/yyyy", _this.value_);
},
longDate: function() {
return _this.specifiersFormatter_.format("dddd, dd MMMM yyyy", _this.value_);
},
fullDateShortTime: function() {
return _this.formatters_.longDate() + " " + _this.formatters_.shortTime();
},
fullDateLongTime: function() {
return _this.formatters_.longDate() + " " + _this.formatters_.longTime();
},
generalDateShortTime: function() {
return _this.formatters_.shortDate() + " " + _this.formatters_.shortTime();
},
generalDateLongTime: function() {
return _this.formatters_.shortDate() + " " + _this.formatters_.longTime();
},
monthDate: function() {
return _this.specifiersFormatter_.format("MMMM dd", _this.value_);
},
roundTrip: function() {
var result = JSON.stringify(_this.value_);
return result.substring(1, result.length - 1);
},
rfc1123: function() {
return _this.value_.toUTCString();
},
sortable: function() {
return _this.specifiersFormatter_.format("yyyy-MM-ddTHH':'mm':'ss", _this.value_);
},
shortTime: function() {
return _this.specifiersFormatter_.format("HH:mm", _this.value_);
},
longTime: function() {
return _this.specifiersFormatter_.format("HH:mm:ss", _this.value_);
},
universalSortable: function() {
return _this.specifiersFormatter_.format("yyyy-MM-dd HH':'mm':'ssZ", _this.value_);
},
universalFull: function() {
return _this.formatters_.fullDateLongTime();
},
yearMonth: function() {
return _this.specifiersFormatter_.format("yyyy MMMM", _this.value_);
}
};
if ("function" !== typeof optionsProviderConstructor) {
throw new TypeError("Cannot create an instance without a concrete options provider's constructor");
}
if (null == formatInfo) {
throw new Format.Errors.ArgumentNullError("formatInfo");
}
this.optionsProviderConstructor_ = optionsProviderConstructor;
this.formatInfo = formatInfo;
this.baseOptions_ = options || {};
}
InfoFormatter.prototype.format = function(format, value) {
try {
return this.innerFormat_(format, value);
} finally {
this.cleanup_();
}
};
InfoFormatter.prototype.applyOptions = function(value) {
var style = this.optionsProvider_.getStyle();
if (this.formatters_.hasOwnProperty(style)) {
return this.formatters_[style]();
}
throw new Format.Errors.ArgumentError("Option 'style' with base or resolved value '" + style + "' is not supported");
};
InfoFormatter.prototype.getSpecifiersFormatter = function() {
return new DateTime.InfoSpecifierFormatter(this.formatInfo);
};
InfoFormatter.prototype.innerFormat_ = function(format, value) {
if (!format) {
format = DateTime.Specifiers.Standard.generalDateLongTime;
}
this.optionsProvider_ = new this.optionsProviderConstructor_(this.baseOptions_);
this.resolvedOptions = this.optionsProvider_.resolveOptions(format, value);
if (!Format.Utils.isObject(this.resolvedOptions)) {
throw new Format.Errors.InvalidOperationError("Invocation of 'optionsProvider' member's method 'resolveOptions' did not initialize instance member 'resolvedOptions' properly");
}
this.setValue_(value);
this.specifiersFormatter_ = this.getSpecifiersFormatter();
return this.optionsProvider_.getStyle() ? this.applyOptions(this.value_) : this.specifiersFormatter_.format(format, this.value_);
};
InfoFormatter.prototype.setValue_ = function(value) {
this.value_ = value;
if (this.optionsProvider_.useUTC()) {
this.value_ = new Date(this.value_.getTime() + 6e4 * this.value_.getTimezoneOffset());
}
};
InfoFormatter.prototype.cleanup_ = function() {
delete this.resolvedOptions;
delete this.value_;
delete this.optionsProvider_;
delete this.specifiersFormatter_;
};
return InfoFormatter;
}();
DateTime.InfoFormatter = InfoFormatter;
})(DateTime = Globalization.DateTime || (Globalization.DateTime = {}));
})(Globalization = Format.Globalization || (Format.Globalization = {}));
})(Format || (Format = {}));
(function(Format) {
var Globalization;
(function(Globalization) {
var DateTime;
(function(DateTime) {
var Specifiers;
(function(Specifiers) {
Specifiers.Standard = Format.Utils.mapValuesAsKeys({
shortDate: "d",
longDate: "D",
fullDateShortTime: "f",
fullDateLongTime: "F",
generalDateShortTime: "g",
generalDateLongTime: "G",
monthDate: "M",
roundTrip: "O",
rfc1123: "R",
sortable: "s",
shortTime: "t",
longTime: "T",
universalSortable: "u",
universalFull: "U",
yearMonth: "Y"
});
})(Specifiers = DateTime.Specifiers || (DateTime.Specifiers = {}));
})(DateTime = Globalization.DateTime || (Globalization.DateTime = {}));
})(Globalization = Format.Globalization || (Format.Globalization = {}));
})(Format || (Format = {}));
(function(Format) {
var Utils;
(function(Utils) {
var createExtendObject, createCloneFunction = function(cloneFunc) {
return function(object, deep) {
var objectIsArray = Utils.Polyfill.isArray(object);
if (objectIsArray || Utils.isObject(object)) {
return cloneFunc(object, deep, objectIsArray);
} else {
if (Utils.isType("Date", object)) {
return new Date(object.getTime());
}
}
return object;
};
};
Utils.clone = createCloneFunction(function(object, deep, objectIsArray) {
return deep ? Utils.deepExtend(createExtendObject(object, objectIsArray), object) : Utils.extend(createExtendObject(object, objectIsArray), object);
});
createExtendObject = function(object, objectIsArray) {
return objectIsArray ? [] : {};
};
Utils.fastClone = createCloneFunction(function(object) {
return JSON.parse(JSON.stringify(object));
});
})(Utils = Format.Utils || (Format.Utils = {}));
})(Format || (Format = {}));
(function(Format) {
var Utils;
(function(Utils) {
var removeProperty, innerRemoveProperty, removePredicates = {
undefined: function(value) {
return null == value;
},
"": function(value) {
return removePredicates.undefined(value) || "" === value;
},
0: function(value) {
return !value;
}
}, createRemoveFunction = function(predicateKey) {
var context = {
removePredicate: removePredicates[predicateKey + ""]
};
return function(object, deep) {
context.seen = [];
context.deep = deep;
return removeProperty(object, context);
};
};
Utils.removeUndefined = createRemoveFunction(void 0);
Utils.removeEmpty = createRemoveFunction("");
Utils.removeFalsy = createRemoveFunction(0);
removeProperty = function(object, context) {
var objectIsArray = Utils.Polyfill.isArray(object);
if (objectIsArray || Utils.isObject(object)) {
context.seen.push(object);
for (context.key in object) {
if (object.hasOwnProperty(context.key)) {
innerRemoveProperty(object, context);
}
}
}
return objectIsArray ? Utils.Enumerable.compact(object) : object;
};
innerRemoveProperty = function(object, context) {
var value = object[context.key];
if (context.removePredicate(value)) {
delete object[context.key];
} else {
if (context.deep && Utils.Polyfill.indexOf(context.seen, value) === -1) {
removeProperty(value, context);
}
}
};
})(Utils = Format.Utils || (Format.Utils = {}));
})(Format || (Format = {}));
(function(Format) {
var Utils;
(function(Utils) {
var Function;
(function(Function) {
function getName(func) {
validateFunctionArgument(func, "getName");
if (void 0 !== func.name) {
return func.name;
}
var typeNameGroups = typeNameRegExp.exec(func.toString());
return typeNameGroups && typeNameGroups[1] ? typeNameGroups[1] : "";
}
function getReturnName(func) {
validateFunctionArgument(func, "getReturnName");
var returnNameGroups = returnNameRegExp.exec(func.toString());
if (returnNameGroups) {
return returnNameGroups[1];
} else {
return void 0;
}
}
function getEmpty() {
return empty;
}
var typeNameRegExp, returnNameRegExp, validateFunctionArgument, empty;
Function.getName = getName;
typeNameRegExp = /function +(\w+)/;
Function.getReturnName = getReturnName;
returnNameRegExp = /(?:(?:=>)|(?:return\s))\s*(?:\w+\.)*([A-Za-z]+)/;
validateFunctionArgument = function(func, methodName) {
if ("func