MCPcopy Create free account
hub / github.com/apitable/apitable / str2Currency

Function str2Currency

packages/core/src/utils/string.ts:297–337  ·  view source on GitHub ↗
(
  input: string | null,
  symbol: string = '',
  digits: number = 3,
  splitter: string = ',',
  symbolAlign = SymbolAlign.default,
)

Source from the content-addressed store, hash-verified

295 * @param symbolAlign currency symbol position
296 */
297export function str2Currency(
298 input: string | null,
299 symbol: string = '',
300 digits: number = 3,
301 splitter: string = ',',
302 symbolAlign = SymbolAlign.default,
303): string | null {
304 let tempStr = ('' + input).trim();
305 let sign = '';
306
307 if (input == null || tempStr === '') {
308 return null;
309 }
310 if (tempStr.startsWith('-')) {
311 sign = '-';
312 tempStr = tempStr.substring(1);
313 }
314 if (tempStr.includes('e')) {
315 if (symbolAlign === SymbolAlign.right) {
316 return `${sign}${tempStr}${symbol}`;
317 }
318 return `${sign}${symbol}${tempStr}`;
319 }
320 if (!tempStr.includes('.')) {
321 tempStr += '.';
322 }
323
324 const regExp = new RegExp(`(\\d)(?=(\\d{${digits}})+\\.)`, 'g');
325
326 tempStr = tempStr.replace(regExp, function(_$0: string, $1: any) {
327 return $1 + splitter;
328 }).replace(/\.$/, '');
329 if (!tempStr) {
330 return null;
331 }
332
333 if (symbolAlign === SymbolAlign.right) {
334 return `${sign}${tempStr}${symbol}`;
335 }
336 return `${sign}${symbol}${tempStr}`;
337}
338
339export function truncateText(text: string, maxLength: number = 20, postfix: string = '...') {
340 if (text.length <= maxLength) return text;

Callers 4

cellValueToStringMethod · 0.90
numberFormatFunction · 0.90
cellValueToStringMethod · 0.90
setEditorValueFunction · 0.90

Calls 1

replaceMethod · 0.45

Tested by

no test coverage detected