Skip to content

Commit

Permalink
feat: expose array-like methods for searching data
Browse files Browse the repository at this point in the history
Instead of limiting an end-user to a string search, enable `predicate` functions to `find` and `filter` values.

BREAKING CHANGE: exposed api changed from string search to predicate methods
  • Loading branch information
Tyler Stewart committed Aug 4, 2020
1 parent 641ff87 commit 038c1ca
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 35 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "canadian-city-timezones",
"version": "1.0.0",
"version": "2.0.0",
"description": "Searchable timezones for all Canadian cities, towns, townships, villages, hamlets, and municipalities.",
"main": "src/index.js",
"types": "src/index.d.ts",
Expand All @@ -19,6 +19,10 @@
"timezones"
],
"author": "trs",
"repository": {
"type": "git",
"url": "https://github.com/autovance/canadian-city-timezones"
},
"license": "MIT",
"devDependencies": {
"csv-parser": "^2.3.3",
Expand Down
7 changes: 5 additions & 2 deletions src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const fetch = require('node-fetch');
const csv = require('csv-parser');
const geoTz = require('geo-tz');
const removeAccents = require('remove-accents');
const {removeSpecialCharacters} = require('./util');

const mkdirAsync = promisify(mkdir);
const pipelineAsync = promisify(pipeline);
Expand All @@ -27,11 +26,15 @@ const VALID_CSD_TYPES = [
'Village'
];

const VALID_CHARACTERS_REGEX = /[^a-zA-Z0-9 ]/ig;

const removeSpecialCharacters = (value = '') => value.replace(VALID_CHARACTERS_REGEX, '').toLocaleLowerCase();

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function * getCityData() {
const req = await fetch(CAN_CITY_LIST);
// Download file first as the csv parser would prematurely end being piped it directly
// Download file first as the csv parser would prematurely end being piped in directly
await pipelineAsync(
req.body,
createWriteStream('gc.csv')
Expand Down
8 changes: 6 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ declare interface TimezoneResult {
timezone: string;
}

declare function find(query: string): Promise<TimezoneResult>;
declare type Predicate = (data: TimezoneResult) => boolean;

declare function findAll(query: string): Promise<TimezoneResult[]>;
declare function values(): AsyncGenerator<TimezoneResult[]>;

declare function find(predicate: Predicate): Promise<TimezoneResult>;

declare function findAll(predicate: Predicate): AsyncGenerator<TimezoneResult[]>;
36 changes: 15 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,37 @@ async function * getDataIterator() {
}
}

const isPartialMatchFactory = (query) => {
const searchItems = query.split(' ').map((item) => removeSpecialCharacters(item));

return (data) => {
const values = [
data.name,
data.province
];

return searchItems.every((item) => values.join().includes(item));
}
async function* values() {
yield* getDataIterator();
}

async function find(query) {
const isPartialMatch = isPartialMatchFactory(query);
async function find(predicate) {
if (typeof predicate !== 'function') {
throw new TypeError(`${String(predicate)} is not a function`);
}

for await (const data of getDataIterator()) {
if (isPartialMatch(data)) {
if (predicate(data)) {
return data;
}
}
return null;
}

async function findAll(query) {
const isPartialMatch = isPartialMatchFactory(query);
async function* filter(predicate) {
if (typeof predicate !== 'function') {
throw new TypeError(`${String(predicate)} is not a function`);
}

const results = [];
for await (const data of getDataIterator()) {
if (isPartialMatch(data)) {
results.push(data);
if (predicate(data)) {
yield data;
}
}
return results;
}

module.exports = {
values,
find,
findAll
filter
};
9 changes: 0 additions & 9 deletions src/util.js

This file was deleted.

0 comments on commit 038c1ca

Please sign in to comment.