isISIN.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isISIN;
  6. var _assertString = _interopRequireDefault(require("./util/assertString"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var isin = /^[A-Z]{2}[0-9A-Z]{9}[0-9]$/;
  9. // this link details how the check digit is calculated:
  10. // https://www.isin.org/isin-format/. it is a little bit
  11. // odd in that it works with digits, not numbers. in order
  12. // to make only one pass through the ISIN characters, the
  13. // each alpha character is handled as 2 characters within
  14. // the loop.
  15. function isISIN(str) {
  16. (0, _assertString.default)(str);
  17. if (!isin.test(str)) {
  18. return false;
  19. }
  20. var double = true;
  21. var sum = 0;
  22. // convert values
  23. for (var i = str.length - 2; i >= 0; i--) {
  24. if (str[i] >= 'A' && str[i] <= 'Z') {
  25. var value = str[i].charCodeAt(0) - 55;
  26. var lo = value % 10;
  27. var hi = Math.trunc(value / 10);
  28. // letters have two digits, so handle the low order
  29. // and high order digits separately.
  30. for (var _i = 0, _arr = [lo, hi]; _i < _arr.length; _i++) {
  31. var digit = _arr[_i];
  32. if (double) {
  33. if (digit >= 5) {
  34. sum += 1 + (digit - 5) * 2;
  35. } else {
  36. sum += digit * 2;
  37. }
  38. } else {
  39. sum += digit;
  40. }
  41. double = !double;
  42. }
  43. } else {
  44. var _digit = str[i].charCodeAt(0) - '0'.charCodeAt(0);
  45. if (double) {
  46. if (_digit >= 5) {
  47. sum += 1 + (_digit - 5) * 2;
  48. } else {
  49. sum += _digit * 2;
  50. }
  51. } else {
  52. sum += _digit;
  53. }
  54. double = !double;
  55. }
  56. }
  57. var check = Math.trunc((sum + 9) / 10) * 10 - sum;
  58. return +str[str.length - 1] === check;
  59. }
  60. module.exports = exports.default;
  61. module.exports.default = exports.default;