I have created a Javascript with awesome-phonenumber npm package.
- index.js
const yargs = require('yargs');
var PhoneNumber = require( 'awesome-phonenumber' );
const argv = yargs
.usage('Usage: $0 -n [num]')
.command('login', '(login)', {
number: {
description: 'number',
type: 'number',
}
})
.option('phoneNumber', {
alias: 'n',
description: 'number',
type: 'charecter',
})
.option('json', {
alias :'json',
description: 'To print output in json',
type: 'boolean',
})
.help()
.alias('help', 'h')
.argv;
if (argv._[0] == 'login') {
console.log ("Login");
process.exit();
}
if (argv.n) {
var pn = PhoneNumber( argv.n, 'IN' );
if (!pn.isValid( )) {
console.log("! Invalid number");
return false;
} else {
console.log(JSON.stringify( pn, null, 4 ));
}
}
when i run node index.js -n 919912345678
or node index.js -n 09912345678
it is working fine.
PS C:\Users\anil kumar jilla\truecaller-nodejs> node index.js -n 919912345678
{
"number": {
"input": " 919912345678",
"international": " 91 99123 45678",
"national": "099123 45678",
"e164": " 919912345678",
"rfc3966": "tel: 91-99123-45678",
"significant": "9912345678"
},
"regionCode": "IN",
"valid": true,
"possible": true,
"canBeInternationallyDialled": true,
"type": "mobile",
"possibility": "is-possible"
}
when i run node index.js -n 9912345678
. it is showing this error.
PS C:\Users\anil kumar jilla\truecaller-nodejs> node "c:\Users\anil kumar jilla\truecaller-nodejs\index.js"
PS C:\Users\anil kumar jilla\truecaller-nodejs> node index.js -n 9912345678
C:\Users\anil kumar jilla\truecaller-nodejs\node_modules\awesome-phonenumber\lib\index.js:528
function V(a,b){if(!(this instanceof V))return new V(a,b);if("string"===typeof a)var c=!1;else try{bb(a),c=!0}catch(e){c=!1}let d;if(!c&&"string"!==typeof a)throw Error("Invalid phone number, expected a string");if(!c&&null!=b&&"string"!==typeof b)throw Error("Invalid region code, expected a string");c||(a=a.trim(),b&&" "===a.charAt(0)&&(b=null),b||({ea:b=null,parsed:d}=Db(a)));this.g={number:{},regionCode:b,valid:!1,possible:!1};if(c)this.h=a;else{this.h=null;this.g.number.input=a;if(!b){this.g.possibility=
^
Error: Invalid phone number, expected a string
at new V (C:\Users\anil kumar jilla\truecaller-nodejs\node_modules\awesome-phonenumber\lib\index.js:528:164)
at V (C:\Users\anil kumar jilla\truecaller-nodejs\node_modules\awesome-phonenumber\lib\index.js:528:48)
at Object.<anonymous> (C:\Users\anil kumar jilla\truecaller-nodejs\index.js:33:14)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47
I created a test.js to see the code is working or not
- test.js
var PhoneNumber = require( 'awesome-phonenumber' );
const number = "9912345678"
var pn = PhoneNumber(number , 'IN' );
if (!pn.isValid()) {
console.log("! Invalid number");
return false;
} else {
console.log(JSON.stringify( pn, null, 4 ));
}
then it is working
i dont know what the mistake i did
CodePudding user response:
91
or 0091
are the region codes for India. The 091
or 01
are strings, whereas in your second failing example it is not.
CodePudding user response:
Just convert your input in string, it will work.
if (argv.s.toString()) {
var pn = PhoneNumber( argv.s.toString(), 'IN' );
if (!pn.isValid( )) {
console.log("!invalid number");
return false;
} else {
console.log(JSON.stringify( pn, null, 4 ));
}
}