From 9cdb68de6a44ce47dca526ed6ced6bc38c36df5a Mon Sep 17 00:00:00 2001 From: MiningCryptoLive Date: Mon, 29 Aug 2022 12:21:41 -0400 Subject: [PATCH] Create memoizer.js --- www/fix/intl-format-cache/src/memoizer.js | 81 +++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 www/fix/intl-format-cache/src/memoizer.js diff --git a/www/fix/intl-format-cache/src/memoizer.js b/www/fix/intl-format-cache/src/memoizer.js new file mode 100644 index 0000000..3627be2 --- /dev/null +++ b/www/fix/intl-format-cache/src/memoizer.js @@ -0,0 +1,81 @@ +/* +Copyright (c) 2014, Yahoo! Inc. All rights reserved. +Copyrights licensed under the New BSD License. +See the accompanying LICENSE file for terms. +*/ + +/* jshint esnext: true */ + +import {bind, objCreate} from './es5'; + +export default createFormatCache; + +// ----------------------------------------------------------------------------- + +function createFormatCache(FormatConstructor) { + var cache = objCreate(null); + + return function () { + var args = Array.prototype.slice.call(arguments); + var cacheId = getCacheId(args); + var format = cacheId && cache[cacheId]; + + if (!format) { + format = new (bind.apply(FormatConstructor, [null].concat(args)))(); + + if (cacheId) { + cache[cacheId] = format; + } + } + + return format; + }; +} + +// -- Utilities ---------------------------------------------------------------- + +function getCacheId(inputs) { + // When JSON is not available in the runtime, we will not create a cache id. + if (typeof JSON === 'undefined') { return; } + + var cacheId = []; + + var i, len, input; + + for (i = 0, len = inputs.length; i < len; i += 1) { + input = inputs[i]; + + if (input && typeof input === 'object') { + cacheId.push(orderedProps(input)); + } else { + cacheId.push(input); + } + } + + return JSON.stringify(cacheId); +} + +function orderedProps(obj) { + var props = [], + keys = []; + + var key, i, len, prop; + + for (key in obj) { + if (obj.hasOwnProperty(key)) { + keys.push(key); + } + } + + var orderedKeys = keys.sort(); + + for (i = 0, len = orderedKeys.length; i < len; i += 1) { + key = orderedKeys[i]; + prop = {}; + + prop[key] = obj[key]; + props[i] = prop; + } + + return props; +}