functioncountBy(items, groupName) { let counts = []; for (let item of items) { let name = groupName(item); let known = counts.find(c = c.name == name); if (!known) { counts.push({name, count: 1}); } else { known.count++; } } return counts; }
console.log(countBy([1, 2, 3, 4, 5], n => n > 2)); // → [{name: false, count: 2}, {name: true, count: 3}]
这是characterScript,作用是判断code属于哪一种语言。
1 2 3 4 5 6 7 8 9 10 11 12 13
functioncharacterScript(code) { for (let script ofSCRIPTS) { if (script.ranges.some(([from, to]) => { return code >= from && code < to; })) { return script; } } returnnull; }