Skip to content

Instantly share code, notes, and snippets.

View agodin3z's full-sized avatar
🍻
Working hard~

Andrés Godínez agodin3z

🍻
Working hard~
View GitHub Profile
@agodin3z
agodin3z / getCardType.js
Last active November 17, 2020 01:58
Get credit/debit card type
export const getCardType = (num) => {
let type = 'unknow';
if (num.match('^3[47]\\d{0,13}')) {
type = 'amex';
} else if (num.match('^(?:6011|65\\d{0,2}|64[4-9]\\d?)\\d{0,12}')) {
type = 'discover';
} else if (num.match('^3(?:0([0-5]|9)|[689]\\d?)\\d{0,11}')) {
type = 'diners';
} else if (num.match('^(5[1-5]\\d{0,2}|22[2-9]\\d{0,1}|2[3-7]\\d{0,2})\\d{0,12}')) {
type = 'mastercard';
@agodin3z
agodin3z / validateCard.js
Created November 17, 2020 02:00
Validate credit/debit card number (Luhn Algorithm)
export const validateCard = ((arr) => {
return (num) => {
let len = num.length;
let bit = 1;
let sum = 0;
let val;
while (len) {
val = parseInt(num.charAt(--len), 10);
// eslint-disable-next-line no-bitwise,no-cond-assign
sum += (bit ^= 1) ? arr[val] : val;
@agodin3z
agodin3z / Luhn.js
Created November 17, 2020 02:27 — forked from jscari/Luhn.js
Luhn algorithm - generate / validate
var Luhn = {
// length of our number (check digit included)
length: 10,
pow2: [0, 2, 4, 6, 8, 1, 3, 5, 7, 9],
// compute check digit
checksum: function (x) {
var sum = 0;
var n;
var odd = false;
for (var i = x.length - 1; i >= 0; --i) {
@agodin3z
agodin3z / TypeORM Simple WHERE Clause Generator.md
Created August 24, 2024 19:57
Generates TypeORM WHERE clauses for simple search queries across specified fields, optionally merging with default conditions.

TypeORM Simple WHERE Clause Generator

Preview:
/**
 * Generates a simple WHERE clause for TypeORM based on query and fields.
 *
 * @template T - The entity type.
 * @param {string} query - The search query.
 * @param {string} fields - A comma-separated list of fields to search in.
 * @param {FindOptionsWhere<T>} [defaultWhere={}] - Default WHERE conditions.