-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9ff94a1
Showing
6 changed files
with
2,492 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
npm-debug.log* | ||
node_modules/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# isni-utils | ||
|
||
Validates and generates ISNI (International Standard Name Identifier) codes | ||
|
||
## Usage | ||
|
||
Usage as a Node library: | ||
|
||
```js | ||
const isni = require('isni-utils'); | ||
|
||
isni.validate('0000-0001-2147-8925') // true | ||
isni.validate('0000000121478925') // true | ||
isni.validate('1234-1234-1234-1234') // false | ||
|
||
isni.generate() // Generates a random ISNI code | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module.exports = { | ||
validate(isni) { | ||
const isniString = isni?.toString() || ''; | ||
const stripped = isniString.replace(/\s+/g, '').replace(/-/g, ''); | ||
if (stripped.length !== 16) { | ||
return false; | ||
} | ||
const baseDigits = stripped.substring(0, 15); | ||
const checksum = stripped.substring(15, 16); | ||
const calculatedChecksum = calculateChecksum(baseDigits); | ||
if (checksum !== calculatedChecksum) { | ||
return false; | ||
} | ||
return true; | ||
}, | ||
generate() { | ||
const baseDigits = Math.floor(Math.random() * 1000000000000000).toString().padStart(15, '0'); | ||
const checksum = calculateChecksum(baseDigits); | ||
return baseDigits.toString() + checksum.toString(); | ||
} | ||
} | ||
|
||
const calculateChecksum = (isniBaseDigits) => { | ||
let total = 0; | ||
for (let i = 0; i < 15; i++) { | ||
total = (total + parseInt(isniBaseDigits.charAt(i), 10)) * 2; | ||
} | ||
const remainder = total % 11; | ||
const result = (12 - remainder) % 11; | ||
return result === 10 ? 'X' : result.toString(); | ||
} |
Oops, something went wrong.