当前位置: 首页>>代码示例>>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;未经允许,请勿转载。