Skip to content

Commit

Permalink
Bundle: Enhcance bundleMapQueue logic (amend)
Browse files Browse the repository at this point in the history
This amends commit b996f05.

Ref #70
  • Loading branch information
rxaviers committed Aug 5, 2020
1 parent 754a275 commit 2bac803
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
15 changes: 6 additions & 9 deletions src/bundle/lookup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -16,27 +17,23 @@ export default function(Cldr, cldr, minLanguageId) {
availableBundleMapQueue = Cldr._availableBundleMapQueue;

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

arrayForEach(availableBundleMapQueue, function(bundle, i) {
var existing, maxBundle, minBundle, subtags;
subtags = coreSubtags(bundle);
maxBundle = coreLikelySubtags(Cldr, cldr, subtags);
if (typeof maxBundle === "undefined") {
if (maxBundle === undefined) {
availableBundleMapQueue.splice(i, 1);
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
2 changes: 1 addition & 1 deletion test/unit/bundle/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe("Bundle Lookup", function() {
new Cldr("bg");
}).to.throw();

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

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

0 comments on commit 2bac803

Please sign in to comment.