UNPKG

1.57 kBJavaScriptView Raw
1/*
2Copyright (c) 2014, Yahoo! Inc. All rights reserved.
3Copyrights licensed under the New BSD License.
4See the accompanying LICENSE file for terms.
5*/
6var __spreadArrays = (this && this.__spreadArrays) || function () {
7 for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
8 for (var r = Array(s), k = 0, i = 0; i < il; i++)
9 for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
10 r[k] = a[j];
11 return r;
12};
13// -- Utilities ----------------------------------------------------------------
14function getCacheId(inputs) {
15 return JSON.stringify(inputs.map(function (input) {
16 return input && typeof input === 'object' ? orderedProps(input) : input;
17 }));
18}
19function orderedProps(obj) {
20 return Object.keys(obj)
21 .sort()
22 .map(function (k) {
23 var _a;
24 return (_a = {}, _a[k] = obj[k], _a);
25 });
26}
27var memoizeFormatConstructor = function (FormatConstructor, cache) {
28 if (cache === void 0) { cache = {}; }
29 return function () {
30 var _a;
31 var args = [];
32 for (var _i = 0; _i < arguments.length; _i++) {
33 args[_i] = arguments[_i];
34 }
35 var cacheId = getCacheId(args);
36 var format = cacheId && cache[cacheId];
37 if (!format) {
38 format = new ((_a = FormatConstructor).bind.apply(_a, __spreadArrays([void 0], args)))();
39 if (cacheId) {
40 cache[cacheId] = format;
41 }
42 }
43 return format;
44 };
45};
46export default memoizeFormatConstructor;