Permalink
Browse files

Cusip validation & testing

  • Loading branch information...
1 parent 5450bb5 commit 694b012d1e390b8acceb0315ccb370c0ab10f92b @mshafrir committed Jul 20, 2012
Showing with 74 additions and 10 deletions.
  1. +32 −3 lib/cusip.js
  2. +29 −2 src/cusip.coffee
  3. +7 −3 tests/run.js
  4. +6 −2 tests/src/run.coffee
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -5,15 +5,15 @@ _ = require('underscore')
String::trim = -> @replace(/^\s*|\s*$/g, '')
-cusip_sources = [
+CUSIP_SOURCES = [
require('./sources/adrbymellon').AdrByMellonCusipSource,
require('./sources/forbes').ForbesCusipSource,
require('./sources/quantum').QuantumCusipSource,
require('./sources/fidelity').FidelityCusipSource
]
exports.lookup = (cusip_code, callback) ->
- async.parallel _.map(cusip_sources, (cusip_source) ->
+ async.parallel _.map(CUSIP_SOURCES, (cusip_source) ->
(parallel_callback) ->
(new cusip_source(cusip_code, parallel_callback)).fetch()
),
@@ -23,3 +23,30 @@ exports.lookup = (cusip_code, callback) ->
cusip_data = _.extend {code: cusip_code},
if _.isEmpty(compacted) then {} else _.first compacted
callback(if is_found then cusip_data else null)
+
+exports.validate = (cusip_code) ->
+ # CUSIP code must have length of 9
+ return false unless cusip_code? and cusip_code.length is 9
+
+ sum = 0
+ NUMS = _.invoke(_.range(10), 'toString')
+ chars = _.first(cusip_code.toUpperCase().split(''), 8)
+ _.each chars, (c, i) ->
+ # Is a number?
+ if _.include(NUMS, c) then v = parseInt(c, 10)
+
+ # Is a special character?
+ else if c is '*' then v = 36
+ else if c is '@' then v = 37
+ else if c is '#' then v = 38
+
+ # Is a letter?
+ else
+ v = c.charCodeAt(0) - 64
+
+ # Double v on even iterations
+ v = v * 2 if (i % 2) is 1
+
+ sum += Math.floor(v / 10) + (v % 10)
+
+ parseInt(_.last(cusip_code), 10) is (10 - (sum % 10)) % 10
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,3 +1,7 @@
-cusip = require('../lib/cusip')
+Cusip = require('../lib/cusip')
-cusip.lookup('881624209', (cusip_data) -> console.log(cusip_data))
+cusip_code = '079860101'
+
+console.log("Is #{cusip_code} valid?", Cusip.validate(cusip_code))
+
+Cusip.lookup(cusip_code, (cusip_data) -> console.log(cusip_data))

0 comments on commit 694b012

Please sign in to comment.