Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.0.0 #30

Merged
merged 39 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
71c98d3
Update project configuration
cblanc Apr 7, 2019
209efe4
Update author info
cblanc Apr 7, 2019
7e57321
Use ~ for pinning deps
cblanc Apr 7, 2019
0444f92
Add tslint config
cblanc Apr 7, 2019
7468275
Add @types/node
cblanc Apr 7, 2019
0299504
Update gitignore
cblanc Apr 7, 2019
780287d
Migrate unit tests to typescript
cblanc Apr 7, 2019
4497103
Migrate exhaustive test suite to typescript
cblanc Apr 7, 2019
b7e615a
Port to typescript
cblanc Apr 8, 2019
47597a3
Switch to csv-parse from csv; Add build script
cblanc Apr 13, 2019
fd9c12f
Enable strict compilation
cblanc Apr 13, 2019
3ba63a2
Fix link to repo
cblanc Apr 13, 2019
417ef51
Updated readme
cblanc Apr 13, 2019
c075fc3
Update changelog
cblanc Apr 13, 2019
81db2de
Add typedoc and script
cblanc Apr 13, 2019
acf9378
Add linter to tests
cblanc Apr 14, 2019
18b06cf
Move out fixture logic
cblanc Apr 14, 2019
6c3229a
Update design
cblanc Apr 14, 2019
eceee23
Convert existing private methods to static methods
cblanc Apr 14, 2019
4dbede0
Point instances to a static method if postcode is invalid
cblanc Apr 14, 2019
43349b5
Implement toDistrict
cblanc Apr 14, 2019
7f06280
Implement toSubDistrict static method
cblanc Apr 14, 2019
861a364
Memoize and return on one line
cblanc Apr 14, 2019
6e552e3
Require minimum node of 8.0.0; Bump major version
cblanc Apr 14, 2019
4d52e18
Add toSubDistrict tests
cblanc Apr 18, 2019
8eab216
Fix
cblanc Apr 18, 2019
5d66fa3
Update README.md
cblanc Apr 18, 2019
d6b6702
Update README.md
cblanc Apr 18, 2019
4d6c841
Move out interface definitions
cblanc Apr 19, 2019
2336c47
Implement Postcode.parse
cblanc Apr 19, 2019
01bf744
Add benchmarks
cblanc Apr 19, 2019
14423da
Add npm install version
cblanc Apr 19, 2019
85e9bd5
Implement basic benchmarks
cblanc Apr 19, 2019
1cd240a
Benchmark static methods
cblanc Apr 19, 2019
3737f70
Fix
cblanc Apr 19, 2019
fcc6e98
Merge remote-tracking branch 'origin/master' into 2.0.0
cblanc Apr 19, 2019
b62073d
Documentation fixes
cblanc Apr 19, 2019
445e748
Prepare for publish
cblanc Apr 19, 2019
87d0356
Final update to docs
cblanc Apr 19, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
node_modules
#
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# dotenv environment variables file
.env

dist

.cache

.DS_Store
docs

10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

Any changes, including backwards incompatible changes will be listed here

## 2.0.0 (12/02/2019)

