當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript lodash.unionBy函數代碼示例

本文整理匯總了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
  };
}
開發者ID:zthomae,項目名稱:xanadu,代碼行數:34,代碼來源:inventory.ts

示例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
}
開發者ID:viccom,項目名稱:influxdb,代碼行數:26,代碼來源:ranges.ts

示例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');
      }
    });
開發者ID:gravitee-io,項目名稱:gravitee-management-webui,代碼行數:10,代碼來源:tenants.controller.ts

示例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' }]
  }
開發者ID:ovrmrw,項目名稱:jspm-angular2-sample,代碼行數:21,代碼來源:note-list.service.ts

示例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}`);
    }
  }
開發者ID:ActivityWatch,項目名稱:thankful,代碼行數:53,代碼來源:background.ts

示例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
      }
    }
  })
開發者ID:influxdata,項目名稱:influxdb,代碼行數:88,代碼來源:dashboards.ts

示例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
}
開發者ID:viccom,項目名稱:influxdb,代碼行數:77,代碼來源:dashboards.ts


注:本文中的lodash.unionBy函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。