Skip to content

Commit dd1315d

Browse files
committed
feat(Regex): Exports and documents used Regexs
1 parent 9d576c8 commit dd1315d

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

lib/index.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
1+
/**
2+
* @hidden
3+
*/
14
export const DISTRICT_SPLIT_REGEX = /^([a-z]{1,2}\d)([a-z])$/i;
5+
/**
6+
* Tests for the unit section of a postcode
7+
*/
28
export const UNIT_REGEX = /[a-z]{2}$/i;
9+
/**
10+
* Tests for the inward code section of a postcode
11+
*/
312
export const INCODE_REGEX = /\d[a-z]{2}$/i;
13+
/**
14+
* Tests for the outward code section of a postcode
15+
*/
416
export const OUTCODE_REGEX = /^[a-z]{1,2}\d[a-z\d]?$/i;
17+
/**
18+
* Tests for a valid postcode
19+
*/
520
export const POSTCODE_REGEX = /^[a-z]{1,2}\d[a-z\d]?\s*\d[a-z]{2}$/i;
21+
22+
/**
23+
* Tests for the area section of a postcode
24+
*/
625
export const AREA_REGEX = /^[a-z]{1,2}/i;
726

27+
/**
28+
* @hidden
29+
*/
830
interface Validator {
931
(input: string): boolean;
1032
}
1133

1234
interface Parser {
35+
/**
36+
* @hidden
37+
*/
1338
(postcode: string): string | null;
1439
}
1540

@@ -37,6 +62,10 @@ type InvalidPostcode = {
3762
unit: null;
3863
};
3964

65+
/**
66+
* Invalid postcode prototype
67+
* @hidden
68+
*/
4069
const invalidPostcode: InvalidPostcode = {
4170
valid: false,
4271
postcode: null,
@@ -209,6 +238,36 @@ export const toSubDistrict: Parser = (postcode) => {
209238

210239
/**
211240
* Returns a ValidPostcode or InvalidPostcode object from a postcode string
241+
*
242+
* @example
243+
*
244+
* ```
245+
* import { parse } from "postcode";
246+
*
247+
* const {
248+
* postcode, // => "SW1A 2AA"
249+
* outcode, // => "SW1A"
250+
* incode, // => "2AA"
251+
* area, // => "SW"
252+
* district, // => "SW1"
253+
* unit, // => "AA"
254+
* sector, // => "SW1A 2"
255+
* subDistrict, // => "SW1A"
256+
* valid, // => true
257+
* } = parse("Sw1A 2aa");
258+
*
259+
* const {
260+
* postcode, // => null
261+
* outcode, // => null
262+
* incode, // => null
263+
* area, // => null
264+
* district, // => null
265+
* unit, // => null
266+
* sector, // => null
267+
* subDistrict, // => null
268+
* valid, // => false
269+
* } = parse(" Oh no, ): ");
270+
* ```
212271
*/
213272
export const parse = (postcode: string): ValidPostcode | InvalidPostcode => {
214273
if (!isValid(postcode)) return { ...invalidPostcode };

0 commit comments

Comments
 (0)