本文整理汇总了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
);
}
示例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));
}
示例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}`);
}
示例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);
}
示例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);
});
}
示例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;
}
示例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)) };
}
示例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;
}
示例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);
}
示例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];
}