@@ -121,6 +121,8 @@ const toUnit: Parser = postcode => {
121121 return firstOrNull ( match ) ;
122122} ;
123123
124+ const returnNull = ( ) => null ;
125+
124126/**
125127 * Postcode
126128 *
@@ -145,6 +147,18 @@ class Postcode {
145147 constructor ( postcode : string ) {
146148 this . _raw = postcode ;
147149 this . _valid = isValid ( postcode ) ;
150+
151+ // All parse methods should return null if invalid
152+ if ( ! this . _valid ) {
153+ this . incode = returnNull ;
154+ this . outcode = returnNull ;
155+ this . area = returnNull ;
156+ this . district = returnNull ;
157+ this . subDistrict = returnNull ;
158+ this . sector = returnNull ;
159+ this . unit = returnNull ;
160+ this . normalise = returnNull ;
161+ }
148162 }
149163
150164 static isValid = isValid ;
@@ -164,21 +178,18 @@ class Postcode {
164178 }
165179
166180 incode ( ) : string | null {
167- if ( ! this . _valid ) return null ;
168181 if ( this . _incode ) return this . _incode ;
169182 this . _incode = toIncode ( this . _raw ) ;
170183 return this . _incode ;
171184 }
172185
173186 outcode ( ) : string | null {
174- if ( ! this . _valid ) return null ;
175187 if ( this . _outcode ) return this . _outcode ;
176188 this . _outcode = toOutcode ( this . _raw ) ;
177189 return this . _outcode ;
178190 }
179191
180192 area ( ) : string | null {
181- if ( ! this . _valid ) return null ;
182193 if ( this . _area ) return this . _area ;
183194 this . _area = toArea ( this . _raw ) ;
184195 return this . _area ;
@@ -206,21 +217,17 @@ class Postcode {
206217
207218 sector ( ) : string | null {
208219 if ( this . _sector ) return this . _sector ;
209- const normalised = this . normalise ( ) ;
210- if ( normalised === null ) return null ;
211- this . _sector = toSector ( normalised ) ;
220+ this . _sector = toSector ( this . _raw ) ;
212221 return this . _sector ;
213222 }
214223
215224 unit ( ) : string | null {
216- if ( ! this . _valid ) return null ;
217225 if ( this . _unit ) return this . _unit ;
218226 this . _unit = toUnit ( this . _raw ) ;
219227 return this . _unit ;
220228 }
221229
222230 normalise ( ) : string | null {
223- if ( ! this . _valid ) return null ;
224231 return `${ this . outcode ( ) } ${ this . incode ( ) } ` ;
225232 }
226233}
0 commit comments