本文整理汇总了TypeScript中@sirian/common.Var.stringify方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Var.stringify方法的具体用法?TypeScript Var.stringify怎么用?TypeScript Var.stringify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类@sirian/common.Var
的用法示例。
在下文中一共展示了Var.stringify方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: normalize
public normalize(selected: string) {
const choices = this.getChoices();
const strChoice = Var.stringify(selected);
for (const [key, choice] of KV.entries(choices)) {
if (Var.stringify(key) === strChoice) {
return choice;
}
}
const values = new Set<T>();
for (const choice of KV.values(choices)) {
if (Var.stringify(choice) === strChoice) {
values.add(choice);
}
}
switch (values.size) {
case 0:
throw new InvalidArgumentError(`Value "${strChoice}" is invalid`);
case 1:
return [...values][0];
default:
throw new InvalidArgumentError(`Value "${strChoice}" is ambiguous`);
}
}
示例2: constructor
constructor(init: IParameterInit<T>) {
this.name = Var.stringify(init.name);
this.description = Var.stringify(init.description);
this.defaultValue = init.defaultValue;
this.normalizer = init.normalizer;
this.validator = init.validator;
this.multiple = !!init.multiple;
this.allowedValues = init.allowedValues || [];
}
示例3: getCommandAliasesText
protected getCommandAliasesText(definition: CommandDefinition) {
const aliases = definition.getAliases();
if (!aliases.length) {
return "";
}
const text = aliases.join("|");
return Var.stringify(`[${text}]`);
}
示例4: constructor
constructor(...args: any[]) {
const init = Option.resolveArgs<T>(args);
super(init);
this.name = Var.stringify(this.name).replace(/^-+/, "");
this.shortcuts = Option.parseShortcuts(init.shortcut);
this.required = Boolean(init.required);
this.valueRequired = Boolean(init.valueRequired) || this.required;
this.default = init.default;
}
示例5: splitByWidth
public static splitByWidth(str: string, width: number) {
str = Var.stringify(str);
if (this.width(str) <= width) {
return [str];
}
const graphemes = Unicode.getGraphemes(str);
return Arr.chunk(graphemes, width)
.map((a) => Str.padRight(a.join(""), width, " "));
}
示例6: getSynopsis
public getSynopsis() {
const messages = [super.getSynopsis()];
const choices = this.getChoices();
const keys: any[] = KV.keys(choices); // todo: remove any[], typescript issue at keys.map
const widths = keys.map((key: any) => StrUtil.width(key));
const width = Math.max(...widths);
for (const [key, value] of KV.entries(choices)) {
const k = Var.stringify(key);
messages.push(` [<comment>${Str.padLeft(k, width)}</comment>] ${value}`);
}
return messages.join("\n");
}
示例7: getCompletions
public getCompletions(text: string): string[] {
const values = this.options.autocomplete;
if (!values) {
return [];
}
const set = new Set<string>();
for (const value of values) {
const str = Var.stringify(value);
if (str.startsWith(text)) {
set.add(str);
}
}
return [...set];
}
示例8: write
public write(messages: string | string[] = [], init: Partial<IOutputWriteOptions> = {}) {
const options = {
type: OutputType.NORMAL,
verbosity: OutputVerbosity.NORMAL,
newline: 0,
...init,
};
messages = Arr.cast(messages);
if (options.verbosity > this.getVerbosity()) {
return this;
}
const formatter = this.getFormatter();
for (let message of messages) {
message = Var.stringify(message);
switch (options.type) {
case OutputType.PLAIN:
message = StrUtil.stripTags(formatter.decorate(message));
break;
case OutputType.RAW:
break;
case OutputType.NORMAL:
default:
message = formatter.decorate(message);
break;
}
this.doWrite(message);
this.newLine(options.newline);
}
return this;
}
示例9: renderError
public renderError(e: Error) {
const stack = ErrorStackParser.parse(e);
const output = this.errorOutput;
let len = 0;
let title = "";
const message = Var.stringify(e.message).trim();
if ("" === message || output.isVerbose()) {
title = ` [${e.name || e.constructor.name}] `;
len = StrUtil.width(title);
}
const width = output.getWidth();
const lines: Array<[string, number]> = [];
if (message) {
for (const line of message.split(/\r?\n/)) {
for (const chunk of StrUtil.splitByWidth(line, width - 4)) {
const lineLength = StrUtil.width(chunk) + 4;
lines.push([chunk, lineLength]);
if (lineLength > len) {
len = lineLength;
}
}
}
}
const messages = [];
const emptyLine = `<error>${StrUtil.spaces(len)}</error>`;
messages.push(emptyLine);
if ("" === message || output.isVerbose()) {
messages.push(`<error>${Str.padRight(title, len)}</error>`);
}
for (const [line, lineLength] of lines) {
messages.push(`<error> ${Formatter.escape(line)} ${StrUtil.spaces(len - lineLength)}</error>`);
}
messages.push(emptyLine);
messages.push("");
if (output.isVerbose() && stack.length) {
messages.push("<comment>Error trace:</comment>");
for (const frame of stack) {
const fn = frame.functionName || "{anonymous}";
const args = (frame.args || []).join(", ");
const file = frame.fileName || "";
const line = frame.lineNumber;
const col = frame.columnNumber;
messages.push(
Formatter.format(` %s(%s) at <info>%s</info> <comment>%d:%d</comment>`, fn, args, file, line, col),
);
}
messages.push("");
messages.push("");
}
output.writeln(messages, {verbosity: OutputVerbosity.QUIET});
}
示例10: setName
public setName(name: string) {
this.name = Var.stringify(name);
return this;
}