From 8a53256746a5438782dc792b140ffd656540b202 Mon Sep 17 00:00:00 2001 From: Taylor Thurlow Date: Wed, 9 Jun 2021 10:55:47 -0700 Subject: [PATCH 1/7] Disable tests --- __test__/index.test.ts | 794 +++++++++++++++--------------- __test__/interface-export.test.ts | 116 ++--- package.json | 6 - 3 files changed, 455 insertions(+), 461 deletions(-) diff --git a/__test__/index.test.ts b/__test__/index.test.ts index 588f4129..c746e7c9 100644 --- a/__test__/index.test.ts +++ b/__test__/index.test.ts @@ -1,404 +1,404 @@ -import csc, { ICountry, ICity, IState } from '../index'; +// import csc, { ICountry, ICity, IState } from '../index'; -const executeAllTests = function (csc: any) { - test('Check for Country By Code ', () => { - const code = 'CA'; - const country = csc.getCountryByCode(code); - expect(country).toEqual({ - name: 'Canada', - phonecode: '1', - isoCode: 'CA', - flag: '🇨🇦', - currency: 'CAD', - latitude: '60.00000000', - longitude: '-95.00000000', - timezones: [ - { - zoneName: 'America/Atikokan', - gmtOffset: -18000, - gmtOffsetName: 'UTC-05:00', - abbreviation: 'EST', - tzName: 'Eastern Standard Time (North America)', - }, - { - zoneName: 'America/Blanc-Sablon', - gmtOffset: -14400, - gmtOffsetName: 'UTC-04:00', - abbreviation: 'AST', - tzName: 'Atlantic Standard Time', - }, - { - zoneName: 'America/Cambridge_Bay', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America)', - }, - { - zoneName: 'America/Creston', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America)', - }, - { - zoneName: 'America/Dawson', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America)', - }, - { - zoneName: 'America/Dawson_Creek', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America)', - }, - { - zoneName: 'America/Edmonton', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America)', - }, - { - zoneName: 'America/Fort_Nelson', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America)', - }, - { - zoneName: 'America/Glace_Bay', - gmtOffset: -14400, - gmtOffsetName: 'UTC-04:00', - abbreviation: 'AST', - tzName: 'Atlantic Standard Time', - }, - { - zoneName: 'America/Goose_Bay', - gmtOffset: -14400, - gmtOffsetName: 'UTC-04:00', - abbreviation: 'AST', - tzName: 'Atlantic Standard Time', - }, - { - zoneName: 'America/Halifax', - gmtOffset: -14400, - gmtOffsetName: 'UTC-04:00', - abbreviation: 'AST', - tzName: 'Atlantic Standard Time', - }, - { - zoneName: 'America/Inuvik', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America', - }, - { - zoneName: 'America/Iqaluit', - gmtOffset: -18000, - gmtOffsetName: 'UTC-05:00', - abbreviation: 'EST', - tzName: 'Eastern Standard Time (North America', - }, - { - zoneName: 'America/Moncton', - gmtOffset: -14400, - gmtOffsetName: 'UTC-04:00', - abbreviation: 'AST', - tzName: 'Atlantic Standard Time', - }, - { - zoneName: 'America/Nipigon', - gmtOffset: -18000, - gmtOffsetName: 'UTC-05:00', - abbreviation: 'EST', - tzName: 'Eastern Standard Time (North America', - }, - { - zoneName: 'America/Pangnirtung', - gmtOffset: -18000, - gmtOffsetName: 'UTC-05:00', - abbreviation: 'EST', - tzName: 'Eastern Standard Time (North America', - }, - { - zoneName: 'America/Rainy_River', - gmtOffset: -21600, - gmtOffsetName: 'UTC-06:00', - abbreviation: 'CST', - tzName: 'Central Standard Time (North America', - }, - { - zoneName: 'America/Rankin_Inlet', - gmtOffset: -21600, - gmtOffsetName: 'UTC-06:00', - abbreviation: 'CST', - tzName: 'Central Standard Time (North America', - }, - { - zoneName: 'America/Regina', - gmtOffset: -21600, - gmtOffsetName: 'UTC-06:00', - abbreviation: 'CST', - tzName: 'Central Standard Time (North America', - }, - { - zoneName: 'America/Resolute', - gmtOffset: -21600, - gmtOffsetName: 'UTC-06:00', - abbreviation: 'CST', - tzName: 'Central Standard Time (North America', - }, - { - zoneName: 'America/St_Johns', - gmtOffset: -12600, - gmtOffsetName: 'UTC-03:30', - abbreviation: 'NST', - tzName: 'Newfoundland Standard Time', - }, - { - zoneName: 'America/Swift_Current', - gmtOffset: -21600, - gmtOffsetName: 'UTC-06:00', - abbreviation: 'CST', - tzName: 'Central Standard Time (North America', - }, - { - zoneName: 'America/Thunder_Bay', - gmtOffset: -18000, - gmtOffsetName: 'UTC-05:00', - abbreviation: 'EST', - tzName: 'Eastern Standard Time (North America', - }, - { - zoneName: 'America/Toronto', - gmtOffset: -18000, - gmtOffsetName: 'UTC-05:00', - abbreviation: 'EST', - tzName: 'Eastern Standard Time (North America', - }, - { - zoneName: 'America/Vancouver', - gmtOffset: -28800, - gmtOffsetName: 'UTC-08:00', - abbreviation: 'PST', - tzName: 'Pacific Standard Time (North America', - }, - { - zoneName: 'America/Whitehorse', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America', - }, - { - zoneName: 'America/Winnipeg', - gmtOffset: -21600, - gmtOffsetName: 'UTC-06:00', - abbreviation: 'CST', - tzName: 'Central Standard Time (North America', - }, - { - zoneName: 'America/Yellowknife', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America', - }, - ], - }); - }); +// const executeAllTests = function (csc: any) { +// test('Check for Country By Code ', () => { +// const code = 'CA'; +// const country = csc.getCountryByCode(code); +// expect(country).toEqual({ +// name: 'Canada', +// phonecode: '1', +// isoCode: 'CA', +// flag: '🇨🇦',', +// currency: 'CAD', +// latitude: '60.00000000', +// longitude: '-95.00000000', +// timezones: [ +// { +// zoneName: 'America/Atikokan', +// gmtOffset: -18000, +// gmtOffsetName: 'UTC-05:00', +// abbreviation: 'EST', +// tzName: 'Eastern Standard Time (North America)', +// }, +// { +// zoneName: 'America/Blanc-Sablon', +// gmtOffset: -14400, +// gmtOffsetName: 'UTC-04:00', +// abbreviation: 'AST', +// tzName: 'Atlantic Standard Time', +// }, +// { +// zoneName: 'America/Cambridge_Bay', +// gmtOffset: -25200, +// gmtOffsetName: 'UTC-07:00', +// abbreviation: 'MST', +// tzName: 'Mountain Standard Time (North America)', +// }, +// { +// zoneName: 'America/Creston', +// gmtOffset: -25200, +// gmtOffsetName: 'UTC-07:00', +// abbreviation: 'MST', +// tzName: 'Mountain Standard Time (North America)', +// }, +// { +// zoneName: 'America/Dawson', +// gmtOffset: -25200, +// gmtOffsetName: 'UTC-07:00', +// abbreviation: 'MST', +// tzName: 'Mountain Standard Time (North America)', +// }, +// { +// zoneName: 'America/Dawson_Creek', +// gmtOffset: -25200, +// gmtOffsetName: 'UTC-07:00', +// abbreviation: 'MST', +// tzName: 'Mountain Standard Time (North America)', +// }, +// { +// zoneName: 'America/Edmonton', +// gmtOffset: -25200, +// gmtOffsetName: 'UTC-07:00', +// abbreviation: 'MST', +// tzName: 'Mountain Standard Time (North America)', +// }, +// { +// zoneName: 'America/Fort_Nelson', +// gmtOffset: -25200, +// gmtOffsetName: 'UTC-07:00', +// abbreviation: 'MST', +// tzName: 'Mountain Standard Time (North America)', +// }, +// { +// zoneName: 'America/Glace_Bay', +// gmtOffset: -14400, +// gmtOffsetName: 'UTC-04:00', +// abbreviation: 'AST', +// tzName: 'Atlantic Standard Time', +// }, +// { +// zoneName: 'America/Goose_Bay', +// gmtOffset: -14400, +// gmtOffsetName: 'UTC-04:00', +// abbreviation: 'AST', +// tzName: 'Atlantic Standard Time', +// }, +// { +// zoneName: 'America/Halifax', +// gmtOffset: -14400, +// gmtOffsetName: 'UTC-04:00', +// abbreviation: 'AST', +// tzName: 'Atlantic Standard Time', +// }, +// { +// zoneName: 'America/Inuvik', +// gmtOffset: -25200, +// gmtOffsetName: 'UTC-07:00', +// abbreviation: 'MST', +// tzName: 'Mountain Standard Time (North America', +// }, +// { +// zoneName: 'America/Iqaluit', +// gmtOffset: -18000, +// gmtOffsetName: 'UTC-05:00', +// abbreviation: 'EST', +// tzName: 'Eastern Standard Time (North America', +// }, +// { +// zoneName: 'America/Moncton', +// gmtOffset: -14400, +// gmtOffsetName: 'UTC-04:00', +// abbreviation: 'AST', +// tzName: 'Atlantic Standard Time', +// }, +// { +// zoneName: 'America/Nipigon', +// gmtOffset: -18000, +// gmtOffsetName: 'UTC-05:00', +// abbreviation: 'EST', +// tzName: 'Eastern Standard Time (North America', +// }, +// { +// zoneName: 'America/Pangnirtung', +// gmtOffset: -18000, +// gmtOffsetName: 'UTC-05:00', +// abbreviation: 'EST', +// tzName: 'Eastern Standard Time (North America', +// }, +// { +// zoneName: 'America/Rainy_River', +// gmtOffset: -21600, +// gmtOffsetName: 'UTC-06:00', +// abbreviation: 'CST', +// tzName: 'Central Standard Time (North America', +// }, +// { +// zoneName: 'America/Rankin_Inlet', +// gmtOffset: -21600, +// gmtOffsetName: 'UTC-06:00', +// abbreviation: 'CST', +// tzName: 'Central Standard Time (North America', +// }, +// { +// zoneName: 'America/Regina', +// gmtOffset: -21600, +// gmtOffsetName: 'UTC-06:00', +// abbreviation: 'CST', +// tzName: 'Central Standard Time (North America', +// }, +// { +// zoneName: 'America/Resolute', +// gmtOffset: -21600, +// gmtOffsetName: 'UTC-06:00', +// abbreviation: 'CST', +// tzName: 'Central Standard Time (North America', +// }, +// { +// zoneName: 'America/St_Johns', +// gmtOffset: -12600, +// gmtOffsetName: 'UTC-03:30', +// abbreviation: 'NST', +// tzName: 'Newfoundland Standard Time', +// }, +// { +// zoneName: 'America/Swift_Current', +// gmtOffset: -21600, +// gmtOffsetName: 'UTC-06:00', +// abbreviation: 'CST', +// tzName: 'Central Standard Time (North America', +// }, +// { +// zoneName: 'America/Thunder_Bay', +// gmtOffset: -18000, +// gmtOffsetName: 'UTC-05:00', +// abbreviation: 'EST', +// tzName: 'Eastern Standard Time (North America', +// }, +// { +// zoneName: 'America/Toronto', +// gmtOffset: -18000, +// gmtOffsetName: 'UTC-05:00', +// abbreviation: 'EST', +// tzName: 'Eastern Standard Time (North America', +// }, +// { +// zoneName: 'America/Vancouver', +// gmtOffset: -28800, +// gmtOffsetName: 'UTC-08:00', +// abbreviation: 'PST', +// tzName: 'Pacific Standard Time (North America', +// }, +// { +// zoneName: 'America/Whitehorse', +// gmtOffset: -25200, +// gmtOffsetName: 'UTC-07:00', +// abbreviation: 'MST', +// tzName: 'Mountain Standard Time (North America', +// }, +// { +// zoneName: 'America/Winnipeg', +// gmtOffset: -21600, +// gmtOffsetName: 'UTC-06:00', +// abbreviation: 'CST', +// tzName: 'Central Standard Time (North America', +// }, +// { +// zoneName: 'America/Yellowknife', +// gmtOffset: -25200, +// gmtOffsetName: 'UTC-07:00', +// abbreviation: 'MST', +// tzName: 'Mountain Standard Time (North America', +// }, +// ], +// }); +// }); - test('Check for Canada States', () => { - const code = 'CA'; - const country = csc.getCountryByCode(code); - const states = csc.getStatesOfCountry(country.isoCode); - const names = states.map((state: IState) => { - return state.name; - }); - expect(names).toEqual([ - 'Alberta', - 'British Columbia', - 'Manitoba', - 'New Brunswick', - 'Newfoundland and Labrador', - 'Northwest Territories', - 'Nova Scotia', - 'Nunavut', - 'Ontario', - 'Prince Edward Island', - 'Quebec', - 'Saskatchewan', - 'Yukon', - ]); - }); +// test('Check for Canada States', () => { +// const code = 'CA'; +// const country = csc.getCountryByCode(code); +// const states = csc.getStatesOfCountry(country.isoCode); +// const names = states.map((state: IState) => { +// return state.name; +// }); +// expect(names).toEqual([ +// 'Alberta', +// 'British Columbia', +// 'Manitoba', +// 'New Brunswick', +// 'Newfoundland and Labrador', +// 'Northwest Territories', +// 'Nova Scotia', +// 'Nunavut', +// 'Ontario', +// 'Prince Edward Island', +// 'Quebec', +// 'Saskatchewan', +// 'Yukon', +// ]); +// }); - test('Check All States for United States Of America', () => { - const code = 'US'; - const country = csc.getCountryByCode(code); - const states = csc.getStatesOfCountry(country.isoCode); - const names = states.map((state: IState) => { - return state.name; - }); - expect(names).toEqual([ - 'Alabama', - 'Alaska', - 'American Samoa', - 'Arizona', - 'Arkansas', - 'Baker Island', - 'California', - 'Colorado', - 'Connecticut', - 'Delaware', - 'District of Columbia', - 'Florida', - 'Georgia', - 'Guam', - 'Hawaii', - 'Howland Island', - 'Idaho', - 'Illinois', - 'Indiana', - 'Iowa', - 'Jarvis Island', - 'Johnston Atoll', - 'Kansas', - 'Kentucky', - 'Kingman Reef', - 'Louisiana', - 'Maine', - 'Maryland', - 'Massachusetts', - 'Michigan', - 'Midway Atoll', - 'Minnesota', - 'Mississippi', - 'Missouri', - 'Montana', - 'Navassa Island', - 'Nebraska', - 'Nevada', - 'New Hampshire', - 'New Jersey', - 'New Mexico', - 'New York', - 'North Carolina', - 'North Dakota', - 'Northern Mariana Islands', - 'Ohio', - 'Oklahoma', - 'Oregon', - 'Palmyra Atoll', - 'Pennsylvania', - 'Puerto Rico', - 'Rhode Island', - 'South Carolina', - 'South Dakota', - 'Tennessee', - 'Texas', - 'United States Minor Outlying Islands', - 'United States Virgin Islands', - 'Utah', - 'Vermont', - 'Virginia', - 'Wake Island', - 'Washington', - 'West Virginia', - 'Wisconsin', - 'Wyoming', - ]); - }); +// test('Check All States for United States Of America', () => { +// const code = 'US'; +// const country = csc.getCountryByCode(code); +// const states = csc.getStatesOfCountry(country.isoCode); +// const names = states.map((state: IState) => { +// return state.name; +// }); +// expect(names).toEqual([ +// 'Alabama', +// 'Alaska', +// 'American Samoa', +// 'Arizona', +// 'Arkansas', +// 'Baker Island', +// 'California', +// 'Colorado', +// 'Connecticut', +// 'Delaware', +// 'District of Columbia', +// 'Florida', +// 'Georgia', +// 'Guam', +// 'Hawaii', +// 'Howland Island', +// 'Idaho', +// 'Illinois', +// 'Indiana', +// 'Iowa', +// 'Jarvis Island', +// 'Johnston Atoll', +// 'Kansas', +// 'Kentucky', +// 'Kingman Reef', +// 'Louisiana', +// 'Maine', +// 'Maryland', +// 'Massachusetts', +// 'Michigan', +// 'Midway Atoll', +// 'Minnesota', +// 'Mississippi', +// 'Missouri', +// 'Montana', +// 'Navassa Island', +// 'Nebraska', +// 'Nevada', +// 'New Hampshire', +// 'New Jersey', +// 'New Mexico', +// 'New York', +// 'North Carolina', +// 'North Dakota', +// 'Northern Mariana Islands', +// 'Ohio', +// 'Oklahoma', +// 'Oregon', +// 'Palmyra Atoll', +// 'Pennsylvania', +// 'Puerto Rico', +// 'Rhode Island', +// 'South Carolina', +// 'South Dakota', +// 'Tennessee', +// 'Texas', +// 'United States Minor Outlying Islands', +// 'United States Virgin Islands', +// 'Utah', +// 'Vermont', +// 'Virginia', +// 'Wake Island', +// 'Washington', +// 'West Virginia', +// 'Wisconsin', +// 'Wyoming', +// ]); +// }); - test('Check States for India', () => { - const code = 'IN'; - const country = csc.getCountryByCode(code); - const states = csc.getStatesOfCountry(country.isoCode); - const names = states.map((state: IState) => { - return state.name; - }); +// test('Check States for India', () => { +// const code = 'IN'; +// const country = csc.getCountryByCode(code); +// const states = csc.getStatesOfCountry(country.isoCode); +// const names = states.map((state: IState) => { +// return state.name; +// }); - expect(names).toEqual([ - 'Andaman and Nicobar Islands', - 'Andhra Pradesh', - 'Arunachal Pradesh', - 'Assam', - 'Bihar', - 'Chandigarh', - 'Chhattisgarh', - 'Dadra and Nagar Haveli', - 'Daman and Diu', - 'Delhi', - 'Goa', - 'Gujarat', - 'Haryana', - 'Himachal Pradesh', - 'Jammu and Kashmir', - 'Jharkhand', - 'Karnataka', - 'Kerala', - 'Ladakh', - 'Lakshadweep', - 'Madhya Pradesh', - 'Maharashtra', - 'Manipur', - 'Meghalaya', - 'Mizoram', - 'Nagaland', - 'Odisha', - 'Puducherry', - 'Punjab', - 'Rajasthan', - 'Sikkim', - 'Tamil Nadu', - 'Telangana', - 'Tripura', - 'Uttar Pradesh', - 'Uttarakhand', - 'West Bengal', - ]); - }); +// expect(names).toEqual([ +// 'Andaman and Nicobar Islands', +// 'Andhra Pradesh', +// 'Arunachal Pradesh', +// 'Assam', +// 'Bihar', +// 'Chandigarh', +// 'Chhattisgarh', +// 'Dadra and Nagar Haveli', +// 'Daman and Diu', +// 'Delhi', +// 'Goa', +// 'Gujarat', +// 'Haryana', +// 'Himachal Pradesh', +// 'Jammu and Kashmir', +// 'Jharkhand', +// 'Karnataka', +// 'Kerala', +// 'Ladakh', +// 'Lakshadweep', +// 'Madhya Pradesh', +// 'Maharashtra', +// 'Manipur', +// 'Meghalaya', +// 'Mizoram', +// 'Nagaland', +// 'Odisha', +// 'Puducherry', +// 'Punjab', +// 'Rajasthan', +// 'Sikkim', +// 'Tamil Nadu', +// 'Telangana', +// 'Tripura', +// 'Uttar Pradesh', +// 'Uttarakhand', +// 'West Bengal', +// ]); +// }); - test('Check Cities for Delhi', () => { - const countryCode = 'IN'; - const stateCode = 'DL'; - const cities = csc.getCitiesOfState(countryCode, stateCode); - const names = cities.map((city: ICity) => { - return city.name; - }); - expect(names).toEqual([ - 'Alīpur', - 'Bawāna', - 'Central Delhi', - 'Delhi', - 'Deoli', - 'East Delhi', - 'Karol Bāgh', - 'Najafgarh', - 'Narela', - 'New Delhi', - 'North Delhi', - 'North East Delhi', - 'North West Delhi', - 'Nāngloi Jāt', - 'Pitampura', - 'Rohini', - 'South Delhi', - 'South West Delhi', - 'West Delhi', - ]); - }); +// test('Check Cities for Delhi', () => { +// const countryCode = 'IN'; +// const stateCode = 'DL'; +// const cities = csc.getCitiesOfState(countryCode, stateCode); +// const names = cities.map((city: ICity) => { +// return city.name; +// }); +// expect(names).toEqual([ +// 'Alīpur', +// 'Bawāna', +// 'Central Delhi', +// 'Delhi', +// 'Deoli', +// 'East Delhi', +// 'Karol Bāgh', +// 'Najafgarh', +// 'Narela', +// 'New Delhi', +// 'North Delhi', +// 'North East Delhi', +// 'North West Delhi', +// 'Nāngloi Jāt', +// 'Pitampura', +// 'Rohini', +// 'South Delhi', +// 'South West Delhi', +// 'West Delhi', +// ]); +// }); - test('Get State by State ISOCode and Country Code', () => { - const countryCode = 'PK'; - const stateCode = 'KP'; - const state = csc.getStateByCodeAndCountry(stateCode, countryCode); - expect(state.name).toEqual('Khyber Pakhtunkhwa'); - }); -}; -export default executeAllTests; -executeAllTests(csc); +// test('Get State by State ISOCode and Country Code', () => { +// const countryCode = 'PK'; +// const stateCode = 'KP'; +// const state = csc.getStateByCodeAndCountry(stateCode, countryCode); +// expect(state.name).toEqual('Khyber Pakhtunkhwa'); +// }); +// }; +// export default executeAllTests; +// executeAllTests(csc); diff --git a/__test__/interface-export.test.ts b/__test__/interface-export.test.ts index 697d8c96..ee333184 100644 --- a/__test__/interface-export.test.ts +++ b/__test__/interface-export.test.ts @@ -1,69 +1,69 @@ // importing interfaces from main index.ts file -import { ICountry, ICity, IState } from '../index'; +// import { ICountry, ICity, IState } from '../index'; // writing tests for Interfaces // https://stackoverflow.com/questions/14425568/interface-type-check-with-typescript -function isValidCountryObjectStructure(object: any): object is ICountry { - return 'name' in object && 'phonecode' in object && 'isoCode' in object && 'flag' in object; -} +// function isValidCountryObjectStructure(object: any): object is ICountry { +// return 'name' in object && 'phonecode' in object && 'isoCode' in object && 'flag' in object; +// } -test('Check for Interface export when Type Structure is Same', () => { - const country: ICountry = { - name: 'India', - phonecode: '+91', - isoCode: 'IN', - flag: '🇮🇳', - currency: 'INR', - latitude: '20.00000000', - longitude: '77.00000000', - timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] - }; - let isCountry = isValidCountryObjectStructure(country) - expect(isCountry).toEqual(true); -}); +// test('Check for Interface export when Type Structure is Same', () => { +// const country: ICountry = { +// name: 'India', +// phonecode: '+91', +// isoCode: 'IN', +// flag: '🇮🇳', +// currency: 'INR', +// latitude: '20.00000000', +// longitude: '77.00000000', +// timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] +// }; +// let isCountry = isValidCountryObjectStructure(country) +// expect(isCountry).toEqual(true); +// }); -test('Check for Interface export when Type Structure is Not Same', () => { - const country = { - phonecode: '+91', // missing name field - isoCode: 'IN', - flag: '🇮🇳' - }; - let isCountry = isValidCountryObjectStructure(country) - expect(isCountry).toEqual(false); -}); +// test('Check for Interface export when Type Structure is Not Same', () => { +// const country = { +// phonecode: '+91', // missing name field +// isoCode: 'IN', +// flag: '🇮🇳' +// }; +// let isCountry = isValidCountryObjectStructure(country) +// expect(isCountry).toEqual(false); +// }); -function isValidCountryObjectAndValueType(object: any): object is ICountry { - return typeof typeof object.name == "string" && typeof object.phonecode == "string" && typeof object.isoCode == "string" && typeof object.flag == "string"; -} +// function isValidCountryObjectAndValueType(object: any): object is ICountry { +// return typeof typeof object.name == "string" && typeof object.phonecode == "string" && typeof object.isoCode == "string" && typeof object.flag == "string"; +// } -test('Check for Interface export when Type Structure is Same and Value is of same type as well', () => { - const country: ICountry = { - name: 'India', - phonecode: '+91', - isoCode: 'IN', - flag: '🇮🇳', - currency: 'INR', - latitude: '20.00000000', - longitude: '77.00000000', - timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] - }; - let isCountry = isValidCountryObjectAndValueType(country) - expect(isCountry).toEqual(true); -}); +// test('Check for Interface export when Type Structure is Same and Value is of same type as well', () => { +// const country: ICountry = { +// name: 'India', +// phonecode: '+91', +// isoCode: 'IN', +// flag: '🇮🇳', +// currency: 'INR', +// latitude: '20.00000000', +// longitude: '77.00000000', +// timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] +// }; +// let isCountry = isValidCountryObjectAndValueType(country) +// expect(isCountry).toEqual(true); +// }); -test('Check for Interface export when Type Structure is Same and Value is of same type as well', () => { - const country = { - name: 'India', - phonecode: 91, // wrong type - isoCode: 'IN', - flag: '🇮🇳', - currency: 'INR', - latitude: '20.00000000', - longitude: '77.00000000', - timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] - }; - let isCountry = isValidCountryObjectAndValueType(country) - expect(isCountry).toEqual(false); -}); +// test('Check for Interface export when Type Structure is Same and Value is of same type as well', () => { +// const country = { +// name: 'India', +// phonecode: 91, // wrong type +// isoCode: 'IN', +// flag: '🇮🇳', +// currency: 'INR', +// latitude: '20.00000000', +// longitude: '77.00000000', +// timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] +// }; +// let isCountry = isValidCountryObjectAndValueType(country) +// expect(isCountry).toEqual(false); +// }); diff --git a/package.json b/package.json index 33a53fff..51944602 100644 --- a/package.json +++ b/package.json @@ -18,12 +18,6 @@ "type": "git", "url": "git+https://github.com/harpreetkhalsagtbit/country-state-city.git" }, - "husky": { - "hooks": { - "pre-commit": "npm test", - "pre-push": "npm test" - } - }, "keywords": [ "Country", "State", From a4b837bd867711726cf907efcc15097d5d8431c8 Mon Sep 17 00:00:00 2001 From: Taylor Thurlow Date: Wed, 9 Jun 2021 10:58:08 -0700 Subject: [PATCH 2/7] Refactor into classes --- index.ts | 79 +++--------------------------------------------- src/city.ts | 34 +++++++++++++++++++++ src/country.ts | 31 +++++++++++++++++++ src/interface.ts | 38 ++++------------------- src/state.ts | 35 +++++++++++++++++++++ 5 files changed, 110 insertions(+), 107 deletions(-) create mode 100644 src/city.ts create mode 100644 src/country.ts create mode 100644 src/state.ts diff --git a/index.ts b/index.ts index 7cdb9f98..6605222f 100644 --- a/index.ts +++ b/index.ts @@ -1,76 +1,5 @@ -import countryList from './lib/country.json'; -import stateList from './lib/state.json'; -import cityList from './lib/city.json'; -import { ICountry, ICity, IState } from './src/interface'; +import Country from './src/country'; +import State from './src/state'; +import City from './src/city'; -export { ICountry, ICity, IState } from './src/interface'; - -const _findEntryByCode = (source: any, code: string) => { - if (code && source != null) { - const codex = source.findIndex((c: any) => { - return c.isoCode === code; - }); - return codex !== -1 ? source[codex] : ''; - } - return ''; -}; - -const _findStateByCodeAndCountryCode = (source: any, code: string, countryCode: string) => { - if (code && countryCode && source != null) { - const codex = source.findIndex((c: any) => { - return c.isoCode === code && c.countryCode === countryCode; - }); - return codex !== -1 ? source[codex] : ''; - } - return ''; -}; - -const compare = (a: any, b: any) => { - if (a.name < b.name) return -1; - if (a.name > b.name) return 1; - return 0; -}; - -export default { - getStatesOfCountry(countryCode: string): IState[] { - const states = stateList.filter((value) => { - return value.countryCode === countryCode; - }); - return states.sort(compare); - }, - getCitiesOfState(countryCode: string, stateCode: string): ICity[] { - const cities = cityList.filter((value: { countryCode: string; stateCode: string }) => { - return value.countryCode === countryCode && value.stateCode === stateCode; - }); - return cities.sort(compare); - }, - getCitiesOfCountry(countryCode: string): ICity[] { - const cities = cityList.filter((value: { countryCode: string }) => { - return value.countryCode === countryCode; - }); - return cities.sort(compare); - }, - getAllCountries(): ICountry[] { - return countryList; - }, - getAllStates(): IState[] { - return stateList; - }, - getAllCities(): ICity[] { - return cityList; - }, - getCountryByCode(isoCode: string): ICountry { - return _findEntryByCode(countryList, isoCode); - }, - // to be deprecate - getStateByCode(isoCode: string): IState { - // eslint-disable-next-line no-console - console.warn( - `WARNING! 'getStateByCode' has been deprecated, please use the new 'getStateByCodeAndCountry' function instead!`, - ); - return _findEntryByCode(stateList, isoCode); - }, - getStateByCodeAndCountry(isoCode: string, countryCode: string): IState { - return _findStateByCodeAndCountryCode(stateList, isoCode, countryCode); - }, -}; +export { Country, State, City }; diff --git a/src/city.ts b/src/city.ts new file mode 100644 index 00000000..4b2a421b --- /dev/null +++ b/src/city.ts @@ -0,0 +1,34 @@ +import cityList from '../lib/city.json'; + +export default class City { + constructor( + public name: string, + public countryCode: string, + public stateCode: string, + public latitude?: string | null, + public longitude?: string | null, + ) {} + + // Get a list of cities belonging to a specific state and country. + static withinStateAndCountry(stateIsoCode: string, countryIsoCode: string): City[] { + const cities = cityList.filter((value: { countryCode: string; stateCode: string }) => { + return value.countryCode === countryIsoCode && value.stateCode === stateIsoCode; + }); + + return cities.sort((a: City, b: City) => (a.name > b.name ? 1 : -1)); + } + + // Get a list of cities belonging to a specific country. + static withinCountry(countryIsoCode: string): City[] { + const cities = cityList.filter((value: { countryCode: string }) => { + return value.countryCode === countryIsoCode; + }); + + return cities.sort((a: City, b: City) => (a.name > b.name ? 1 : -1)); + } + + // Get a list of all cities. + static all(): City[] { + return cityList as City[]; + } +} diff --git a/src/country.ts b/src/country.ts new file mode 100644 index 00000000..f37c5a7d --- /dev/null +++ b/src/country.ts @@ -0,0 +1,31 @@ +import countryList from '../lib/country.json'; +import { Timezones } from './interface'; + +export default class Country { + constructor( + public name: string, + public phonecode: string, + public isoCode: string, + public flag: string, + public currency: string, + public latitude?: string | null, + public longitude?: string | null, + public timezones?: Timezones[], + ) {} + + static byIsoCode(isoCode: string): Country | null { + if (isoCode && countryList != null) { + const codex = countryList.findIndex((c: any) => { + return c.isoCode === isoCode; + }); + + return codex !== -1 ? countryList[codex] : null; + } + + return null; + } + + static all(): Country[] { + return countryList; + } +} diff --git a/src/interface.ts b/src/interface.ts index e8be58f1..f0cfcb93 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -1,33 +1,7 @@ -interface Timezones { - zoneName: string; - gmtOffset: number; - gmtOffsetName: string; - abbreviation: string; - tzName: string; -} - -export interface ICountry { - name: string; - phonecode: string; - isoCode: string; - flag: string; - currency: string; - latitude: string; - longitude: string; - timezones?: Timezones[]; -} - -export interface IState { - name: string; - isoCode: string; - countryCode: string; - latitude?: string | null; - longitude?: string | null; -} -export interface ICity { - name: string; - countryCode: string; - stateCode: string; - latitude?: string | null; - longitude?: string | null; +export interface Timezones { + zoneName: string; + gmtOffset: number; + gmtOffsetName: string; + abbreviation: string; + tzName: string; } diff --git a/src/state.ts b/src/state.ts new file mode 100644 index 00000000..bfa96c1c --- /dev/null +++ b/src/state.ts @@ -0,0 +1,35 @@ +import Country from './country'; +import stateList from '../lib/state.json'; + +export default class State { + constructor( + public name: string, + public isoCode: string, + public countryCode: string, + public latitude?: string | null, + public longitude?: string | null, + ) {} + + // Get a list of states belonging to a specific country. + static withinCountry(countryIsoCode: string): State[] { + const states = stateList.filter((value) => value.countryCode === countryIsoCode); + return states.sort((a: State, b: State) => (a.name > b.name ? 1 : -1)); + } + + // Find a country by it's ISO code and the country in which it is contained. + static byIsoCodeAndCountry(isoCode: string, country: Country): State | null { + if (isoCode && country.isoCode && stateList != null) { + const codex = stateList.findIndex((c: any) => { + return c.isoCode === isoCode && c.countryCode === country.isoCode; + }); + return codex !== -1 ? stateList[codex] : null; + } + + return null; + } + + // Get a list of all states. + static all(): State[] { + return stateList; + } +} From 36bc9c44d777a75579bd46f2b4a32b762844967b Mon Sep 17 00:00:00 2001 From: Taylor Thurlow Date: Wed, 9 Jun 2021 12:06:33 -0700 Subject: [PATCH 3/7] Set sideEffects to false in package.json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 51944602..8079984f 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "Library for fetching Country, its States and Cities", "main": "dist/index.js", "types": "dist/index.d.ts", + "sideEffects": false, "scripts": { "test": "jest", "build": "tsc -p tsconfig.json", From bbb180be85a617fccc026d813789042b10f98236 Mon Sep 17 00:00:00 2001 From: Taylor Thurlow Date: Wed, 9 Jun 2021 12:07:07 -0700 Subject: [PATCH 4/7] Update tsconfig for ES6 and tree-shaking support --- tsconfig.json | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index b52dbe11..0ed93efd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,15 @@ { "compilerOptions": { - "target": "es5", + "target": "es6", "declaration": true, - "module": "commonjs", + "module": "es6", "noImplicitAny": true, - "lib": ["es5", "es2015", "es6"], + "lib": ["es6"], "resolveJsonModule": true, "outDir": "./dist", "strict": true, - "esModuleInterop": true + "esModuleInterop": true, + "moduleResolution": "node" }, "exclude": [ "node_modules", From ac85c4cbac955633fc93e6963212b6fa8fad867b Mon Sep 17 00:00:00 2001 From: Taylor Thurlow Date: Fri, 11 Jun 2021 11:03:17 -0700 Subject: [PATCH 5/7] Revert "Disable tests" This reverts commit 8a53256746a5438782dc792b140ffd656540b202. --- __test__/index.test.ts | 794 +++++++++++++++--------------- __test__/interface-export.test.ts | 116 ++--- package.json | 6 + 3 files changed, 461 insertions(+), 455 deletions(-) diff --git a/__test__/index.test.ts b/__test__/index.test.ts index c746e7c9..588f4129 100644 --- a/__test__/index.test.ts +++ b/__test__/index.test.ts @@ -1,404 +1,404 @@ -// import csc, { ICountry, ICity, IState } from '../index'; +import csc, { ICountry, ICity, IState } from '../index'; -// const executeAllTests = function (csc: any) { -// test('Check for Country By Code ', () => { -// const code = 'CA'; -// const country = csc.getCountryByCode(code); -// expect(country).toEqual({ -// name: 'Canada', -// phonecode: '1', -// isoCode: 'CA', -// flag: '🇨🇦',', -// currency: 'CAD', -// latitude: '60.00000000', -// longitude: '-95.00000000', -// timezones: [ -// { -// zoneName: 'America/Atikokan', -// gmtOffset: -18000, -// gmtOffsetName: 'UTC-05:00', -// abbreviation: 'EST', -// tzName: 'Eastern Standard Time (North America)', -// }, -// { -// zoneName: 'America/Blanc-Sablon', -// gmtOffset: -14400, -// gmtOffsetName: 'UTC-04:00', -// abbreviation: 'AST', -// tzName: 'Atlantic Standard Time', -// }, -// { -// zoneName: 'America/Cambridge_Bay', -// gmtOffset: -25200, -// gmtOffsetName: 'UTC-07:00', -// abbreviation: 'MST', -// tzName: 'Mountain Standard Time (North America)', -// }, -// { -// zoneName: 'America/Creston', -// gmtOffset: -25200, -// gmtOffsetName: 'UTC-07:00', -// abbreviation: 'MST', -// tzName: 'Mountain Standard Time (North America)', -// }, -// { -// zoneName: 'America/Dawson', -// gmtOffset: -25200, -// gmtOffsetName: 'UTC-07:00', -// abbreviation: 'MST', -// tzName: 'Mountain Standard Time (North America)', -// }, -// { -// zoneName: 'America/Dawson_Creek', -// gmtOffset: -25200, -// gmtOffsetName: 'UTC-07:00', -// abbreviation: 'MST', -// tzName: 'Mountain Standard Time (North America)', -// }, -// { -// zoneName: 'America/Edmonton', -// gmtOffset: -25200, -// gmtOffsetName: 'UTC-07:00', -// abbreviation: 'MST', -// tzName: 'Mountain Standard Time (North America)', -// }, -// { -// zoneName: 'America/Fort_Nelson', -// gmtOffset: -25200, -// gmtOffsetName: 'UTC-07:00', -// abbreviation: 'MST', -// tzName: 'Mountain Standard Time (North America)', -// }, -// { -// zoneName: 'America/Glace_Bay', -// gmtOffset: -14400, -// gmtOffsetName: 'UTC-04:00', -// abbreviation: 'AST', -// tzName: 'Atlantic Standard Time', -// }, -// { -// zoneName: 'America/Goose_Bay', -// gmtOffset: -14400, -// gmtOffsetName: 'UTC-04:00', -// abbreviation: 'AST', -// tzName: 'Atlantic Standard Time', -// }, -// { -// zoneName: 'America/Halifax', -// gmtOffset: -14400, -// gmtOffsetName: 'UTC-04:00', -// abbreviation: 'AST', -// tzName: 'Atlantic Standard Time', -// }, -// { -// zoneName: 'America/Inuvik', -// gmtOffset: -25200, -// gmtOffsetName: 'UTC-07:00', -// abbreviation: 'MST', -// tzName: 'Mountain Standard Time (North America', -// }, -// { -// zoneName: 'America/Iqaluit', -// gmtOffset: -18000, -// gmtOffsetName: 'UTC-05:00', -// abbreviation: 'EST', -// tzName: 'Eastern Standard Time (North America', -// }, -// { -// zoneName: 'America/Moncton', -// gmtOffset: -14400, -// gmtOffsetName: 'UTC-04:00', -// abbreviation: 'AST', -// tzName: 'Atlantic Standard Time', -// }, -// { -// zoneName: 'America/Nipigon', -// gmtOffset: -18000, -// gmtOffsetName: 'UTC-05:00', -// abbreviation: 'EST', -// tzName: 'Eastern Standard Time (North America', -// }, -// { -// zoneName: 'America/Pangnirtung', -// gmtOffset: -18000, -// gmtOffsetName: 'UTC-05:00', -// abbreviation: 'EST', -// tzName: 'Eastern Standard Time (North America', -// }, -// { -// zoneName: 'America/Rainy_River', -// gmtOffset: -21600, -// gmtOffsetName: 'UTC-06:00', -// abbreviation: 'CST', -// tzName: 'Central Standard Time (North America', -// }, -// { -// zoneName: 'America/Rankin_Inlet', -// gmtOffset: -21600, -// gmtOffsetName: 'UTC-06:00', -// abbreviation: 'CST', -// tzName: 'Central Standard Time (North America', -// }, -// { -// zoneName: 'America/Regina', -// gmtOffset: -21600, -// gmtOffsetName: 'UTC-06:00', -// abbreviation: 'CST', -// tzName: 'Central Standard Time (North America', -// }, -// { -// zoneName: 'America/Resolute', -// gmtOffset: -21600, -// gmtOffsetName: 'UTC-06:00', -// abbreviation: 'CST', -// tzName: 'Central Standard Time (North America', -// }, -// { -// zoneName: 'America/St_Johns', -// gmtOffset: -12600, -// gmtOffsetName: 'UTC-03:30', -// abbreviation: 'NST', -// tzName: 'Newfoundland Standard Time', -// }, -// { -// zoneName: 'America/Swift_Current', -// gmtOffset: -21600, -// gmtOffsetName: 'UTC-06:00', -// abbreviation: 'CST', -// tzName: 'Central Standard Time (North America', -// }, -// { -// zoneName: 'America/Thunder_Bay', -// gmtOffset: -18000, -// gmtOffsetName: 'UTC-05:00', -// abbreviation: 'EST', -// tzName: 'Eastern Standard Time (North America', -// }, -// { -// zoneName: 'America/Toronto', -// gmtOffset: -18000, -// gmtOffsetName: 'UTC-05:00', -// abbreviation: 'EST', -// tzName: 'Eastern Standard Time (North America', -// }, -// { -// zoneName: 'America/Vancouver', -// gmtOffset: -28800, -// gmtOffsetName: 'UTC-08:00', -// abbreviation: 'PST', -// tzName: 'Pacific Standard Time (North America', -// }, -// { -// zoneName: 'America/Whitehorse', -// gmtOffset: -25200, -// gmtOffsetName: 'UTC-07:00', -// abbreviation: 'MST', -// tzName: 'Mountain Standard Time (North America', -// }, -// { -// zoneName: 'America/Winnipeg', -// gmtOffset: -21600, -// gmtOffsetName: 'UTC-06:00', -// abbreviation: 'CST', -// tzName: 'Central Standard Time (North America', -// }, -// { -// zoneName: 'America/Yellowknife', -// gmtOffset: -25200, -// gmtOffsetName: 'UTC-07:00', -// abbreviation: 'MST', -// tzName: 'Mountain Standard Time (North America', -// }, -// ], -// }); -// }); +const executeAllTests = function (csc: any) { + test('Check for Country By Code ', () => { + const code = 'CA'; + const country = csc.getCountryByCode(code); + expect(country).toEqual({ + name: 'Canada', + phonecode: '1', + isoCode: 'CA', + flag: '🇨🇦', + currency: 'CAD', + latitude: '60.00000000', + longitude: '-95.00000000', + timezones: [ + { + zoneName: 'America/Atikokan', + gmtOffset: -18000, + gmtOffsetName: 'UTC-05:00', + abbreviation: 'EST', + tzName: 'Eastern Standard Time (North America)', + }, + { + zoneName: 'America/Blanc-Sablon', + gmtOffset: -14400, + gmtOffsetName: 'UTC-04:00', + abbreviation: 'AST', + tzName: 'Atlantic Standard Time', + }, + { + zoneName: 'America/Cambridge_Bay', + gmtOffset: -25200, + gmtOffsetName: 'UTC-07:00', + abbreviation: 'MST', + tzName: 'Mountain Standard Time (North America)', + }, + { + zoneName: 'America/Creston', + gmtOffset: -25200, + gmtOffsetName: 'UTC-07:00', + abbreviation: 'MST', + tzName: 'Mountain Standard Time (North America)', + }, + { + zoneName: 'America/Dawson', + gmtOffset: -25200, + gmtOffsetName: 'UTC-07:00', + abbreviation: 'MST', + tzName: 'Mountain Standard Time (North America)', + }, + { + zoneName: 'America/Dawson_Creek', + gmtOffset: -25200, + gmtOffsetName: 'UTC-07:00', + abbreviation: 'MST', + tzName: 'Mountain Standard Time (North America)', + }, + { + zoneName: 'America/Edmonton', + gmtOffset: -25200, + gmtOffsetName: 'UTC-07:00', + abbreviation: 'MST', + tzName: 'Mountain Standard Time (North America)', + }, + { + zoneName: 'America/Fort_Nelson', + gmtOffset: -25200, + gmtOffsetName: 'UTC-07:00', + abbreviation: 'MST', + tzName: 'Mountain Standard Time (North America)', + }, + { + zoneName: 'America/Glace_Bay', + gmtOffset: -14400, + gmtOffsetName: 'UTC-04:00', + abbreviation: 'AST', + tzName: 'Atlantic Standard Time', + }, + { + zoneName: 'America/Goose_Bay', + gmtOffset: -14400, + gmtOffsetName: 'UTC-04:00', + abbreviation: 'AST', + tzName: 'Atlantic Standard Time', + }, + { + zoneName: 'America/Halifax', + gmtOffset: -14400, + gmtOffsetName: 'UTC-04:00', + abbreviation: 'AST', + tzName: 'Atlantic Standard Time', + }, + { + zoneName: 'America/Inuvik', + gmtOffset: -25200, + gmtOffsetName: 'UTC-07:00', + abbreviation: 'MST', + tzName: 'Mountain Standard Time (North America', + }, + { + zoneName: 'America/Iqaluit', + gmtOffset: -18000, + gmtOffsetName: 'UTC-05:00', + abbreviation: 'EST', + tzName: 'Eastern Standard Time (North America', + }, + { + zoneName: 'America/Moncton', + gmtOffset: -14400, + gmtOffsetName: 'UTC-04:00', + abbreviation: 'AST', + tzName: 'Atlantic Standard Time', + }, + { + zoneName: 'America/Nipigon', + gmtOffset: -18000, + gmtOffsetName: 'UTC-05:00', + abbreviation: 'EST', + tzName: 'Eastern Standard Time (North America', + }, + { + zoneName: 'America/Pangnirtung', + gmtOffset: -18000, + gmtOffsetName: 'UTC-05:00', + abbreviation: 'EST', + tzName: 'Eastern Standard Time (North America', + }, + { + zoneName: 'America/Rainy_River', + gmtOffset: -21600, + gmtOffsetName: 'UTC-06:00', + abbreviation: 'CST', + tzName: 'Central Standard Time (North America', + }, + { + zoneName: 'America/Rankin_Inlet', + gmtOffset: -21600, + gmtOffsetName: 'UTC-06:00', + abbreviation: 'CST', + tzName: 'Central Standard Time (North America', + }, + { + zoneName: 'America/Regina', + gmtOffset: -21600, + gmtOffsetName: 'UTC-06:00', + abbreviation: 'CST', + tzName: 'Central Standard Time (North America', + }, + { + zoneName: 'America/Resolute', + gmtOffset: -21600, + gmtOffsetName: 'UTC-06:00', + abbreviation: 'CST', + tzName: 'Central Standard Time (North America', + }, + { + zoneName: 'America/St_Johns', + gmtOffset: -12600, + gmtOffsetName: 'UTC-03:30', + abbreviation: 'NST', + tzName: 'Newfoundland Standard Time', + }, + { + zoneName: 'America/Swift_Current', + gmtOffset: -21600, + gmtOffsetName: 'UTC-06:00', + abbreviation: 'CST', + tzName: 'Central Standard Time (North America', + }, + { + zoneName: 'America/Thunder_Bay', + gmtOffset: -18000, + gmtOffsetName: 'UTC-05:00', + abbreviation: 'EST', + tzName: 'Eastern Standard Time (North America', + }, + { + zoneName: 'America/Toronto', + gmtOffset: -18000, + gmtOffsetName: 'UTC-05:00', + abbreviation: 'EST', + tzName: 'Eastern Standard Time (North America', + }, + { + zoneName: 'America/Vancouver', + gmtOffset: -28800, + gmtOffsetName: 'UTC-08:00', + abbreviation: 'PST', + tzName: 'Pacific Standard Time (North America', + }, + { + zoneName: 'America/Whitehorse', + gmtOffset: -25200, + gmtOffsetName: 'UTC-07:00', + abbreviation: 'MST', + tzName: 'Mountain Standard Time (North America', + }, + { + zoneName: 'America/Winnipeg', + gmtOffset: -21600, + gmtOffsetName: 'UTC-06:00', + abbreviation: 'CST', + tzName: 'Central Standard Time (North America', + }, + { + zoneName: 'America/Yellowknife', + gmtOffset: -25200, + gmtOffsetName: 'UTC-07:00', + abbreviation: 'MST', + tzName: 'Mountain Standard Time (North America', + }, + ], + }); + }); -// test('Check for Canada States', () => { -// const code = 'CA'; -// const country = csc.getCountryByCode(code); -// const states = csc.getStatesOfCountry(country.isoCode); -// const names = states.map((state: IState) => { -// return state.name; -// }); -// expect(names).toEqual([ -// 'Alberta', -// 'British Columbia', -// 'Manitoba', -// 'New Brunswick', -// 'Newfoundland and Labrador', -// 'Northwest Territories', -// 'Nova Scotia', -// 'Nunavut', -// 'Ontario', -// 'Prince Edward Island', -// 'Quebec', -// 'Saskatchewan', -// 'Yukon', -// ]); -// }); + test('Check for Canada States', () => { + const code = 'CA'; + const country = csc.getCountryByCode(code); + const states = csc.getStatesOfCountry(country.isoCode); + const names = states.map((state: IState) => { + return state.name; + }); + expect(names).toEqual([ + 'Alberta', + 'British Columbia', + 'Manitoba', + 'New Brunswick', + 'Newfoundland and Labrador', + 'Northwest Territories', + 'Nova Scotia', + 'Nunavut', + 'Ontario', + 'Prince Edward Island', + 'Quebec', + 'Saskatchewan', + 'Yukon', + ]); + }); -// test('Check All States for United States Of America', () => { -// const code = 'US'; -// const country = csc.getCountryByCode(code); -// const states = csc.getStatesOfCountry(country.isoCode); -// const names = states.map((state: IState) => { -// return state.name; -// }); -// expect(names).toEqual([ -// 'Alabama', -// 'Alaska', -// 'American Samoa', -// 'Arizona', -// 'Arkansas', -// 'Baker Island', -// 'California', -// 'Colorado', -// 'Connecticut', -// 'Delaware', -// 'District of Columbia', -// 'Florida', -// 'Georgia', -// 'Guam', -// 'Hawaii', -// 'Howland Island', -// 'Idaho', -// 'Illinois', -// 'Indiana', -// 'Iowa', -// 'Jarvis Island', -// 'Johnston Atoll', -// 'Kansas', -// 'Kentucky', -// 'Kingman Reef', -// 'Louisiana', -// 'Maine', -// 'Maryland', -// 'Massachusetts', -// 'Michigan', -// 'Midway Atoll', -// 'Minnesota', -// 'Mississippi', -// 'Missouri', -// 'Montana', -// 'Navassa Island', -// 'Nebraska', -// 'Nevada', -// 'New Hampshire', -// 'New Jersey', -// 'New Mexico', -// 'New York', -// 'North Carolina', -// 'North Dakota', -// 'Northern Mariana Islands', -// 'Ohio', -// 'Oklahoma', -// 'Oregon', -// 'Palmyra Atoll', -// 'Pennsylvania', -// 'Puerto Rico', -// 'Rhode Island', -// 'South Carolina', -// 'South Dakota', -// 'Tennessee', -// 'Texas', -// 'United States Minor Outlying Islands', -// 'United States Virgin Islands', -// 'Utah', -// 'Vermont', -// 'Virginia', -// 'Wake Island', -// 'Washington', -// 'West Virginia', -// 'Wisconsin', -// 'Wyoming', -// ]); -// }); + test('Check All States for United States Of America', () => { + const code = 'US'; + const country = csc.getCountryByCode(code); + const states = csc.getStatesOfCountry(country.isoCode); + const names = states.map((state: IState) => { + return state.name; + }); + expect(names).toEqual([ + 'Alabama', + 'Alaska', + 'American Samoa', + 'Arizona', + 'Arkansas', + 'Baker Island', + 'California', + 'Colorado', + 'Connecticut', + 'Delaware', + 'District of Columbia', + 'Florida', + 'Georgia', + 'Guam', + 'Hawaii', + 'Howland Island', + 'Idaho', + 'Illinois', + 'Indiana', + 'Iowa', + 'Jarvis Island', + 'Johnston Atoll', + 'Kansas', + 'Kentucky', + 'Kingman Reef', + 'Louisiana', + 'Maine', + 'Maryland', + 'Massachusetts', + 'Michigan', + 'Midway Atoll', + 'Minnesota', + 'Mississippi', + 'Missouri', + 'Montana', + 'Navassa Island', + 'Nebraska', + 'Nevada', + 'New Hampshire', + 'New Jersey', + 'New Mexico', + 'New York', + 'North Carolina', + 'North Dakota', + 'Northern Mariana Islands', + 'Ohio', + 'Oklahoma', + 'Oregon', + 'Palmyra Atoll', + 'Pennsylvania', + 'Puerto Rico', + 'Rhode Island', + 'South Carolina', + 'South Dakota', + 'Tennessee', + 'Texas', + 'United States Minor Outlying Islands', + 'United States Virgin Islands', + 'Utah', + 'Vermont', + 'Virginia', + 'Wake Island', + 'Washington', + 'West Virginia', + 'Wisconsin', + 'Wyoming', + ]); + }); -// test('Check States for India', () => { -// const code = 'IN'; -// const country = csc.getCountryByCode(code); -// const states = csc.getStatesOfCountry(country.isoCode); -// const names = states.map((state: IState) => { -// return state.name; -// }); + test('Check States for India', () => { + const code = 'IN'; + const country = csc.getCountryByCode(code); + const states = csc.getStatesOfCountry(country.isoCode); + const names = states.map((state: IState) => { + return state.name; + }); -// expect(names).toEqual([ -// 'Andaman and Nicobar Islands', -// 'Andhra Pradesh', -// 'Arunachal Pradesh', -// 'Assam', -// 'Bihar', -// 'Chandigarh', -// 'Chhattisgarh', -// 'Dadra and Nagar Haveli', -// 'Daman and Diu', -// 'Delhi', -// 'Goa', -// 'Gujarat', -// 'Haryana', -// 'Himachal Pradesh', -// 'Jammu and Kashmir', -// 'Jharkhand', -// 'Karnataka', -// 'Kerala', -// 'Ladakh', -// 'Lakshadweep', -// 'Madhya Pradesh', -// 'Maharashtra', -// 'Manipur', -// 'Meghalaya', -// 'Mizoram', -// 'Nagaland', -// 'Odisha', -// 'Puducherry', -// 'Punjab', -// 'Rajasthan', -// 'Sikkim', -// 'Tamil Nadu', -// 'Telangana', -// 'Tripura', -// 'Uttar Pradesh', -// 'Uttarakhand', -// 'West Bengal', -// ]); -// }); + expect(names).toEqual([ + 'Andaman and Nicobar Islands', + 'Andhra Pradesh', + 'Arunachal Pradesh', + 'Assam', + 'Bihar', + 'Chandigarh', + 'Chhattisgarh', + 'Dadra and Nagar Haveli', + 'Daman and Diu', + 'Delhi', + 'Goa', + 'Gujarat', + 'Haryana', + 'Himachal Pradesh', + 'Jammu and Kashmir', + 'Jharkhand', + 'Karnataka', + 'Kerala', + 'Ladakh', + 'Lakshadweep', + 'Madhya Pradesh', + 'Maharashtra', + 'Manipur', + 'Meghalaya', + 'Mizoram', + 'Nagaland', + 'Odisha', + 'Puducherry', + 'Punjab', + 'Rajasthan', + 'Sikkim', + 'Tamil Nadu', + 'Telangana', + 'Tripura', + 'Uttar Pradesh', + 'Uttarakhand', + 'West Bengal', + ]); + }); -// test('Check Cities for Delhi', () => { -// const countryCode = 'IN'; -// const stateCode = 'DL'; -// const cities = csc.getCitiesOfState(countryCode, stateCode); -// const names = cities.map((city: ICity) => { -// return city.name; -// }); -// expect(names).toEqual([ -// 'Alīpur', -// 'Bawāna', -// 'Central Delhi', -// 'Delhi', -// 'Deoli', -// 'East Delhi', -// 'Karol Bāgh', -// 'Najafgarh', -// 'Narela', -// 'New Delhi', -// 'North Delhi', -// 'North East Delhi', -// 'North West Delhi', -// 'Nāngloi Jāt', -// 'Pitampura', -// 'Rohini', -// 'South Delhi', -// 'South West Delhi', -// 'West Delhi', -// ]); -// }); + test('Check Cities for Delhi', () => { + const countryCode = 'IN'; + const stateCode = 'DL'; + const cities = csc.getCitiesOfState(countryCode, stateCode); + const names = cities.map((city: ICity) => { + return city.name; + }); + expect(names).toEqual([ + 'Alīpur', + 'Bawāna', + 'Central Delhi', + 'Delhi', + 'Deoli', + 'East Delhi', + 'Karol Bāgh', + 'Najafgarh', + 'Narela', + 'New Delhi', + 'North Delhi', + 'North East Delhi', + 'North West Delhi', + 'Nāngloi Jāt', + 'Pitampura', + 'Rohini', + 'South Delhi', + 'South West Delhi', + 'West Delhi', + ]); + }); -// test('Get State by State ISOCode and Country Code', () => { -// const countryCode = 'PK'; -// const stateCode = 'KP'; -// const state = csc.getStateByCodeAndCountry(stateCode, countryCode); -// expect(state.name).toEqual('Khyber Pakhtunkhwa'); -// }); -// }; -// export default executeAllTests; -// executeAllTests(csc); + test('Get State by State ISOCode and Country Code', () => { + const countryCode = 'PK'; + const stateCode = 'KP'; + const state = csc.getStateByCodeAndCountry(stateCode, countryCode); + expect(state.name).toEqual('Khyber Pakhtunkhwa'); + }); +}; +export default executeAllTests; +executeAllTests(csc); diff --git a/__test__/interface-export.test.ts b/__test__/interface-export.test.ts index ee333184..697d8c96 100644 --- a/__test__/interface-export.test.ts +++ b/__test__/interface-export.test.ts @@ -1,69 +1,69 @@ // importing interfaces from main index.ts file -// import { ICountry, ICity, IState } from '../index'; +import { ICountry, ICity, IState } from '../index'; // writing tests for Interfaces // https://stackoverflow.com/questions/14425568/interface-type-check-with-typescript -// function isValidCountryObjectStructure(object: any): object is ICountry { -// return 'name' in object && 'phonecode' in object && 'isoCode' in object && 'flag' in object; -// } +function isValidCountryObjectStructure(object: any): object is ICountry { + return 'name' in object && 'phonecode' in object && 'isoCode' in object && 'flag' in object; +} -// test('Check for Interface export when Type Structure is Same', () => { -// const country: ICountry = { -// name: 'India', -// phonecode: '+91', -// isoCode: 'IN', -// flag: '🇮🇳', -// currency: 'INR', -// latitude: '20.00000000', -// longitude: '77.00000000', -// timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] -// }; -// let isCountry = isValidCountryObjectStructure(country) -// expect(isCountry).toEqual(true); -// }); +test('Check for Interface export when Type Structure is Same', () => { + const country: ICountry = { + name: 'India', + phonecode: '+91', + isoCode: 'IN', + flag: '🇮🇳', + currency: 'INR', + latitude: '20.00000000', + longitude: '77.00000000', + timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] + }; + let isCountry = isValidCountryObjectStructure(country) + expect(isCountry).toEqual(true); +}); -// test('Check for Interface export when Type Structure is Not Same', () => { -// const country = { -// phonecode: '+91', // missing name field -// isoCode: 'IN', -// flag: '🇮🇳' -// }; -// let isCountry = isValidCountryObjectStructure(country) -// expect(isCountry).toEqual(false); -// }); +test('Check for Interface export when Type Structure is Not Same', () => { + const country = { + phonecode: '+91', // missing name field + isoCode: 'IN', + flag: '🇮🇳' + }; + let isCountry = isValidCountryObjectStructure(country) + expect(isCountry).toEqual(false); +}); -// function isValidCountryObjectAndValueType(object: any): object is ICountry { -// return typeof typeof object.name == "string" && typeof object.phonecode == "string" && typeof object.isoCode == "string" && typeof object.flag == "string"; -// } +function isValidCountryObjectAndValueType(object: any): object is ICountry { + return typeof typeof object.name == "string" && typeof object.phonecode == "string" && typeof object.isoCode == "string" && typeof object.flag == "string"; +} -// test('Check for Interface export when Type Structure is Same and Value is of same type as well', () => { -// const country: ICountry = { -// name: 'India', -// phonecode: '+91', -// isoCode: 'IN', -// flag: '🇮🇳', -// currency: 'INR', -// latitude: '20.00000000', -// longitude: '77.00000000', -// timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] -// }; -// let isCountry = isValidCountryObjectAndValueType(country) -// expect(isCountry).toEqual(true); -// }); +test('Check for Interface export when Type Structure is Same and Value is of same type as well', () => { + const country: ICountry = { + name: 'India', + phonecode: '+91', + isoCode: 'IN', + flag: '🇮🇳', + currency: 'INR', + latitude: '20.00000000', + longitude: '77.00000000', + timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] + }; + let isCountry = isValidCountryObjectAndValueType(country) + expect(isCountry).toEqual(true); +}); -// test('Check for Interface export when Type Structure is Same and Value is of same type as well', () => { -// const country = { -// name: 'India', -// phonecode: 91, // wrong type -// isoCode: 'IN', -// flag: '🇮🇳', -// currency: 'INR', -// latitude: '20.00000000', -// longitude: '77.00000000', -// timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] -// }; -// let isCountry = isValidCountryObjectAndValueType(country) -// expect(isCountry).toEqual(false); -// }); +test('Check for Interface export when Type Structure is Same and Value is of same type as well', () => { + const country = { + name: 'India', + phonecode: 91, // wrong type + isoCode: 'IN', + flag: '🇮🇳', + currency: 'INR', + latitude: '20.00000000', + longitude: '77.00000000', + timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] + }; + let isCountry = isValidCountryObjectAndValueType(country) + expect(isCountry).toEqual(false); +}); diff --git a/package.json b/package.json index 8079984f..63796c97 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,12 @@ "type": "git", "url": "git+https://github.com/harpreetkhalsagtbit/country-state-city.git" }, + "husky": { + "hooks": { + "pre-commit": "npm test", + "pre-push": "npm test" + } + }, "keywords": [ "Country", "State", From 75796173eb3cebce8ff35584927e044ea0394bb7 Mon Sep 17 00:00:00 2001 From: Taylor Thurlow Date: Fri, 11 Jun 2021 11:56:25 -0700 Subject: [PATCH 6/7] Remove checks for list and parameter presence Also eliminate an unnecessary Country import. --- src/country.ts | 12 ++++-------- src/state.ts | 17 ++++++----------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/country.ts b/src/country.ts index f37c5a7d..4e26d715 100644 --- a/src/country.ts +++ b/src/country.ts @@ -14,15 +14,11 @@ export default class Country { ) {} static byIsoCode(isoCode: string): Country | null { - if (isoCode && countryList != null) { - const codex = countryList.findIndex((c: any) => { - return c.isoCode === isoCode; - }); + const codex = countryList.findIndex((c: any) => { + return c.isoCode === isoCode; + }); - return codex !== -1 ? countryList[codex] : null; - } - - return null; + return codex !== -1 ? countryList[codex] : null; } static all(): Country[] { diff --git a/src/state.ts b/src/state.ts index bfa96c1c..cf38b517 100644 --- a/src/state.ts +++ b/src/state.ts @@ -1,4 +1,3 @@ -import Country from './country'; import stateList from '../lib/state.json'; export default class State { @@ -16,16 +15,12 @@ export default class State { return states.sort((a: State, b: State) => (a.name > b.name ? 1 : -1)); } - // Find a country by it's ISO code and the country in which it is contained. - static byIsoCodeAndCountry(isoCode: string, country: Country): State | null { - if (isoCode && country.isoCode && stateList != null) { - const codex = stateList.findIndex((c: any) => { - return c.isoCode === isoCode && c.countryCode === country.isoCode; - }); - return codex !== -1 ? stateList[codex] : null; - } - - return null; + // Find a state by it's ISO code and the country in which it is contained. + static byIsoCodeAndCountry(isoCode: string, countryIsoCode: string): State | null { + const codex = stateList.findIndex((c: any) => { + return c.isoCode === isoCode && c.countryCode === countryIsoCode; + }); + return codex !== -1 ? stateList[codex] : null; } // Get a list of all states. From 1541648354a6c41802b52bf66e86d645d1d48f87 Mon Sep 17 00:00:00 2001 From: Taylor Thurlow Date: Fri, 11 Jun 2021 11:56:41 -0700 Subject: [PATCH 7/7] Add tests for new classes --- __test__/amdModule.test.js | 5 - __test__/city.test.ts | 71 ++++++ __test__/country.test.ts | 45 ++++ __test__/index.test.ts | 404 ------------------------------ __test__/interface-export.test.ts | 69 ----- __test__/state.test.ts | 185 ++++++++++++++ 6 files changed, 301 insertions(+), 478 deletions(-) delete mode 100644 __test__/amdModule.test.js create mode 100644 __test__/city.test.ts create mode 100644 __test__/country.test.ts delete mode 100644 __test__/index.test.ts delete mode 100644 __test__/interface-export.test.ts create mode 100644 __test__/state.test.ts diff --git a/__test__/amdModule.test.js b/__test__/amdModule.test.js deleted file mode 100644 index 42948d31..00000000 --- a/__test__/amdModule.test.js +++ /dev/null @@ -1,5 +0,0 @@ -const csc = require('../index').default; - -const executeAllTests = require('./index.test').default; - -executeAllTests(csc); diff --git a/__test__/city.test.ts b/__test__/city.test.ts new file mode 100644 index 00000000..0f17984b --- /dev/null +++ b/__test__/city.test.ts @@ -0,0 +1,71 @@ +import City from '../src/city'; + +const executeAllTests = () => { + describe('constructor', () => { + test('constructs a city', () => { + const city = new City('Foo', 'XX', 'XX', null, null); + + expect(city).not.toBeNull(); + }); + }); + + describe('withinStateAndCountry', () => { + test('it finds cities', () => { + const lookupResult = City.withinStateAndCountry('DL', 'IN'); + const names = lookupResult.map((city) => city.name); + + expect(names).toEqual([ + 'Alīpur', + 'Bawāna', + 'Central Delhi', + 'Delhi', + 'Deoli', + 'East Delhi', + 'Karol Bāgh', + 'Najafgarh', + 'Narela', + 'New Delhi', + 'North Delhi', + 'North East Delhi', + 'North West Delhi', + 'Nāngloi Jāt', + 'Pitampura', + 'Rohini', + 'South Delhi', + 'South West Delhi', + 'West Delhi', + ]); + }); + }); + + describe('withinCountry', () => { + test('it finds cities', () => { + const lookupResult = City.withinCountry('TV'); + const names = lookupResult.map((city) => city.name); + + expect(names).toEqual([ + 'Alapi Village', + 'Asau Village', + 'Fakaifou Village', + 'Funafuti', + 'Kulia Village', + 'Niulakita', + 'Savave Village', + 'Tanrake Village', + 'Toga Village', + ]); + }); + }); + + describe('all', () => { + test('it gets all cities', () => { + const result = City.all(); + + expect(result).not.toBeNull(); + expect(result[0]).not.toEqual([]); + }); + }); +}; + +export default executeAllTests; +executeAllTests(); diff --git a/__test__/country.test.ts b/__test__/country.test.ts new file mode 100644 index 00000000..07212224 --- /dev/null +++ b/__test__/country.test.ts @@ -0,0 +1,45 @@ +import Country from '../src/country'; + +const executeAllTests = () => { + describe('constructor', () => { + test('constructs a country', () => { + const country = new Country('Foo', '1', 'XX', 'FLAGEMOJI', '$', null, null, []); + + expect(country).not.toBeNull(); + }); + }); + + describe('byIsoCode', () => { + test('it finds Canada', () => { + const lookupResult = Country.byIsoCode('CA'); + + expect(lookupResult).not.toBeNull(); + expect(lookupResult!.name).toEqual('Canada'); + expect(lookupResult!.isoCode).toEqual('CA'); + }); + + test('it finds the USA', () => { + const lookupResult = Country.byIsoCode('US'); + + expect(lookupResult).not.toBeNull(); + expect(lookupResult!.name).toEqual('United States'); + expect(lookupResult!.isoCode).toEqual('US'); + }); + + test('it returns null when not found', () => { + expect(Country.byIsoCode('XX')).toBeNull(); + }); + }); + + describe('all', () => { + test('it gets all countries', () => { + const result = Country.all(); + + expect(result).not.toBeNull(); + expect(result[0]).not.toEqual([]); + }); + }); +}; + +export default executeAllTests; +executeAllTests(); diff --git a/__test__/index.test.ts b/__test__/index.test.ts deleted file mode 100644 index 588f4129..00000000 --- a/__test__/index.test.ts +++ /dev/null @@ -1,404 +0,0 @@ -import csc, { ICountry, ICity, IState } from '../index'; - -const executeAllTests = function (csc: any) { - test('Check for Country By Code ', () => { - const code = 'CA'; - const country = csc.getCountryByCode(code); - expect(country).toEqual({ - name: 'Canada', - phonecode: '1', - isoCode: 'CA', - flag: '🇨🇦', - currency: 'CAD', - latitude: '60.00000000', - longitude: '-95.00000000', - timezones: [ - { - zoneName: 'America/Atikokan', - gmtOffset: -18000, - gmtOffsetName: 'UTC-05:00', - abbreviation: 'EST', - tzName: 'Eastern Standard Time (North America)', - }, - { - zoneName: 'America/Blanc-Sablon', - gmtOffset: -14400, - gmtOffsetName: 'UTC-04:00', - abbreviation: 'AST', - tzName: 'Atlantic Standard Time', - }, - { - zoneName: 'America/Cambridge_Bay', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America)', - }, - { - zoneName: 'America/Creston', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America)', - }, - { - zoneName: 'America/Dawson', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America)', - }, - { - zoneName: 'America/Dawson_Creek', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America)', - }, - { - zoneName: 'America/Edmonton', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America)', - }, - { - zoneName: 'America/Fort_Nelson', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America)', - }, - { - zoneName: 'America/Glace_Bay', - gmtOffset: -14400, - gmtOffsetName: 'UTC-04:00', - abbreviation: 'AST', - tzName: 'Atlantic Standard Time', - }, - { - zoneName: 'America/Goose_Bay', - gmtOffset: -14400, - gmtOffsetName: 'UTC-04:00', - abbreviation: 'AST', - tzName: 'Atlantic Standard Time', - }, - { - zoneName: 'America/Halifax', - gmtOffset: -14400, - gmtOffsetName: 'UTC-04:00', - abbreviation: 'AST', - tzName: 'Atlantic Standard Time', - }, - { - zoneName: 'America/Inuvik', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America', - }, - { - zoneName: 'America/Iqaluit', - gmtOffset: -18000, - gmtOffsetName: 'UTC-05:00', - abbreviation: 'EST', - tzName: 'Eastern Standard Time (North America', - }, - { - zoneName: 'America/Moncton', - gmtOffset: -14400, - gmtOffsetName: 'UTC-04:00', - abbreviation: 'AST', - tzName: 'Atlantic Standard Time', - }, - { - zoneName: 'America/Nipigon', - gmtOffset: -18000, - gmtOffsetName: 'UTC-05:00', - abbreviation: 'EST', - tzName: 'Eastern Standard Time (North America', - }, - { - zoneName: 'America/Pangnirtung', - gmtOffset: -18000, - gmtOffsetName: 'UTC-05:00', - abbreviation: 'EST', - tzName: 'Eastern Standard Time (North America', - }, - { - zoneName: 'America/Rainy_River', - gmtOffset: -21600, - gmtOffsetName: 'UTC-06:00', - abbreviation: 'CST', - tzName: 'Central Standard Time (North America', - }, - { - zoneName: 'America/Rankin_Inlet', - gmtOffset: -21600, - gmtOffsetName: 'UTC-06:00', - abbreviation: 'CST', - tzName: 'Central Standard Time (North America', - }, - { - zoneName: 'America/Regina', - gmtOffset: -21600, - gmtOffsetName: 'UTC-06:00', - abbreviation: 'CST', - tzName: 'Central Standard Time (North America', - }, - { - zoneName: 'America/Resolute', - gmtOffset: -21600, - gmtOffsetName: 'UTC-06:00', - abbreviation: 'CST', - tzName: 'Central Standard Time (North America', - }, - { - zoneName: 'America/St_Johns', - gmtOffset: -12600, - gmtOffsetName: 'UTC-03:30', - abbreviation: 'NST', - tzName: 'Newfoundland Standard Time', - }, - { - zoneName: 'America/Swift_Current', - gmtOffset: -21600, - gmtOffsetName: 'UTC-06:00', - abbreviation: 'CST', - tzName: 'Central Standard Time (North America', - }, - { - zoneName: 'America/Thunder_Bay', - gmtOffset: -18000, - gmtOffsetName: 'UTC-05:00', - abbreviation: 'EST', - tzName: 'Eastern Standard Time (North America', - }, - { - zoneName: 'America/Toronto', - gmtOffset: -18000, - gmtOffsetName: 'UTC-05:00', - abbreviation: 'EST', - tzName: 'Eastern Standard Time (North America', - }, - { - zoneName: 'America/Vancouver', - gmtOffset: -28800, - gmtOffsetName: 'UTC-08:00', - abbreviation: 'PST', - tzName: 'Pacific Standard Time (North America', - }, - { - zoneName: 'America/Whitehorse', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America', - }, - { - zoneName: 'America/Winnipeg', - gmtOffset: -21600, - gmtOffsetName: 'UTC-06:00', - abbreviation: 'CST', - tzName: 'Central Standard Time (North America', - }, - { - zoneName: 'America/Yellowknife', - gmtOffset: -25200, - gmtOffsetName: 'UTC-07:00', - abbreviation: 'MST', - tzName: 'Mountain Standard Time (North America', - }, - ], - }); - }); - - test('Check for Canada States', () => { - const code = 'CA'; - const country = csc.getCountryByCode(code); - const states = csc.getStatesOfCountry(country.isoCode); - const names = states.map((state: IState) => { - return state.name; - }); - expect(names).toEqual([ - 'Alberta', - 'British Columbia', - 'Manitoba', - 'New Brunswick', - 'Newfoundland and Labrador', - 'Northwest Territories', - 'Nova Scotia', - 'Nunavut', - 'Ontario', - 'Prince Edward Island', - 'Quebec', - 'Saskatchewan', - 'Yukon', - ]); - }); - - test('Check All States for United States Of America', () => { - const code = 'US'; - const country = csc.getCountryByCode(code); - const states = csc.getStatesOfCountry(country.isoCode); - const names = states.map((state: IState) => { - return state.name; - }); - expect(names).toEqual([ - 'Alabama', - 'Alaska', - 'American Samoa', - 'Arizona', - 'Arkansas', - 'Baker Island', - 'California', - 'Colorado', - 'Connecticut', - 'Delaware', - 'District of Columbia', - 'Florida', - 'Georgia', - 'Guam', - 'Hawaii', - 'Howland Island', - 'Idaho', - 'Illinois', - 'Indiana', - 'Iowa', - 'Jarvis Island', - 'Johnston Atoll', - 'Kansas', - 'Kentucky', - 'Kingman Reef', - 'Louisiana', - 'Maine', - 'Maryland', - 'Massachusetts', - 'Michigan', - 'Midway Atoll', - 'Minnesota', - 'Mississippi', - 'Missouri', - 'Montana', - 'Navassa Island', - 'Nebraska', - 'Nevada', - 'New Hampshire', - 'New Jersey', - 'New Mexico', - 'New York', - 'North Carolina', - 'North Dakota', - 'Northern Mariana Islands', - 'Ohio', - 'Oklahoma', - 'Oregon', - 'Palmyra Atoll', - 'Pennsylvania', - 'Puerto Rico', - 'Rhode Island', - 'South Carolina', - 'South Dakota', - 'Tennessee', - 'Texas', - 'United States Minor Outlying Islands', - 'United States Virgin Islands', - 'Utah', - 'Vermont', - 'Virginia', - 'Wake Island', - 'Washington', - 'West Virginia', - 'Wisconsin', - 'Wyoming', - ]); - }); - - test('Check States for India', () => { - const code = 'IN'; - const country = csc.getCountryByCode(code); - const states = csc.getStatesOfCountry(country.isoCode); - const names = states.map((state: IState) => { - return state.name; - }); - - expect(names).toEqual([ - 'Andaman and Nicobar Islands', - 'Andhra Pradesh', - 'Arunachal Pradesh', - 'Assam', - 'Bihar', - 'Chandigarh', - 'Chhattisgarh', - 'Dadra and Nagar Haveli', - 'Daman and Diu', - 'Delhi', - 'Goa', - 'Gujarat', - 'Haryana', - 'Himachal Pradesh', - 'Jammu and Kashmir', - 'Jharkhand', - 'Karnataka', - 'Kerala', - 'Ladakh', - 'Lakshadweep', - 'Madhya Pradesh', - 'Maharashtra', - 'Manipur', - 'Meghalaya', - 'Mizoram', - 'Nagaland', - 'Odisha', - 'Puducherry', - 'Punjab', - 'Rajasthan', - 'Sikkim', - 'Tamil Nadu', - 'Telangana', - 'Tripura', - 'Uttar Pradesh', - 'Uttarakhand', - 'West Bengal', - ]); - }); - - test('Check Cities for Delhi', () => { - const countryCode = 'IN'; - const stateCode = 'DL'; - const cities = csc.getCitiesOfState(countryCode, stateCode); - const names = cities.map((city: ICity) => { - return city.name; - }); - expect(names).toEqual([ - 'Alīpur', - 'Bawāna', - 'Central Delhi', - 'Delhi', - 'Deoli', - 'East Delhi', - 'Karol Bāgh', - 'Najafgarh', - 'Narela', - 'New Delhi', - 'North Delhi', - 'North East Delhi', - 'North West Delhi', - 'Nāngloi Jāt', - 'Pitampura', - 'Rohini', - 'South Delhi', - 'South West Delhi', - 'West Delhi', - ]); - }); - - test('Get State by State ISOCode and Country Code', () => { - const countryCode = 'PK'; - const stateCode = 'KP'; - const state = csc.getStateByCodeAndCountry(stateCode, countryCode); - expect(state.name).toEqual('Khyber Pakhtunkhwa'); - }); -}; -export default executeAllTests; -executeAllTests(csc); diff --git a/__test__/interface-export.test.ts b/__test__/interface-export.test.ts deleted file mode 100644 index 697d8c96..00000000 --- a/__test__/interface-export.test.ts +++ /dev/null @@ -1,69 +0,0 @@ -// importing interfaces from main index.ts file -import { ICountry, ICity, IState } from '../index'; - -// writing tests for Interfaces -// https://stackoverflow.com/questions/14425568/interface-type-check-with-typescript - -function isValidCountryObjectStructure(object: any): object is ICountry { - return 'name' in object && 'phonecode' in object && 'isoCode' in object && 'flag' in object; -} - -test('Check for Interface export when Type Structure is Same', () => { - const country: ICountry = { - name: 'India', - phonecode: '+91', - isoCode: 'IN', - flag: '🇮🇳', - currency: 'INR', - latitude: '20.00000000', - longitude: '77.00000000', - timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] - }; - let isCountry = isValidCountryObjectStructure(country) - expect(isCountry).toEqual(true); -}); - -test('Check for Interface export when Type Structure is Not Same', () => { - const country = { - phonecode: '+91', // missing name field - isoCode: 'IN', - flag: '🇮🇳' - }; - let isCountry = isValidCountryObjectStructure(country) - expect(isCountry).toEqual(false); -}); - -function isValidCountryObjectAndValueType(object: any): object is ICountry { - return typeof typeof object.name == "string" && typeof object.phonecode == "string" && typeof object.isoCode == "string" && typeof object.flag == "string"; -} - -test('Check for Interface export when Type Structure is Same and Value is of same type as well', () => { - const country: ICountry = { - name: 'India', - phonecode: '+91', - isoCode: 'IN', - flag: '🇮🇳', - currency: 'INR', - latitude: '20.00000000', - longitude: '77.00000000', - timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] - }; - let isCountry = isValidCountryObjectAndValueType(country) - expect(isCountry).toEqual(true); -}); - -test('Check for Interface export when Type Structure is Same and Value is of same type as well', () => { - const country = { - name: 'India', - phonecode: 91, // wrong type - isoCode: 'IN', - flag: '🇮🇳', - currency: 'INR', - latitude: '20.00000000', - longitude: '77.00000000', - timezones: [{"zoneName":"Asia\/Kolkata","gmtOffset":19800,"gmtOffsetName":"UTC+05:30","abbreviation":"IST","tzName":"Indian Standard Time"}] - }; - let isCountry = isValidCountryObjectAndValueType(country) - expect(isCountry).toEqual(false); -}); - diff --git a/__test__/state.test.ts b/__test__/state.test.ts new file mode 100644 index 00000000..ff76055f --- /dev/null +++ b/__test__/state.test.ts @@ -0,0 +1,185 @@ +import State from '../src/state'; + +const executeAllTests = () => { + describe('constructor', () => { + test('constructs a state', () => { + const state = new State('Foo', 'XX', 'XX', null, null); + + expect(state).not.toBeNull(); + }); + }); + + describe('withinCountry', () => { + test('it finds states within Canada', () => { + const lookupResult = State.withinCountry('CA'); + const names = lookupResult.map((state) => state.name); + + expect(names).toEqual([ + 'Alberta', + 'British Columbia', + 'Manitoba', + 'New Brunswick', + 'Newfoundland and Labrador', + 'Northwest Territories', + 'Nova Scotia', + 'Nunavut', + 'Ontario', + 'Prince Edward Island', + 'Quebec', + 'Saskatchewan', + 'Yukon', + ]); + }); + + test('it finds states within the USA', () => { + const lookupResult = State.withinCountry('US'); + const names = lookupResult.map((state) => state.name); + + expect(names).toEqual([ + 'Alabama', + 'Alaska', + 'American Samoa', + 'Arizona', + 'Arkansas', + 'Baker Island', + 'California', + 'Colorado', + 'Connecticut', + 'Delaware', + 'District of Columbia', + 'Florida', + 'Georgia', + 'Guam', + 'Hawaii', + 'Howland Island', + 'Idaho', + 'Illinois', + 'Indiana', + 'Iowa', + 'Jarvis Island', + 'Johnston Atoll', + 'Kansas', + 'Kentucky', + 'Kingman Reef', + 'Louisiana', + 'Maine', + 'Maryland', + 'Massachusetts', + 'Michigan', + 'Midway Atoll', + 'Minnesota', + 'Mississippi', + 'Missouri', + 'Montana', + 'Navassa Island', + 'Nebraska', + 'Nevada', + 'New Hampshire', + 'New Jersey', + 'New Mexico', + 'New York', + 'North Carolina', + 'North Dakota', + 'Northern Mariana Islands', + 'Ohio', + 'Oklahoma', + 'Oregon', + 'Palmyra Atoll', + 'Pennsylvania', + 'Puerto Rico', + 'Rhode Island', + 'South Carolina', + 'South Dakota', + 'Tennessee', + 'Texas', + 'United States Minor Outlying Islands', + 'United States Virgin Islands', + 'Utah', + 'Vermont', + 'Virginia', + 'Wake Island', + 'Washington', + 'West Virginia', + 'Wisconsin', + 'Wyoming', + ]); + }); + + test('it finds states within India', () => { + const lookupResult = State.withinCountry('IN'); + const names = lookupResult.map((state) => state.name); + + expect(names).toEqual([ + 'Andaman and Nicobar Islands', + 'Andhra Pradesh', + 'Arunachal Pradesh', + 'Assam', + 'Bihar', + 'Chandigarh', + 'Chhattisgarh', + 'Dadra and Nagar Haveli', + 'Daman and Diu', + 'Delhi', + 'Goa', + 'Gujarat', + 'Haryana', + 'Himachal Pradesh', + 'Jammu and Kashmir', + 'Jharkhand', + 'Karnataka', + 'Kerala', + 'Ladakh', + 'Lakshadweep', + 'Madhya Pradesh', + 'Maharashtra', + 'Manipur', + 'Meghalaya', + 'Mizoram', + 'Nagaland', + 'Odisha', + 'Puducherry', + 'Punjab', + 'Rajasthan', + 'Sikkim', + 'Tamil Nadu', + 'Telangana', + 'Tripura', + 'Uttar Pradesh', + 'Uttarakhand', + 'West Bengal', + ]); + }); + }); + + describe('byIsoCodeAndCountry', () => { + test('finds California', () => { + const lookupResult = State.byIsoCodeAndCountry('CA', 'US'); + + expect(lookupResult).not.toBeNull(); + expect(lookupResult!.name).toEqual('California'); + }); + + test('finds Punjab', () => { + const lookupResult = State.byIsoCodeAndCountry('PB', 'IN'); + + expect(lookupResult).not.toBeNull(); + expect(lookupResult!.name).toEqual('Punjab'); + }); + + test('it returns null when not found', () => { + expect(State.byIsoCodeAndCountry('XX', 'XX')).toBeNull(); + }); + }); + + describe('all', () => { + test('it gets all states', () => { + const result = State.all(); + + expect(result).not.toBeNull(); + expect(result[0]).not.toEqual([]); + }); + }); +}; + +export default executeAllTests; +executeAllTests();