Skip to content

Commit

Permalink
Bundle: Enhcance bundleMapQueue logic
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
morwoen authored Aug 5, 2020
1 parent 9efaa3c commit b996f05
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/bundle/lookup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import coreLikelySubtags from "../core/likely_subtags";
import coreRemoveLikelySubtags from "../core/remove_likely_subtags";
import coreSubtags from "../core/subtags";
import arrayForEach from "../util/array/for_each";

/**
* bundleLookup( minLanguageId )
Expand All @@ -17,19 +16,27 @@ export default function(Cldr, cldr, minLanguageId) {
availableBundleMapQueue = Cldr._availableBundleMapQueue;

if (availableBundleMapQueue.length) {
arrayForEach(availableBundleMapQueue, function(bundle) {
while (availableBundleMapQueue.length > 0) {
const bundle = availableBundleMapQueue.shift();
if (!bundle) {
break;
}

var existing, maxBundle, minBundle, subtags;
subtags = coreSubtags(bundle);
maxBundle = coreLikelySubtags(Cldr, cldr, subtags);
if (typeof maxBundle === "undefined") {
throw new Error(`Could not find likelySubtags for ${bundle}`);
}

minBundle = coreRemoveLikelySubtags(Cldr, cldr, maxBundle);
minBundle = minBundle.join(Cldr.localeSep);
existing = availableBundleMap[minBundle];
if (existing && existing.length < bundle.length) {
return;
}
availableBundleMap[minBundle] = bundle;
});
Cldr._availableBundleMapQueue = [];
}
}

return availableBundleMap[minLanguageId] || null;
Expand Down
31 changes: 31 additions & 0 deletions test/unit/bundle/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,35 @@ describe("Bundle Lookup", function() {
sr = new Cldr("sr-RS");
expect(sr.attributes.bundle).to.equal("sr");
});

it("should remove problematic bundle from the global _availableBundleMapQueue on failure", function() {
Cldr.load(
{
main: { bg: {} } // valid
},
{
main: { xx: {} } // invalid
},
{
main: { sr: {} } // valid
}
);

expect(Cldr._availableBundleMapQueue).to.eql(["bg", "xx", "sr"]);

expect(() => {
// triggers the loading of bundle queue
new Cldr("bg");
}).to.throw();

expect(Cldr._availableBundleMapQueue).to.eql(["sr"]);

// the invalid bundle has been removed so we can load bg
new Cldr("bg");

// and sr, which will now trigger the loading of the rest of the queue
new Cldr("sr");

expect(Cldr._availableBundleMapQueue).to.eql([]);
});
});

0 comments on commit b996f05

Please sign in to comment.