• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

TypeScript collection-utils.iterableFirst函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中collection-utils.iterableFirst函数的典型用法代码示例。如果您正苦于以下问题:TypeScript iterableFirst函数的具体用法?TypeScript iterableFirst怎么用?TypeScript iterableFirst使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了iterableFirst函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: replaceClass

    function replaceClass(
        setOfOneClass: ReadonlySet<ClassType>,
        builder: GraphRewriteBuilder<ClassType>,
        forwardingRef: TypeRef
    ): TypeRef {
        const c = defined(iterableFirst(setOfOneClass));
        const properties = c.getProperties();

        const shouldBe = shouldBeMap(properties);
        if (shouldBe === undefined) {
            return panic(`We shouldn't be replacing class ${c.getCombinedName()} with a map`);
        }

        // Now reconstitute all the types in the new graph.  TypeGraphs are
        // immutable, so any change in the graph actually means building a new
        // graph, and the types in the new graph are different objects.
        // Reconstituting a type means generating the "same" type in the new
        // type graph.  Except we don't get Type objects but TypeRef objects,
        // which is a type-to-be.
        return builder.getMapType(
            c.getAttributes(),
            unifyTypes(
                shouldBe,
                c.getAttributes(),
                builder,
                unionBuilderForUnification(builder, false, false, conflateNumbers),
                conflateNumbers
            ),
            forwardingRef
        );
    }
开发者ID:nrkn,项目名称:quicktype,代码行数:31,代码来源:InferMaps.ts


示例2: Set

export function makeGroupsToFlatten<T extends SetOperationType>(
    setOperations: Iterable<T>,
    include: ((members: ReadonlySet<Type>) => boolean) | undefined
): Type[][] {
    const typeGroups = new EqualityMap<Set<Type>, Set<Type>>();
    for (const u of setOperations) {
        // FIXME: We shouldn't have to make a new set here once we got rid
        // of immutable.
        const members = new Set(setOperationMembersRecursively(u, undefined)[0]);

        if (include !== undefined) {
            if (!include(members)) continue;
        }

        let maybeSet = typeGroups.get(members);
        if (maybeSet === undefined) {
            maybeSet = new Set();
            if (members.size === 1) {
                maybeSet.add(defined(iterableFirst(members)));
            }
        }
        maybeSet.add(u);
        typeGroups.set(members, maybeSet);
    }

    return Array.from(typeGroups.values()).map(ts => Array.from(ts));
}
开发者ID:nrkn,项目名称:quicktype,代码行数:27,代码来源:TypeUtils.ts


示例3: replace

 function replace(
     setOfOneUnion: ReadonlySet<Type>,
     builder: GraphRewriteBuilder<Type>,
     forwardingRef: TypeRef
 ): TypeRef {
     const t = defined(iterableFirst(setOfOneUnion));
     if (t instanceof UnionType) {
         return replaceUnion(t, builder, forwardingRef, transformedTypes, ctx.debugPrintTransformations);
     }
     if (t instanceof ArrayType) {
         return replaceArray(t, builder, forwardingRef, ctx.debugPrintTransformations);
     }
     if (t instanceof EnumType) {
         return replaceEnum(t, builder, forwardingRef, ctx.debugPrintTransformations);
     }
     if (isPrimitiveStringTypeKind(t.kind)) {
         return replaceTransformedStringType(
             t as PrimitiveType,
             t.kind,
             builder,
             forwardingRef,
             ctx.debugPrintTransformations
         );
     }
     return panic(`Cannot make transformation for type ${t.kind}`);
 }
开发者ID:nrkn,项目名称:quicktype,代码行数:26,代码来源:MakeTransformations.ts


示例4: replace

    function replace(
        setOfOneType: ReadonlySet<ObjectType>,
        builder: GraphRewriteBuilder<ObjectType>,
        forwardingRef: TypeRef
    ): TypeRef {
        const o = defined(iterableFirst(setOfOneType));
        const attributes = o.getAttributes();
        const properties = o.getProperties();
        const additionalProperties = o.getAdditionalProperties();

        function reconstituteProperties(): ReadonlyMap<string, ClassProperty> {
            return mapMap(properties, cp =>
                builder.makeClassProperty(builder.reconstituteTypeRef(cp.typeRef), cp.isOptional)
            );
        }

        function makeClass(): TypeRef {
            return builder.getUniqueClassType(attributes, true, reconstituteProperties(), forwardingRef);
        }

        function reconstituteAdditionalProperties(): TypeRef {
            return builder.reconstituteType(defined(additionalProperties));
        }

        if (additionalProperties === undefined) {
            return makeClass();
        }

        if (properties.size === 0) {
            return builder.getMapType(attributes, reconstituteAdditionalProperties(), forwardingRef);
        }

        if (additionalProperties.kind === "any") {
            // FIXME: Warn that we're losing additional property semantics.
            builder.setLostTypeAttributes();
            return makeClass();
        }

        // FIXME: Warn that we're losing class semantics.
        const propertyTypes = setMap(properties.values(), cp => cp.type).add(additionalProperties);
        let union = builder.lookupTypeRefs(Array.from(propertyTypes).map(t => t.typeRef));
        if (union === undefined) {
            const reconstitutedTypes = setMap(propertyTypes, t => builder.reconstituteType(t));
            union = builder.getUniqueUnionType(emptyTypeAttributes, new Set(reconstitutedTypes));

            // This is the direct unification alternative.  Weirdly enough, it is a tiny
            // bit slower.  It gives the same results.
            /*
            union = unifyTypes(
                propertyTypes,
                combineTypeAttributes(propertyTypes.toArray().map(t => t.getAttributes())),
                builder,
                unionBuilderForUnification(builder, false, false, false, conflateNumbers),
                conflateNumbers
            );
            */
        }

        return builder.getMapType(attributes, union, forwardingRef);
    }