- *Breaking Change*: Require minimum node.js of 8.0.0
- Ported to typescript (now exports typings)
- Provides a cleaner, more modern API to extract and parse while supporting old methods
- Add static methods to extract single datapoints
- Add a ValidPostcode class with accessor methods which can be destructured
- Updated documentation: [postcodejs.ideal-postcodes.dev](https://postcodejs.ideal-postcodes.dev)
- Added benchmarking

## 1.0.1 (12/02/2019)

- Add runkit example
Expand Down
136 changes: 120 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,125 @@
[![CircleCI](https://circleci.com/gh/ideal-postcodes/postcode.svg?style=svg)](https://circleci.com/gh/ideal-postcodes/postcode) [![Coverage Status](https://coveralls.io/repos/github/ideal-postcodes/postcode/badge.svg?branch=master)](https://coveralls.io/github/ideal-postcodes/postcode?branch=master) ![Dependencies](https://img.shields.io/david/ideal-postcodes/postcode.svg?style=flat) ![Size](https://img.shields.io/bundlephobia/min/postcode.svg?style=flat)
<h1 align="center">
<img src="https://img.ideal-postcodes.co.uk/Postcode.js%20Logo@3x.png" alt="Postcode.js">
</h1>

# Postcode
> Validate & parse UK postcodes

[![CircleCI](https://circleci.com/gh/ideal-postcodes/postcode.svg?style=svg)](https://circleci.com/gh/ideal-postcodes/postcode)
[![Coverage Status](https://coveralls.io/repos/github/ideal-postcodes/postcode/badge.svg?branch=master)](https://coveralls.io/github/ideal-postcodes/postcode?branch=master)
![Dependencies](https://img.shields.io/david/ideal-postcodes/postcode.svg?style=flat)
![Size](https://img.shields.io/bundlephobia/min/postcode.svg?style=flat)
![Downloads](https://img.shields.io/npm/dm/postcode.svg)
[![Try postcode on RunKit](https://badge.runkitcdn.com/postcode.svg)](https://npm.runkit.com/postcode)

Utility methods for UK Postcodes.
Utility methods for UK Postcodes, including validating the shape of a postcode, extracting postcode elements (like incodes, outcodes, areas and [more](#Definitions)).

Included is a test suite that tests against all postcodes listed in the Ordnance Survey's postcode dataset as of January 2014.
Tested against ~1.7 million postcodes on ONSPD.

## Features

- [Check](#validate) whether a postcode conforms to the [correct format](https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Formatting)
- [Extract](#parse) useful elements of a postcode like incode, outcode, sector
- [Single purpose static methods](#static-methods)
- Tested against a list of ~1.7 million postcodes listed on ONS Postcode Directory

## Links

- [GitHub Repository](https://github.com/ideal-postcodes/openapi)
- [API Documentation](https://postcodejs.ideal-postcodes.dev)
- [Try postcode.js on RunKit](https://npm.runkit.com/postcode)
- [Postcode element definitions](#definitions)
- [Caveat on postcode validation](#note-on-postcode-Validation)
- [NPM Package](https://www.npmjs.com/package/postcode)

## Getting Started

Install with `npm install postcode`
### Installation

```bash
npm install postcode
```

### Wield

```javascript
import Postcode from "postcode";
```

### Validate

```javascript
Postcode.isValid("AA1 1AB"); // => true
```

### Parse

Pass a string to `Postcode.parse`. This will return a valid or invalid postcode instance which can be easily destructured.

#### Valid Postcode

```
const {
normalised, // => "SW1A 2AA"
outcode, // => "SW1A"
incode, // => "2AA"
area, // => "SW"
district, // => "SW1"
unit, // => "AA"
sector, // => "SW1A 2"
subDistrict, // => "SW1A"
valid, // => true
} = Postcode.parse("Sw1A 2aa");
```

#### Invalid postcode

Create an instance of Postcode to perform utility methods, like so
```
const {
normalised, // => null
outcode, // => null
incode, // => null
area, // => null
district, // => null
unit, // => null
sector, // => null
subDistrict, // => null
valid, // => false
} = Postcode.parse(" Oh no, ): ");
```

#### Accessor Overview

| Postcode | .outcode | .incode | .area | .district | .subDistrict | .sector | .unit |
|----------|----------|---------|-------|-----------|--------------|---------|-------|
| AA9A 9AA | AA9A | 9AA | AA | AA9 | AA9A | AA9A 9 | AA |
| A9A 9AA | A9A | 9AA | A | A9 | A9A | A9A 9 | AA |
| A9 9AA | A9 | 9AA | A | A9 | `null` | A9 9 | AA |
| A99 9AA | A99 | 9AA | A | A99 | `null` | A99 9 | AA |
| AA9 9AA | AA9 | 9AA | AA | AA9 | `null` | AA9 9 | AA |
| AA99 9AA | AA99 | 9AA | AA | AA99 | `null` | AA99 9 | AA |

### Static Methods

If you're just after a single value, you would be better served by calling a static method on `Postcode`.

```javascript
Postcode.isValid("Sw1A 2aa"); // => true

Postcode.toNormalised("Sw1A 2aa"); // => "SW1A 2AA"
Postcode.toOutcode("Sw1A 2aa"); // => "SW1A"
Postcode.toIncode("Sw1A 2aa"); // => "2AA"
Postcode.toArea("Sw1A 2aa"); // => "AA"
Postcode.toDistrict("Sw1A 2aa"); // => "SW1"
Postcode.toSubDistrict("Sw1A 2aa"); // => "SW1A"
Postcode.toSector("Sw1A 2aa"); // => "SW1A 2"
Postcode.toUnit("Sw1A 2aa"); // => "AA"
```

### Older API

Below documents the old validation API, which continues to be supported.

Create an instance of Postcode to perform utility methods, like so:

```javascript
const Postcode = require("postcode");
Expand Down Expand Up @@ -91,23 +198,20 @@ The postcode sector is made up of the postcode district, the single space, and t

The postcode unit is two characters added to the end of the postcode sector. Each postcode unit generally represents a street, part of a street, a single address, a group of properties, a single property, a sub-section of the property, an individual organisation or (for instance Driver and Vehicle Licensing Agency) a subsection of the organisation. The level of discrimination is often based on the amount of mail received by the premises or business. Examples of postcode units include "NY" (from "SW1W 0NY"), "GZ" (from "PO16 7GZ"), "HF" (from "GU16 7HF"), or "JQ" (from "L1 8JQ").

Sources:

- https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Formatting
- https://en.wikipedia.org/wiki/London_postal_district#Numbered_divisions

## Testing

```npm test```

## Note on Postcode Validation

Postcodes cannot be validated just with a regular expression. Proper postcode validation requires having a full list of postcodes to check against. Relying on a regex will produce false postives/negatives.

A complete list of Postcodes can be obtained from the ONS Postcode Directory, which is updated every 3 months.

## Testing

```bash
npm test
```

## License

MIT

Contains Ordnance Survey Data © Crown Copyright & Database Right 2014
Contains Ordnance Survey Data © Crown Copyright & Database Right
Loading