faker icon indicating copy to clipboard operation
faker copied to clipboard

feat(phone)!: add new style parameter

Open matthewmayer opened this issue 1 year ago • 1 comments

fix #1542

WIP

Script for doing the grunt work of renaming the formats.ts and generating the initial raw and national files

const {
    allLocales
} = require("@faker-js/faker")
const lpn = require("libphonenumber-js")
const fs = require("fs")
const fsx = require("fs-extra")
//guess the country for locales with no country attached
const fallbacks = {
    "az": "AZ",
    "da": "DK",
    "de": "DE",
    "dv": "MV",
    "el": "GR",
    "en": "US",
    "es": "ES",
    "fa": "IR",
    "fr": "FR",
    "he": "IL",
    "hr": "HR",
    "hu": "HU",
    "hy": "AM",
    "it": "IT",
    "ja": "JP",
    "ko": "KR",
    "lv": "LV",
    "mk": "MK",
    "ne": "NP",
    "nl": "NL",
    "pl": "PL",
    "ro": "RO",
    "ru": "RU",
    "sk": "SK",
    "sv": "SE",
    "th": "TH",
    "tr": "TR",
    "uk": "UA",
    "vi": "VN"
}

for (let locale of Object.keys(allLocales)) {
    //loop through all locales that have an existing formats.ts
    const filename = "../faker/src/locales/" + locale + "/phone_number/formats.ts"
    if (fs.existsSync(filename)) {
        //extract formats array from TS
        const data = fs.readFileSync(filename, "utf8")
        let arrayString = data.replace('export default ', '').replace(';', '');
        let formats = eval(arrayString)
        //figure out the country code
        let country = null
        if (locale.includes("_")) {
            country = locale.split("_")[1]
        } else if (fallbacks[locale]) {
            country = fallbacks[locale]
        }
        if (country) {
            let countryCode = lpn.getCountryCallingCode(country).toString()
            let nationals = []
            let raws = []
            for (let format of formats) {
                if (!format.includes("x")) { //skip extensions
                    //replace each # in the format string with placeholder
                    //we need to make sure its not one of the ones in the format or country code
                    //so we can losslessly restore it later
                    //so start at 2, and increment until we find a free one
                    let hash = 2
                    while (format.includes(hash) || countryCode.includes(hash)) {
                        hash++
                    }
                    //do the same for !
                    let pling = hash + 1
                    while (format.includes(pling) || countryCode.includes(pling)) {
                        pling++
                    }
                    //replace the # and ! with the placeholders
                    let phone = format.replaceAll('#', hash).replaceAll('!', pling)
                    //later we'll need to restore them
                    const restorePlaceholders = (string) => {
                        return string.replaceAll(hash, "#").replaceAll(pling, "!")
                    }
                    try {
                        //parse the phone number
                        const phoneNumber = lpn.parsePhoneNumber(phone, country)
                        //format national
                        let national = restorePlaceholders(phoneNumber.formatNational())
                        nationals.push(national)
                        //format raw
                        const raw = restorePlaceholders(phoneNumber.getURI().replace("tel:", ""))
                        raws.push(raw)
                    } catch (e) {
                        //
                    }
                }
                //move the file from formats.ts to human.ts
                try {
                    fsx.moveSync(filename, filename.replace("formats", "human"))
                } catch (e) {

                }
                //write out the new files, avoiding dupes
                fs.writeFileSync(filename.replace("formats", "national"), "export default " + JSON.stringify([...new Set(nationals)]) + ";")
                fs.writeFileSync(filename.replace("formats", "raw"), "export default " + JSON.stringify([...new Set(raws)]) + ";")

            }
        }
    }
}

matthewmayer avatar Dec 13 '23 17:12 matthewmayer

Codecov Report

Attention: Patch coverage is 99.35897% with 14 lines in your changes are missing coverage. Please review.

Project coverage is 99.57%. Comparing base (aade09b) to head (d6dbf8f).

:exclamation: Current head d6dbf8f differs from pull request most recent head 7b8bcd6. Consider uploading reports for the commit 7b8bcd6 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #2578      +/-   ##
==========================================
- Coverage   99.57%   99.57%   -0.01%     
==========================================
  Files        2859     3034     +175     
  Lines      248602   250972    +2370     
  Branches      985      994       +9     
==========================================
+ Hits       247541   249900    +2359     
- Misses       1061     1072      +11     
Files Coverage Δ
src/locales/af_ZA/phone_number/format/human.ts 100.00% <ø> (ø)
src/locales/af_ZA/phone_number/format/index.ts 100.00% <100.00%> (ø)
...locales/af_ZA/phone_number/format/international.ts 100.00% <100.00%> (ø)
src/locales/af_ZA/phone_number/format/national.ts 100.00% <100.00%> (ø)
src/locales/af_ZA/phone_number/index.ts 100.00% <100.00%> (ø)
src/locales/ar/index.ts 100.00% <ø> (ø)
src/locales/az/phone_number/format/human.ts 100.00% <ø> (ø)
src/locales/az/phone_number/format/index.ts 100.00% <100.00%> (ø)
...rc/locales/az/phone_number/format/international.ts 100.00% <100.00%> (ø)
src/locales/az/phone_number/format/national.ts 100.00% <100.00%> (ø)
... and 287 more

... and 3 files with indirect coverage changes

codecov[bot] avatar Feb 08 '24 20:02 codecov[bot]

We'll need rebase after https://github.com/faker-js/faker/pull/2712 lands

matthewmayer avatar Mar 04 '24 14:03 matthewmayer

The description states it is still WIP. Is that outdated or what is left to do?

ST-DDT avatar Mar 06 '24 18:03 ST-DDT

Nope sorry not WIP any more. Ready for final review.

matthewmayer avatar Mar 07 '24 03:03 matthewmayer

I am sorry but I am lost, using parameters with faker.phone.number() with parameters is deprecated. So why this PR seems to add parameters ? Is the deprecation canceled or something ?

romain-sen avatar Apr 05 '24 09:04 romain-sen

The new parameters are entirely different and incompatible with the previous ones.

ST-DDT avatar Apr 05 '24 09:04 ST-DDT

In which version will it be available ?

romain-sen avatar Apr 05 '24 10:04 romain-sen

In which version will it be available ?

v9.0.0 (+preview releases)

We plan to release an alpha soon. Maybe next week?

ST-DDT avatar Apr 05 '24 10:04 ST-DDT