Skip to content

Commit

Permalink
Switch to hash.js from sha1 (#67)
Browse files Browse the repository at this point in the history
* Switch to hash.js from sha1

Hash.js pulling just sha1 is 2.3k gzipped, while sha1 is 8k. Switch to save some bytes and align with what calypso uses.

* Add tests for hashed lookups
  • Loading branch information
blowery authored and sirreal committed Dec 6, 2018
1 parent 3708d82 commit 778473b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 26 deletions.
13 changes: 7 additions & 6 deletions packages/i18n-calypso/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var debug = require( 'debug' )( 'i18n-calypso' ),
Jed = require( 'jed' ),
moment = require( 'moment-timezone' ),
sha1 = require( 'sha1' ),
sha1 = require( 'hash.js/lib/hash/sha/1' ),
EventEmitter = require( 'events' ).EventEmitter,
interpolateComponents = require( 'interpolate-components' ).default,
LRU = require( 'lru' ),
Expand Down Expand Up @@ -201,16 +201,17 @@ I18N.prototype.setLocale = function( localeData ) {
var hashLength, minHashLength, maxHashLength, keyHash = localeData[ '' ][ 'key-hash' ];

var transform = function( string, hashLength ) {
if ( typeof hashCache[ hashLength + string ] !== 'undefined' ) {
return hashCache[ hashLength + string ];
const lookupPrefix = hashLength === false ? '' : String( hashLength );
if ( typeof hashCache[ lookupPrefix + string ] !== 'undefined' ) {
return hashCache[ lookupPrefix + string ];
}
var hash = sha1( string );
var hash = sha1().update( string ).digest('hex');

if ( hashLength ) {
return hashCache[ hashLength + string ] = hash.substr( 0, hashLength );
return hashCache[ lookupPrefix + string ] = hash.substr( 0, hashLength );
}

return hashCache[ hashLength + string ] = hash;
return hashCache[ lookupPrefix + string ] = hash;
};

var generateLookup = function( hashLength ) {
Expand Down
33 changes: 14 additions & 19 deletions packages/i18n-calypso/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/i18n-calypso/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"create-react-class": "^15.6.2",
"debug": "^3.1.0",
"globby": "^6.1.0",
"hash.js": "^1.1.5",
"interpolate-components": "1.1.1",
"jed": "1.0.2",
"jstimezonedetect": "1.0.5",
Expand All @@ -32,7 +33,6 @@
"lru": "^3.1.0",
"moment-timezone": "0.5.11",
"react": "0.14.8 || ^15.5.0 || ^16.0.0",
"sha1": "^1.1.1",
"xgettext-js": "^2.0.0"
},
"devDependencies": {
Expand Down
45 changes: 45 additions & 0 deletions packages/i18n-calypso/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,49 @@ describe( 'I18n', function() {
} );
} );
} );

describe( 'hashed locale data', function() {
it( 'should find keys when looked up by simple hash', function() {
i18n.setLocale( {
'': {
localeSlug: 'xx-pig-latin',
'key-hash': 'sha1'
},
'0f7d0d088b6ea936fb25b477722d734706fe8b40': [
null,
'implesa'
]
});
assert.equal( i18n.translate( 'simple' ), 'implesa' );
} );

it( 'should find keys when looked up by single length hash', function() {
i18n.setLocale( {
'': {
localeSlug: 'xx-pig-latin',
'key-hash': 'sha1-1'
},
'0': [
null,
'implesa'
]
});
assert.equal( i18n.translate( 'simple' ), 'implesa' );
} );

it( 'should find keys when looked up by multi length hash', function() {
i18n.setLocale( {
'': {
localeSlug: 'xx-pig-latin',
'key-hash': 'sha1-1-2'
},
'0': [ null,'implesa' ],
'78': [ null, 'edra' ], // red has a sha1 of 78988010b890ce6f4d2136481f392787ec6d6106
'7d': [ null, 'reyga' ] // grey has a sha1 of 7d1f8f911da92c0ea535cad461fd773281a79638
});
assert.equal( i18n.translate( 'simple' ), 'implesa' );
assert.equal( i18n.translate( 'red' ), 'edra' );
assert.equal( i18n.translate( 'grey' ), 'reyga' );
} );
} );
} );

0 comments on commit 778473b

Please sign in to comment.