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

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

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

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



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

示例1: 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


示例2: singularize

 singularize(): TypeNames {
     return TypeNames.makeWithDistance(
         setMap(this.names, pluralize.singular),
         definedMap(this._alternativeNames, an => setMap(an, pluralize.singular)),
         this.distance + 1
     );
 }
开发者ID:nrkn,项目名称:quicktype,代码行数:7,代码来源:TypeNames.ts


示例3: mapMap

 protected reconstituteSetOperation<T extends BaseGraphRewriteBuilder>(
     builder: TypeReconstituter<T>,
     canonicalOrder: boolean,
     getType: (members: ReadonlySet<TypeRef> | undefined) => void
 ): void {
     const sortedMemberRefs = mapMap(this.sortedMembers.entries(), t => t.typeRef);
     const membersInOrder = canonicalOrder ? this.sortedMembers : this.members;
     const maybeMembers = builder.lookupMap(sortedMemberRefs);
     if (maybeMembers === undefined) {
         getType(undefined);
         const reconstituted = builder.reconstituteMap(sortedMemberRefs);
         builder.setSetOperationMembers(setMap(membersInOrder, t => defined(reconstituted.get(t))));
     } else {
         getType(setMap(membersInOrder, t => defined(maybeMembers.get(t))));
     }
 }
开发者ID:nrkn,项目名称:quicktype,代码行数:16,代码来源:Type.ts


示例4: 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


示例5: isCanonical

    get isCanonical(): boolean {
        const members = this.members;
        if (members.size <= 1) return false;
        const kinds = setMap(members, t => t.kind);
        if (kinds.size < members.size) return false;
        if (kinds.has("union") || kinds.has("intersection")) return false;
        if (kinds.has("none") || kinds.has("any")) return false;
        if (kinds.has("string") && kinds.has("enum")) return false;

        let numObjectTypes = 0;
        if (kinds.has("class")) numObjectTypes += 1;
        if (kinds.has("map")) numObjectTypes += 1;
        if (kinds.has("object")) numObjectTypes += 1;
        if (numObjectTypes > 1) return false;

        return true;
    }
开发者ID:nrkn,项目名称:quicktype,代码行数:17,代码来源:Type.ts


示例6: Graph

 return new Graph(components.map(ns => setMap(ns, n => this._nodes[n])), false, componentSuccessors);
开发者ID:nrkn,项目名称:quicktype,代码行数:1,代码来源:Graph.ts


示例7: definedMap

 definedMap(this._alternativeNames, an => setMap(an, pluralize.singular)),
开发者ID:nrkn,项目名称:quicktype,代码行数:1,代码来源:TypeNames.ts


示例8: members

 get members(): ReadonlySet<Type> {
     return setMap(this.getMemberRefs(), tref => derefTypeRef(tref, this.graph));
 }
开发者ID:nrkn,项目名称:quicktype,代码行数:3,代码来源:Type.ts


示例9: shouldBeMap

function shouldBeMap(properties: ReadonlyMap<string, ClassProperty>): ReadonlySet<Type> | undefined {
    // Only classes with a certain number of properties are inferred
    // as maps.
    const numProperties = properties.size;
    if (numProperties < 2) return undefined;

    // If all property names are digit-only, we always make a map, no
    // questions asked.
    if (iterableEvery(properties.keys(), n => /^[0-9]+$/.test(n))) {
        return setMap(properties.values(), cp => cp.type);
    }

    // If all properties are strings or null then an object must have at least
    // `stringMapSizeThreshold` to qualify as a map.
    if (
        numProperties < stringMapSizeThreshold &&
        iterableEvery(properties.values(), cp => isPrimitiveStringTypeKind(cp.type.kind) || cp.type.kind === "null")
    ) {
        return undefined;
    }

    if (numProperties < mapSizeThreshold) {
        const names = Array.from(properties.keys());
        const probabilities = names.map(nameProbability);
        const product = probabilities.reduce((a, b) => a * b, 1);
        const probability = Math.pow(product, 1 / numProperties);
        // The idea behind this is to have a probability around 0.0025 for
        // n=1, up to around 1.0 for n=20.  I.e. when we only have a few
        // properties, they need to look really weird to infer a map, but
        // when we have more we'll accept more ordinary names.  The details
        // of the formula are immaterial because I pulled it out of my ass.

        // FIXME: Use different exponents and start values depending on
        // the property type kind.  For string properties, for example, we
        // should be more conservative, with class properties more
        // aggressive.  An exponent of 6 is probably good for string
        // properties, and maybe a start value of 0.002, whereas for classes
        // we want maybe 0.004 and 5, or maybe something even more
        // trigger-happy.
        const exponent = 5;
        const scale = Math.pow(22, exponent);
        const limit = Math.pow(numProperties + 2, exponent) / scale + (0.0025 - Math.pow(3, exponent) / scale);
        if (probability > limit) return undefined;
    }

    // FIXME: simplify this - it's no longer necessary with the new
    // class properties.

    // We need to handle three cases for maps (and the fourth case
    // where we leave the class as is):
    //
    // 1. All property types are null.
    // 2. Some property types are null or nullable.
    // 3. No property types are null or nullable.
    let firstNonNullCases: ReadonlySet<Type> | undefined = undefined;
    const allCases = new Set<Type>();
    let canBeMap = true;
    // Check that all the property types are the same, modulo nullability.
    for (const [, p] of properties) {
        // The set of types first property can be, minus null.
        const nn = removeNullFromType(p.type)[1];
        if (nn.size > 0) {
            if (firstNonNullCases !== undefined) {
                // The set of non-null cases for all other properties must
                // be the the same, otherwise we won't infer a map.
                if (!setOperationCasesEqual(nn, firstNonNullCases, true, (a, b) => a.structurallyCompatible(b, true))) {
                    canBeMap = false;
                    break;
                }
            } else {
                firstNonNullCases = nn;
            }
        }
        allCases.add(p.type);
    }
    if (!canBeMap) {
        return undefined;
    }
    return allCases;
}
开发者ID:nrkn,项目名称:quicktype,代码行数:80,代码来源:InferMaps.ts



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
TypeScript collection-utils.setUnionInto函数代码示例发布时间:2022-05-24
下一篇:
TypeScript collection-utils.setFilter函数代码示例发布时间: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