-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (43 loc) · 1.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const laptops = require("./laptops.json");
function _lower(input) {
return input.toLowerCase().trim();
}
module.exports = {
getAll: function () {
return laptops.sort((a,b)=>_lower(a.brands) < _lower(b.brands) ? -1 : _lower(a.brands) > _lower(b.brands) ? 1 : 0)
},
getBrand: function (param) {
const isBrand = isNaN(param);
const queryType = isBrand ? "brands" : "models";
return laptops.find(function (lap) {
if (_lower(lap[queryType]) === _lower(param)) {
return lap;
}
});
},
getModel: function (brand) {
// brand = _lower(brand);
if (!brand || brand == "") {
throw new Error('Error in getting Model');
}
const response = laptops.find(function (lapbrand) {
if(_lower(lapbrand.brands)===_lower(brand)){
return true
}
});
// console.log(response.models.sort())
return response.models;
},
getSeries: function (brand) {
// brand = _lower(brand);
if (!brand || brand == "") {
throw new Error('Error in getting Laptop Series');
}
const response = laptops.find(function (lapbrand) {
if(_lower(lapbrand.brands)===_lower(brand)){
return true
}
});
return response.series;
},
};