本文整理汇总了TypeScript中lodash.unionBy函数的典型用法代码示例。如果您正苦于以下问题:TypeScript unionBy函数的具体用法?TypeScript unionBy怎么用?TypeScript unionBy使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了unionBy函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: updateInventory
export function updateInventory(
inventory: Inventory, itemName: ItemName, amount: number, maxAmount = amount
): Inventory {
let newStack;
if (hasItem(inventory, itemName)) {
const itemStack = getItem(inventory, itemName);
newStack = _.cloneDeep(itemStack);
Item.changeStackAmount(newStack, amount);
} else {
const isFull = inventoryIsFull(inventory);
if (!isFull && amount > 0) {
newStack = Item.createItemStack(createItem(itemName), amount, maxAmount);
} else if (isFull && amount > 0) {
throw new Error(
'Attempted to add a non-present item to a full inventory!'
);
} else {
// amount <= 0
throw new Error(
'Attempted to update an inventory with a non-present item with a non-positive amount!'
);
}
}
const newItemStacks = Item.removeEmptyStacks(_.unionBy([ newStack ], inventory.itemStacks, 'item.name'));
return {
itemStacks: newItemStacks,
maximumCapacity: inventory.maximumCapacity
};
}
示例2: default
export default (state: RangeState = initialState, action: Action) => {
switch (action.type) {
case ActionTypes.DeleteTimeRange: {
const {dashboardID} = action.payload
const ranges = state.filter(r => r.dashboardID !== dashboardID)
return ranges
}
case ActionTypes.RetainRangesDashboardTimeV1: {
const {dashboardIDs} = action.payload
const ranges = state.filter(r => dashboardIDs.includes(r.dashboardID))
return ranges
}
case ActionTypes.SetDashboardTimeV1: {
const {dashboardID, timeRange} = action.payload
const newTimeRange = [{dashboardID, ...timeRange}]
const ranges = _.unionBy(newTimeRange, state, 'dashboardID')
return ranges
}
}
return state
}
示例3:
]).then(function (resultArray) {
that.NotificationService.show("Tenants saved with success");
// that.loadTenants();
that.tenantsToCreate = [];
that.tenantsToUpdate = [];
let createResult = resultArray[0];
if (createResult) {
that.tenants = _.unionBy(createResult.data, that.tenants, 'name');
}
});
示例4: experiment
/* この仕組みを使って差分だけデータを取得する関数を作る */
experiment() {
let a = [{ key: 'x', timestamp: '100' }];
let b = [{ key: 'x', timestamp: '200' }];
let c = lodash.union(a, b); // 2つの配列を1つにまとめる。
console.log(c); // [{ key: 'x', timestamp: '100' }, { key: 'x', timestamp: '200' }]
let d = lodash.unionBy(a, b, 'key'); // 2つの配列を1つにまとめ、同一キーのオブジェクトは左にあるものが生きる。(この場合はa)
console.log(d); // [{ key: 'x', timestamp: '100' }]
let e = lodash.unionBy(b, a, 'key'); // 2つの配列を1つにまとめ、同一キーのオブジェクトは左にあるものが生きる。(この場合はb)
console.log(e); // [{ key: 'x', timestamp: '200' }]
let f = [{ key: 'x', timestamp: '100' }, { key: 'x', timestamp: '200' }];
let g = lodash.uniqBy(f, 'key'); // 同一キーのオブジェクトは左にあるものが生きる。
console.log(g); // [{ key: 'x', timestamp: '100' }]
let h = [{ key: 'x', timestamp: '200' }, { key: 'x', timestap: '100' }];
let i = lodash.uniqBy(h, 'key');
console.log(i); // [{ key: 'x', timestamp: '200' }]
}
示例5: stethoscope
// TODO: Refactor out logging logic into seperate file
async function stethoscope() {
try {
const currentTab = await getCurrentTab();
const currentTabArray = (await isTabActive(currentTab))
? [currentTab]
: [];
const audibleTabs = await browser.tabs.query({ audible: true });
const tabs = filter(
unionBy(currentTabArray, audibleTabs, 'url'),
tab => !tab.incognito
);
const timeSinceLastPoll = pollTimer();
if (timeSinceLastPoll > 70) {
// If significantly more than 60s, reset timers.
// This is usually indicative of computer being suspended.
// See: https://github.com/SuperuserLabs/thankful/issues/61
console.log('suspend detected, resetting timers');
each(tabTimers, tabTimer => tabTimer());
}
const currentUrls = tabs.map(tab => canonicalizeUrl(tab.url));
const goneUrls = difference(Object.keys(tabTimers), currentUrls);
const stillUrls = intersection(Object.keys(tabTimers), currentUrls);
const newUrls = difference(currentUrls, Object.keys(tabTimers));
goneUrls.forEach(url => {
const duration = tabTimers[url]();
const title = tabTitles[url];
delete tabTimers[url];
delete tabTitles[url];
db.logActivity(url, duration, { title: title });
});
stillUrls.forEach(url => {
const duration = tabTimers[url]();
let title = find(tabs, tab => canonicalizeUrl(tab.url) === url).title;
tabTitles[url] = title;
db.logActivity(url, duration, { title: title });
});
newUrls.forEach(url => {
tabTimers[url] = valueConstantTicker();
tabTimers[url]();
tabTitles[url] = find(
tabs,
tab => canonicalizeUrl(tab.url) === url
).title;
});
await rescheduleAlarm();
} catch (error) {
console.log(`Stethoscope error: ${error}`);
}
}
示例6: produce
return produce(state, draftState => {
switch (action.type) {
case ActionTypes.SetDashboards: {
const {list, status} = action.payload
draftState.status = status
if (list) {
draftState.list = list
}
return
}
case ActionTypes.RemoveDashboard: {
const {id} = action.payload
draftState.list = draftState.list.filter(l => l.id !== id)
return
}
case ActionTypes.SetDashboard: {
const {dashboard} = action.payload
draftState.list = _.unionBy([dashboard], state.list, 'id')
return
}
case ActionTypes.EditDashboard: {
const {dashboard} = action.payload
draftState.list = draftState.list.map(d => {
if (d.id === dashboard.id) {
return dashboard
}
return d
})
return
}
case ActionTypes.RemoveCell: {
const {dashboard, cell} = action.payload
draftState.list = draftState.list.map(d => {
if (d.id === dashboard.id) {
const cells = d.cells.filter(c => c.id !== cell.id)
d.cells = cells
}
return d
})
return
}
case ActionTypes.AddDashboardLabels: {
const {dashboardID, labels} = action.payload
draftState.list = draftState.list.map(d => {
if (d.id === dashboardID) {
d.labels = [...d.labels, ...labels]
}
return d
})
return
}
case ActionTypes.RemoveDashboardLabels: {
const {dashboardID, labels} = action.payload
draftState.list = draftState.list.map(d => {
if (d.id === dashboardID) {
const updatedLabels = d.labels.filter(el => {
const labelToRemove = labels.find(l => l.id === el.id)
return !labelToRemove
})
d.labels = updatedLabels
}
return d
})
return
}
}
})
示例7: default
export default (state: State = [], action: Action): State => {
switch (action.type) {
case ActionTypes.LoadDashboards: {
const {dashboards} = action.payload
return [...dashboards]
}
case ActionTypes.DeleteDashboard: {
const {dashboardID} = action.payload
return [...state.filter(d => d.id !== dashboardID)]
}
case ActionTypes.LoadDashboard: {
const {dashboard} = action.payload
const newDashboards = _.unionBy([dashboard], state, 'id')
return newDashboards
}
case ActionTypes.UpdateDashboard: {
const {dashboard} = action.payload
const newState = state.map(
d => (d.id === dashboard.id ? {...dashboard} : d)
)
return [...newState]
}
case ActionTypes.DeleteCell: {
const {dashboard, cell} = action.payload
const newState = state.map(d => {
if (d.id !== dashboard.id) {
return {...d}
}
const cells = d.cells.filter(c => c.id !== cell.id)
return {...d, cells}
})
return [...newState]
}
case ActionTypes.AddDashboardLabels: {
const {dashboardID, labels} = action.payload
const newState = state.map(d => {
if (d.id === dashboardID) {
return {...d, labels: [...d.labels, ...labels]}
}
return d
})
return [...newState]
}
case ActionTypes.RemoveDashboardLabels: {
const {dashboardID, labels} = action.payload
const newState = state.map(d => {
if (d.id === dashboardID) {
const updatedLabels = d.labels.filter(l => {
return !labels.includes(l)
})
return {...d, labels: updatedLabels}
}
return d
})
return [...newState]
}
}
return state
}