function fuseNumberWithCustomFormatLeft()

in src/formattingService/formattingService.ts [1024:1122]


    function fuseNumberWithCustomFormatLeft(value: string, format: string, numberFormatInfo: GlobalizeNumberFormat, suppressModifyValue?: boolean): string {
        let groupSymbolIndex = format.indexOf(",");
        let enableGroups = groupSymbolIndex > -1 && groupSymbolIndex < Math.max(format.lastIndexOf("0"), format.lastIndexOf("#")) && numberFormatInfo[","];
        let groupDigitCount = 0;
        let groupIndex = 0;
        let groupSizes = numberFormatInfo.groupSizes || [3];
        let groupSize = groupSizes[0];
        let groupSeparator = numberFormatInfo[","];
        let sign = "";
        let firstChar = value.charAt(0);
        if (firstChar === "+" || firstChar === "-") {
            sign = numberFormatInfo[firstChar];
            value = value.substr(1);
        }
        let isZero = value === "0";
        let result = "";
        let leftBuffer = "";
        let vi = value.length - 1;
        let fmtOnly = true;
        // Iterate through format chars and replace 0 and # with the digits from the value string
        for (let fi = format.length - 1; fi > -1; fi--) {
            let formatChar = format.charAt(fi);
            switch (formatChar) {
                case ZeroPlaceholder:
                case DigitPlaceholder:
                    fmtOnly = false;
                    if (leftBuffer !== "") {
                        result = leftBuffer + result;
                        leftBuffer = "";
                    }
                    if (!suppressModifyValue) {
                        if (vi > -1 || formatChar === ZeroPlaceholder) {
                            if (enableGroups) {
                                // If the groups are enabled we'll need to keep track of the current group index and periodically insert group separator,
                                if (groupDigitCount === groupSize) {
                                    result = groupSeparator + result;
                                    groupIndex++;
                                    if (groupIndex < groupSizes.length) {
                                        groupSize = groupSizes[groupIndex];
                                    }
                                    groupDigitCount = 1;
                                } else {
                                    groupDigitCount++;
                                }
                            }
                        }
                        if (vi > -1) {
                            if (isZero && formatChar === DigitPlaceholder) {
                                // Special case - if we need to format a zero value and the # symbol is used - we don't copy it into the result)
                            } else {
                                result = value.charAt(vi) + result;
                            }
                            vi--;
                        } else if (formatChar !== DigitPlaceholder) {
                            result = formatChar + result;
                        }
                    }
                    break;
                case ",":
                    // We should skip all the , chars
                    break;
                default:
                    leftBuffer = formatChar + leftBuffer;
                    break;
            }
        }

        // If the value didn't fit into the number of zeros provided in the format then we should insert the missing part of the value into the result
        if (!suppressModifyValue) {
            if (vi > -1 && result !== "") {
                if (enableGroups) {
                    while (vi > -1) {
                        if (groupDigitCount === groupSize) {
                            result = groupSeparator + result;
                            groupIndex++;
                            if (groupIndex < groupSizes.length) {
                                groupSize = groupSizes[groupIndex];
                            }
                            groupDigitCount = 1;
                        } else {
                            groupDigitCount++;
                        }
                        result = value.charAt(vi) + result;
                        vi--;
                    }
                } else {
                    result = value.substr(0, vi + 1) + result;
                }
            }
            // Insert sign in front of the leftBuffer and result
            return sign + leftBuffer + result;
        }

        if (fmtOnly)
            // If the format doesn't specify any digits to be displayed, then just return the format we've parsed up until now.
            return sign + leftBuffer + result;

        return sign + leftBuffer + value + result;
    }