Skip to content

Commit 64dd766

Browse files
morwoenrxaviers
authored andcommitted
Bundle: Enhcance bundleMapQueue logic
* Fix bug where bundleMapQueue is not cleared after an error * Fix linter errors * Updated lookup logic for processing _availableBundleMapQueue to use .shift instead of .forEach Signed-off-by: Georgi Tsaklev <[email protected]>
1 parent 2dcd374 commit 64dd766

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

src/bundle/lookup.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ define([
22
"../core/likely_subtags",
33
"../core/remove_likely_subtags",
44
"../core/subtags",
5-
"../util/array/for_each"
6-
], function( coreLikelySubtags, coreRemoveLikelySubtags, coreSubtags, arrayForEach ) {
5+
], function( coreLikelySubtags, coreRemoveLikelySubtags, coreSubtags ) {
76

87
/**
98
* bundleLookup( minLanguageId )
@@ -18,20 +17,28 @@ define([
1817
var availableBundleMap = Cldr._availableBundleMap,
1918
availableBundleMapQueue = Cldr._availableBundleMapQueue;
2019

21-
if ( availableBundleMapQueue.length ) {
22-
arrayForEach( availableBundleMapQueue, function( bundle ) {
20+
if (availableBundleMapQueue.length) {
21+
while (availableBundleMapQueue.length > 0) {
22+
const bundle = availableBundleMapQueue.shift();
23+
if (!bundle) {
24+
break;
25+
}
26+
2327
var existing, maxBundle, minBundle, subtags;
24-
subtags = coreSubtags( bundle );
25-
maxBundle = coreLikelySubtags( Cldr, cldr, subtags );
26-
minBundle = coreRemoveLikelySubtags( Cldr, cldr, maxBundle );
27-
minBundle = minBundle.join( Cldr.localeSep );
28-
existing = availableBundleMap[ minBundle ];
29-
if ( existing && existing.length < bundle.length ) {
28+
subtags = coreSubtags(bundle);
29+
maxBundle = coreLikelySubtags(Cldr, cldr, subtags);
30+
if (typeof maxBundle === "undefined") {
31+
throw new Error(`Could not find likelySubtags for ${bundle}`);
32+
}
33+
34+
minBundle = coreRemoveLikelySubtags(Cldr, cldr, maxBundle);
35+
minBundle = minBundle.join(Cldr.localeSep);
36+
existing = availableBundleMap[minBundle];
37+
if (existing && existing.length < bundle.length) {
3038
return;
3139
}
32-
availableBundleMap[ minBundle ] = bundle;
33-
});
34-
Cldr._availableBundleMapQueue = [];
40+
availableBundleMap[minBundle] = bundle;
41+
}
3542
}
3643

3744
return availableBundleMap[ minLanguageId ] || null;

test/unit/bundle/lookup.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,51 @@ define([
5656

5757
});
5858

59+
it("should always favor the grandest parent", function() {
60+
var sr;
61+
Cldr.load({
62+
main: { sr: {} }
63+
});
64+
expect(Cldr._availableBundleMapQueue).to.include.members(["sr"]);
65+
sr = new Cldr("sr");
66+
expect(Cldr._availableBundleMapQueue).to.eql([]);
67+
expect(Cldr._availableBundleMap).to.contain.keys("sr");
68+
expect(Cldr._availableBundleMap.sr).to.equal("sr");
69+
expect(sr.attributes.bundle).to.equal("sr");
70+
sr = new Cldr("sr-Cyrl");
71+
expect(sr.attributes.bundle).to.equal("sr");
72+
sr = new Cldr("sr-RS");
73+
expect(sr.attributes.bundle).to.equal("sr");
74+
});
75+
76+
it("should remove problematic bundle from the global _availableBundleMapQueue on failure", function() {
77+
Cldr.load(
78+
{
79+
main: { bg: {} } // valid
80+
},
81+
{
82+
main: { xx: {} } // invalid
83+
},
84+
{
85+
main: { sr: {} } // valid
86+
}
87+
);
88+
89+
expect(Cldr._availableBundleMapQueue).to.eql(["bg", "xx", "sr"]);
90+
91+
expect(() => {
92+
// triggers the loading of bundle queue
93+
new Cldr("bg");
94+
}).to.throw();
95+
96+
expect(Cldr._availableBundleMapQueue).to.eql(["sr"]);
97+
98+
// the invalid bundle has been removed so we can load bg
99+
new Cldr("bg");
100+
101+
// and sr, which will now trigger the loading of the rest of the queue
102+
new Cldr("sr");
103+
104+
expect(Cldr._availableBundleMapQueue).to.eql([]);
105+
});
59106
});

0 commit comments

Comments
 (0)