Skip to content

Instantly share code, notes, and snippets.

@ximik777
Last active July 7, 2016 15:30
Show Gist options
  • Save ximik777/7853543d8d0179510fa23236d39dc255 to your computer and use it in GitHub Desktop.
Save ximik777/7853543d8d0179510fa23236d39dc255 to your computer and use it in GitHub Desktop.
Conversion ID cards in all formats
function cardList(card_id, upeercase) {
if (!card_id) return [];
card_id += '';
upeercase = !!(upeercase);
var arr = [],
dechex = function (number) {
return parseInt(number < 0 ? 0xFFFFFFFF + number + 1 : number, 10).toString(16)
}, str_split = function (str, split) {
if (!str) return false;
split = split || 1;
if (split < 1) {
return false
}
str += '';
var arr = [], pos = 0, len = str.length;
while (pos < len) {
arr.push(str.slice(pos, pos += split))
}
return arr
};
arr[0] = card_id;
arr[1] = str_split(card_id, 2).reverse().join('');
if (/^[0-9]*$/.test(card_id)) {
arr[2] = dechex(card_id.substr(1));
arr[3] = str_split(arr[2], 2).reverse().join('');
arr[4] = dechex(card_id.length % 2 ? '0' + card_id : card_id);
arr[5] = str_split(arr[4], 2).reverse().join('');
}
if (upeercase) {
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].toUpperCase();
}
}
return arr;
}
console.log(cardList('322B3DF6', 1));
console.log(cardList('94131203890', 1));
console.log(cardList('9841694710', 1));
console.log(cardList('4131203890', 1));
console.log(cardList('841694710', 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment