Skip to content
This repository has been archived by the owner on May 26, 2019. It is now read-only.

Commit

Permalink
Refactor into a Intl format memoizer
Browse files Browse the repository at this point in the history
**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
ericf committed Sep 18, 2014
1 parent 121dd75 commit ce3b4c3
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 134 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function (grunt) {
tmp: {
expand : true,
flatten: true,
src : 'tmp/src/*.*',
src : 'tmp/src/*.js',
dest : 'lib/'
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Produces instances of JavaScript `Intl` formats, and caches them for reuse.
Overview
--------

This is an helper package used within [Yahoo's IntlJS suite][IntlJS]. It provides a cache layer for creating [`Intl`][Intl] format instances: [`IntlNumberFormat`][Intl-NF], [`Intl.DateTimeFormat`][Intl-DTF], and [`IntlMessageFormat`][Intl-MF].
This is an helper package used within [Yahoo's IntlJS suite][IntlJS]. It provides a cache/memoize layer for creating [`Intl`][Intl] format instances: [`IntlNumberFormat`][Intl-NF], [`Intl.DateTimeFormat`][Intl-DTF], and [`IntlMessageFormat`][Intl-MF].


License
Expand Down
3 changes: 2 additions & 1 deletion index.js
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});
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "Produces instances of JavaScript `Intl` formats, and caches them for reuse.",
"main": "index.js",
"jsnext:main": "src/formats.js",
"jsnext:main": "src/memoizer.js",
"scripts": {
"prepublish": "grunt",
"test": "echo \"Error: no test specified\" && exit 1"
Expand All @@ -22,15 +22,12 @@
"url": "https://github.com/yahoo/intl-format-cache/issues"
},
"homepage": "https://github.com/yahoo/intl-format-cache",
"dependencies": {
"intl-messageformat": "^1.0.0-rc-2"
},
"dependencies": {},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-bundle-jsnext-lib": "^0.2.1",
"grunt-cli": "^0.1.13",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-copy": "^0.5.0",
"intl": "^0.1.4"
"grunt-contrib-copy": "^0.6.0"
}
}
30 changes: 30 additions & 0 deletions src/es5.js
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;
};
125 changes: 0 additions & 125 deletions src/formats.js

This file was deleted.

82 changes: 82 additions & 0 deletions src/memoizer.js
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;
}

0 comments on commit ce3b4c3

Please sign in to comment.