Start sharing components as a team!Share components as a team!Join Bit to build your applications faster.Get Started Free

is-isin

v10.11.0arrow_drop_down
v10.11.0
STATUS
Passing
DOWNLOADS
4
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
2 years ago
SIZE
868 B
check if the string is an ISIN ISIN
1 contributor
Install is-isin as a package?
Copied
npm i @bit/chriso.validator-js.is-isin
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
isISIN.js
35 Lines(29 sloc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import assertString from './util/assertString';

const isin = /^[A-Z]{2}[0-9A-Z]{9}[0-9]$/;


export default function isISIN(str) {
  assertString(str);
  if (!isin.test(str)) {
    return false;
  }

  const checksumStr = str.replace(/[A-Z]/g, character => (parseInt(character, 36)));

  let sum = 0;
  let digit;
  let tmpNum;
  let shouldDouble = true;
  for (let i = checksumStr.length - 2; i >= 0; i--) {
    digit = checksumStr.substring(i, (i + 1));
    tmpNum = parseInt(digit, 10);
    if (shouldDouble) {
      tmpNum *= 2;
      if (tmpNum >= 10) {
        sum += tmpNum + 1;
      } else {
        sum += tmpNum;
      }
    } else {
      sum += tmpNum;
    }
    shouldDouble = !shouldDouble;
  }

  return parseInt(str.substr(str.length - 1), 10) === (10000 - sum) % 10;
}