本文整理汇总了TypeScript中lodash.truncate函数的典型用法代码示例。如果您正苦于以下问题:TypeScript truncate函数的具体用法?TypeScript truncate怎么用?TypeScript truncate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了truncate函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: peertubeTruncate
// Consistent with .length, lodash truncate function is not
function peertubeTruncate (str: string, maxLength: number) {
const options = {
length: maxLength
}
const truncatedStr = truncate(str, options)
// The truncated string is okay, we can return it
if (truncatedStr.length <= maxLength) return truncatedStr
// Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2
// We always use the .length so we need to truncate more if needed
options.length -= truncatedStr.length - maxLength
return truncate(str, options)
}
示例2: writeSettings
app.patch('/system', (req, res) => {
if(req.body.application) {
req.body.application.taxRate = +req.body.application.taxRate;
req.body.application.businessName = _.truncate(req.body.application.businessName, { length: 50, omission: '' });
req.body.application.locationName = +req.body.application.locationName;
req.body.application.customBusinessCurrency = _.truncate(req.body.application.customBusinessCurrency, { length: 25, omission: '' });
}
if(req.body.printer) {
req.body.printer.characterWidth = +req.body.printer.characterWidth;
}
writeSettings(req.body, () => {
recordAuditMessage(req, MESSAGE_CATEGORIES.SYSTEM, `System settings were updated.`, { settings: req.body });
res.json({ flash: 'Settings updated successfully.', data: req.body });
});
});
示例3: truncate
return function truncate(string:string, maxChars:number = 50, doTruncate:boolean = true):string {
if (!doTruncate) {
return string;
}
return _.truncate(string, {
length: maxChars,
separator: /,? +/,
omission: `…`
});
}
示例4: changeMOTD
changeMOTD(player, motd: string) {
const guild: Guild = player.guild;
if(!guild.isMod(player)) return 'You do not have enough privileges to do this!';
motd = motd.trim();
if(!motd) return 'Please be nice, give them a message to look at.';
if(motd.length > 500) motd = _.truncate(motd, { length: 500 });
guild.changeMOTD(motd);
}
示例5:
export const recordErrorMessage = (req, module, message, stack, foundAt = 'Server') => {
message = _.truncate(message, { length: 500, omission: '' });
const insertRecord: any = { module, message, stack, foundAt };
insertRecord.locationId = +req.header('X-Location');
insertRecord.terminalId = req.header('X-Terminal');
if(_.isNaN(insertRecord.locationId)) {
return;
}
ErrorMessage
.forge(insertRecord)
.save();
};
示例6: normalizeActor
function normalizeActor (actor: any) {
if (!actor || !actor.url) return
if (typeof actor.url !== 'string') {
actor.url = actor.url.href || actor.url.url
}
if (actor.summary && typeof actor.summary === 'string') {
actor.summary = truncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max })
if (actor.summary.length < CONSTRAINTS_FIELDS.USERS.DESCRIPTION.min) {
actor.summary = null
}
}
return
}
示例7: switch
export const formatColumnValue = (
column: string,
value: string,
charLimit: number
): string => {
switch (column) {
case 'timestamp':
return moment(+value / 1000000).format(DEFAULT_TIME_FORMAT)
case 'procid':
case 'host':
case 'appname':
return truncateText(value, column)
case 'message':
value = (value || 'No Message Provided').replace('\\n', '')
if (value.indexOf(' ') > charLimit - 5) {
value = _.truncate(value, {length: charLimit - 5})
}
return value
}
return value
}
示例8: parseSpelExpressions
export const evaluateExpression = (context: object, value: string): IExpressionChange => {
if (!value) {
return { value, spelError: null, spelPreview: '' };
}
const stringify = (obj: any): string => {
return obj === null ? 'null' : obj === undefined ? 'undefined' : JSON.stringify(obj, null, 2);
};
try {
const exprs = parseSpelExpressions(value);
const results = exprs.map(expr => expr.eval(context));
return { value, spelError: null, spelPreview: results.join('') };
} catch (err) {
const spelError: ISpelError = {
message: null,
context: null,
contextTruncated: null,
};
if (err.name && err.message) {
if (err.name === 'NullPointerException' && err.state && err.state.activeContext) {
spelError.context = stringify(err.state.activeContext.peek());
spelError.contextTruncated = truncate(spelError.context, { length: 200 });
}
spelError.message = `${err.name}: ${err.message}`;
} else {
try {
spelError.message = JSON.stringify(err);
} catch (ignored) {
spelError.message = err.toString();
}
}
return { value, spelError, spelPreview: null };
}
};
示例9: saveProject
saveProject(formValue) {
this.projectData.update({ visibility: formValue.visibility });
this.projectData.update({ name: truncate(formValue.name, { length: 20 }) });
}
示例10:
const cleanName = (printName, length = 22) => {
return _.truncate(printName, { length, omission: '' });
};