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


TypeScript ramda.pickBy函數代碼示例

本文整理匯總了TypeScript中ramda.pickBy函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript pickBy函數的具體用法?TypeScript pickBy怎麽用?TypeScript pickBy使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了pickBy函數的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1:

export const getMinions = (entities: EntityContainer): MinionContainer =>
  R.pickBy(R.propEq('type', CardType.Minion), entities);
開發者ID:zernie,項目名稱:typescript-redux-card-game,代碼行數:2,代碼來源:Minion.ts

示例2: async

export const asyncLoadAssociationsDecorator = async ({
  modelName,
  fields,
}: {
  modelName: string;
  fields: (Fields & WithHidden & WithAssociation) | PositionsField | EnumField;
}): Promise<{
  modelName;
  fields: (Fields & WithHidden) | PositionsField | EnumField | AssociationField;
}> => {
  const TAG = '[asyncLoadAssociationsDecorator]';
  logger.log(TAG, { fields });

  const relationShips = [DynamicFormTypes.Association, DynamicFormTypes.ManyToMany];
  const associations = R.filter(field => R.contains(field.type)(relationShips))(fields);

  if (R.not(R.isEmpty(associations))) {
    logger.debug(TAG, 'associations is', associations);

    // 當已經拉取過數據後不再進行拉取,在這裏認為如果已存在的 items 數量和 value 中的不一致,則重新拉取
    // TODO 如果按第一次已經拉取過來看,其實不需要再次拉取,相關數據應該從組件中傳出
    // const filteredAssociations = R.pickBy(field => R.not(R.has('associations', field)))(
    const filteredAssociations = R.pickBy(field => {
      const loaded = idx(field, _ => _.associations[field.name]) as any;
      if (loaded) {
        return idx(loaded, _ => _.existItems.length) != idx(field, _ => _.value.length);
      }
      return true;
    })(associations);
    logger.log(TAG, { filteredAssociations });
    if (R.isEmpty(filteredAssociations)) {
      return { modelName, fields };
    }

    // TODO add onSearch query in options
    const wrappedAssociations = await Promise.all(
      R.values(filteredAssociations).map(async field => {
        const selectable = R.pathOr([], ['options', 'selectable'])(field);
        logger.debug(TAG, { field, selectable });
        if (selectable) {
          const primaryKey = _.first(AppContext.adapters.models.getPrimaryKeys(selectable));
          const fieldsOfAssociations = AppContext.adapters.models.getFieldsOfAssociations();

          const foreignOpts = [
            {
              modelName: selectable,
              association: fieldsOfAssociations[selectable],
              onSearch: _.debounce(async (value, callback) => {
                logger.log(TAG, 'onSearch', { value });

                AppContext.adapters.models
                  .loadAssociation(selectable, { keywords: value })
                  .then(response => {
                    const items = extractItemsBy(primaryKey)([response]);
                    callback(items);
                  })
                  .catch(reason => {
                    logger.error(TAG, reason);
                  });
              }, 500),
            },
          ];
          logger.debug(TAG, { fieldsOfAssociations, foreignOpts });

          try {
            const results = await bluebird.props({
              itemsResponse: AppContext.adapters.models.loadAssociation(selectable),
              existItemsResponse: AppContext.adapters.models.loadAssociationByIds(
                selectable,
                field.value,
              ),
            });

            // 當前方法隻處理了單個外鍵的情況,沒有考慮如聯合主鍵的處理
            const foreignKeysResponse = {
              [selectable]: {
                items: _.compact(extractItemsBy(primaryKey)([results.itemsResponse])),
                existItems: _.compact(extractItemsBy(primaryKey)([results.existItemsResponse])),
              },
            };
            logger.debug(TAG, { foreignOpts, foreignKeysResponse });
            return { ...field, foreignOpts, associations: foreignKeysResponse };
          } catch (e) {
            logger.error(TAG, e);
          }
        }
        logger.warn(TAG, 'no foreignKeys with association', { field });
        return { ...field, type: DynamicFormTypes.Input };
      }),
    );

    const pairedWrappedAssociations = R.zipObj(R.keys(filteredAssociations), wrappedAssociations);
    logger.debug(TAG, { pairedWrappedAssociations });

    // FIXME 臨時解決關聯數據從 entities 到 ids 的轉換
    const transformedAssociations = R.map(association => {
      const primaryKey = _.first(AppContext.adapters.models.getPrimaryKeys(association.name));
      let value;

      if (_.isArrayLike(association.value)) {
//.........這裏部分代碼省略.........
開發者ID:danielwii,項目名稱:asuna-admin,代碼行數:101,代碼來源:async.ts

示例3:

export const getDeck = (game: Game): CardContainer =>
  R.pickBy(R.propEq('zone', Zone.Deck), game.cards);
開發者ID:zernie,項目名稱:typescript-redux-card-game,代碼行數:2,代碼來源:Game.ts


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