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

Feat/web api missing functions #69

Open
wants to merge 12 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
92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,98 @@ const { getDairaByBaladyiaName } = require('@dzcode-io/leblad');
console.log(getDairaByBaladyiaName('ES SENIA')); // will print the daira object ({name: "ES SENIA"...})
```

#### getBaladiyaByCodeAndDairaCodeAndWilayaCode(mattricule:number, code: number, bcode:number, projection?: string[])

Takes a Wialya code , Daira code and Baladyia code and returns a baladiya.

**Arguments**

- `mattricule: number` (**required**) the Wilaya id
- `code: number` (**required**) the Daira id
- `bcode: number` (**required**) the Baladiya id
- `projection: string[]` (optional) Array of Wilaya Object attributes

**Examples**

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

console.log(getBaladiyaByCodeAndDairaCodeAndWilayaCode(1, 101, 101)); // will print the daira object ({name: "ADRAR"...})
```

#### getBaladiyaByCodeAndDairaCodeAndWilayaCode(mattricule:number, code: number, bcode:number, projection?: string[])

Takes a Wialya mattricule , Daira code and Baladyia code and returns a baladiya.

**Arguments**

- `mattricule: number` (**required**) the Wilaya id
- `code: number` (**required**) the Daira id
- `bcode: number` (**required**) the Baladiya id
- `projection: string[]` (optional) Array of Wilaya Object attributes

**Examples**

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

console.log(getBaladiyaByCodeAndDairaCodeAndWilayaCode(1, 101, 101)); // will print the baladiya object ({name: "ADRAR"...})
```

#### getBaldiyaByCodeForWilaya(mattricule:number, code: number, projection?: string[])

Takes a Wialya mattricule, Baladyia code and returns a baladiya.

**Arguments**

- `mattricule: number` (**required**) the Wilaya id
- `code: number` (**required**) the Baladiya id
- `projection: string[]` (optional) Array of Baladiya Object attributes

**Examples**

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

console.log(getBaldiyaByCodeForWilaya(1, 121)); // will print the baldiya object ({name: "OULED AHMED TIMMI"...})
```

#### getBaldiyatsForDairaByCodeForWilayaByCode(mattricule:number, code: number, projection?: string[])

Takes a Wialya mattricule and Daira code and returns a list of baladiyats.

**Arguments**

- `mattricule: number` (**required**) the Wilaya id
- `code: number` (**required**) the Daira id
- `projection: string[]` (optional) Array of Baladiya Object attributes

**Examples**

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

console.log(getBaldiyatsForDairaByCodeForWilayaByCode(1, 101)); // will print the list of baldiyats ([{name: "OULED AHMED TIMMI"...}, {....} , {....} ...])
```

#### getDairaByCodeAndWilayaCode(mattricule:number, code: number, projection?: string[])

Takes a Wialya mattricule and Daira code and returns a daira.

**Arguments**

- `mattricule: number` (**required**) the Wilaya id
- `code: number` (**required**) the Daira id
- `projection: string[]` (optional) Array of Daira Object attributes

**Examples**

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

console.log(getDairaByCodeAndWilayaCode (1, 103)); // will print the daira object ({name: "CHAROUINE"...},)
```

## Helper methods

#### projectObject(data: (object|array), projection?: string[])
Expand Down
31 changes: 31 additions & 0 deletions src/api/getBaladiyaByCodeAndDairaCodeAndWilayaCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const projectBaladiya = require('../utils/projections/projectObject');

const getBaladiyaByCodeAndDairaCodeAndWilayaCode = (data) =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do you have any use case for such a feature? I believe that Baladyia code is unique overall, so if you'd use it it won't matter to add extra params. And if you want to get a baladyia by wilaya or daira code you'd just use their methods with a projection

Copy link
Member Author

@abderrahmaneMustapha abderrahmaneMustapha Jun 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please explain how can i get the same result by using projection

Copy link
Collaborator

@Fcmam5 Fcmam5 Jun 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have these methods in place:

const {
  getBaladyiatsForWilaya,
  getBaladyiatsForDaira
} = require("@dzcode-io/leblad");

const rs1 = getBaladyiatsForWilaya(31); // if this wasn't implemented, we'd use getWilayaByCode and project only "dairats"

const rs2 = getBaladyiatsForDaira("ORAN");

Maybe it'd be nice to have another getBaladyiatsForDairaCode method. If you want to create it, please do it on a separate PR, based on develop

