Skip to content

Commit

Permalink
Merge pull request apache#4 from ritwickdey/indian-number-format
Browse files Browse the repository at this point in the history
feat: Added Indian number and currency formatter
  • Loading branch information
mittalsuraj18 authored Mar 7, 2023
2 parents ed9c99d + 90a4650 commit abfa03d
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export const D3_NUMBER_FORMAT_DESCRIPTION_PERCENTAGE_TEXT = t(

// input choices & options
export const D3_FORMAT_OPTIONS: [string, string][] = [
[NumberFormats.INDIAN_FORMAT_NUMBER, t('Indian format')],
[NumberFormats.INDIAN_FORMAT_CURRENCY, t('Indian currency format')],
[NumberFormats.INDIAN_FORMAT_NUMBER_SHORT, t('Indian format (in units)')],
[NumberFormats.INDIAN_FORMAT_CURRENCY_SHORT, t('Indian currency format (in units)')],
[NumberFormats.SMART_NUMBER, t('Adaptive formatting')],
['~g', t('Original value')],
[',d', ',d (12345.432 => 12,345)'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ const SI = SI_3_DIGIT;
const SMART_NUMBER = 'SMART_NUMBER';
const SMART_NUMBER_SIGNED = 'SMART_NUMBER_SIGNED';
const OVER_MAX_HIDDEN = 'OVER_MAX_HIDDEN';
const INDIAN_FORMAT_NUMBER = 'INDIAN_FORMAT_NUMBER';
const INDIAN_FORMAT_CURRENCY = 'INDIAN_FORMAT_CURRENCY';

const INDIAN_FORMAT_NUMBER_SHORT = 'INDIAN_FORMAT_NUMBER_SHORT';
const INDIAN_FORMAT_CURRENCY_SHORT = 'INDIAN_FORMAT_CURRENCY_SHORT';

const NumberFormats = {
DOLLAR,
Expand Down Expand Up @@ -84,6 +89,10 @@ const NumberFormats = {
SMART_NUMBER,
SMART_NUMBER_SIGNED,
OVER_MAX_HIDDEN,
INDIAN_FORMAT_NUMBER,
INDIAN_FORMAT_CURRENCY,
INDIAN_FORMAT_NUMBER_SHORT,
INDIAN_FORMAT_CURRENCY_SHORT,
};

export default NumberFormats;
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// import { format as d3Format } from 'd3-format';
import NumberFormatter from '../NumberFormatter';

// const float2PointFormatter = d3Format(`.2~f`);
const indianLocaleFormatter = Intl.NumberFormat('en-IN', {
maximumFractionDigits: 2,
minimumFractionDigits: 0
}).format // d3Format or regex whould be alternative

function formatIndianUnitNumber(
value: number,
{ refoldAfterCrs = false },
): string {
let formated_number = 0;
let sufix = [];

do {
if (value >= 10000000) {
formated_number = value / 10000000;
sufix.unshift('Cr');
} else if (value >= 100000) {
formated_number = value / 100000;
sufix.unshift('L');
} else if (value >= 1000) {
formated_number = value / 1000;
sufix.unshift('K');
} else {
formated_number = value;
}
value = formated_number;
} while (refoldAfterCrs && value >= 1000);

return indianLocaleFormatter(value) + sufix.join(' ');
}

function formatIndianNumber(value: number): string {
return indianLocaleFormatter(value)
}

export default function createIndianNumberFormatter(
config: {
prefix?: string;
foldToUnit?: boolean;
refoldAfterCrs?: boolean;
id?: string;
label?: string;
description?: string;
} = {
prefix: '',
foldToUnit: false,
refoldAfterCrs: false,
},
) {
const { prefix, foldToUnit, refoldAfterCrs, id, label, description } = config;

return new NumberFormatter({
description: description || 'Indian Number Formatter',
formatFunc: function (value) {
const sign = value >= 0 ? '' : '-';
debugger
const formattedNumber = foldToUnit
? formatIndianUnitNumber(Math.abs(value), { refoldAfterCrs })
: formatIndianNumber(Math.abs(value));
return sign + prefix + formattedNumber;
},
id: id || 'indian_nunber_formatter',
label: label || 'Indian Number Formatter',
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ export {
export { default as NumberFormatterRegistry } from './NumberFormatterRegistry';
export { default as createD3NumberFormatter } from './factories/createD3NumberFormatter';
export { default as createDurationFormatter } from './factories/createDurationFormatter';
export { default as createIndianNumberFormatter } from './factories/createIndianNumberFormatter';
export { default as createSiAtMostNDigitFormatter } from './factories/createSiAtMostNDigitFormatter';
export { default as createSmartNumberFormatter } from './factories/createSmartNumberFormatter';
46 changes: 27 additions & 19 deletions superset-frontend/src/setup/setupFormatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
getTimeFormatterRegistry,
smartDateFormatter,
smartDateVerboseFormatter,
createD3NumberFormatter,
createIndianNumberFormatter,
} from '@superset-ui/core';

export default function setupFormatters() {
Expand Down Expand Up @@ -69,27 +69,35 @@ export default function setupFormatters() {
createDurationFormatter({ formatSubMilliseconds: true }),
)
.registerValue(
'CURRENCY_INDIA',
createD3NumberFormatter({
locale: {
decimal: '.',
thousands: ',',
grouping: [3, 2, 2, 2, 2, 2, 2, 2, 2, 2],
currency: ['₹', ''],
},
formatString: '$,.0f',
NumberFormats.INDIAN_FORMAT_NUMBER,
createIndianNumberFormatter({
prefix: '',
foldToUnit: false,
refoldAfterCrs: false,
}),
)
.registerValue(
'NUMBER_INDIA',
createD3NumberFormatter({
locale: {
decimal: '.',
thousands: ',',
grouping: [3, 2, 2, 2, 2, 2, 2, 2, 2, 2],
currency: ['', ''],
},
formatString: '$,.0f',
NumberFormats.INDIAN_FORMAT_CURRENCY,
createIndianNumberFormatter({
prefix: '₹',
foldToUnit: false,
refoldAfterCrs: false,
}),
)
.registerValue(
NumberFormats.INDIAN_FORMAT_NUMBER_SHORT,
createIndianNumberFormatter({
prefix: '',
foldToUnit: true,
refoldAfterCrs: true,
}),
)
.registerValue(
NumberFormats.INDIAN_FORMAT_CURRENCY_SHORT,
createIndianNumberFormatter({
prefix: '₹',
foldToUnit: true,
refoldAfterCrs: false,
}),
);

Expand Down

0 comments on commit abfa03d

Please sign in to comment.