开发者ID:nrkn,项目名称:quicktype,代码行数:60,代码来源:ReplaceObjectType.ts


示例5: panic

export function unifyTypes<T extends Type>(
    types: ReadonlySet<Type>,
    typeAttributes: TypeAttributes,
    typeBuilder: GraphRewriteBuilder<T>,
    unionBuilder: UnionBuilder<TypeBuilder & TypeLookerUp, TypeRef[], TypeRef[]>,
    conflateNumbers: boolean,
    maybeForwardingRef?: TypeRef
): TypeRef {
    typeAttributes = typeBuilder.reconstituteTypeAttributes(typeAttributes);
    if (types.size === 0) {
        return panic("Cannot unify empty set of types");
    } else if (types.size === 1) {
        const first = defined(iterableFirst(types));
        if (!(first instanceof UnionType)) {
            return typeBuilder.reconstituteTypeRef(first.typeRef, typeAttributes, maybeForwardingRef);
        }
    }

    const typeRefs = Array.from(types).map(t => t.typeRef);
    const maybeTypeRef = typeBuilder.lookupTypeRefs(typeRefs, maybeForwardingRef);
    if (maybeTypeRef !== undefined) {
        typeBuilder.addAttributes(maybeTypeRef, typeAttributes);
        return maybeTypeRef;
    }

    const accumulator = new TypeRefUnionAccumulator(conflateNumbers);
    const nestedAttributes = typeBuilder.reconstituteTypeAttributes(accumulator.addTypes(types));
    typeAttributes = combineTypeAttributes("union", typeAttributes, nestedAttributes);

    return typeBuilder.withForwardingRef(maybeForwardingRef, forwardingRef => {
        typeBuilder.registerUnion(typeRefs, forwardingRef);
        return unionBuilder.buildUnion(accumulator, false, typeAttributes, forwardingRef);
    });
}
开发者ID:nrkn,项目名称:quicktype,代码行数:34,代码来源:UnifyClasses.ts


示例6: combineNames

// FIXME: In the case of overlapping prefixes and suffixes we will
// produce a name that includes the overlap twice.  For example, for
// the names "aaa" and "aaaa" we have the common prefix "aaa" and the
// common suffix "aaa", so we will produce the combined name "aaaaaa".
function combineNames(names: ReadonlySet<string>): string {
    let originalFirst = iterableFirst(names);
    if (originalFirst === undefined) {
        return panic("Named type has no names");
    }
    if (names.size === 1) {
        return originalFirst;
    }

    const namesSet = setMap(names, s =>
        splitIntoWords(s)
            .map(w => w.word.toLowerCase())
            .join("_")
    );
    const first = defined(iterableFirst(namesSet));
    if (namesSet.size === 1) {
        return first;
    }

    let prefixLength = first.length;
    let suffixLength = first.length;
    for (const n of iterableSkip(namesSet, 1)) {
        prefixLength = Math.min(prefixLength, n.length);
        for (let i = 0; i < prefixLength; i++) {
            if (first[i] !== n[i]) {
                prefixLength = i;
                break;
            }
        }

        suffixLength = Math.min(suffixLength, n.length);
        for (let i = 0; i < suffixLength; i++) {
            if (first[first.length - i - 1] !== n[n.length - i - 1]) {
                suffixLength = i;
                break;
            }
        }
    }
    const prefix = prefixLength > 2 ? first.substr(0, prefixLength) : "";
    const suffix = suffixLength > 2 ? first.substr(first.length - suffixLength) : "";
    const combined = prefix + suffix;
    if (combined.length > 2) {
        return combined;
    }
    return first;
}
开发者ID:nrkn,项目名称:quicktype,代码行数:50,代码来源:TypeNames.ts


