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

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

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

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

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

请发表评论

全部评论

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