-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.ts
More file actions
71 lines (61 loc) · 1.97 KB
/
index.ts
File metadata and controls
71 lines (61 loc) · 1.97 KB
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
import * as Csv from 'fast-csv';
interface IPinCodeRegister {
OfficeName: string;
PinCode: string;
OfficeType: string;
DeliveryStatus: string;
DivisionName: string;
RegionName: string;
CircleName: string;
Taluk: string;
DistrictName: string;
StateName: string;
Telephone: string;
RelatedSubOffice: string;
RelatedHeadOffice: string;
}
function extract(data: string[]): IPinCodeRegister {
const [ OfficeName, PinCode, OfficeType, DeliveryStatus, DivisionName,
RegionName, CircleName, Taluk, DistrictName, StateName,
Telephone, RelatedSubOffice, RelatedHeadOffice ] = data;
return { OfficeName, PinCode, OfficeType, DeliveryStatus, DivisionName,
RegionName, CircleName, Taluk, DistrictName, StateName,
Telephone, RelatedSubOffice, RelatedHeadOffice };
}
function verify(
by: string,
searchTerm: string,
register: IPinCodeRegister,
{ fullMatch } = { fullMatch: true },
): boolean {
if (fullMatch) {
return register[by] === searchTerm;
} else {
return register[by].indexOf(searchTerm) !== -1;
}
}
function searchBy(by: string, searchTerm: string, callback: Function, opts: any = { }) {
const result = [];
Csv.fromPath('pinCode.csv')
.on('data', (data: string[]) => {
const register = extract(data);
if (verify(by, searchTerm, register, opts)) {
result.push(register);
}
}).on('end', () => {
return callback(result);
});
}
export const seachByPin = (pin: string, callback: Function) => {
if (pin.length < 6 || pin.length > 6) return callback('Invalid PinCode');
searchBy('PinCode', pin, callback);
}
export const seachByArea = (area: string, callback: Function) => {
searchBy('OfficeName', area, callback, { fullMatch: false });
}
export const seachByDistrict = (district, callback) => {
searchBy('DistrictName', district, callback, { fullMatch: false });
}
export const seachByTaluk = (taluk, callback) => {
searchBy('Taluk', taluk, callback, { fullMatch: false });
}