MCPcopy Create free account
hub / github.com/angular/angular / parseNumberFormat

Function parseNumberFormat

packages/common/src/i18n/format_number.ts:307–363  ·  view source on GitHub ↗
(format: string, minusSign = '-')

Source from the content-addressed store, hash-verified

305}
306
307function parseNumberFormat(format: string, minusSign = '-'): ParsedNumberFormat {
308 const p = {
309 minInt: 1,
310 minFrac: 0,
311 maxFrac: 0,
312 posPre: '',
313 posSuf: '',
314 negPre: '',
315 negSuf: '',
316 gSize: 0,
317 lgSize: 0,
318 };
319
320 const patternParts = format.split(PATTERN_SEP);
321 const positive = patternParts[0];
322 const negative = patternParts[1];
323
324 const positiveParts =
325 positive.indexOf(DECIMAL_SEP) !== -1
326 ? positive.split(DECIMAL_SEP)
327 : [
328 positive.substring(0, positive.lastIndexOf(ZERO_CHAR) + 1),
329 positive.substring(positive.lastIndexOf(ZERO_CHAR) + 1),
330 ],
331 integer = positiveParts[0],
332 fraction = positiveParts[1] || '';
333
334 p.posPre = integer.substring(0, integer.indexOf(DIGIT_CHAR));
335
336 for (let i = 0; i < fraction.length; i++) {
337 const ch = fraction.charAt(i);
338 if (ch === ZERO_CHAR) {
339 p.minFrac = p.maxFrac = i + 1;
340 } else if (ch === DIGIT_CHAR) {
341 p.maxFrac = i + 1;
342 } else {
343 p.posSuf += ch;
344 }
345 }
346
347 const groups = integer.split(GROUP_SEP);
348 p.gSize = groups[1] ? groups[1].length : 0;
349 p.lgSize = groups[2] || groups[1] ? (groups[2] || groups[1]).length : 0;
350
351 if (negative) {
352 const trunkLen = positive.length - p.posPre.length - p.posSuf.length,
353 pos = negative.indexOf(DIGIT_CHAR);
354
355 p.negPre = negative.substring(0, pos).replace(/'/g, '');
356 p.negSuf = negative.slice(pos + trunkLen).replace(/'/g, '');
357 } else {
358 p.negPre = minusSign + p.posPre;
359 p.negSuf = p.posSuf;
360 }
361
362 return p;
363}
364

Callers 3

formatCurrencyFunction · 0.85
formatPercentFunction · 0.85
formatNumberFunction · 0.85

Calls 4

indexOfMethod · 0.80
charAtMethod · 0.80
sliceMethod · 0.65
replaceMethod · 0.45

Tested by

no test coverage detected