Skip to content
This repository has been archived by the owner on Aug 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2360 from bbc/calendar-change-numerals
Browse files Browse the repository at this point in the history
Use Eastern Arabic numerals in Jalaali dates
  • Loading branch information
jamesdonoh authored Oct 14, 2019
2 parents 2f3afa7 + 35b181f commit fb577e3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/utilities/psammead-calendars/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

| Version | Description |
|---------|-------------|
| 2.0.0 | [PR#2360](https://github.com/bbc/psammead/pull/2360) Use Eastern Arabic numerals for Jalaali dates |
| 1.0.0 | [PR#2190](https://github.com/bbc/psammead/pull/2190) Initial creation of package. |
6 changes: 4 additions & 2 deletions packages/utilities/psammead-calendars/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ npm install @bbc/psammead-calendars --save

### Jalaali

The functionality of this helper is to to convert a `Gregorian` calendar moment into its `Jalaali` equivalent and outputting it as a string to be used by other components. This helper relies on `jalaali-js` which contains the logic to carry out the conversions from `Gregorian` to `Jalaali`. Documentation of this dependency can be found [here](https://www.npmjs.com/package/jalaali-js). This helper currently only supports Pashto and Persian/Farsi Jalaali calendar month names.
Converts a Gregorian calendar date represented by a [moment](https://momentjs.com) object into its Jalaali calendar equivalent and outputs it as a string to be used by other components. This helper relies on `jalaali-js` which contains the logic to carry out the conversions from Gregorian to Jalaali. Documentation of this dependency can be found [here](https://www.npmjs.com/package/jalaali-js).

This helper currently only supports Pashto and Persian/Farsi Jalaali calendar month names and Eastern Arabic numerals (translated using [psammead-locales](../psammead-locales)).

## Usage

Expand All @@ -28,7 +30,7 @@ import { jalaali } from "@bbc/psammead-calendars"
const nowMoment = moment().locale('fa');
const formattedDate = jalaali.formatDate(nowMoment);
```
The Jalaali formatter contains a function called `formatDate()`. This function takes a `moment` object as its argument, which needs to have `ps` or `fa` set as its locale. The function then uses the `moment` with it's logic to return a string in a day, year, month format e.g. `11 دی 1397`.
The Jalaali formatter contains a function called `formatDate()`. This function takes a `moment` object as its argument, which needs to have `ps` or `fa` set as its locale. The function then uses the `moment` with its logic to return a string in a day, year, month format using Eastern Arabic numerals e.g. `۱۱ دی ۱۳۹۷`.

If an unsupported `moment` or locale set on the `moment` is passed in, then the output will `return null`.

Expand Down
16 changes: 15 additions & 1 deletion packages/utilities/psammead-calendars/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/utilities/psammead-calendars/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bbc/psammead-calendars",
"version": "1.0.0",
"version": "2.0.0",
"main": "dist/index.js",
"module": "esm/index.js",
"sideEffects": false,
Expand All @@ -19,6 +19,7 @@
},
"homepage": "https://github.com/BBC-News/psammead/blob/latest/packages/components/psammead-calendars/README.md",
"dependencies": {
"@bbc/psammead-locales": "^2.23.0",
"jalaali-js": "1.1.0"
},
"peerDependencies": {
Expand Down
15 changes: 12 additions & 3 deletions packages/utilities/psammead-calendars/src/calendars/jalaali.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const jalaaliJs = require('jalaali-js');
import jalaaliJs from 'jalaali-js';
import {
EasternArabic,
makeNumeralTranslator,
} from '@bbc/psammead-locales/numerals';

const jalaaliMonths = {
fa: [
Expand Down Expand Up @@ -31,6 +35,8 @@ const jalaaliMonths = {
],
};

const useEasternNumerals = makeNumeralTranslator(EasternArabic);

// Helper function to check if argument passed through is a supported moment
const isSupportedMoment = moment => {
if (
Expand All @@ -53,9 +59,12 @@ const jalaali = {
gregorianMoment.date(),
);
const localeJalaaliMonths = jalaaliMonths[gregorianMoment.locale()];

const jalaaliDay = useEasternNumerals(jalaaliDate.jd.toString());
const jalaaliMonth = localeJalaaliMonths[jalaaliDate.jm - 1];
const output = `${jalaaliDate.jd} ${jalaaliMonth} ${jalaaliDate.jy}`;
return output;
const jalaaliYear = useEasternNumerals(jalaaliDate.jy.toString());

return `${jalaaliDay} ${jalaaliMonth} ${jalaaliYear}`;
}
return null;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ describe('Jalaali Conversion Tests', () => {
const testScenarios = [
{
testMoment: moment('2019-01-01').locale('fa'),
expected: '11 دی 1397',
expected: '۱۱ دی ۱۳۹۷',
summary:
'should return first day of the year 2019 in Jalaali for persian',
},
{
testMoment: moment('2019-12-31').locale('fa'),
expected: '10 دی 1398',
expected: '۱۰ دی ۱۳۹۸',
summary: 'should return last day of the year 2019 in Jalaali for persian',
},
{
testMoment: moment('2025-02-01').locale('fa'),
expected: '13 بهمن 1403',
expected: '۱۳ بهمن ۱۴۰۳',
summary: 'should return first day of Febuary 2025 in Jalaali for persian',
},
{
Expand All @@ -27,17 +27,17 @@ describe('Jalaali Conversion Tests', () => {
},
{
testMoment: moment('2019-01-01').locale('ps'),
expected: '11 مرغومی 1397',
expected: '۱۱ مرغومی ۱۳۹۷',
summary: 'should return first day of the year 2019 in Jalaali for pashto',
},
{
testMoment: moment('2019-12-31').locale('ps'),
expected: '10 مرغومی 1398',
expected: '۱۰ مرغومی ۱۳۹۸',
summary: 'should return last day of the year 2019 in Jalaali for pashto',
},
{
testMoment: moment('2025-02-01').locale('ps'),
expected: '13 سلواغه 1403',
expected: '۱۳ سلواغه ۱۴۰۳',
summary: 'should return first day of Febuary 2025 in Jalaali for pashto',
},
{
Expand Down

0 comments on commit fb577e3

Please sign in to comment.