diff --git a/.eslintrc.js b/.eslintrc.js index 28b76bdf039..e0b62edef9b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -12,6 +12,7 @@ module.exports = defineConfig({ browser: true, node: true, }, + reportUnusedDisableDirectives: true, extends: [ 'eslint:recommended', 'plugin:@typescript-eslint/recommended', diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 12e7b59c458..8aca7a54dac 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,7 +4,20 @@ If you want to make `Faker` a better, please read the following contribution gui # Important -Please make sure that you run `pnpm run preflight` before making a PR to ensure that everything is working from the start. +Please make sure that you run `pnpm run preflight` before making a PR to ensure that everything is working from the start. +This is a shorthand for running the following scripts in order: + +- `pnpm install` - installs npm packages defined in package.json +- `pnpm run generate:locales` - generates locale files +- `pnpm run generate:api-doc` - generates API documentation +- `pnpm run format` - runs [prettify](https://github.com/prettier/prettier) to format code +- `pnpm run lint` - runs [ESLint](https://github.com/eslint/eslint) to enforce project code standards +- `pnpm run build:clean` - removes artifacts from previous builds +- `pnpm run build:code` - builds the code, both CommonJS and ESM versions +- `pnpm run build:types` - builds the TypeScript type definitions +- `pnpm run test:update-snapshots ` - runs all tests, and updates any snapshots if needed +- `pnpm run ts-check:scripts` - checks that there are no TypeScript errors in script files +- `pnpm run ts-check:tests` - checks that there are no TypeScript errors in test or script files ## Good to know diff --git a/docs/guide/localization.md b/docs/guide/localization.md index 407d411fe27..3c90df20b00 100644 --- a/docs/guide/localization.md +++ b/docs/guide/localization.md @@ -91,6 +91,7 @@ In this example there are 5 locales. Each of these is checked in order, and the | `en_CA` | English (Canada) | `fakerEN_CA` | | `en_GB` | English (Great Britain) | `fakerEN_GB` | | `en_GH` | English (Ghana) | `fakerEN_GH` | +| `en_HK` | English (Hong Kong) | `fakerEN_HK` | | `en_IE` | English (Ireland) | `fakerEN_IE` | | `en_IN` | English (India) | `fakerEN_IN` | | `en_NG` | English (Nigeria) | `fakerEN_NG` | diff --git a/docs/guide/upgrading.md b/docs/guide/upgrading.md index 0572f5f1149..03d1acb9a61 100644 --- a/docs/guide/upgrading.md +++ b/docs/guide/upgrading.md @@ -123,20 +123,12 @@ fakerES.music.songName(); // 'I Want to Hold Your Hand' (fallback from en) fakerES_noFallbacks.music.songName(); // throws a FakerError ``` -This also has an impact on data that aren't applicable to a locale, for example Chinese doesn't use prefixes in names. +This also has an impact on data that aren't applicable to a locale, for example Hong Kong (`en_HK`) doesn't use ZIP codes/postcodes. ```ts -import { faker, fakerZH_CN, zh_CN } from '@faker-js/faker'; - -const fakerZH_CN_noFallbacks = new Faker({ - locale: [zh_CN], -}); - -faker.name.prefix(); // 'Mr' -// Previously: -//fakerZH_CN_noFallbacks.person.prefix(); // undefined -// Now: -fakerZH_CN.person.prefix(); // throws a FakerError +import { fakerEN_US, fakerEN_HK } from '@faker-js/faker'; +fakerEN_US.location.zipCode(); // 90210 +fakerEN_HK.location.zipCode(); // throws a FakerError ``` ### `faker.mersenne` and `faker.helpers.repeatString` removed @@ -316,9 +308,9 @@ faker.number.float({ max: 100, precision: 0.01 }); // 35.21 The method `faker.datatype.array` has been deprecated and will be removed in v9. If you need an array of useful values, you are better off creating your own one using `faker.helpers.multiple`. -### `faker.datatype.datetime` deprecated in favor of `faker.date.between` +### `faker.datatype.datetime` deprecated in favor of `faker.date.between` and `faker.date.anytime` -The `datetime` method previously found in `faker.datatype` has been deprecated, use `faker.date.between` instead. +The `datetime` method previously found in `faker.datatype` has been deprecated, use `faker.date.between` or `faker.date.anytime` instead. ### `allowLeadingZeros` behavior change in `faker.string.numeric` diff --git a/package.json b/package.json index 7d1cbe64aaf..80e7f7b1b4a 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,7 @@ "docs:serve": "vitepress serve docs --port 5173", "docs:diff": "tsx ./scripts/diff.ts", "format": "prettier --cache --write .", - "lint": "eslint --cache --cache-strategy content .", + "lint": "eslint --cache --cache-strategy content --report-unused-disable-directives .", "ts-check": "run-s ts-check:scripts ts-check:tests", "ts-check:scripts": "tsc --project tsconfig.check-scripts.json", "ts-check:tests": "tsc --project tsconfig.check-tests.json", diff --git a/src/internal/deprecated.ts b/src/internal/deprecated.ts index a49e64f640d..fe2cdc94ddd 100644 --- a/src/internal/deprecated.ts +++ b/src/internal/deprecated.ts @@ -1,4 +1,3 @@ -/* eslint-disable jsdoc/check-tag-names */ /* eslint-disable jsdoc/require-param */ /** diff --git a/src/locale-proxy.ts b/src/locale-proxy.ts index c5c6aa1b733..ae5ede1757a 100644 --- a/src/locale-proxy.ts +++ b/src/locale-proxy.ts @@ -31,6 +31,10 @@ export function createLocaleProxy(locale: LocaleDefinition): LocaleProxy { target: LocaleDefinition, categoryName: keyof LocaleDefinition ): LocaleDefinition[keyof LocaleDefinition] { + if (typeof categoryName === 'symbol' || categoryName === 'nodeType') { + return target[categoryName]; + } + if (categoryName in proxies) { return proxies[categoryName]; } @@ -69,7 +73,9 @@ function createCategoryProxy< entryName: keyof CategoryData ): CategoryData[keyof CategoryData] { const value = target[entryName]; - if (value === null) { + if (typeof entryName === 'symbol' || entryName === 'nodeType') { + return value; + } else if (value === null) { throw new FakerError( `The locale data for '${categoryName}.${entryName.toString()}' aren't applicable to this locale. If you think this is a bug, please report it at: https://github.com/faker-js/faker` diff --git a/src/locale/en_HK.ts b/src/locale/en_HK.ts new file mode 100644 index 00000000000..a1d9494780a --- /dev/null +++ b/src/locale/en_HK.ts @@ -0,0 +1,13 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ + +import { Faker } from '../faker'; +import base from '../locales/base'; +import en from '../locales/en'; +import en_HK from '../locales/en_HK'; + +export const faker = new Faker({ + locale: [en_HK, en, base], +}); diff --git a/src/locale/index.ts b/src/locale/index.ts index 8772dca61b1..6b52d54e0b1 100644 --- a/src/locale/index.ts +++ b/src/locale/index.ts @@ -20,6 +20,7 @@ import { faker as fakerEN_BORK } from './en_BORK'; import { faker as fakerEN_CA } from './en_CA'; import { faker as fakerEN_GB } from './en_GB'; import { faker as fakerEN_GH } from './en_GH'; +import { faker as fakerEN_HK } from './en_HK'; import { faker as fakerEN_IE } from './en_IE'; import { faker as fakerEN_IN } from './en_IN'; import { faker as fakerEN_NG } from './en_NG'; @@ -84,6 +85,7 @@ export { fakerEN_CA, fakerEN_GB, fakerEN_GH, + fakerEN_HK, fakerEN_IE, fakerEN_IN, fakerEN_NG, @@ -149,6 +151,7 @@ export const allFakers = { en_CA: fakerEN_CA, en_GB: fakerEN_GB, en_GH: fakerEN_GH, + en_HK: fakerEN_HK, en_IE: fakerEN_IE, en_IN: fakerEN_IN, en_NG: fakerEN_NG, diff --git a/src/locales/en_HK/company/index.ts b/src/locales/en_HK/company/index.ts new file mode 100644 index 00000000000..a7c20c6c039 --- /dev/null +++ b/src/locales/en_HK/company/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { CompanyDefinitions } from '../../..'; +import suffix from './suffix'; + +const company: CompanyDefinitions = { + suffix, +}; + +export default company; diff --git a/src/locales/en_HK/company/suffix.ts b/src/locales/en_HK/company/suffix.ts new file mode 100644 index 00000000000..88602e407a6 --- /dev/null +++ b/src/locales/en_HK/company/suffix.ts @@ -0,0 +1 @@ +export default ['Ltd.', 'Co. Ltd.']; diff --git a/src/locales/en_HK/index.ts b/src/locales/en_HK/index.ts new file mode 100644 index 00000000000..cbbfed81797 --- /dev/null +++ b/src/locales/en_HK/index.ts @@ -0,0 +1,22 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { LocaleDefinition } from '../..'; +import company from './company'; +import internet from './internet'; +import location from './location'; +import metadata from './metadata'; +import person from './person'; +import phone_number from './phone_number'; + +const en_HK: LocaleDefinition = { + company, + internet, + location, + metadata, + person, + phone_number, +}; + +export default en_HK; diff --git a/src/locales/en_HK/internet/domain_suffix.ts b/src/locales/en_HK/internet/domain_suffix.ts new file mode 100644 index 00000000000..54279038014 --- /dev/null +++ b/src/locales/en_HK/internet/domain_suffix.ts @@ -0,0 +1 @@ +export default ['com', 'hk', 'com.hk', 'org.hk']; diff --git a/src/locales/en_HK/internet/index.ts b/src/locales/en_HK/internet/index.ts new file mode 100644 index 00000000000..5726872bcb7 --- /dev/null +++ b/src/locales/en_HK/internet/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { InternetDefinitions } from '../../..'; +import domain_suffix from './domain_suffix'; + +const internet: InternetDefinitions = { + domain_suffix, +}; + +export default internet; diff --git a/src/locales/en_HK/location/building_number.ts b/src/locales/en_HK/location/building_number.ts new file mode 100644 index 00000000000..20cefab6b18 --- /dev/null +++ b/src/locales/en_HK/location/building_number.ts @@ -0,0 +1 @@ +export default ['###', '##', '#']; diff --git a/src/locales/en_HK/location/city.ts b/src/locales/en_HK/location/city.ts new file mode 100644 index 00000000000..da0fa5f320f --- /dev/null +++ b/src/locales/en_HK/location/city.ts @@ -0,0 +1,106 @@ +export default [ + 'Aberdeen', + 'Ap Lei Chau', + 'Causeway Bay', + 'Chai Wan', + 'Cheung Chau', + 'Cheung Fat', + 'Cheung Sha Wan', + 'Choi Hung Chuen', + 'Choi Ming', + 'Chuk Yuen', + 'Cyberport', + 'Discovery Bay', + 'Fairview Park', + 'Fanling', + 'Fo Tan', + 'Fu Shan', + 'Fu Shin', + 'Fu Tai', + 'Happy Valley', + 'Heng Fa Chuen', + 'Heng On', + 'Hin Keng', + 'Ho Man Tin', + 'Hung Hom Bay', + 'Kam Tai', + 'Kam Tin', + 'Kennedy Town', + 'Kowloon', + 'Kowloon Bay', + 'Kowloon Central', + 'Kowloon City', + 'Kowloon East', + 'Kwai Chung', + 'Kwai Fong', + 'Kwai Shing', + 'Kwong Yuen', + 'Kwun Tong', + 'Lai King', + 'Lai Kok', + 'Lam Tin', + 'Lamma', + 'Lee On', + 'Lei Muk Shue', + 'Lei Tung', + 'Leung King', + 'Lok Fu', + 'Ma On Shan', + 'Mei Foo Sun Chuen', + 'Mei Lam', + 'Mong Kok', + 'Mui Wo', + 'Ngau Chi Wan', + 'Ngau Tau Kok', + 'Oi Man', + 'Peak', + 'Peng Chau', + 'Po Lam', + 'Pok Fu Lam', + 'Repulse Bay', + 'Sai Kung', + 'Sai Ying Pun', + 'San Tin', + 'Sau Mau Ping', + 'Sha Kok', + 'Sha Tau Kok', + 'Sha Tin', + 'Sham Shui Po', + 'Shau Kei Wan', + 'Shek Kip Mei', + 'Shek Lei', + 'Shek Wai Kok', + 'Shek Wu Hui', + 'Sheung Tak', + 'Sheung Wan', + 'Shun Lee', + 'Siu Sai Wan', + 'So Uk', + 'Stanley', + 'Sun Chui', + 'Tai Hing', + 'Tai Kok Tsui', + 'Tai Koo Shing', + 'Tai O', + 'Tai Po', + 'Tin Yiu', + 'Tin Yuet', + 'To Kwa Wan', + 'Tsat Tsz Mui', + 'Tseung Kwan O', + 'Tsim Sha Tsui', + 'Tsing Yi', + 'Tsuen Wan', + 'Tsz Wan Shan', + 'Tuen Mun', + 'Tung Chung', + 'Wah Fu', + 'Wah Ming', + 'Wan Chai', + 'Wan Tau Tong', + 'Wo Che', + 'Wong Tai Sin', + 'Yau Tong', + 'Yau Yat Tsuen', + 'Yuen Long', +]; diff --git a/src/locales/en_HK/location/default_country.ts b/src/locales/en_HK/location/default_country.ts new file mode 100644 index 00000000000..1689f8f4302 --- /dev/null +++ b/src/locales/en_HK/location/default_country.ts @@ -0,0 +1 @@ +export default ['Hong Kong']; diff --git a/src/locales/en_HK/location/index.ts b/src/locales/en_HK/location/index.ts new file mode 100644 index 00000000000..b7eb3b3157e --- /dev/null +++ b/src/locales/en_HK/location/index.ts @@ -0,0 +1,34 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { LocationDefinitions } from '../../..'; +import building_number from './building_number'; +import city from './city'; +import default_country from './default_country'; +import postcode from './postcode'; +import postcode_by_state from './postcode_by_state'; +import state from './state'; +import state_abbr from './state_abbr'; +import street_cantonese_part from './street_cantonese_part'; +import street_english_part from './street_english_part'; +import street_pattern from './street_pattern'; +import street_prefix from './street_prefix'; +import street_suffix from './street_suffix'; + +const location: LocationDefinitions = { + building_number, + city, + default_country, + postcode, + postcode_by_state, + state, + state_abbr, + street_cantonese_part, + street_english_part, + street_pattern, + street_prefix, + street_suffix, +}; + +export default location; diff --git a/src/locales/en_HK/location/postcode.ts b/src/locales/en_HK/location/postcode.ts new file mode 100644 index 00000000000..5384027f605 --- /dev/null +++ b/src/locales/en_HK/location/postcode.ts @@ -0,0 +1,2 @@ +// https://www.hongkongpost.hk/en/about_us/tips/postcode/index.html +export default null; diff --git a/src/locales/en_HK/location/postcode_by_state.ts b/src/locales/en_HK/location/postcode_by_state.ts new file mode 100644 index 00000000000..5384027f605 --- /dev/null +++ b/src/locales/en_HK/location/postcode_by_state.ts @@ -0,0 +1,2 @@ +// https://www.hongkongpost.hk/en/about_us/tips/postcode/index.html +export default null; diff --git a/src/locales/en_HK/location/state.ts b/src/locales/en_HK/location/state.ts new file mode 100644 index 00000000000..c31f6b013f2 --- /dev/null +++ b/src/locales/en_HK/location/state.ts @@ -0,0 +1 @@ +export default ['Hong Kong Island', 'Kowloon', 'New Territories']; diff --git a/src/locales/en_HK/location/state_abbr.ts b/src/locales/en_HK/location/state_abbr.ts new file mode 100644 index 00000000000..53239368802 --- /dev/null +++ b/src/locales/en_HK/location/state_abbr.ts @@ -0,0 +1 @@ +export default ['HK', 'KLN', 'NT']; diff --git a/src/locales/en_HK/location/street_cantonese_part.ts b/src/locales/en_HK/location/street_cantonese_part.ts new file mode 100644 index 00000000000..cbd67960c8a --- /dev/null +++ b/src/locales/en_HK/location/street_cantonese_part.ts @@ -0,0 +1,80 @@ +export default [ + 'Wan', + 'On', + 'Tai', + 'Man', + 'Fung', + 'Cheung', + 'Tung', + 'Hing', + 'Po', + 'Wah', + 'Tak', + 'Shing', + 'Lung', + 'Yuen', + 'Wing', + 'Hong', + 'Yip', + 'King', + 'Kwong', + 'Hoi', + 'Ming', + 'Wa', + 'Lok', + 'Yan', + 'Wai', + 'Chi', + 'Fuk', + 'Lai', + 'Lee', + 'Fu', + 'Tin', + 'Kai', + 'Sai', + 'Shun', + 'Ping', + 'Yee', + 'Wo', + 'Chung', + 'Hang', + 'Ning', + 'Wong', + 'Yue', + 'Choi', + 'Wang', + 'Ching', + 'Sau', + 'Shan', + 'Tsui', + 'Tau', + 'Sheung', + 'Lam', + 'Fat', + 'Hung', + 'Chuk', + 'Shek', + 'Kok', + 'Cheong', + 'Fong', + 'Nam', + 'Lei', + 'Yu', + 'Mei', + 'Pak', + 'Fai', + 'Kwai', + 'Sing', + 'Kung', + 'Chau', + 'Tong', + 'San', + 'Chiu', + 'Chun', + 'Yin', + 'Yuk', + 'Ting', + 'Kam', + 'Lun', + 'Oi', +]; diff --git a/src/locales/en_HK/location/street_english_part.ts b/src/locales/en_HK/location/street_english_part.ts new file mode 100644 index 00000000000..d9e8ebf576a --- /dev/null +++ b/src/locales/en_HK/location/street_english_part.ts @@ -0,0 +1,56 @@ +export default [ + 'Aldrich', + 'Arran', + 'Austin', + 'Baker', + 'Battery', + 'Bel-Air', + 'Bonham', + 'Boundary', + 'Bowen', + 'Breezy', + 'Caine', + 'Cameron', + 'Canal', + 'Cape', + 'Chatham', + 'Church', + 'College', + 'Comet', + 'Connaught', + 'Cornwall', + "Cox's", + 'Cross', + 'Douglas', + 'Dragon', + 'Eastern', + 'Electric', + 'Expo', + 'Findlay', + 'First', + 'Garden', + 'Gillies', + 'Greig', + 'Hospital', + "Jardine's", + 'Jordan', + 'Kennedy', + 'Kimberley', + 'Leighton', + 'Maidstone', + 'Maple', + 'Marsh', + 'Monmouth', + 'Oaklands', + 'Peel', + 'Poplar', + 'Rose', + 'Second', + 'Seymour', + 'Stewart', + 'Third', + 'Village', + 'Water', + 'Waterloo', + 'Wylie', +]; diff --git a/src/locales/en_HK/location/street_pattern.ts b/src/locales/en_HK/location/street_pattern.ts new file mode 100644 index 00000000000..7482e4a23ab --- /dev/null +++ b/src/locales/en_HK/location/street_pattern.ts @@ -0,0 +1,8 @@ +// Hong Kong has a mix of street names +// Some are English, for example "Argyle Street" +// Some are Cantonese, usually with two syllables and an English suffix, e.g. "Choi Wan Road" +// Real life examples: https://geographic.org/streetview/hong_kong/index.html +export default [ + '{{location.street_english_part}} {{location.street_suffix}}', + '{{location.street_cantonese_part}} {{location.street_cantonese_part}} {{location.street_suffix}}', +]; diff --git a/src/locales/en_HK/location/street_prefix.ts b/src/locales/en_HK/location/street_prefix.ts new file mode 100644 index 00000000000..7646bbd17d0 --- /dev/null +++ b/src/locales/en_HK/location/street_prefix.ts @@ -0,0 +1 @@ +export default null; diff --git a/src/locales/en_HK/location/street_suffix.ts b/src/locales/en_HK/location/street_suffix.ts new file mode 100644 index 00000000000..f33f2d1c1eb --- /dev/null +++ b/src/locales/en_HK/location/street_suffix.ts @@ -0,0 +1,11 @@ +export default [ + 'Street', + 'Road', + 'Lane', + 'Path', + 'Terrace', + 'Avenue', + 'Drive', + 'Crescent', + 'Court', +]; diff --git a/src/locales/en_HK/metadata.ts b/src/locales/en_HK/metadata.ts new file mode 100644 index 00000000000..f61fd2a3cb5 --- /dev/null +++ b/src/locales/en_HK/metadata.ts @@ -0,0 +1,13 @@ +import type { PreBuiltMetadataDefinitionsForCountry } from '../../definitions/metadata'; + +const metadata: PreBuiltMetadataDefinitionsForCountry = { + title: 'English (Hong Kong)', + code: 'en_HK', + country: 'HK', + language: 'en', + endonym: 'English (Hong Kong)', + dir: 'ltr', + script: 'Latn', +}; + +export default metadata; diff --git a/src/locales/en_HK/person/index.ts b/src/locales/en_HK/person/index.ts new file mode 100644 index 00000000000..fe538ef9573 --- /dev/null +++ b/src/locales/en_HK/person/index.ts @@ -0,0 +1,16 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { PersonDefinitions } from '../../..'; +import last_name from './last_name'; +import last_name_patterns from './last_name_patterns'; +import name_ from './name'; + +const person: PersonDefinitions = { + last_name, + last_name_patterns, + name: name_, +}; + +export default person; diff --git a/src/locales/en_HK/person/last_name.ts b/src/locales/en_HK/person/last_name.ts new file mode 100644 index 00000000000..7202f3822d5 --- /dev/null +++ b/src/locales/en_HK/person/last_name.ts @@ -0,0 +1,99 @@ +export default [ + 'Au', + 'Chan', + 'Chang', + 'Chen', + 'Cheng', + 'Cheuk', + 'Cheung', + 'Chiu', + 'Cho', + 'Choi', + 'Chong', + 'Chow', + 'Choy', + 'Chu', + 'Chui', + 'Chung', + 'Fan', + 'Fok', + 'Fu', + 'Fung', + 'He', + 'Ho', + 'Hong', + 'Hu', + 'Huang', + 'Hui', + 'Ip', + 'Kan', + 'Keung', + 'Ko', + 'Kong', + 'Kwan', + 'Kwok', + 'Kwong', + 'Lai', + 'Lam', + 'Lau', + 'Law', + 'Lee', + 'Leung', + 'Li', + 'Liang', + 'Lin', + 'Ling', + 'Liu', + 'Lu', + 'Lui', + 'Luk', + 'Lung', + 'Ma', + 'Mak', + 'Man', + 'Mok', + 'Ng', + 'Ngai', + 'Pang', + 'Poon', + 'Pun', + 'Shiu', + 'Shum', + 'Sin', + 'Siu', + 'So', + 'Suen', + 'Sun', + 'Sze', + 'Szeto', + 'Tai', + 'Tam', + 'Tan', + 'Tang', + 'Tong', + 'Tsang', + 'Tse', + 'Tsoi', + 'Tsui', + 'Wan', + 'Wang', + 'Wong', + 'Wu', + 'Xu', + 'Yan', + 'Yang', + 'Yeung', + 'Yim', + 'Yin', + 'Yip', + 'Yiu', + 'Yu', + 'Yue', + 'Yuen', + 'Yung', + 'Zhang', + 'Zhao', + 'Zheng', + 'Zhou', + 'Zhu', +]; diff --git a/src/locales/en_HK/person/last_name_patterns.ts b/src/locales/en_HK/person/last_name_patterns.ts new file mode 100644 index 00000000000..c66a770f4e3 --- /dev/null +++ b/src/locales/en_HK/person/last_name_patterns.ts @@ -0,0 +1 @@ +export default [{ value: '{{person.last_name}}', weight: 1 }]; diff --git a/src/locales/en_HK/person/name.ts b/src/locales/en_HK/person/name.ts new file mode 100644 index 00000000000..3ed8a80c86e --- /dev/null +++ b/src/locales/en_HK/person/name.ts @@ -0,0 +1,3 @@ +export default [ + { value: '{{person.firstName}} {{person.lastName}}', weight: 1 }, +]; diff --git a/src/locales/en_HK/phone_number/formats.ts b/src/locales/en_HK/phone_number/formats.ts new file mode 100644 index 00000000000..3707487d739 --- /dev/null +++ b/src/locales/en_HK/phone_number/formats.ts @@ -0,0 +1,9 @@ +export default [ + '2### ####', + '3### ####', + '4### ####', + '5### ####', + '6### ####', + '7### ####', + '9### ####', +]; diff --git a/src/locales/en_HK/phone_number/index.ts b/src/locales/en_HK/phone_number/index.ts new file mode 100644 index 00000000000..bf48a8b5d05 --- /dev/null +++ b/src/locales/en_HK/phone_number/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { PhoneNumberDefinitions } from '../../..'; +import formats from './formats'; + +const phone_number: PhoneNumberDefinitions = { + formats, +}; + +export default phone_number; diff --git a/src/locales/index.ts b/src/locales/index.ts index b0f87395451..22054501f02 100644 --- a/src/locales/index.ts +++ b/src/locales/index.ts @@ -20,6 +20,7 @@ export { default as en_BORK } from './en_BORK'; export { default as en_CA } from './en_CA'; export { default as en_GB } from './en_GB'; export { default as en_GH } from './en_GH'; +export { default as en_HK } from './en_HK'; export { default as en_IE } from './en_IE'; export { default as en_IN } from './en_IN'; export { default as en_NG } from './en_NG'; diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index e776784c8fd..0c5fddc8d10 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -166,6 +166,7 @@ export class DatatypeModule { * When not provided or larger than `8640000000000000`, `2100-01-01` is considered * as maximum generated date. Defaults to `4102444800000`. * + * @see faker.date.anytime() * @see faker.date.between() * * @example @@ -175,7 +176,7 @@ export class DatatypeModule { * * @since 5.5.0 * - * @deprecated Use `faker.date.between({ from: min, to: max })` instead. + * @deprecated Use `faker.date.between({ from: min, to: max })` or `faker.date.anytime()` instead. */ datetime( options: @@ -201,7 +202,7 @@ export class DatatypeModule { ): Date { deprecated({ deprecated: 'faker.datatype.datetime({ min, max })', - proposed: 'faker.date.between({ from, to })', + proposed: 'faker.date.between({ from, to }) or faker.date.anytime()', since: '8.0', until: '9.0', }); diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index ad8be056287..feac3692215 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -51,6 +51,41 @@ export class DateModule { } } + /** + * Generates a random date that can be either in the past or in the future. + * + * @param options The optional options object. + * @param options.refDate The date to use as reference point for the newly generated date. Defaults to `faker.defaultRefDate()`. + * + * @see faker.date.between() For dates in a specific range. + * @see faker.date.past() For dates explicitly in the past. + * @see faker.date.future() For dates explicitly in the future. + * + * @example + * faker.date.anytime() // '2022-07-31T01:33:29.567Z' + * + * @since 8.0.0 + */ + anytime( + options: { + /** + * The date to use as reference point for the newly generated date. + * + * @default faker.defaultRefDate() + */ + refDate?: string | Date | number; + } = {} + ): Date { + const { refDate } = options; + + const date = toDate(refDate, this.faker.defaultRefDate); + + return this.between({ + from: new Date(date.getTime() - 1000 * 60 * 60 * 24 * 365), + to: new Date(date.getTime() + 1000 * 60 * 60 * 24 * 365), + }); + } + /** * Generates a random date in the past. * diff --git a/test/__snapshots__/date.spec.ts.snap b/test/__snapshots__/date.spec.ts.snap index a42c8786b40..701c8cc7430 100644 --- a/test/__snapshots__/date.spec.ts.snap +++ b/test/__snapshots__/date.spec.ts.snap @@ -1,5 +1,11 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`date > 42 > anytime > with only Date refDate 1`] = `2020-11-22T03:05:49.801Z`; + +exports[`date > 42 > anytime > with only number refDate 1`] = `2020-11-22T03:05:49.801Z`; + +exports[`date > 42 > anytime > with only string refDate 1`] = `2020-11-22T03:05:49.801Z`; + exports[`date > 42 > between > with Date dates 1`] = `2021-03-15T19:30:57.091Z`; exports[`date > 42 > between > with mixed dates 1`] = `2021-03-15T19:30:57.091Z`; @@ -121,6 +127,12 @@ exports[`date > 42 > weekday > with abbreviated = true and context = true 1`] = exports[`date > 42 > weekday > with context = true 1`] = `"Tuesday"`; +exports[`date > 1211 > anytime > with only Date refDate 1`] = `2021-12-31T12:49:38.848Z`; + +exports[`date > 1211 > anytime > with only number refDate 1`] = `2021-12-31T12:49:38.848Z`; + +exports[`date > 1211 > anytime > with only string refDate 1`] = `2021-12-31T12:49:38.848Z`; + exports[`date > 1211 > between > with Date dates 1`] = `2021-04-17T11:58:13.327Z`; exports[`date > 1211 > between > with mixed dates 1`] = `2021-04-17T11:58:13.327Z`; @@ -243,6 +255,12 @@ exports[`date > 1211 > weekday > with abbreviated = true and context = true 1`] exports[`date > 1211 > weekday > with context = true 1`] = `"Saturday"`; +exports[`date > 1337 > anytime > with only Date refDate 1`] = `2020-08-31T23:49:36.088Z`; + +exports[`date > 1337 > anytime > with only number refDate 1`] = `2020-08-31T23:49:36.088Z`; + +exports[`date > 1337 > anytime > with only string refDate 1`] = `2020-08-31T23:49:36.088Z`; + exports[`date > 1337 > between > with Date dates 1`] = `2021-03-09T04:11:24.667Z`; exports[`date > 1337 > between > with mixed dates 1`] = `2021-03-09T04:11:24.667Z`; diff --git a/test/all_functional.spec.ts b/test/all_functional.spec.ts index 25619c9e774..941ac63b558 100644 --- a/test/all_functional.spec.ts +++ b/test/all_functional.spec.ts @@ -42,6 +42,7 @@ const BROKEN_LOCALE_METHODS = { 'en_CA', 'en_GB', 'en_GH', + 'en_HK', 'en_IE', 'en_IN', 'en_NG', @@ -72,6 +73,8 @@ const BROKEN_LOCALE_METHODS = { 'zh_TW', 'zu_ZA', ], + zipCode: ['en_HK'], + zipCodeByState: ['en_HK'], }, random: { locale: '*', // locale() has been pseudo removed diff --git a/test/date.spec.ts b/test/date.spec.ts index bfa01e7d2a4..d456b53ca24 100644 --- a/test/date.spec.ts +++ b/test/date.spec.ts @@ -13,6 +13,14 @@ const refDate = '2021-02-21T17:09:15.711Z'; describe('date', () => { seededTests(faker, 'date', (t) => { + t.describe('anytime', (t) => { + t.it('with only string refDate', { refDate }) + .it('with only Date refDate', { refDate: new Date(refDate) }) + .it('with only number refDate', { + refDate: new Date(refDate).getTime(), + }); + }); + t.describeEach( 'past', 'future' @@ -188,12 +196,21 @@ describe('date', () => { }); // No changes to these methods - t.skip('birthdate').skip('month').skip('weekday'); + t.skip('anytime').skip('birthdate').skip('month').skip('weekday'); }); }); describe(`random seeded tests for seed ${faker.seed()}`, () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { + describe('anytime()', () => { + it('should return a date', () => { + const actual = faker.date.anytime(); + + expect(actual).toBeDefined(); + expect(actual).toBeInstanceOf(Date); + }); + }); + describe('past()', () => { it('should return a date 5 years in the past', () => { const today = new Date(); diff --git a/test/locale-imports.spec.ts b/test/locale-imports.spec.ts index 2a691b269d4..5d12ded5525 100644 --- a/test/locale-imports.spec.ts +++ b/test/locale-imports.spec.ts @@ -77,7 +77,6 @@ describe('locale imports', () => { }); it(`should be possible to directly import('../src/locale/${locale}')`, async () => { - // eslint-disable-next-line @typescript-eslint/no-var-requires const { faker } = await import(`../src/locale/${locale}`); expect(faker).toBeDefined(); diff --git a/test/locale-proxy.spec.ts b/test/locale-proxy.spec.ts index 88ce9555645..fcb97e349ab 100644 --- a/test/locale-proxy.spec.ts +++ b/test/locale-proxy.spec.ts @@ -7,6 +7,16 @@ describe('LocaleProxy', () => { const locale = createLocaleProxy(en); const enAirline = en.airline ?? { never: 'missing' }; + describe('locale', () => { + it('should be possible to use equals on locale', () => { + expect(locale).toEqual(createLocaleProxy(en)); + }); + + it('should be possible to use not equals on locale', () => { + expect(locale).not.toEqual(createLocaleProxy({})); + }); + }); + describe('category', () => { it('should be possible to check for a missing category', () => { expect('category' in locale).toBe(true);