Javascript libphonenumber 独立(没有大量的谷歌依赖)?替代库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11528017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
libphonenumber standalone (without masses of google dependencies)? Alternate lib?
提问by S42
I am looking at using http://code.google.com/p/libphonenumber/for a well-established project. Today the project does not use Google's libraries for JavaScript, favoring jQuery, jQueryUI, requirejs, and so on.
我正在考虑将http://code.google.com/p/libphonenumber/用于一个完善的项目。今天,该项目不使用 Google 的 JavaScript 库,而是支持 jQuery、jQueryUI、requirejs 等。
libphonenumber looks awesome ... except that the javascript version (svn co http://libphonenumber.googlecode.com/svn/trunk/javascript/libphonenumber-js) is laced with goog.require calls. If one runs the demo (libphonenumber-js/i18n/phonenumbers/demo.html if you checked out as suggested) it pulls in tons of google libraries from closure-library.googlecode.com :
libphonenumber 看起来很棒……除了 javascript 版本(svn co http://libphonenumber.googlecode.com/svn/trunk/javascript/libphonenumber-js)带有 goog.require 调用。如果运行演示(libphonenumber-js/i18n/phonenumbers/demo.html,如果您按照建议签出),它会从closure-library.googlecode.com 中提取大量谷歌库:
GET base.js
GET deps.js
GET error.js
GET string.js
GET asserts.js
GET array.js
GET useragent.js
GET browserfeature.js
GET tagname.js
GET classes.js
GET math.js
GET coordinate.js
GET size.js
GET object.js
GET dom.js
GET json.js
GET util.js
GET descriptor.js
GET fielddescriptor.js
GET message.js
GET serializer.js
GET objectserializer.js
GET stringbuffer.js
GET lazydeserializer.js
GET pbliteserializer.js
I believe if I compile this using the closure compiler ("If you give the use_closure_library parameter a value of true, the compiler looks for goog.require() statements in the source code and supplies the Closure Library code requested by any such statements.", https://developers.google.com/closure/compiler/docs/api-ref) I can cut down the raw number of requests, but this still seems like a rather excessive amount of content for a phone number parser, even a full-featured one.
我相信如果我使用闭包编译器编译它(“如果你给 use_closure_library 参数一个值为 true,编译器会在源代码中查找 goog.require() 语句并提供任何此类语句请求的闭包库代码。” , https://developers.google.com/closure/compiler/docs/api-ref) 我可以减少请求的原始数量,但这对于电话号码解析器来说似乎仍然是相当多的内容,即使是功能齐全的一款。
My question has two possible answers:
我的问题有两个可能的答案:
- A way to use libphonenumber in JavaScript without having to pull in all the Google JavaScript base libraries
- An alternate standalone (as in doesn't have dozens of dependencies) first-class phone number processing library with both JavaScript and Java implementations
- 一种在 JavaScript 中使用 libphonenumber 的方法,而无需引入所有 Google JavaScript 基础库
- 一个替代的独立(因为没有几十个依赖项)一流的电话号码处理库,具有 JavaScript 和 Java 实现
Any and all suggestions most appreciated.
任何和所有建议最值得赞赏。
回答by Hymanocnr
I've got a custom build(currently 220KB) that I use for my International Telephone Inputplugin, with plenty of helper functions exposed. Read the sourcefor details.
回答by Gil SH
You can also use my lib. https://github.com/Gilshallem/phoneparser
你也可以使用我的库。 https://github.com/Gilshallem/phoneparser
Its only got one method but you can do a lot with it
它只有一种方法,但你可以用它做很多事情
parsePhone("12025550104");
result: { countryCode:1, areaCode:202, number:5550104, countryISOCode:"US" }
回答by Grokify
Here are two implementations of Google libphonenumber in JavaScript that have zero dependencies and are implemented in a single file. I've used Nathan Hammond's version without issue but it is not on NPM. Rui Marinho's version is on NPM.
以下是 JavaScript 中 Google libphonenumber 的两个实现,它们具有零依赖关系并在单个文件中实现。我使用 Nathan Hammond 的版本没有问题,但它不在 NPM 上。Rui Marinho 的版本在 NPM 上。
回答by Chockomonkey
I just spent 2 days figuring this out. For now, anyway, you can download a minified version of libphonenumber-js from here
我只是花了 2 天的时间来解决这个问题。现在,无论如何,您可以从这里下载 libphonenumber-js 的缩小版本
drop it in place, with the usual
将其放置到位,使用通常的
<script type="text/javascript" language="javascript" src="/static/js/libphonenumber-js.min.js"></script>
and get busy coding!
并忙于编码!
<script>
$(".phone-format").keyup(function () {
var val_old = $(this).val();
var newString = new libphonenumber.AsYouType('US').input(val_old);
$(this).focus().val('').val(newString);
});
</script>