本文整理汇总了TypeScript中underscore.countBy函数的典型用法代码示例。如果您正苦于以下问题:TypeScript countBy函数的具体用法?TypeScript countBy怎么用?TypeScript countBy使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了countBy函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it(`тест с числом игроков равным ${count_players}`, () => {
// Создаем массив с игроками
players = Array.apply(null, {length: count_players}).map(() => getPlayer());
// Отправляем в тестируемую функцию
game_players = Player.RolesForPlayers(players);
// По приходу из коллекции игроков, забираем у каждого его роль и суем в массив
roles_array = _.pluck(game_players, 'role');
// Затем считаем, сколько пришлось игроков на каждую роль
count_roles = _.countBy(roles_array, _.identity);
// Поскольку данные персонажи есть не всегда, надо явно проставить ноль для сравнения
count_roles[Roles.COMMISSAR] = count_roles[Roles.COMMISSAR] || 0;
count_roles[Roles.WHORE] = count_roles[Roles.WHORE] || 0;
mafia_count = Math.floor((count_players - MIN_PLAYERS) / STEP_CHANGE_ROLES) + 1;
doctor_count = 1;
whore_count = 1;
commissar_count = count_players >= MIN_PLAYERS + STEP_CHANGE_ROLES ? 1 : 0;
expect(count_roles[Roles.MAFIA]).toBe(mafia_count);
expect(count_roles[Roles.DOCTOR]).toBe(doctor_count);
expect(count_roles[Roles.COMMISSAR]).toBe(commissar_count);
expect(count_roles[Roles.WHORE]).toBe(whore_count);
expect(count_roles[Roles.INHABITANT]).toBe(count_players - mafia_count - doctor_count - commissar_count - whore_count);
});
示例2: moveItemsToVault
// cribbed from dimFarmingService, but modified
async function moveItemsToVault(
storeService: StoreServiceType,
store: DimStore,
items: DimItem[],
dimItemService
): Promise<void> {
const reservations = {};
// reserve space for all move-asides
reservations[store.id] = _.countBy(items, 'type');
for (const item of items) {
// Move a single item. We reevaluate the vault each time in case things have changed.
const vault = storeService.getVault();
const vaultSpaceLeft = vault!.spaceLeftForItem(item);
if (vaultSpaceLeft <= 1) {
// If we're down to one space, try putting it on other characters
const otherStores = storeService.getStores().filter((store) => !store.isVault && store.id !== store.id);
const otherStoresWithSpace = otherStores.filter((store) => store.spaceLeftForItem(item));
if (otherStoresWithSpace.length) {
await dimItemService.moveTo(item, otherStoresWithSpace[0], false, item.amount, items, reservations);
continue;
}
}
await dimItemService.moveTo(item, vault, false, item.amount, items, reservations);
}
}
示例3: bucketsService
return bucketsService().then((buckets) => {
const postmasterItems: DimItem[] = flatMap(
buckets.byCategory.Postmaster,
(bucket: InventoryBucket) => store.buckets[bucket.id]
);
const postmasterItemCountsByType = _.countBy(postmasterItems, (i) => i.bucket.id);
// If any category is full, we'll move enough aside
const itemsToMove: DimItem[] = [];
_.each(postmasterItemCountsByType, (count, bucket) => {
if (count > 0) {
const items: DimItem[] = store.buckets[bucket];
const capacity = store.capacityForItem(items[0]);
const numNeededToMove = Math.max(0, count + items.length - capacity);
if (numNeededToMove > 0) {
// We'll move the lowest-value item to the vault.
const candidates = _.sortBy(items.filter((i) => !i.equipped && !i.notransfer), (i) => {
let value = {
Common: 0,
Uncommon: 1,
Rare: 2,
Legendary: 3,
Exotic: 4
}[i.tier];
// And low-stat
if (i.primStat) {
value += i.primStat.value / 1000;
}
return value;
});
itemsToMove.push(..._.first(candidates, numNeededToMove));
}
}
});
// TODO: it'd be nice if this were a loadout option
return moveItemsToVault(store.getStoresService(), store, itemsToMove, dimItemService)
.then(() => {
toaster.pop(
'success',
t('Loadouts.MakeRoom'),
t('Loadouts.MakeRoomDone', {
count: postmasterItems.length,
movedNum: itemsToMove.length,
store: store.name,
context: store.gender
})
);
})
.catch((e) => {
toaster.pop(
'error',
t('Loadouts.MakeRoom'),
t('Loadouts.MakeRoomError', { error: e.message })
);
throw e;
}) as IPromise<void>;
});
示例4: expect
unsubscribe = store.subscribe(() => {
expect(store.getState().status).toBe(GameStatus.START_THE_GAME);
let count = _.countBy(store.getState().players, player => {
return player.role;
});
expect(count[Roles.INHABITANT]).toBe(3);
expect(count[Roles.DOCTOR]).toBe(1);
expect(count[Roles.WHORE]).toBe(1);
expect(count[Roles.MAFIA]).toBe(1);
// Отписываемся т. к. store общий для всех тестов
unsubscribe();
done();
});
示例5: function
var uniqContact = function(args){
args.HasError = false;
args.ErrorMessage = "";
var fullNames =_.map(this.Contacts,function(contact:any){return contact.Email});
var itemcounts = _.countBy(fullNames, function (n) { return n; });
var dupes = _.reduce(itemcounts, function (memo:Array<string>, item, idx) {
if (item > 1)
memo.push(idx);
return memo;
}, []);
if (dupes.length != 0) {
args.HasError = true;
args.ErrorMessage = _.reduce(dupes, function (memo:string, fullName:string) {
return memo + fullName + " ";
},"Each contact must be unique. Not unique values: ");
return;
}
};
示例6: init
function init() {
const count = countBy(TITLE_DEFAULT, (t) => t.title);
for (const c in count) {
if (c in count && count[c] > 1) {
throw `称谓重复: ${c}`;
}
}
for (const t of TITLE_DEFAULT) {
if (!t.son) {
throw `${t.title}缺少儿子称谓`;
}
if (!t.daughter) {
throw `${t.title}缺少女儿称谓`;
}
if (t.son && t.son.title) {
t.son.compare = '';
}
if (t.daughter && t.daughter.title) {
t.daughter.compare = '';
}
}
}