Files
NapCatQQ/.pnpm-store/v3/files/77/1684147e9f87aefda6190ec3cc215ebbd9bb004a97bbbe2dfeccbc9085b09533c1401f4c3535c4b13110ea51fe26919a74f48207848df0655d2230b776dcb2
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

45 lines
1.0 KiB
Plaintext

/**
* @template {unknown[]|(readonly unknown[])} T
* @param {T} input
* @returns {Exclude<T[number], undefined>[]}
*/
export function filter (input) {
/** @type {Exclude<T[number], undefined>[]} */
const result = [];
for (const item of input) {
if (item !== undefined) {
result.push(/** @type {Exclude<T[number], undefined>} */ (item));
}
}
return result;
}
// TODO [engine:node@>=21.0.0]: You can use the built in Object.groupBy() instead
/**
* Ponyfill for Object.groupBy
*
* @template T
* @template {string | number | symbol} K
* @param {Iterable<T>} iterable
* @param {function(T, number): K} callback
* @returns {Partial<Record<K, T[]>>}
*/
export function groupBy (iterable, callback) {
/** @type {Partial<Record<K, T[]>>} */
const obj = {};
let i = 0;
for (const value of iterable) {
const key = callback(value, i++);
const existingGroup = obj[key];
if (existingGroup) {
existingGroup.push(value);
} else {
obj[key] = [value];
}
}
return obj;
}