catamphetamine / libphonenumber-js

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript
https://catamphetamine.gitlab.io/libphonenumber-js/
MIT License
2.81k stars 216 forks source link

A bug in minified release #374

Closed JacksonGL closed 4 years ago

JacksonGL commented 4 years ago

In the minified releases, there is following code snippet (formatted for better illustration) that uses a variable before it was initialized:

var v = n(12), 
    ...,
    E = '[' + O + ']{' + L + '}', // L is used
    ...,
    L = 2, ...  // but initialized here

I traced back to the source:

import {
    MIN_LENGTH_FOR_NSN,
    VALID_DIGITS,
    ...
} from './constants'

... 
const MIN_LENGTH_PHONE_NUMBER_PATTERN = '[' + VALID_DIGITS + ']{' + MIN_LENGTH_FOR_NSN + '}'

The concatenated string is used to construct a regex for matching phone numbers. It was supposed to be /...[0-90-9٠-٩۰-۹]{2}.../; now it becomes /...[0-90-9٠-٩۰-۹]{undefined}.../. Looks like this is a bug in the minifier.

catamphetamine commented 4 years ago

I have replaced Webpack with Rollup. See if the new version libphonenumber-js@1.7.40 works.

JacksonGL commented 4 years ago

Looks like Rollup statically generates the string, which inlined MIN_LENGTH_FOR_NSN. So it works :)