* Generates random zip code from specified format. If format is not specified, * the locale's zip format is used. * * @param options The format used to generate the zip code or an options object. * @param options.state The state to generate the zip code for. * If the current locale do
(
options:
| string
| {
/**
* The state to generate the zip code for.
*
* If the current locale does not have a corresponding `postcode_by_state` definition, an error is thrown.
*/
state?: string;
/**
* The optional format used to generate the zip code.
*
* This won't be used if the state option is specified.
*
* @default faker.definitions.location.postcode
*/
format?: string;
} = {}
)
| 230 | * @since 8.0.0 |
| 231 | */ |
| 232 | zipCode( |
| 233 | options: |
| 234 | | string |
| 235 | | { |
| 236 | /** |
| 237 | * The state to generate the zip code for. |
| 238 | * |
| 239 | * If the current locale does not have a corresponding `postcode_by_state` definition, an error is thrown. |
| 240 | */ |
| 241 | state?: string; |
| 242 | /** |
| 243 | * The optional format used to generate the zip code. |
| 244 | * |
| 245 | * This won't be used if the state option is specified. |
| 246 | * |
| 247 | * @default faker.definitions.location.postcode |
| 248 | */ |
| 249 | format?: string; |
| 250 | } = {} |
| 251 | ): string { |
| 252 | if (typeof options === 'string') { |
| 253 | options = { format: options }; |
| 254 | } |
| 255 | |
| 256 | const { state } = options; |
| 257 | |
| 258 | if (state != null) { |
| 259 | const zipPattern = |
| 260 | this.faker.definitions.location.postcode_by_state[state]; |
| 261 | |
| 262 | if (zipPattern == null) { |
| 263 | throw new FakerError( |
| 264 | `No zip code definition found for state "${state}"` |
| 265 | ); |
| 266 | } |
| 267 | |
| 268 | return this.faker.helpers.fake(zipPattern); |
| 269 | } |
| 270 | |
| 271 | let { format = this.faker.definitions.location.postcode } = options; |
| 272 | if (typeof format === 'string') { |
| 273 | format = [format]; |
| 274 | } |
| 275 | |
| 276 | format = this.faker.helpers.arrayElement(format); |
| 277 | |
| 278 | return this.faker.helpers.replaceSymbols(format); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Generates a random localized city name. |
no test coverage detected