forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#4 from ritwickdey/indian-number-format
feat: Added Indian number and currency formatter
- Loading branch information
Showing
5 changed files
with
129 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...tend/packages/superset-ui-core/src/number-format/factories/createIndianNumberFormatter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters