-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
65 lines (60 loc) · 1.57 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const stateList = require('./src/state.json');
const districtList = require('./src/district.json');
const schoolList = require('./src/school.json');
module.exports = {
getStateById: function (id){
return _findEntryById(stateList, id);
},
getStateByShortname: function(code){
return _findEntryByShortname(stateList, code)
},
getDistrictById: function (id){
return _findEntryById(districtList, id);
},
getSchoolByCode: function(code){
return _findEntryByCode(schoolList, code);
},
getDisctrictOfState: function (stateId){
var district = districtList.filter(function (value) {
return value.state_id === stateId
})
return district.sort(compare)
},
getSchoolsOfDisctrict: function (districtId){
var schools = schoolList.filter(function (value) {
return value.district_id === districtId
})
return schools.sort(compare)
},
getAllStates: function (){
return stateList;
}
}
let _findEntryById = (source, id) => {
if (id && source != null) {
let idx = source.findIndex((c) => c.id === id);
return (idx !== -1) ? source[idx] : "";
}
else return "";
}
let _findEntryByCode = (source, code) => {
if (code && source != null) {
let codex = source.findIndex((c) => c.schoolcode === code);
return (codex !== -1) ? source[codex] : "";
}
else return "";
}
let _findEntryByShortname = (source, code) => {
if (code && source != null) {
let codex = source.findIndex((c) => c.shortname === code);
return (codex !== -1) ? source[codex] : "";
}
else return "";
}
function compare(a,b) {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
}