/**
* Takes a wilaya mattricule ,a daira code , bladiya code and return the baladyia.
*
* @example Get a baladiya by code, daira code, and wilaya mattricule(mattricule:1, code:101, bcode:101)
*
* //returns {name : "ADRAR"}
* getWilayaByBaladyiaName(1, 101,101)
*
* @param { integer } mattricule wilaya mattricule
* @param { integer } code daira code
* @param { integer } bcode baladiya code
Comment on lines +12 to +14
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean?

Suggested change
* @param { integer } mattricule wilaya mattricule
* @param { integer } code daira code
* @param { integer } bcode baladiya code
* @param { number } mattricule wilaya mattricule
* @param { number } code daira code
* @param { number } bcode baladiya code

* @param { String[] } projection a list of Baladyia object attributes to keep
* @returns { Object | null } Returns a baldiya's object, or null
*/
(mattricule, code, bcode, projection)=>{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix linting issues

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked everything and learned a lot from it 😁; there is no cause to dislike you, and thank you very much for your valuable feedbacks.

const wilaya = data.find((w) => w.mattricule === mattricule);
if (wilaya) {
const daira = wilaya.dairats.find((d)=> d.code === code);
if( daira){
const baladiya = daira.baladyiats.find((b)=> b.code === bcode);
return projectBaladiya(baladiya, projection);
}

}
return null;
};

module.exports = getBaladiyaByCodeAndDairaCodeAndWilayaCode;
38 changes: 38 additions & 0 deletions src/api/getBaldiyaByCodeForWilaya.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const projectBaladiya = require('../utils/projections/projectObject');

const getBaldiyaByCodeForWilaya = (data) =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd name it getBaladyiaByWilayaCode. But again, why we don't just use getWilayaByCode then call the projection.

/**
* Takes a wilaya code (mattricule) and a baladiya code and return a baladiya
*
* @example Get the baladiya of adrar in adrara(mattricule:1 ,code: 101)
*
*
* getBaldiyaByCodeForWilaya(1, 101)
*
* @param { Number } mattricule wilaya code (mattricule)
* @param { Number } code baladiya code (code)
Comment on lines +12 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check jsdoc here and in all changed files

Suggested change
* @param { Number } mattricule wilaya code (mattricule)
* @param { Number } code baladiya code (code)
* @param { number } mattricule wilaya code (mattricule)
* @param { number } code baladiya code (code)

* @param { String[] } projection a list of Baladyia object attributes to keep
* @returns { Object[] | null } get baladiya's object
*/

(mattricule, code, projections) => {
const wilaya = data.find((w) => w.mattricule === mattricule);
if (wilaya) {
const baladyiats = wilaya.dairats.reduce(
(acc, daira) => [...acc, ...daira.baladyiats],
[],
);

const baladiya = baladyiats.find((w)=> w.code === code);
if (baladiya){
return projectBaladiya(baladiya, projections);
}


}


return null;
};

module.exports = getBaldiyaByCodeForWilaya;
31 changes: 31 additions & 0 deletions src/api/getBaldiyatsForDairaByCodeForWilayaByCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const projectBaladiya = require('../utils/projections/projectObject');

const getBaldiyatsForDairaByCodeForWilayaByCode = (data) =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as my previous comments, plus this name can be simplified to getBaldiyatsByWilayaCode or getBaldiyatsByDairaCode. And if you could find a counterexample/argument to keep such composed methods which means that daira code is not enough to identify wilayas. we can name this: getBaldiyatsByWilayaAndDairaCodes

/**
* Takes a wilaya code (mattricule), daira code (code) and returns array of Baladiyates of wilaya.
*
* @example Get Baladiyats list of adrar daira in adrar wilaya (mattricule:1 ,code: 101)
*
*
* getBaldiyatsForDairaByCodeForWilayaByCode(1, 101)
*
* @param { Number } mattricule wilaya code (mattricule)
* @param { Number } code baladiya code (code)
* @param { String[] } projections a list of Baladyia object attributes to keep
* @returns { Object[] | null } list of all baladiyas for wilaya
*/

(mattricule, code, projections) => {
const wilaya = data.find((w) => w.mattricule === mattricule);
if (wilaya) {
const daira = wilaya.dairats.find((d) => d.code === code);

if(daira){
return projectBaladiya(daira.baladyiats, projections);
}

}
return null;
};

module.exports = getBaldiyatsForDairaByCodeForWilayaByCode;
26 changes: 26 additions & 0 deletions src/api/getDairaByCodeAndWilayaCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const projectDaira = require('../utils/projections/projectObject');

