dom/phonenumberutils/PhoneNumberNormalizer.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/phonenumberutils/PhoneNumberNormalizer.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,55 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
     1.6 +
     1.7 +// Don't modify this code. Please use:
     1.8 +// https://github.com/andreasgal/PhoneNumber.js
     1.9 +
    1.10 +"use strict";
    1.11 +
    1.12 +this.EXPORTED_SYMBOLS = ["PhoneNumberNormalizer"];
    1.13 +
    1.14 +this.PhoneNumberNormalizer = (function() {
    1.15 +  const UNICODE_DIGITS = /[\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9]/g;
    1.16 +  const VALID_ALPHA_PATTERN = /[a-zA-Z]/g;
    1.17 +  const LEADING_PLUS_CHARS_PATTERN = /^[+\uFF0B]+/g;
    1.18 +  const NON_DIALABLE_CHARS = /[^,#+\*\d]/g;
    1.19 +
    1.20 +  // Map letters to numbers according to the ITU E.161 standard
    1.21 +  var E161 = {
    1.22 +    'a': 2, 'b': 2, 'c': 2,
    1.23 +    'd': 3, 'e': 3, 'f': 3,
    1.24 +    'g': 4, 'h': 4, 'i': 4,
    1.25 +    'j': 5, 'k': 5, 'l': 5,
    1.26 +    'm': 6, 'n': 6, 'o': 6,
    1.27 +    'p': 7, 'q': 7, 'r': 7, 's': 7,
    1.28 +    't': 8, 'u': 8, 'v': 8,
    1.29 +    'w': 9, 'x': 9, 'y': 9, 'z': 9
    1.30 +  };
    1.31 +
    1.32 +  // Normalize a number by converting unicode numbers and symbols to their
    1.33 +  // ASCII equivalents and removing all non-dialable characters.
    1.34 +  function NormalizeNumber(number, numbersOnly) {
    1.35 +    if (typeof number !== 'string') {
    1.36 +      return '';
    1.37 +    }
    1.38 +
    1.39 +    number = number.replace(UNICODE_DIGITS,
    1.40 +                            function (ch) {
    1.41 +                              return String.fromCharCode(48 + (ch.charCodeAt(0) & 0xf));
    1.42 +                            });
    1.43 +    if (!numbersOnly) {
    1.44 +      number = number.replace(VALID_ALPHA_PATTERN,
    1.45 +                              function (ch) {
    1.46 +                                return String(E161[ch.toLowerCase()] || 0);
    1.47 +                              });
    1.48 +    }
    1.49 +    number = number.replace(LEADING_PLUS_CHARS_PATTERN, "+");
    1.50 +    number = number.replace(NON_DIALABLE_CHARS, "");
    1.51 +    return number;
    1.52 +  };
    1.53 +
    1.54 +
    1.55 +  return {
    1.56 +    Normalize: NormalizeNumber
    1.57 +  };
    1.58 +})();
    1.59 \ No newline at end of file

mercurial