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.77k stars 217 forks source link

React-Native: Error: undefined is not a function (evaluating \'text[typeof Symbol === \'function\' ? Symbol.iterator : \'@@iterator\']()\') #198

Closed edolix closed 6 years ago

edolix commented 6 years ago

Hi, i'm using this lib in a ReactNative App and methods parse and format catch this error:

Error: undefined is not a function (evaluating \'text[typeof Symbol === \'function\' ? Symbol.iterator : \'@@iterator\']()\') drop_and_substitute_characters
....

How i'm using lib:

import { parse, format } from 'libphonenumber-js'

let p = parse('8 (800) 555 35 35', 'RU')
console.log(p)

Installed from: npm install libphonenumber-js

I'm doing something wrong? Thanks!

catamphetamine commented 6 years ago

Seems that Symbol.iterator is undefined - that's what the error says. It's not supposed to be undefined. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator You can check that by manually editing the js file in node_modules by adding a console.log. Perhaps your ES6 Symbol polyfill is not complete: Symbol seems to be defined but seems that it's not the ES6 Symbol.

catamphetamine commented 6 years ago

You can try using import 'babel-polyfill' or something like that (as the first line of your program).

catamphetamine commented 6 years ago

Or, actually, my previous comment was incorrect: the Symbol polyfill seems fine but text[Symbol.iterator] is undefined for some reason.

edolix commented 6 years ago

@catamphetamine thanks for the quick reply!

Symbol.iterator is equal to '@@iterator', text is a string (the input number) so: '8 (800) 555 35 35'.

edolix commented 6 years ago

Is the package updated on npm?

catamphetamine commented 6 years ago

Yeah, seems that you're not in a full ES6 environment. Are you using Babel?

catamphetamine commented 6 years ago

Is the package updated on npm?

What do you mean?

edolix commented 6 years ago

Yeah, seems that you're not in a full ES6 environment. Are you using Babel?

I'm on ReactNative but it seems that Symbol are not fully supported by RN.

https://github.com/facebook/react-native/issues/15902

catamphetamine commented 6 years ago

Well, I could look into a way of polyfilling symbol iterator somehow. I'll post if I find something.

edolix commented 6 years ago

I found a workaround here https://github.com/facebook/react-native/issues/15902#issuecomment-375521246. It seems to work!

catamphetamine commented 6 years ago

@edolix Yeah, that's one way to do it, and actually it's better in a sense that other libraries relying on Symbol don't break.

The other thing is that I found a way to bypass Symbol.iterator in the code and released a new version libphonenumber-js@1.1.6. If you feel like trying it out (while temporarily reverting your core-js fix) then you could do that. Otherwise your present workaround seems fine.

catamphetamine commented 6 years ago

CC @sachinB94 @m-hosoi ^^^^^^^

edolix commented 6 years ago

If you feel like trying it out ....

SUPER! I'll try it tomorrow for sure 👍

@catamphetamine thanks again for your help!

edolix commented 6 years ago

@catamphetamine v1.1.6 works without any workaround! Thanks!