@konfirm/iso13616
Version:
ISO 13616-1:2007 - International Bank Account Number
615 lines (597 loc) • 17.1 kB
JavaScript
;
/**
* Simple type detection
*
* @param {*} input
* @return {string} type
*/
function type(input) {
return input === null ? 'null' : Array.isArray(input) ? 'array' : typeof input;
}
/**
* Error thrown if an Alphabet receives less characters than needed
*
* @class InvalidInputError
* @extends {Error}
*/
let InvalidInputError$1 = class InvalidInputError extends Error {
/**
* Creates an instance of InvalidInputError
*
* @memberof InvalidInputError
*/
constructor(source) {
super(`Alphabets requires a string(able), got (${type(source)}) ${source}`);
const { constructor, constructor: { name } } = this;
this.name = name;
Error.captureStackTrace(this, constructor);
}
};
/**
* Error thrown if an Alphabet receives duplicate characters
*
* @class DuplicateCharacterError
* @extends {Error}
*/
let DuplicateCharacterError$1 = class DuplicateCharacterError extends Error {
/**
* Creates an instance of DuplicateCharacterError
*
* @param {object} [meta={source, duplicate}]
* @memberof DuplicateCharacterError
*/
constructor(source, duplicate) {
super(`Alphabets cannot contain duplicate characters, found "${duplicate}" in "${source}"`);
const { constructor, constructor: { name } } = this;
this.name = name;
Error.captureStackTrace(this, constructor);
}
};
const storage$1 = new WeakMap();
const defaultCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const stringable = (input) => /string|object/.test(type(input));
/**
* Immutable alphabet
*
* @class Alphabet
*/
class Alphabet {
constructor(source = defaultCharacters) {
const characters = stringable(source) ? String(source) : '';
const alphabet = Array.from(characters);
const duplicate = alphabet.filter((v, i, a) => a.indexOf(v) !== i).join('');
if (!characters.length) {
throw new InvalidInputError$1(source);
}
if (duplicate.length) {
throw new DuplicateCharacterError$1(characters, duplicate);
}
storage$1.set(this, { characters, alphabet });
}
/**
* Obtain the configured characters
*
* @readonly
* @memberof Alphabet
*/
get characters() {
const { characters } = storage$1.get(this);
return characters;
}
/**
* Get the number of bytes of the alphabet
*
* @readonly
* @memberof Alphabet
*/
get byteLength() {
return this.characters.length;
}
/**
* Get the character length of the alphabet
*
* @readonly
* @memberof Alphabet
*/
get length() {
const { alphabet: { length } } = storage$1.get(this);
return length;
}
/**
* Extract part of the alphabet and return it as a (singleton) instance of alphabet
*
* @param {number} start
* @param {number} end
* @returns {Alphabet} instance
* @memberof Alphabet
*/
slice(start, end) {
const { alphabet } = storage$1.get(this);
const { constructor: Ctor } = Object.getPrototypeOf(this);
return Ctor.from(alphabet.slice(start, end).join(''));
}
/**
* Get the character at given index
*
* @param {number} index
* @returns {string} char
* @memberof Alphabet
*/
charAt(index) {
const { alphabet } = storage$1.get(this);
return alphabet[index];
}
/**
* Get the character code at given index
*
* @param {numer} index
* @returns {number} charcode
* @memberof Alphabet
*/
charCodeAt(index) {
const { characters, length } = this;
return index >= 0 && index < length ? characters.charCodeAt(index) : undefined;
}
/**
* Get the code point at given index
*
* @param {numer} index
* @returns {number} codepoint
* @memberof Alphabet
*/
codePointAt(index) {
const char = this.charAt(index);
return char ? char.codePointAt(0) : undefined;
}
/**
* Get the index of the given character
*
* @param {string} char
* @returns {number} index
* @memberof Alphabet
*/
indexOf(char) {
const { alphabet } = storage$1.get(this);
return alphabet.indexOf(char);
}
/**
* Map any amount of indices to the corresponding characters (wrapping the
* indices around if they exceed the length of the alphabet)
*
* @param {number} ...list
* @returns {string} [char]
* @memberof Alphabet
*/
map(...list) {
const { length } = this;
const normal = (index) => index < 0 ? normal(length + index) : index;
return list.map((index) => this.charAt(normal(index) % length));
}
/**
* Convert the Alphabet into a string
*
* @returns {string} chars
* @memberof Alphabet
*/
toString() {
return this.characters;
}
/**
* Ensure the Alphabet is properly JSON-stringified
*
* @returns
* @memberof Alphabet
*/
toJSON() {
return String(this);
}
/**
* Obtain a singleton instance of Alphabet representing the provided characters
*
* @static
* @param {*} characters
* @returns
* @memberof Alphabet
*/
static from(characters) {
if (!storage$1.has(this)) {
storage$1.set(this, new Map());
}
const map = storage$1.get(this);
if (!map.has(characters)) {
map.set(characters, new this(characters));
}
return map.get(characters);
}
}
const storage = new WeakMap();
/**
* Implement the common ISO 7064 implementation mechanics
*
* @class ISO7064
*/
class ISO7064 {
constructor(options = {}) {
storage.set(this, options);
}
/**
* The algorithm name
*
* @readonly
* @memberof ISO7064
*/
get algorithm() {
const { algorithm } = storage.get(this);
return algorithm || 'Custom';
}
/**
* The specification name
*
* @readonly
* @memberof ISO7064
*/
get specification() {
const { algorithm } = this;
return `ISO 7064, ${algorithm}`;
}
/**
* The designation (always 0, except for the Modulus implementations)
*
* @readonly
* @memberof ISO7064
*/
get designation() {
const { designation } = storage.get(this);
return designation || 0;
}
/**
* The alphabet of allowed characters and from which to obtain the indices
*
* @readonly
* @memberof ISO7064
*/
get indices() {
const { indices, alphabet } = storage.get(this);
return indices || alphabet;
}
/**
* The checksum alphabet
*
* @readonly
* @memberof ISO7064
*/
get alphabet() {
const { alphabet } = storage.get(this);
return alphabet;
}
/**
* The modulus
*
* @readonly
* @memberof ISO7064
*/
get modulus() {
const { modulus, alphabet } = storage.get(this);
return modulus || (alphabet && alphabet.length);
}
/**
* The radix
*
* @readonly
* @memberof ISO7064
*/
get radix() {
const { radix } = storage.get(this);
return radix;
}
/**
* Does the checksum consist of double digits
*
* @readonly
* @memberof ISO7064
*/
get double() {
const { double } = storage.get(this);
return double || false;
}
/**
* Normalize input, removing any character not allowed in the input
*
* @param {string} input
* @returns {string} normalized
* @memberof ISO7064
*/
normalize(input) {
const { indices = 'a-zA-Z0-9' } = this;
const purge = new RegExp(`[^${indices}]+`, 'g');
return String(input)
.toUpperCase()
.replace(purge, '');
}
/**
* Calculate the checksum for input
*
* @param {string} input
* @returns {string} checksum
* @memberof ISO7064
*/
checksum(input) {
throw new Error('Checksum method not implemented');
}
/**
* Validate the input
*
* @param {string} input
* @returns {boolean} valid
* @memberof ISO7064
*/
validate(input) {
const { indices, alphabet, double } = this;
const pattern = new RegExp(`([${indices}]+)([${alphabet}]{${Number(double) + 1}})`);
const match = this.normalize(input).match(pattern);
if (match) {
const [, num, cc] = match;
return this.checksum(num) === cc;
}
return false;
}
/**
* Generate the normalized output including the checksum
*
* @param {string} input
* @returns {string} generated
* @memberof ISO7064
*/
generate(input) {
const normal = this.normalize(input);
return `${normal}${this.checksum(input)}`;
}
/**
* Create a new instance based on the current settings with optional overrides
*
* @param {object} options
* @returns
* @memberof ISO7064
*/
factory(options = {}) {
const { indices, alphabet, modulus, radix, double } = this;
const { constructor: Ctor } = Object.getPrototypeOf(this);
return new Ctor(Object.assign({ indices,
alphabet,
modulus,
radix,
double }, options));
}
}
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol */
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
/**
* Pure checksum calculation implementation
*
* @class PureISO7064
* @extends {ISO7064}
*/
class PureISO7064 extends ISO7064 {
/**
* Creates an instance of PureISO7064
*
* @param {*} options
* @memberof PureISO7064
*/
constructor(options = {}) {
const { alphabet, radix = 2, indices = alphabet === null || alphabet === void 0 ? void 0 : alphabet.slice(0, -1) } = options, rest = __rest(options, ["alphabet", "radix", "indices"]);
super(Object.assign({ alphabet, indices, radix }, rest));
}
/**
* Calculate the checksum for input
*
* @param {string} input
* @returns {string} checksum
* @memberof PureISO7064
*/
checksum(input) {
const { modulus, radix, double, indices, alphabet } = this;
const initial = alphabet.charAt(0).repeat(Number(double) + 1);
const normal = this.normalize(input) + initial;
const sum = Array.from(normal)
.map((char) => indices.indexOf(char))
.reduce((carry, pos) => (carry * radix + pos) % modulus, 0);
const checksum = (modulus + 1 - (sum % modulus)) % modulus;
return (double
? [(checksum / radix) | 0, checksum % radix]
: [checksum])
.map((index) => alphabet.charAt(index))
.join('');
}
}
/**
* Hybrid checksum calculation implementation
*
* @class HybridISO7064
* @extends {ISO7064}
*/
class HybridISO7064 extends ISO7064 {
/**
* Calculate the checksum for input
*
* @param {string} input
* @returns {string} checksum
* @memberof HybridISO7064
*/
checksum(input) {
const { modulus: mod, indices, alphabet } = this;
const sum = Array.from(this.normalize(input))
.map((char) => indices.indexOf(char))
.reduce((carry, pos) => (((carry % (mod + 1)) + pos) % mod || mod) * 2, mod) % (mod + 1);
return alphabet.charAt((mod + 1 - sum) % mod);
}
}
const alphabet = {
num: Alphabet.from('0123456789'),
numX: Alphabet.from('0123456789X'),
alpha: Alphabet.from('ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
alphanum: Alphabet.from('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
alphanumA: Alphabet.from('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*')
};
new PureISO7064({
algorithm: 'MOD 11-2',
designation: 1,
alphabet: alphabet.numX
});
new PureISO7064({
algorithm: 'MOD 37-2',
designation: 2,
alphabet: alphabet.alphanumA
});
const Mod97_10 = new PureISO7064({
algorithm: 'MOD 97-10',
designation: 3,
modulus: 97,
radix: 10,
alphabet: alphabet.num,
indices: alphabet.num,
double: true
});
new PureISO7064({
algorithm: 'MOD 661-26',
designation: 4,
modulus: 661,
radix: 26,
alphabet: alphabet.alpha,
indices: alphabet.alpha,
double: true
});
new PureISO7064({
algorithm: 'MOD 1271-36',
designation: 5,
modulus: 1271,
radix: 36,
alphabet: alphabet.alphanumA,
double: true
});
new HybridISO7064({
algorithm: 'MOD 11,10',
designation: 6,
alphabet: alphabet.num
});
new HybridISO7064({
algorithm: 'MOD 27,26',
designation: 7,
alphabet: alphabet.alpha
});
new HybridISO7064({
algorithm: 'MOD 37,36',
designation: 8,
alphabet: alphabet.alphanum
});
/**
* Extension of the ISO7064 MOD 97-10 Algorithm
*/
const CustomMod97_10 = Mod97_10.factory({
algorithm: 'MOD 97-10 (Custom)',
designation: 3,
indices: Alphabet.from('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
});
/**
* Validate the input to satisfy the ISO 13616 checksum
*
* @static
* @param {string|number} input
* @returns {boolean} valid
* @memberof ISO13616
*/
function validate(input) {
const { country, checksum: check, account } = match(String(input));
return checksum(account, country) === check;
}
/**
* Calculate the ISO 13616 checksum
*
* @static
* @param {string} account
* @param {string} country ISO 3166 code
* @returns {string} checksum
* @memberof ISO13616
*/
function checksum(account, country) {
const { modulus, indices } = CustomMod97_10;
const numeric = Array.from(`${account}${country}`)
.map((char) => indices.indexOf(char))
.filter((value) => value >= 0)
.join('');
const checksum = CustomMod97_10.checksum(numeric);
const number = Number(checksum);
// ISO13616-1:2020 states: the check digits can only be in the range [02..98]
return number <= 98 - modulus ? String(number + modulus) : checksum;
}
/**
* Generate the full ISO 13616 string, including the checksum
*
* @static
* @param {string} account
* @param {string} country
* @param {boolean} formatting (in pairs of 4)
* @returns {string} generated ISO 13616
* @memberof ISO13616
*/
function generate(account, country, formatting = false) {
const generated = `${country}${checksum(account, country)}${account}`;
return formatting ? format(generated) : CustomMod97_10.normalize(generated);
}
/**
* Format the input to match the specified groups of 4 characters
*
* @static
* @param {string|number} input
* @returns {string} formatted
* @memberof ISO13616
*/
function format(input) {
const normal = CustomMod97_10.normalize(String(input));
return Array.from(normal)
.reduce((carry, char, index) => carry + (index && index % 4 === 0 ? ' ' : '') + char, '');
}
/**
* Match the input and return the matches values as object
*
* @static
* @param {string} input
* @returns {ISO13616Match}
* @memberof ISO13616
*/
function match(input) {
const [, country, checksum, account] = CustomMod97_10.normalize(input).match(/^([A-Z]{2})([0-9]{2})(\w{1,30})$/) || [];
return { country, checksum, account };
}
exports.checksum = checksum;
exports.format = format;
exports.generate = generate;
exports.match = match;
exports.validate = validate;
//# sourceMappingURL=main.js.map