const getDairaByCodeAndWilayaCode = (data)=>
/**
* Takes a wilaya mattricule and a daira code and return daira.
*
* @example Get a daira by code and wilaya mattricule(mattricule:1, code:101)
*
* //returns {name : "ADRAR"}
* getWilayaByBaladyiaName(1, 101)
*
* @param { integer } mattricule wilaya mattricule
* @param { integer } code daira code
* @param { String[] } projection a list of Baladyia object attributes to keep
* @returns { Object | null } Returns daira's object, or null
*/
(mattricule, code, projection)=>{
const wilaya = data.find((w) => w.mattricule === mattricule);
if (wilaya) {
const daira = wilaya.dairats.find((d)=> d.code === code);
return projectDaira(daira, projection);
}
return null;
};

module.exports = getDairaByCodeAndWilayaCode;
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const getBaladyiatsForWilaya = require('./api/getBaladyiatsForWilaya');
const getBaladyiatsForDaira = require('./api/getBaladyiatsForDaira');
const getWilayaByBaladyiaName = require('./api/getWilayaByBaladyiaName');
const getDairaByBaladyiaName = require('./api/getDairaByBaladyiaName');

const getDairaByCodeAndWilayaCode = require('./api/getDairaByCodeAndWilayaCode');
const getBaladiyaByCodeAndDairaCodeAndWilayaCode = require('./api/getBaladiyaByCodeAndDairaCodeAndWilayaCode');
const getBaldiyatsForDairaByCodeForWilayaByCode = require('./api/getBaldiyatsForDairaByCodeForWilayaByCode');
const getBaldiyaByCodeForWilaya = require('./api/getBaldiyaByCodeForWilaya');
const data = require('../data/WilayaList.json');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd leave a blank line before the dataset import just for the sake of clarity and readablity


const _getData = () => [...data];
Expand All @@ -35,6 +38,10 @@ module.exports = {
getBaladyiatsForDaira: getBaladyiatsForDaira(_getData()),
getWilayaByBaladyiaName: getWilayaByBaladyiaName(_getData()),
getDairaByBaladyiaName: getDairaByBaladyiaName(_getData()),
getDairaByCodeAndWilayaCode: getDairaByCodeAndWilayaCode(_getData()),
getBaladiyaByCodeAndDairaCodeAndWilayaCode: getBaladiyaByCodeAndDairaCodeAndWilayaCode(_getData()),
getBaldiyatsForDairaByCodeForWilayaByCode: getBaldiyatsForDairaByCodeForWilayaByCode(_getData()),
getBaldiyaByCodeForWilaya: getBaldiyaByCodeForWilaya(_getData()),
utils: {
projectObject,
isValidWilayaCode,
Expand Down
72 changes: 72 additions & 0 deletions tests/api/getBaladiyaByCodeAndDairaCodeAndWilayaCode.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
describe('get matching daira by code and wilaya mattricule', ()=>{
const mockData = [
{
mattricule: 47,
name: 'Ghardaïa',
dairats: [
{
code: 4701,
name: 'GHARDAIA',
name_ar: 'غرداية',
name_en: 'GHARDAIA',
baladyiats: [
{
code: 4701,
name: 'GHARDAIA',
name_en: 'GHARDAIA',
name_ar: 'غرداية',
},
{
code: 4702,
name: 'EL MENIAA',
name_en: 'EL MENIAA',
name_ar: 'المنيعة',
},
],
},
],
},
];



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

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

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

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

it('should return null for invalid wilaya mattricule and baladiya and daira code ', () => {
const result = getBaladiyaByCodeAndDairaCodeAndWilayaCode(mockData)(99, 1, 99);

expect(result).toBeNull();
});

it('should return null for invalid baladiya and daira code ', () => {
const result = getBaladiyaByCodeAndDairaCodeAndWilayaCode(mockData)(47, 88, 99);

expect(result).toBeNull();
});


it('should return null for invalid baladiya code ', () => {
const result = getBaladiyaByCodeAndDairaCodeAndWilayaCode(mockData)(47, 4701, 99);

expect(result).toBeNull();
});

it('should return a matching baldiya for valide wilaya mattricule, baladiya and daira code ', () => {
const result = getBaladiyaByCodeAndDairaCodeAndWilayaCode(mockData)(47, 4701, 4702);
expect(result).toEqual(mockData[0].dairats[0].baladyiats[1]);
});

});
Loading