我創建了一個帶有 awesome-phonenumber npm 包的 Javascript。
- 索引.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 ));
}
}
當我跑步node index.js -n 919912345678或node index.js -n 09912345678作業正常時。
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"
}
當我跑node index.js -n 9912345678。它顯示此錯誤。
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
我創建了一個 test.js 來查看代碼是否正常作業
- 測驗.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 ));
}
然后它正在作業
我不知道我做錯了什么
uj5u.com熱心網友回復:
只需將您的輸入轉換為字串,它就會起作用。
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 ));
}
}
uj5u.com熱心網友回復:
91或者0091是印度的區域代碼。該091或 01有字串,而在你的第二個失敗例子事實并非如此。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/330747.html
標籤:javascript 节点.js 新产品经理
上一篇:@typescript-eslint/eslint-plugin已安裝,但在vscodelint任務中拋出錯誤并要求使用不存在的@typescript-eslint包
下一篇:同一行標題后的分隔線
