isISIN.js 1.8 KB

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