Created
December 8, 2016 12:55
-
-
Save nickovchinnikov/dd6a33929fbc0f182895de2a7726a919 to your computer and use it in GitHub Desktop.
CUSIP validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cusipValidate(cusip) { | |
cusip = cusip.toUpperCase(); | |
if (!/^[0-9A-Z@#*]{9}$/.test(cusip)) { | |
return false; | |
} | |
var sum = 0, | |
cusipChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ*@#".split(''); | |
var cusipLength = cusip.length - 1; | |
for (var i = 0; i < cusipLength; i++) { | |
var item = cusip[i]; | |
var code = item.charCodeAt(0), | |
num, | |
isChar = (code >= 'A'.charCodeAt(0) && code <= 'Z'.charCodeAt(0)); | |
if (isChar) { | |
num = (cusipChars.indexOf(item) + 10); | |
} else { | |
num = Number(item); | |
} | |
if ((i % 2) != 0) { | |
num *= 2; | |
} | |
num = (num % 10) + Math.floor((num / 10)); | |
sum += num; | |
} | |
return ((10 - (sum % 10)) % 10) === Number(cusip[cusip.length - 1]); | |
} | |
var cusipObjCorrect = { | |
"Apple Inc.": "037833100", | |
"Cisco Systems": "17275R102", | |
"Google": "38259P508", | |
"Microsoft": "594918104", | |
"Oracle": "68389X105" | |
}; | |
Object.keys(cusipObjCorrect).map( | |
function (key) { | |
console.log("Cusip validate " + key + ": " + cusipValidate(cusipObjCorrect[key])); | |
} | |
); | |
var cusipArrayNonCorrect = [ | |
"037831200", | |
"172712102", | |
"3825PD508", | |
"5949XD104", | |
"68383X105" | |
]; | |
cusipArrayNonCorrect.map( | |
function (item) { | |
console.log("Cusip validate not correct: " + cusipValidate(item)); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This little function works for stock and bond cusuip validation, but not for Put/Call options. All put/call validation fails.
For instance, ACADEMY SPORTS & OUTDOORS (cusip = 00402L107) option's put cusip = 00402L957, call option = 00402L907, this function returns false.