Skip to content

Commit 7be7cfa

Browse files
committed
Filter the setting list
1 parent b80cee5 commit 7be7cfa

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

Diff for: README.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Node JS Exercise
22

3+
Showing a list of settings on the `/settings` route.
4+
35
## Requirements
46

57
### Global
@@ -24,6 +26,12 @@ yarn
2426
```bash
2527
yarn start
2628
```
29+
You can display the settings on http://localhost:8042/settings.
30+
31+
* Check ESLint
32+
```bash
33+
yarn lint
34+
```
2735

2836
* Build for production
2937
```bash

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"es6": true
3535
},
3636
"rules": {
37+
"arrow-parens": ["error", "as-needed"],
3738
"no-console": "off"
3839
}
3940
}

Diff for: src/modules/SettingsRouter.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,43 @@ class SettingsRouter {
1919
* @returns {Promise}
2020
*/
2121
async init() {
22-
const settingsData = await parseFile(this.settingsFilePath);
22+
const settingsList = await parseFile(this.settingsFilePath);
2323
const componentList = await parseFile(this.componentsFilePath);
24+
const filteredList = SettingsRouter.filterSettingsList(settingsList, componentList);
2425

2526
this.expressRouter.get('/settings', (req, res) => {
26-
res.send(settingsData);
27+
res.send(filteredList);
2728
res.end();
2829
});
2930

3031
return this.expressRouter;
3132
}
33+
34+
/**
35+
* @param {Array} settingsList
36+
* @param {Array} componentList
37+
* @returns {Array}
38+
*/
39+
static filterSettingsList(settingsList, componentList) {
40+
// Put all the component name in an array of string (instead of objects)
41+
const componentNameList = componentList.map(item => item.name);
42+
43+
let settingRequires;
44+
45+
return settingsList.filter(settingsItem => {
46+
({ requires: settingRequires } = settingsItem);
47+
48+
if (!Array.isArray(settingRequires)) {
49+
return false;
50+
}
51+
52+
if (settingRequires.length === 0) {
53+
return true;
54+
}
55+
56+
return settingRequires.some(requiredComponent => componentNameList.includes(requiredComponent));
57+
});
58+
}
3259
}
3360

3461
export default SettingsRouter;

0 commit comments

Comments
 (0)