Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Late review... This should also be break.

}
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([]);
});
});