示例7: makeOneOf

 private makeOneOf(types: ReadonlySet<Type>): Schema {
     const first = iterableFirst(types);
     if (first === undefined) {
         return panic("Must have at least one type for oneOf");
     }
     if (types.size === 1) {
         return this.schemaForType(first);
     }
     return { anyOf: Array.from(types).map((t: Type) => this.schemaForType(t)) };
 }
开发者ID:nrkn,项目名称:quicktype,代码行数:10,代码来源:JSONSchema.ts


示例8: stringify

 stringify(descriptions: ReadonlySet<string>): string | undefined {
     let result = iterableFirst(descriptions);
     if (result === undefined) return undefined;
     if (result.length > 5 + 3) {
         result = `${result.substr(0, 5)}...`;
     }
     if (descriptions.size > 1) {
         result = `${result}, ...`;
     }
     return result;
 }
开发者ID:nrkn,项目名称:quicktype,代码行数:11,代码来源:Description.ts


示例9: replaceString

    function replaceString(
        group: ReadonlySet<PrimitiveType>,
        builder: GraphRewriteBuilder<PrimitiveType>,
        forwardingRef: TypeRef
    ): TypeRef {
        assert(group.size === 1);
        const t = defined(iterableFirst(group));
        const stringTypes = stringTypesForType(t);
        const attributes = mapFilter(t.getAttributes(), a => a !== stringTypes);
        const mappedStringTypes = stringTypes.applyStringTypeMapping(stringTypeMapping);

        if (!mappedStringTypes.isRestricted) {
            return builder.getStringType(attributes, StringTypes.unrestricted, forwardingRef);
        }

        const types: TypeRef[] = [];
        const cases = defined(mappedStringTypes.cases);
        if (cases.size > 0) {
            const keys = new Set(cases.keys());
            const fullCases = enumSets.find(s => setIsSuperset(s, keys));
            if (inference !== "none" && !isAlwaysEmptyString(Array.from(keys)) && fullCases !== undefined) {
                types.push(builder.getEnumType(emptyTypeAttributes, fullCases));
            } else {
                return builder.getStringType(attributes, StringTypes.unrestricted, forwardingRef);
            }
        }
        const transformations = mappedStringTypes.transformations;
        // FIXME: This is probably wrong, or at least overly conservative.  This is for the case
        // where some attributes are identity ones, i.e. where we can't merge the primitive types,
        // like it happens in the line after the `if`.  The case where this occured was with URI
        // attributes: we had two separate string types with different URI attributes, but because
        // both are rewritten via `getPrimitiveType` below without any attributes, they end up
        // being the same string type.
        if (types.length === 0 && transformations.size === 1) {
            const kind = defined(iterableFirst(transformations));
            return builder.getPrimitiveType(kind, attributes, forwardingRef);
        }
        types.push(...Array.from(transformations).map(k => builder.getPrimitiveType(k)));
        assert(types.length > 0, "We got an empty string type");
        return builder.getUnionType(attributes, new Set(types), forwardingRef);
    }
开发者ID:nrkn,项目名称:quicktype,代码行数:41,代码来源:ExpandStrings.ts


示例10: unionMemberName

export function unionMemberName(u: UnionType, member: Type, language: string): [string | undefined, boolean] {
    const identifiers = unionIdentifierTypeAttributeKind.tryGetInAttributes(u.getAttributes());
    if (identifiers === undefined) return [undefined, false];

    const memberNames = unionMemberNamesTypeAttributeKind.tryGetInAttributes(member.getAttributes());
    if (memberNames === undefined) return [undefined, false];

    const names = new Set<string>();
    const fixedNames = new Set<string>();
    for (const i of identifiers) {
        const maybeEntry = memberNames.get(i);
        if (maybeEntry === undefined) continue;
        const maybeName = getFromEntry(maybeEntry, language);
        if (maybeName === undefined) continue;
        const [name, isNameFixed] = maybeName;
        if (isNameFixed) {
            fixedNames.add(name);
        } else {
            names.add(name);
        }
    }

    let size: number;
    let isFixed: boolean;
    let first = iterableFirst(fixedNames);
    if (first !== undefined) {
        size = fixedNames.size;
        isFixed = true;
    } else {
        first = iterableFirst(names);
        if (first === undefined) return [undefined, false];

        size = names.size;
        isFixed = false;
    }

    messageAssert(size === 1, "SchemaMoreThanOneUnionMemberName", { names: Array.from(names) });
    return [first, isFixed];
}
开发者ID:nrkn,项目名称:quicktype,代码行数:39,代码来源:AccessorNames.ts



注:本文中的collection-utils.iterableFirst函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript collection-utils.iterableSome函数代码示例发布时间:2022-05-24
下一篇:
TypeScript collection-utils.hashCodeOf函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap