当前位置: 首页>>代码示例>>TypeScript>>正文


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

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


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

示例1: inferCLIOptions

function inferCLIOptions(opts: Partial<CLIOptions>, targetLanguage: TargetLanguage | undefined): CLIOptions {
    let srcLang = opts.srcLang;
    if (opts.graphqlSchema !== undefined || opts.graphqlIntrospect !== undefined) {
        messageAssert(srcLang === undefined || srcLang === "graphql", "DriverSourceLangMustBeGraphQL", {});
        srcLang = "graphql";
    } else if (opts.src !== undefined && opts.src.length > 0 && opts.src.every(file => _.endsWith(file, ".ts"))) {
        srcLang = "typescript";
    } else {
        messageAssert(srcLang !== "graphql", "DriverGraphQLSchemaNeeded", {});
        srcLang = withDefault<string>(srcLang, "json");
    }

    let language: TargetLanguage;
    if (targetLanguage !== undefined) {
        language = targetLanguage;
    } else {
        const languageName = opts.lang !== undefined ? opts.lang : inferLang(opts, defaultDefaultTargetLanguageName);
        const maybeLanguage = languageNamed(languageName);
        if (maybeLanguage === undefined) {
            return messageError("DriverUnknownOutputLanguage", { lang: languageName });
        }
        language = maybeLanguage;
    }

    /* tslint:disable:strict-boolean-expressions */
    const options: CLIOptions = {
        src: opts.src || [],
        srcLang: srcLang,
        lang: language.displayName,
        topLevel: opts.topLevel || inferTopLevel(opts),
        noRender: !!opts.noRender,
        alphabetizeProperties: !!opts.alphabetizeProperties,
        allPropertiesOptional: !!opts.allPropertiesOptional,
        rendererOptions: opts.rendererOptions || {},
        help: opts.help || false,
        quiet: opts.quiet || false,
        version: opts.version || false,
        out: opts.out,
        buildMarkovChain: opts.buildMarkovChain,
        additionalSchema: opts.additionalSchema || [],
        graphqlSchema: opts.graphqlSchema,
        graphqlIntrospect: opts.graphqlIntrospect,
        httpMethod: opts.httpMethod,
        httpHeader: opts.httpHeader,
        debug: opts.debug,
        telemetry: opts.telemetry
    };
    /* tslint:enable */
    for (const flagName of inferenceFlagNames) {
        const cliName = negatedInferenceFlagName(flagName);
        options[cliName] = !!opts[cliName];
    }
    return options;
}
开发者ID:nrkn,项目名称:quicktype,代码行数:54,代码来源:index.ts

示例2: replaceTransformedStringType

function replaceTransformedStringType(
    t: PrimitiveType,
    kind: PrimitiveStringTypeKind,
    builder: GraphRewriteBuilder<Type>,
    forwardingRef: TypeRef,
    debugPrintTransformations: boolean
): TypeRef {
    const reconstitutedAttributes = builder.reconstituteTypeAttributes(t.getAttributes());
    const targetTypeKind = withDefault(targetTypeKindForTransformedStringTypeKind(kind), kind);
    const stringType = builder.getStringType(emptyTypeAttributes, StringTypes.unrestricted);
    const transformer = new DecodingTransformer(
        builder.typeGraph,
        stringType,
        new ParseStringTransformer(builder.typeGraph, stringType, undefined)
    );
    const attributes = transformationAttributes(
        builder.typeGraph,
        builder.getPrimitiveType(targetTypeKind, reconstitutedAttributes),
        transformer,
        debugPrintTransformations
    );
    return builder.getStringType(attributes, StringTypes.unrestricted, forwardingRef);
}
开发者ID:nrkn,项目名称:quicktype,代码行数:23,代码来源:MakeTransformations.ts

示例3: makeQuicktypeOptions

export async function makeQuicktypeOptions(
    options: CLIOptions,
    targetLanguages?: TargetLanguage[]
): Promise<Partial<Options> | undefined> {
    if (options.help) {
        usage(targetLanguages === undefined ? defaultTargetLanguages : targetLanguages);
        return undefined;
    }
    if (options.version) {
        console.log(`quicktype version ${packageJSON.version}`);
        console.log("Visit quicktype.io for more info.");
        return undefined;
    }
    if (options.buildMarkovChain !== undefined) {
        const contents = fs.readFileSync(options.buildMarkovChain).toString();
        const lines = contents.split("\n");
        const mc = trainMarkovChain(lines, 3);
        console.log(JSON.stringify(mc));
        return undefined;
    }

    let sources: TypeSource[] = [];
    let leadingComments: string[] | undefined = undefined;
    let fixedTopLevels: boolean = false;
    switch (options.srcLang) {
        case "graphql":
            let schemaString: string | undefined = undefined;
            let wroteSchemaToFile = false;
            if (options.graphqlIntrospect !== undefined) {
                schemaString = await introspectServer(
                    options.graphqlIntrospect,
                    withDefault(options.httpMethod, "GET"),
                    withDefault<string[]>(options.httpHeader, [])
                );
                if (options.graphqlSchema !== undefined) {
                    fs.writeFileSync(options.graphqlSchema, schemaString);
                    wroteSchemaToFile = true;
                }
            }
            const numSources = options.src.length;
            if (numSources !== 1) {
                if (wroteSchemaToFile) {
                    // We're done.
                    return undefined;
                }
                if (numSources === 0) {
                    if (schemaString !== undefined) {
                        console.log(schemaString);
                        return undefined;
                    }
                    return messageError("DriverNoGraphQLQueryGiven", {});
                }
            }
            const gqlSources: GraphQLTypeSource[] = [];
            for (const queryFile of options.src) {
                let schemaFileName: string | undefined = undefined;
                if (schemaString === undefined) {
                    schemaFileName = defined(options.graphqlSchema);
                    schemaString = fs.readFileSync(schemaFileName, "utf8");
                }
                const schema = parseJSON(schemaString, "GraphQL schema", schemaFileName);
                const query = await getStream(await readableFromFileOrURL(queryFile));
                const name = numSources === 1 ? options.topLevel : typeNameFromFilename(queryFile);
                gqlSources.push({ kind: "graphql", name, schema, query });
            }
            sources = gqlSources;
            break;
        case "json":
        case "schema":
            sources = await getSources(options);
            break;
        case "typescript":
            sources = [makeTypeScriptSource(options.src)];
            break;
        case "postman":
            for (const collectionFile of options.src) {
                const collectionJSON = fs.readFileSync(collectionFile, "utf8");
                const { sources: postmanSources, description } = sourcesFromPostmanCollection(
                    collectionJSON,
                    collectionFile
                );
                for (const src of postmanSources) {
                    sources.push(Object.assign(
                        { kind: "json" },
                        stringSourceDataToStreamSourceData(src)
                    ) as JSONTypeSource);
                }
                if (postmanSources.length > 1) {
                    fixedTopLevels = true;
                }
                if (description !== undefined) {
                    leadingComments = wordWrap(description).split("\n");
                }
            }
            break;
        default:
            return messageError("DriverUnknownSourceLanguage", { lang: options.srcLang });
    }

    const components = definedMap(options.debug, d => d.split(","));
//.........这里部分代码省略.........
开发者ID:nrkn,项目名称:quicktype,代码行数:101,代码来源:index.ts

示例4:

 columnWidths.push(defined(iterableMax(widths.map(l => withDefault<number>(l[i], 0)))));
开发者ID:nrkn,项目名称:quicktype,代码行数:1,代码来源:Source.ts

示例5: serializeToStringArray

 function serializeToStringArray(source: Source): void {
     switch (source.kind) {
         case "text":
             indentIfNeeded();
             currentLine.push(source.text);
             break;
         case "newline":
             finishLine();
             indent += source.indentationChange;
             indentNeeded = indent;
             break;
         case "sequence":
             for (const s of source.sequence) {
                 serializeToStringArray(s);
             }
             break;
         case "table":
             const t = source.table;
             const numRows = t.length;
             if (numRows === 0) break;
             const widths = t.map(l => l.map(s => sourceLineLength(s, names)));
             const numColumns = defined(iterableMax(t.map(l => l.length)));
             if (numColumns === 0) break;
             const columnWidths: number[] = [];
             for (let i = 0; i < numColumns; i++) {
                 columnWidths.push(defined(iterableMax(widths.map(l => withDefault<number>(l[i], 0)))));
             }
             for (let y = 0; y < numRows; y++) {
                 indentIfNeeded();
                 const row = defined(t[y]);
                 const rowWidths = defined(widths[y]);
                 for (let x = 0; x < numColumns; x++) {
                     const colWidth = columnWidths[x];
                     const src = withDefault<Source>(row[x], { kind: "text", text: "" });
                     const srcWidth = withDefault<number>(rowWidths[x], 0);
                     serializeToStringArray(src);
                     if (x < numColumns - 1 && srcWidth < colWidth) {
                         currentLine.push(repeatString(" ", colWidth - srcWidth));
                     }
                 }
                 if (y < numRows - 1) {
                     finishLine();
                     indentNeeded = indent;
                 }
             }
             break;
         case "annotated":
             const start = currentLocation();
             serializeToStringArray(source.source);
             const end = currentLocation();
             annotations.push({ annotation: source.annotation, span: { start, end } });
             break;
         case "name":
             assert(names.has(source.named), "No name for Named");
             indentIfNeeded();
             currentLine.push(defined(names.get(source.named)));
             break;
         case "modified":
             indentIfNeeded();
             const serialized = serializeRenderResult(source.source, names, indentation).lines;
             assert(serialized.length === 1, "Cannot modify more than one line.");
             currentLine.push(source.modifier(serialized[0]));
             break;
         default:
             return assertNever(source);
     }
 }
开发者ID:nrkn,项目名称:quicktype,代码行数:67,代码来源:Source.ts


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