isIMEI.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isIMEI;
  6. var _assertString = _interopRequireDefault(require("./util/assertString"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var imeiRegexWithoutHypens = /^[0-9]{15}$/;
  9. var imeiRegexWithHypens = /^\d{2}-\d{6}-\d{6}-\d{1}$/;
  10. function isIMEI(str, options) {
  11. (0, _assertString.default)(str);
  12. options = options || {}; // default regex for checking imei is the one without hyphens
  13. var imeiRegex = imeiRegexWithoutHypens;
  14. if (options.allow_hyphens) {
  15. imeiRegex = imeiRegexWithHypens;
  16. }
  17. if (!imeiRegex.test(str)) {
  18. return false;
  19. }
  20. str = str.replace(/-/g, '');
  21. var sum = 0,
  22. mul = 2,
  23. l = 14;
  24. for (var i = 0; i < l; i++) {
  25. var digit = str.substring(l - i - 1, l - i);
  26. var tp = parseInt(digit, 10) * mul;
  27. if (tp >= 10) {
  28. sum += tp % 10 + 1;
  29. } else {
  30. sum += tp;
  31. }
  32. if (mul === 1) {
  33. mul += 1;
  34. } else {
  35. mul -= 1;
  36. }
  37. }
  38. var chk = (10 - sum % 10) % 10;
  39. if (chk !== parseInt(str.substring(14, 15), 10)) {
  40. return false;
  41. }
  42. return true;
  43. }
  44. module.exports = exports.default;
  45. module.exports.default = exports.default;