本文整理汇总了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);
}
示例2: singularize
singularize(): TypeNames {
return TypeNames.makeWithDistance(
setMap(this.names, pluralize.singular),
definedMap(this._alternativeNames, an => setMap(an, pluralize.singular)),
this.distance + 1
);
}
示例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))));
}
}
示例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;
}
示例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;
}
示例6: Graph
return new Graph(components.map(ns => setMap(ns, n => this._nodes[n])), false, componentSuccessors);
示例7: definedMap
definedMap(this._alternativeNames, an => setMap(an, pluralize.singular)),
示例8: members
get members(): ReadonlySet<Type> {
return setMap(this.getMemberRefs(), tref => derefTypeRef(tref, this.graph));
}
示例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;
}