This repository has been archived by the owner on May 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor into a Intl format memoizer
**This is a major-version level refactor.** This refactor proposes a shift in the role of this package in the overall FormatJS system to a memoizer of `Intl*` format instances. These change mean that this package no longer has dependencies on the `intl-*` packages, as the template/component integration layers should be the packages with those dependencies.
- Loading branch information
Showing
7 changed files
with
119 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
module.exports = require('./lib/formats'); | ||
exports = module.exports = require('./lib/memoizer').default; | ||
Object.defineProperty(exports, 'default', {value: exports}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Copyright (c) 2014, Yahoo! Inc. All rights reserved. | ||
Copyrights licensed under the New BSD License. | ||
See the accompanying LICENSE file for terms. | ||
*/ | ||
|
||
/* jslint esnext: true */ | ||
|
||
export {objCreate}; | ||
|
||
// Purposely using the same implementation as the Intl.js `Intl` polyfill. | ||
// Copyright 2013 Andy Earnshaw, MIT License | ||
|
||
var hop = Object.prototype.hasOwnProperty; | ||
|
||
var objCreate = Object.create || function (proto, props) { | ||
var obj, k; | ||
|
||
function F() {} | ||
F.prototype = proto; | ||
obj = new F(); | ||
|
||
for (k in props) { | ||
if (hop.call(props, k)) { | ||
defineProperty(obj, k, props[k]); | ||
} | ||
} | ||
|
||
return obj; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
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 {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 = cache[cacheId]; | ||
|
||
if (!format) { | ||
format = objCreate(FormatConstructor.prototype); | ||
FormatConstructor.apply(format, 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 (!JSON) { 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; | ||
} |