libphonenumber-js
Advanced tools
Comparing version 0.2.10 to 0.2.11
@@ -9,3 +9,5 @@ 'use strict'; | ||
// against the regular expression. | ||
function matches_entirely(regular_expression, text) { | ||
function matches_entirely(regular_expression) { | ||
var text = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1]; | ||
if (typeof regular_expression === 'string') { | ||
@@ -12,0 +14,0 @@ regular_expression = '^(?:' + regular_expression + ')$'; |
@@ -15,3 +15,3 @@ 'use strict'; | ||
exports.default = function (input) { | ||
exports.default = function (input, included_countries) { | ||
return _bluebird2.default.promisify(_xml2js.parseString)(input).then(function (xml) { | ||
@@ -37,2 +37,7 @@ // https://github.com/googlei18n/libphonenumber/blob/master/resources/PhoneNumberMetadata.xml | ||
// Skip this country if it has not been explicitly included | ||
if (included_countries && !included_countries.has(country_code)) { | ||
return 'continue'; | ||
} | ||
// Country metadata | ||
@@ -214,4 +219,4 @@ var country = { | ||
// Never happens | ||
if (format.format.indexOf(_asYouType2.default.DIGIT_PLACEHOLDER) >= 0) { | ||
throw new Error('Phone number format "' + format.format + '" contains a reserved "' + _asYouType2.default.DIGIT_PLACEHOLDER + '" symbol for pattern ' + format.pattern + ' for ' + country_code); | ||
if (format.format.indexOf(DIGIT_PLACEHOLDER) >= 0) { | ||
throw new Error('Phone number format "' + format.format + '" contains a reserved "' + DIGIT_PLACEHOLDER + '" symbol for pattern ' + format.pattern + ' for ' + country_code); | ||
} | ||
@@ -262,3 +267,5 @@ } | ||
for (var _iterator = (0, _getIterator3.default)(xml.phoneNumberMetadata.territories[0].territory), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
_loop(); | ||
var _ret = _loop(); | ||
if (_ret === 'continue') continue; | ||
} | ||
@@ -459,21 +466,8 @@ | ||
var _asYouType = require('../as you type'); | ||
var _asYouType2 = _interopRequireDefault(_asYouType); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// Replaces $NP with national prefix and $FG with the first group ($1) | ||
function national_prefix_formatting_rule(rule, national_prefix) { | ||
if (!rule) { | ||
return; | ||
} | ||
// This constant is copied from "as you type.js" | ||
// to prevent recursive `require()` of `metadata.min.json` | ||
var DIGIT_PLACEHOLDER = 'x'; | ||
// Replace $NP with national prefix and $FG with the first group ($1) | ||
return rule.replace('$NP', national_prefix).replace('$FG', '$1'); | ||
} | ||
// Gets phone type pattern | ||
// Excessive fields from "PhoneNumberMetadata.xml" | ||
@@ -541,2 +535,15 @@ // aren't included to reduce code complexity and size: | ||
// | ||
// Replaces $NP with national prefix and $FG with the first group ($1) | ||
function national_prefix_formatting_rule(rule, national_prefix) { | ||
if (!rule) { | ||
return; | ||
} | ||
// Replace $NP with national prefix and $FG with the first group ($1) | ||
return rule.replace('$NP', national_prefix).replace('$FG', '$1'); | ||
} | ||
// Gets phone type pattern | ||
function phone_type_pattern(territory, type) { | ||
@@ -543,0 +550,0 @@ return territory[type] ? territory[type][0].nationalNumberPattern[0].replace(/\s/g, '') : undefined; |
{ | ||
"name": "libphonenumber-js", | ||
"version": "0.2.10", | ||
"version": "0.2.11", | ||
"description": "A simpler (and smaller) rewrite of Google Android's popular libphonenumber library", | ||
@@ -31,6 +31,4 @@ "main": "index.common.js", | ||
"metadata:update": "npm run metadata:download && babel-node runnable/update", | ||
"metadata:generate": "npm run generate && npm run compress", | ||
"metadata:generate": "babel-node runnable/generate ../PhoneNumberMetadata.xml", | ||
"metadata:download": "curl -sS --remote-name https://raw.githubusercontent.com/googlei18n/libphonenumber/master/resources/PhoneNumberMetadata.xml", | ||
"generate": "babel-node runnable/generate ../PhoneNumberMetadata.xml", | ||
"compress": "babel-node runnable/compress", | ||
"test": "mocha --compilers js:babel-core/register --colors --bail --reporter spec test/ --recursive", | ||
@@ -37,0 +35,0 @@ "test-coverage": "istanbul cover -x \"build/**\" node_modules/mocha/bin/_mocha -- --compilers js:babel-core/register --colors --reporter dot test/ --recursive", |
@@ -172,3 +172,3 @@ # libphonenumber-js | ||
* Clone the repo | ||
* `git clone https://github.com/halt-hammerzeit/libphonenumber-js.git` | ||
* `npm install` | ||
@@ -186,2 +186,11 @@ * `npm run browser-build` | ||
## Including only a specific set of countries | ||
The following command will generate metadata only for the specified set of countries (if anyone needs that) | ||
```sh | ||
npm run metadata:generate NL,BE,FR,DE,LU,AT | ||
# The resulting `metadata.min.json` will only contain those countries | ||
``` | ||
## Contributing | ||
@@ -188,0 +197,0 @@ |
import generate from '../source/tools/generate' | ||
import compress from '../source/tools/compress' | ||
import path from 'path' | ||
import fs from 'fs' | ||
import fs from 'fs' | ||
const input = fs.readFileSync(path.join(__dirname, process.argv[2]), 'utf8') | ||
generate(input).then((output) => | ||
let included_countries | ||
if (process.argv[3]) | ||
{ | ||
included_countries = process.argv[3].split(',') | ||
console.log('Included countries:') | ||
console.log(included_countries) | ||
included_countries = new Set(included_countries) | ||
} | ||
// Generate and compress metadata | ||
generate(input, included_countries).then((output) => | ||
{ | ||
// Write uncompressed metadata into a file for easier debugging | ||
fs.writeFileSync(path.join(__dirname, '../metadata.json'), JSON.stringify(output, undefined, 3)) | ||
// Compress the generated metadata | ||
fs.writeFileSync(path.join(__dirname, '../metadata.min.json'), JSON.stringify(compress(output))) | ||
}) |
// Checks whether the entire input sequence can be matched | ||
// against the regular expression. | ||
export function matches_entirely(regular_expression, text) | ||
export function matches_entirely(regular_expression, text = '') | ||
{ | ||
@@ -5,0 +5,0 @@ if (typeof regular_expression === 'string') |
import { parseString } from 'xml2js' | ||
import Promise from 'bluebird' | ||
import as_you_type from '../as you type' | ||
// This constant is copied from "as you type.js" | ||
// to prevent recursive `require()` of `metadata.min.json` | ||
const DIGIT_PLACEHOLDER = 'x' | ||
@@ -68,3 +70,3 @@ // Excessive fields from "PhoneNumberMetadata.xml" | ||
// | ||
export default function(input) | ||
export default function(input, included_countries) | ||
{ | ||
@@ -86,2 +88,8 @@ return Promise.promisify(parseString)(input).then((xml) => | ||
// Skip this country if it has not been explicitly included | ||
if (included_countries && !included_countries.has(country_code)) | ||
{ | ||
continue | ||
} | ||
// Country metadata | ||
@@ -257,5 +265,5 @@ const country = | ||
// Never happens | ||
if (format.format.indexOf(as_you_type.DIGIT_PLACEHOLDER) >= 0) | ||
if (format.format.indexOf(DIGIT_PLACEHOLDER) >= 0) | ||
{ | ||
throw new Error(`Phone number format "${format.format}" contains a reserved "${as_you_type.DIGIT_PLACEHOLDER}" symbol for pattern ${format.pattern} for ${country_code}`) | ||
throw new Error(`Phone number format "${format.format}" contains a reserved "${DIGIT_PLACEHOLDER}" symbol for pattern ${format.pattern} for ${country_code}`) | ||
} | ||
@@ -262,0 +270,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1094646
4551
239
2
37