Browse Source

Merge pull request #38 from MiningCryptoLive/patch-6

Create memoizer.js
master
yuriy0803 3 years ago committed by GitHub
parent
commit
93f9d2e578
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 81
      www/fix/intl-format-cache/src/memoizer.js

81
www/fix/intl-format-cache/src/memoizer.js vendored

@ -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;
}
Loading…
Cancel
Save