Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added en-IN localization #655

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Joe Bordes (https://github.com/joebordes)
Johannes Kettmann (https://github.com/jkettmann)
Jordy de Jong (https://github.com/jordydejong)
Kable Monck (https://github.com/KableM)
Kathiresan Senthil (https://github.com/theY2Kbug)
Kukuh Yoniatmoko (http://github.com/kukuhyoniatmoko)
Lars Bauer (https://github.com/LarsBauer)
Lauris Bukšis-Haberkorns (https://github.com/Lafriks)
Expand Down
25 changes: 15 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
### Next

### 2.5.0

- Added en-IN localization. Thanks @theY2Kbug


### 2.4.0

- Update a ton of dependencies. Thanks @BenjaminVanRyseghem
- Update the tool-chain. Thanks @BenjaminVanRyseghem


### 2.3.6

- Fix spacing issue with en-GB currency. Thanks @DamienCassou
Expand Down Expand Up @@ -224,17 +230,16 @@

- Introduce new API functions:

- setCulture
- culture
- cultureData
- cultures

- setCulture
- culture
- cultureData
- cultures
- Deprecate API functions:

- setLanguage
- language
- languageData
- languages
- setLanguage
- language
- languageData
- languages

Those deprecated functions will be removed in version 2.0.0

Expand Down Expand Up @@ -328,6 +333,6 @@ Those deprecated functions will be removed in version 2.0.0

Fork `numeraljs` v1.5.3, renaming everything to `numbro`

----
---

_For changes before `numbro` forked [`numeral-js`](https://github.com/adamwdraper/Numeral-js), see [CHANGELOG-Numeraljs.md](CHANGELOG-Numeraljs.md)._
18 changes: 6 additions & 12 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
## Develop your contribution

1. Fork the library

1. Run `npm install` to install dependencies

1. Add your tests to the files in `/tests`

1. To test your tests, run `npm test`

1. When all your tests are passing, submit a pull request to the `develop` branch.
2. Run `npm ci` to install dependencies
3. Add your tests to the files in `/tests`
4. To test your tests, run `npm test`
5. When all your tests are passing, submit a pull request to the `develop` branch.

## Pull request

1. All pull requests should be submitted to the `develop` branch.

1. Add yourself in the `AUTHORS.md` list (Alphabetically sorted)

1. add a line in `CHANGELOG.md` to explain your changes
2. Add yourself in the `AUTHORS.md` list (Alphabetically sorted)
3. add a line in `CHANGELOG.md` to explain your changes
60 changes: 60 additions & 0 deletions languages/en-IN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*!
* numbro.js language configuration
* language : English
* locale: India
* author : Kathiresan Senthil : https://github.com/theY2Kbug
*/


module.exports = {
languageTag: "en-IN",
delimiters: {
thousands: ",",
decimal: ".",
thousandsSize: 2
},
abbreviations: {
thousand: "k",
lakh: "\u004c", //L
crore: "\u0043\u0072" //Cr
},
ordinal: number => {
let b = number % 10;
return (~~(number % 100 / 10) === 1) ? "th" : (b === 1) ? "st" : (b === 2) ? "nd" : (b === 3) ? "rd" : "th";
},
currency: {
symbol: "₹",
position: "prefix",
code: "INR"
},
currencyFormat: {
thousandSeparated: true,
totalLength: 4,
spaceSeparated: false,
spaceSeparatedCurrency: false,
average: true
},
formats: {
fourDigits: {
totalLength: 4,
spaceSeparated: false,
average: true
},
fullWithTwoDecimals: {
output: "currency",
thousandSeparated: true,
spaceSeparated: false,
mantissa: 2
},
fullWithTwoDecimalsNoCurrency: {
mantissa: 2,
thousandSeparated: true
},
fullWithNoDecimals: {
output: "currency",
thousandSeparated: true,
spaceSeparated: false,
mantissa: 0
}
}
};
46 changes: 43 additions & 3 deletions src/formatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const BigNumber = require("bignumber.js");
const powers = {
trillion: Math.pow(10, 12),
billion: Math.pow(10, 9),
crore: Math.pow(10, 7),
million: Math.pow(10, 6),
lakh: Math.pow(10, 5),
thousand: Math.pow(10, 3)
};

Expand Down Expand Up @@ -368,23 +370,45 @@ function computeAverage({ value, forceAverage, lowPrecision = true, abbreviation
if (forceAverage && abbreviations[forceAverage] && powers[forceAverage]) {
abbreviation = abbreviations[forceAverage];
value = value / powers[forceAverage];
} else {
}
else if (Object.keys(abbreviations).length === 3) { //Conditional check subject to change, currently checking if only three abbr. are present in the language
if (abs >= powers.crore || (lowPrecision && roundingFunction(abs / powers.crore) === 1)){
// crore
abbreviation = abbreviations.crore;
value = value / powers.crore;

} else if (abs < powers.crore && abs >= powers.lakh || (lowPrecision && roundingFunction(abs / powers.lakh) === 1)) {
// lakh
abbreviation = abbreviations.lakh;
value = value / powers.lakh;

} else {
// thousand
abbreviation = abbreviations.thousand;
value = value / powers.thousand;
}
}
else {
if (abs >= powers.trillion || (lowPrecision && roundingFunction(abs / powers.trillion) === 1)) {
// trillion
abbreviation = abbreviations.trillion;
value = value / powers.trillion;
} else if (abs < powers.trillion && abs >= powers.billion || (lowPrecision && roundingFunction(abs / powers.billion) === 1)) {
}
else if (abs < powers.trillion && abs >= powers.billion || (lowPrecision && roundingFunction(abs / powers.billion) === 1)) {
// billion
abbreviation = abbreviations.billion;
value = value / powers.billion;

} else if (abs < powers.billion && abs >= powers.million || (lowPrecision && roundingFunction(abs / powers.million) === 1)) {
// million
abbreviation = abbreviations.million;
value = value / powers.million;

} else if (abs < powers.million && abs >= powers.thousand || (lowPrecision && roundingFunction(abs / powers.thousand) === 1)) {
// thousand
abbreviation = abbreviations.thousand;
value = value / powers.thousand;

}
}

Expand Down Expand Up @@ -603,7 +627,6 @@ function indexesOfGroupSpaces(totalLength, groupSize) {
}
counter++;
}

return result;
}

Expand Down Expand Up @@ -636,6 +659,23 @@ function replaceDelimiters(output, value, thousandSeparated, state, decimalSepar
}

let indexesToInsertThousandDelimiters = indexesOfGroupSpaces(characteristic.length, thousandsSize);
let currentLanguage = "";
try {
currentLanguage = state.currentLanguage();
}
catch (e){
"pass";
// console.log(e.name + ": " + e.message + ": "+ e.fileName + ": "+ e.lineNumber);
}

if (currentLanguage === "en-IN"){
indexesToInsertThousandDelimiters = indexesToInsertThousandDelimiters.map((separatorPosition) => separatorPosition-1); // Separate thousands by 2 places at a time and shift all the separator indexes to left by 1

if (characteristic.length%2 !== 0) { //If the length of the number is odd, then there is a separator at position-0
indexesToInsertThousandDelimiters.shift(); // Drop the separator at position-0 i.e. the separator shifted before the number
}
}

indexesToInsertThousandDelimiters.forEach((position, index) => {
characteristic = characteristic.slice(0, position + index) + thousandSeparator + characteristic.slice(position + index);
});
Expand Down
6 changes: 6 additions & 0 deletions src/unformatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,15 @@ function computeUnformattedValue(inputString, delimiters, currencySymbol, ordina
case "thousand":
factor = Math.pow(10, 3);
break;
case "lakh":
factor = Math.pow(10, 5);
break;
case "million":
factor = Math.pow(10, 6);
break;
case "crore":
factor = Math.pow(10, 7);
break;
case "billion":
factor = Math.pow(10, 9);
break;
Expand Down
18 changes: 15 additions & 3 deletions src/validating.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ const validOutputValues = [
const validForceAverageValues = [
"trillion",
"billion",
"crore",
"million",
"lakh",
"thousand"
];

Expand All @@ -59,17 +61,25 @@ const validMandatoryAbbreviations = {
type: "string",
mandatory: true
},
lakh: {
type: "string",
mandatory: false
},
million: {
type: "string",
mandatory: true
mandatory: false
},
crore: {
type: "string",
mandatory: false
},
billion: {
type: "string",
mandatory: true
mandatory: false
},
trillion: {
type: "string",
mandatory: true
mandatory: false
}
},
mandatory: true
Expand All @@ -79,7 +89,9 @@ const validAbbreviations = {
type: "object",
children: {
thousand: "string",
lakh: "string",
million: "string",
crore: "string",
billion: "string",
trillion: "string"
}
Expand Down
Loading