-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (147 loc) · 5.43 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict'
const dt = require('./libs/dt/poligon.data').statesData; // data magna
const features = dt.features; // tab features
let _onRetSuggestion = (key) => {
key = key.toString().trim();
// key = key.trim();
const fnd = [];
const idx = key.substring(0, 2)
features.forEach(elem => {
let itm = elem.properties.NOM.toLowerCase()
itm = itm.toString();
itm = itm.trim();
let tmp = itm.substring(0, 2);
if(tmp === idx){
fnd.push(itm);
}
})
// console.log(fnd)
return fnd;
}
let _onSearching = (key) =>{
const fnd = [];
features.forEach(elem => {
let itm = elem.properties.NOM.toLowerCase()
if(itm.toString() === key.toString()){
fnd.push(itm);
return;
}
})
return fnd.length > 0
? fnd
: _onRetSuggestion(key);
}
const provinces = function(name = String(), options){
if(name && typeof name === 'string'){
let andHisPoligonePoints = false;
let andHisTerritories = false;
if(options){
if(typeof options === 'object'){
if(options.hasOwnProperty('herTerritories') || options.hasOwnProperty('hisPolygonesPoints')){
andHisTerritories = options.herTerritories ? true : false;
andHisPoligonePoints = options.hisPolygonesPoints ? true : false;
}else{
return {
"Error " : 400,
"Message " : `bad kies passed to options param : Expected key : 'herTerritories' OR 'hisPolygonesPoints'`
}
}
}else{
return {
"Error " : 400,
"Message " : `Options Parameter must be an Object : Expected key : 'herTerritories' OR 'hisPolygonesPoints'`
}
}
}
let occurences = _onSearching(name);
let found = [];
let defaultName = name;
name = name.toString();
name = name.toLowerCase();
features.forEach(elem => {
// SCE_SEM | MODIF | OBJECTID
let occurence = name.localeCompare(elem.properties.NOM);
let tempName = elem.properties.NOM.toLowerCase();
if(tempName === name){
delete elem.properties['SCE_SEM'];
delete elem.properties['MODIF'];
elem.properties['OBJECTID'];
// elem.properties['PROVINCE'] = name;
const fnd = {
"item" : elem.properties.OBJECTID,
"Nom" : elem.properties.NOM,
"ChefLieu" : elem.properties.PRINCIPALTOWN,
"Surface" : elem.properties.AREA,
"Population" : elem.properties.POPULATION
// "Territoires" : elem.properties.DISTRICTS
};
andHisPoligonePoints ? fnd['Coords'] = elem.geometry.coordinates : false;
andHisTerritories ? fnd['Territoires'] = elem.properties.DISTRICTS : false;
found.push(fnd);
return true;
}
});
return found.length > 0
? found[0]
: occurences.length > 0
? {
"Error " : 404,
"Message " : `there is no record to key : '${defaultName}' `,
"Suggestions " : occurences
}
: {
"Error " : 404,
"Message " : `there is no record to key : '${defaultName}' `
};
}else{
return onRetAllProvinces(options)
}
}
const onRetAllProvinces = function(options){
let andHisPoligonePoints = false;
let andHisTerritories = false;
if(options){
if(typeof options === 'object'){
if(options.hasOwnProperty('herTerritories') || options.hasOwnProperty('hisPolygonesPoints')){
andHisTerritories = options.herTerritories ? true : false;
andHisPoligonePoints = options.hisPolygonesPoints ? true : false;
}
// herTerritories,hisPolygonesPoints
}
}
let occurences = [];
let found = [];
features.forEach(elem => {
// SCE_SEM | MODIF | OBJECTID
// let occurence = name.localeCompare(elem.properties.NOM);
// let tempName = elem.properties.NOM.toLowerCase();
delete elem.properties['SCE_SEM'];
delete elem.properties['MODIF'];
elem.properties['OBJECTID'];
// elem.properties['PROVINCE'] = name;
const fnd = {
"item" : elem.properties.OBJECTID,
"Nom" : elem.properties.NOM,
"ChefLieu" : elem.properties.PRINCIPALTOWN,
"Surface" : elem.properties.AREA,
"Population" : elem.properties.POPULATION
// "Territoires" : elem.properties.DISTRICTS
};
andHisPoligonePoints ? fnd['Coords'] = elem.geometry.coordinates : false;
andHisTerritories ? fnd['Territoires'] = elem.properties.DISTRICTS : false;
found.push(fnd);
// return true;
});
return found.length > 0
? found
: occurences.length > 0
? occurences : {
"Error " : 404,
"Message " : `there is no record `
};
}
// const provinces = onRetProvinceByName();
// exports.getProvinceByName = onRetProvinceByName
exports.main = {
provinces
}