@forge/cli-shared

9.4.0-next.4-experimental-c5755749.4.0-next.6
out/ui/hint-rotation.js
+out/ui/hint-rotation.jsNew file
+27
Index: package/out/ui/hint-rotation.js
===================================================================
--- package/out/ui/hint-rotation.js
+++ package/out/ui/hint-rotation.js
@@ -0,0 +1,27 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.selectHintsForCategory = selectHintsForCategory;
+exports.createHintRotation = createHintRotation;
+const HINT_CATEGORY_ALL = 'all';
+function selectHintsForCategory(hints, category) {
+    return hints.filter(({ categories }) => category === HINT_CATEGORY_ALL || categories.includes(category) || categories.includes(HINT_CATEGORY_ALL));
+}
+function createHintRotation(hints, random = Math.random) {
+    const remaining = [...hints];
+    return () => {
+        const total = remaining.reduce((sum, { weight }) => sum + weight, 0);
+        if (total <= 0) {
+            return undefined;
+        }
+        const target = random() * total;
+        let cumulative = 0;
+        for (const [index, hint] of remaining.entries()) {
+            cumulative += hint.weight;
+            if (target < cumulative) {
+                remaining.splice(index, 1);
+                return hint.text;
+            }
+        }
+        return remaining.pop().text;
+    };
+}