Skip to content

Commit

Permalink
Merge pull request #51 from blakek/move-to-typescript
Browse files Browse the repository at this point in the history
Move to typescript
  • Loading branch information
blakek authored Nov 11, 2021
2 parents 080981b + ebf8219 commit 6412458
Show file tree
Hide file tree
Showing 14 changed files with 1,870 additions and 1,147 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# https://editorconfig.org/

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/*
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
extends: [require.resolve('amper-scripts/config/eslint')],
root: true
};
26 changes: 26 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Publish update

on:
push:
branches:
- master

jobs:
publish:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: actions/setup-node@v2
with:
node-version: '16'
cache: 'yarn'
registry-url: 'https://registry.npmjs.org'

- run: yarn install

- run: yarn publish
name: Publish to npm
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
29 changes: 29 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Run tests

on: push

jobs:
run-tests:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
node_version:
- '12'
- '14'
- '16'
- '17'

steps:
- uses: actions/checkout@v2

- uses: actions/setup-node@v2
name: Use Node.js v${{matrix.node_version}}
with:
node-version: ${{matrix.node_version}}
cache: 'yarn'

- run: yarn install

- run: yarn test
6 changes: 0 additions & 6 deletions .travis.yml

This file was deleted.

24 changes: 0 additions & 24 deletions index.js

This file was deleted.

33 changes: 26 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "geo2zip",
"version": "2.0.2",
"version": "3.0.0",
"description": "Translates latitude / longitude geolocations to the nearest corresponding U.S. zip code",
"main": "index.js",
"main": "dist/index.js",
"repository": {
"url": "https://github.com/blakek/geo2zip",
"type": "git"
Expand Down Expand Up @@ -33,21 +33,40 @@
"neighbors"
],
"license": "MIT",
"ava": {
"extensions": [
"js",
"ts"
],
"files": [
"src/**/*.test.ts"
],
"ignoredByWatcher": [
"!src/**"
],
"require": [
"ts-node/register"
]
},
"scripts": {
"build": "tsc",
"format-check": "amper-scripts format-check *.js",
"lint": "amper-scripts lint *.js",
"prepack": "run-s build",
"test": "ava",
"validate": "run-s test format-check lint",
"watch": "ava --watch"
},
"devDependencies": {
"amper-scripts": "^1.0.0-0",
"ava": "^3.3.0",
"npm-run-all": "^4.1.5"
"amper-scripts": "^1.2.4",
"ava": "^3.15.0",
"npm-run-all": "^4.1.5",
"ts-node": "^10.4.0",
"typescript": "^4.4.4"
},
"dependencies": {
"sphere-knn": "^1.4.0",
"standardize-geolocation": "^2.0.2",
"us-zips": "^4.0.0"
"standardize-geolocation": "^4.0.0",
"us-zips": "^2021.11.4"
}
}
14 changes: 1 addition & 13 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
module.exports = {
arrowParens: 'avoid',
bracketSpacing: true,
endOfLine: 'lf',
jsxBracketSameLine: false,
jsxSingleQuote: false,
printWidth: 80,
semi: true,
singleQuote: true,
tabWidth: 2,
trailingComma: 'none',
useTabs: false
};
module.exports = require('amper-scripts/config/prettier.config.js');
20 changes: 8 additions & 12 deletions test.js → src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('ava');
const geo2zip = require('.');
import test from 'ava';
import geo2zip from '.';

test('returns zip code nearest a geolocation', async t => {
const testPoints = [
Expand All @@ -13,12 +13,10 @@ test('returns zip code nearest a geolocation', async t => {
}
];

const tests = testPoints.map(async ({ expected, location }) => {
const actual = await geo2zip(location);
t.is(actual[0], expected);
});

return Promise.all(tests);
for (const { expected, location } of testPoints) {
const [actual] = await geo2zip(location);
t.is(actual, expected);
}
});

test('returns a list of zip codes nearest a location, ordered by closeness', async t => {
Expand All @@ -33,10 +31,8 @@ test('returns a list of zip codes nearest a location, ordered by closeness', asy
}
];

const tests = testPoints.map(async ({ expected, location }) => {
for (const { expected, location } of testPoints) {
const actual = await geo2zip(location, { limit: 5 });
t.true(actual.includes(expected));
});

return Promise.all(tests);
}
});
35 changes: 35 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sphereKnn from 'sphere-knn';
import {
GeolocationInput,
standardizeGeolocation
} from 'standardize-geolocation';
import { ZIPCode } from 'us-zips';
import usZips, { ZIPCodeList } from 'us-zips/array';

export interface Options {
limit: number;
}

export const defaultOptions: Options = { limit: 1 };

// Start building lookup when module imported
const lookup = sphereKnn(usZips);

// Don't recreate this function on every run
const pluckZipCode = (obj: { zipCode: string }) => obj.zipCode;

export function geo2zip(
location: GeolocationInput,
extraOptions: Partial<Options> = {}
): Promise<ZIPCode[]> {
const options = { ...defaultOptions, ...extraOptions };
const { latitude, longitude } = standardizeGeolocation(location);

return new Promise(resolve => {
const results = lookup(latitude, longitude, options.limit) as ZIPCodeList;

resolve(results.map(pluckZipCode));
});
}

export default geo2zip;
28 changes: 28 additions & 0 deletions src/types/sphere-knn.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
declare module 'sphere-knn' {
type WithLatitude = { lat: number } | { latitude: number };

type WithLongitude =
| { lon: number }
| { lng: number }
| { long: number }
| { longitude: number };

type Geolocation =
| [number, number]
| { location: [number, number] }
| { position: [number, number] }
| { geometry: { coordinates: [number, number]; type: 'Point' } }
| (WithLatitude & WithLongitude);

type Lookup = (
latitude: number,
longitude: number,
limit?: number,
maxDistance?: number
) => Geolocation[];

type SphereKnn<T = Geolocation> = (points: T[]) => Lookup;

const defaultExport: SphereKnn;
export = defaultExport;
}
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"declaration": true,
"esModuleInterop": true,
"incremental": true,
"module": "CommonJS",
"moduleResolution": "node",
"noImplicitAny": true,
"outDir": "dist",
"sourceMap": true,
"target": "ES2019",
"paths": {
"*": ["node_modules/*", "src/types/*"]
}
},
"include": ["src/**/*"]
}
Loading

0 comments on commit 6412458

Please sign in to comment.