@gvray/eskit
Version:
A rich and colorful toolkit about typescript and javascript.
152 lines • 5.71 kB
JavaScript
;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var isArray_1 = __importDefault(require("./isArray"));
var isString_1 = __importDefault(require("./isString"));
/**
* Iterates over a collection and applies a function to each element.
* 遍历集合并对每个元素应用函数。
*
* This function provides a unified interface for iterating over different
* types of collections including arrays, objects, Maps, Sets, and strings.
* The iteration can be stopped early by returning false from the callback.
* 此函数为遍历不同类型的集合提供统一接口,包括数组、对象、Map、Set和字符串。
* 可以通过从回调函数返回false来提前停止迭代。
*
* @template V - The type of values in the collection / 集合中值的类型
* @template K - The type of keys in the collection / 集合中键的类型
* @param collection - The collection to iterate over / 要遍历的集合
* @param callbackfn - The function to apply to each element / 应用于每个元素的函数
* @returns True if all iterations complete, false if stopped early / 如果所有迭代完成返回true,如果提前停止返回false
*
* @example
* ```typescript
* // Array iteration / 数组遍历
* const numbers = [1, 2, 3, 4, 5]
* each(numbers, (value, index) => {
* console.log(`numbers[${index}] = ${value}`)
* return value < 3 // Stop when value >= 3
* })
*
* // Object iteration / 对象遍历
* const user = { name: 'John', age: 30, city: 'New York' }
* each(user, (value, key) => {
* console.log(`${key}: ${value}`)
* })
*
* // Map iteration / Map遍历
* const userMap = new Map([
* ['name', 'John'],
* ['age', 30],
* ['city', 'New York']
* ])
* each(userMap, (value, key) => {
* console.log(`${key} => ${value}`)
* })
*
* // Set iteration / Set遍历
* const uniqueNumbers = new Set([1, 2, 3, 4, 5])
* each(uniqueNumbers, (value, index) => {
* console.log(`Item ${index}: ${value}`)
* })
*
* // String iteration / 字符串遍历
* each('hello', (char, index) => {
* console.log(`char[${index}] = ${char}`)
* })
* ```
*
* @since 1.0.0
*/
var each = function (collection, callbackfn) {
var e_1, _a, e_2, _b, e_3, _c;
if (!collection) {
return false;
}
if ((0, isArray_1.default)(collection) || (0, isString_1.default)(collection) || collection instanceof Set) {
var i = 0;
try {
for (var collection_1 = __values(collection), collection_1_1 = collection_1.next(); !collection_1_1.done; collection_1_1 = collection_1.next()) {
var item = collection_1_1.value;
var re = callbackfn(item, i, collection);
i++;
if (re === false)
return false;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (collection_1_1 && !collection_1_1.done && (_a = collection_1.return)) _a.call(collection_1);
}
finally { if (e_1) throw e_1.error; }
}
}
else if (collection instanceof Map) {
try {
for (var collection_2 = __values(collection), collection_2_1 = collection_2.next(); !collection_2_1.done; collection_2_1 = collection_2.next()) {
var _d = __read(collection_2_1.value, 2), key = _d[0], value = _d[1];
var re = callbackfn(value, key, collection);
if (re === false)
return false;
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (collection_2_1 && !collection_2_1.done && (_b = collection_2.return)) _b.call(collection_2);
}
finally { if (e_2) throw e_2.error; }
}
}
else {
var entries = Object.entries(collection);
try {
for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
var _e = __read(entries_1_1.value, 2), key = _e[0], value = _e[1];
var re = callbackfn(value, key, collection);
if (re === false)
return false;
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (entries_1_1 && !entries_1_1.done && (_c = entries_1.return)) _c.call(entries_1);
}
finally { if (e_3) throw e_3.error; }
}
}
return true;
};
exports.default = each;
//# sourceMappingURL=each.js.map