Skip to content

Commit

Permalink
feat: add getPhoneCodeForWilaya
Browse files Browse the repository at this point in the history
- Added getPhoneCodeForWilaya method that returns the first phone code from a list of phone codes
for a given wilaya

re dzcode-io#19
  • Loading branch information
joeylnguyen committed Oct 5, 2020
1 parent ddd3b78 commit 75b9b3c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ const { getPhoneCodesForWilaya } = require('@dzcode-io/leblad');
console.log(getPhoneCodesForWilaya(31)); //returns list of phone codes for wilaya 31
```

#### getPhoneCodeForWilaya(wilayaCode: number)

Takes a wilaya code (matricule) and returns the first phone code from a list of phone codes for given wilaya

**Arguments**

- `wilayaCode: number` (**required**) the Wilaya's "matricule"

**Examples**

```javascript
const { getPhoneCodeForWilaya } = require('@dzcode-io/leblad');

console.log(getPhoneCodeForWilaya(16)); //returns first phone code for wilaya 16
```

## Helper methods

#### wilayaProjection(wilaya: (object|array), projection?: string[])
Expand Down
21 changes: 21 additions & 0 deletions src/api/getPhoneCodeForWilaya.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const getPhoneCodesForWilaya = require('./getPhoneCodesForWilaya');

const getPhoneCodeForWilaya = data =>
/**
* Takes a wilaya code (mattricule) and returns the first phone code in wilaya's phone code list.
*
* @example Get phone codes for Alger (code: 16)
*
* //returns 21
* getPhoneCodeForWilaya(16)
*
* @param { Number } wilayaCode wilaya code (mattricule)
* @returns { Number | Undefined } Returns the first phone code for wilaya, or undefined
*/

(wilayaCode) => {
const phoneCodes = getPhoneCodesForWilaya(data)(wilayaCode) || [];
return phoneCodes[0];
};

module.exports = getPhoneCodeForWilaya;
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const getWilayaByZipCode = require('./api/getWilayaByZipCode');
const getZipCodesForWilaya = require('./api/getZipCodesForWilaya');
const getDairatsForWilaya = require('./api/getDairatsForWilaya');
const getPhoneCodesForWilaya = require('./api/getPhoneCodesForWilaya');
const getPhoneCodeForWilaya = require('./api/getPhoneCodeForWilaya');

const data = require('../data/WilayaList.json');

Expand All @@ -23,6 +24,7 @@ module.exports = {
getZipCodesForWilaya: getZipCodesForWilaya(_getData()),
getDairatsForWilaya: getDairatsForWilaya(_getData()),
getPhoneCodesForWilaya: getPhoneCodesForWilaya(_getData()),
getPhoneCodeForWilaya: getPhoneCodeForWilaya(_getData()),
utils: {
wilayaProjection,
isValidWilayaCode,
Expand Down
51 changes: 51 additions & 0 deletions tests/api/getPhoneCodeForWilaya.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
describe('get phone codes for a wilaya', ()=> {
const mockData = [{
mattricule: 16,
name: "Alger",
phoneCodes: [21, 23]
},
{
mattricule: 31,
name: "Oran",
phoneCodes: [41]
},
{
mattricule: 48,
name: "Relizane",
phoneCodes: [46],
}];

let getPhoneCodeForWilaya;

beforeEach(()=> {
getPhoneCodeForWilaya = require('../../src/api/getPhoneCodeForWilaya');
});

it('should export a function', () => {
expect(typeof getPhoneCodeForWilaya).toBe('function');
});

it('should return a curried function that returns the data', () => {
const fn = getPhoneCodeForWilaya(mockData);

expect(typeof fn).toBe('function');
});

it('should return undefined if the phone codes for the given wilaya code could not be found', () => {
const result = getPhoneCodeForWilaya(mockData)(9);

expect(result).toBeUndefined();
});

it('should return the correct phone code for a given wilaya', () => {
const result = getPhoneCodeForWilaya(mockData)(31);

expect(result).toEqual(41);
});

it('should return the first phone code in an array of multiple phone codes for a given wilaya', () => {
const result = getPhoneCodeForWilaya(mockData)(16);

expect(result).toEqual(21);
});
});
1 change: 1 addition & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe("Le'Bled SDK", () => {
getWilayaByZipCode: expect.any(Function),
getDairatsForWilaya: expect.any(Function),
getPhoneCodesForWilaya: expect.any(Function),
getPhoneCodeForWilaya: expect.any(Function),
utils: {
wilayaProjection: expect.any(Function),
isValidZipCode: expect.any(Function),
Expand Down

0 comments on commit 75b9b3c

Please sign in to comment.