{"version":3,"file":"index.esm.mjs","sources":["../src/index.ts"],"sourcesContent":["/**\n * 循環顏色生成器\n * Loop Colors Generator\n *\n * 提供顏色陣列的循環迭代功能，支援隨機順序與自定義生成器。\n * Provides circular iteration over color arrays with random order and custom generator support.\n *\n * @module loop-colors\n */\nimport { ITSArrayListMaybeReadonly } from 'ts-type/lib/type/base';\n\n/**\n * CSS 預設顏色列表\n * Default CSS color palette\n *\n * 包含 7 種基本顏色的十六進位色碼。\n * Contains 7 basic color hex codes.\n *\n * @returns 只讀顏色陣列 / Readonly color array\n */\nexport function cssColors()\n{\n\treturn [\n\t\t'#906',\n\t\t'#66F',\n\t\t'#800',\n\t\t'#C60',\n\t\t'#EAEA00',\n\t\t'#006',\n\t\t'#360',\n\t] as const;\n}\n\n/**\n * CLI 終端機顏色類型\n * CLI terminal color type\n *\n * 終端機可用的標準顏色名稱。\n * Standard color names available in terminal.\n */\nexport type ICliColor = \"cyan\" | \"magenta\" | \"blue\" | \"yellow\" | \"green\" | \"red\";\n\n/**\n * CLI 終端機顏色列表\n * CLI terminal color list\n *\n * @returns 終端機顏色名稱陣列 / Array of terminal color names\n */\nexport function cliColors(): ICliColor[]\n{\n\treturn [\"cyan\", \"magenta\", \"blue\", \"yellow\", \"green\", \"red\"];\n}\n\n/**\n * 循環顏色生成器選項\n * Loop colors generator options\n *\n * @typeParam T - 輸入顏色類型 / Input color type\n * @typeParam R - 輸出類型 / Output type\n */\nexport interface IOptions<T = string, R = T>\n{\n\t/** 隨機函式或是否啟用隨機 / Random function or enable flag */\n\trand?: ((index?: number, length?: number, ...argv: any[]) => number) | boolean;\n\t/** 生成上限次數，-1 表示無限制 / Generation limit, -1 means unlimited */\n\tlimit?: number | -1;\n\t/**\n\t * 自定義生成器函式\n\t * Custom generator function\n\t *\n\t * @param colors - 顏色陣列 / Color array\n\t * @param position - 當前位置 / Current position\n\t * @param idx - 當前索引 / Current index\n\t * @param len - 陣列長度 / Array length\n\t * @returns 處理後的結果 / Processed result\n\t */\n\tgenerator?(colors: readonly T[], position: number, idx: number, len: number): R\n}\n\n/**\n * 建立循環顏色生成器\n * Create loop colors generator\n *\n * 創建一個可迭代的顏色生成器，支援：\n * - 順序或隨機循環遍歷顏色\n * - 自定義生成器處理邏輯\n * - 可設定生成上限次數\n *\n * Creates an iterable color generator supporting:\n * - Sequential or random circular traversal\n * - Custom generator processing logic\n * - Configurable generation limit\n *\n * @typeParam T - 輸入顏色類型 / Input color type\n * @typeParam R - 輸出類型 / Output type\n * @param colors - 顏色陣列 / Color array\n * @param options - 選項配置 / Options configuration\n * @returns  Generator 函式，回傳可迭代物件 / Generator function returning iterable\n *\n * @example\n * ```typescript\n * // 基本順序循環 / Basic sequential loop\n * const gen = loopColors(['#FF0000', '#00FF00', '#0000FF'])();\n * console.log(gen.next().value); // '#FF0000'\n *\n * // 隨機循環 / Random loop\n * const randGen = loopColors(['#FF0000', '#00FF00'], { rand: true })();\n *\n * // 自定義生成器 / Custom generator\n * const upperGen = loopColors(['red', 'blue'], {\n *   generator: (colors, pos) => colors[pos].toUpperCase()\n * })();\n * ```\n */\nexport function loopColors<T, R = T>(colors: ITSArrayListMaybeReadonly<T>, options?: IOptions<T, R>)\n{\n\toptions ??= {};\n\n\t/**\n\t * 保留原始陣列引用以避免外部修改影響遍歷過程，確保閉包內的陣列一致性\n\t *\n\t * Preserves the original array reference to prevent external modifications from affecting the traversal process,\n\t * ensuring array consistency within the closure\n\t */\n\tcolors = colors.slice();\n\n\tlet idx = 0;\n\tconst len = colors.length;\n\n\t/**\n\t * 定義默認的索引獲取邏輯，利用閉包狀態維持遍歷順序\n\t *\n\t * Defines the default index retrieval logic,\n\t * utilizing closure state to maintain traversal order\n\t */\n\t// @ts-ignore\n\tlet getIndex = (index: number, length: number) => idx++ % len;\n\n\tif (options.rand)\n\t{\n\t\tconst rand = options.rand === true ? Math.random : options.rand;\n\n\t\tconst _ = getIndex;\n\n\t\t/**\n\t\t * 當啟用隨機策略時覆寫索引獲取器，\n\t\t * 根據回調或 Math.random 隨機跳轉遍歷位置以避免固定順序導致的模式化輸出\n\t\t *\n\t\t * Overrides the index getter when the random strategy is enabled,\n\t\t * randomly jumping traversal positions based on callbacks or Math.random to avoid patterned output from fixed sequences\n\t\t */\n\t\tgetIndex = (index: number, length: number) =>\n\t\t{\n\t\t\tidx = Math.floor(length * rand(index, length));\n\t\t\treturn _(idx, length);\n\t\t};\n\t}\n\n\t/**\n\t * 將限制作業轉換為整數，若非正數則設為無限遍歷以確保生成器可根據外部條件安全結束而非意外停止\n\t * Converts the limit option to an integer, defaulting to infinite traversal if non-positive to ensure the generator can end safely based on external conditions rather than stopping prematurely\n\t */\n\tlet limit = options.limit | 0;\n\tlimit = limit > 0 ? limit : Infinity;\n\n\t/**\n\t * 採用直接陣列索引訪問作為默認實現，實現遍歷邏輯與結果發送的解耦，\n\t * 允許外部自定義發射邏輯而不干擾核心算法\n\t *\n\t * Adopts direct array index access as the default implementation,\n\t * decoupling traversal logic from yield emission to allow external customization of emission logic without interfering with the core algorithm\n\t */\n\t// @ts-ignore\n\tconst generator: IOptions<T, R>[\"generator\"] = options.generator ?? ((colors, position) => colors[position]);\n\n\treturn function* (startIndex?: number): Generator<R, undefined, R>\n\t{\n\t\tif (typeof startIndex !== 'undefined')\n\t\t{\n\t\t\tstartIndex |= 0;\n\t\t\tidx = startIndex >= 0 ? startIndex : idx;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tidx = 0;\n\t\t}\n\n\t\t/**\n\t\t * 支援從指定索引恢復遍歷而不重置內部狀態，\n\t\t * 使用 do-while 確保至少產出一個值以符合生成器契約並處理 limit === 0 的邊界情況\n\t\t *\n\t\t * Supports resuming traversal from a specified index without resetting internal state,\n\t\t * uses do-while to guarantee at least one yield to match generator contract expectations and handle edge cases like limit === 0\n\t\t */\n\t\tdo\n\t\t{\n\t\t\tyield generator(colors, getIndex(idx, len), idx, len);\n\t\t}\n\t\twhile (--limit > 0);\n\n\t\treturn;\n\t};\n}\n\nexport default loopColors;\n"],"names":["cssColors","cliColors","loopColors","colors","options","_options$generator","slice","idx","len","length","getIndex","index","rand","Math","random","_","floor","limit","Infinity","generator","position","startIndex"],"mappings":"SAoBgBA;EAEf,OAAO,EACN,QACA,QACA,QACA,QACA,WACA,QACA;AAEF;;SAiBgBC;EAEf,OAAO,EAAC,QAAQ,WAAW,QAAQ,UAAU,SAAS;AACvD;;AA+DgB,SAAAC,WAAqBC,GAAsCC;EAAwB,IAAAC;EAElGD,cAAAA,IAAY,KAQZD,IAASA,EAAOG;EAEhB,IAAIC,IAAM;EACV,MAAMC,IAAML,EAAOM;EASnB,IAAIC,WAAWA,CAACC,GAAeF,MAAmBF,MAAQC;EAE1D,IAAIJ,EAAQQ,MACZ;IACC,MAAMA,KAAwB,MAAjBR,EAAQQ,OAAgBC,KAAKC,SAASV,EAAQQ,MAErDG,IAAIL;IASVA,WAAWA,CAACC,GAAeF,OAE1BF,IAAMM,KAAKG,MAAMP,IAASG,EAAKD,GAAOF,KAC/BM,EAAER,GAAKE;AAEhB;EAMA,IAAIQ,IAAwB,IAAhBb,EAAQa;EACpBA,IAAQA,IAAQ,IAAIA,IAAQC;EAU5B,MAAMC,cAASd,IAAgCD,EAAQe,mBAAS,MAAAd,IAAAA,IAAK,CAACF,GAAQiB,MAAajB,EAAOiB;EAElG,OAAO,UAAWC;IAKhBd,SAHyB,MAAfc,KAEVA,KAAc,MACM,IAAIA,IAAad,IAI/B;IAUP;YAEOY,EAAUhB,GAAQO,SAASH,GAAKC,IAAMD,GAAKC;eAEzCS,IAAQ;;AAInB;;"}