Skip to content

Commit

Permalink
Refactor for better code reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelbucket-dev committed Oct 25, 2022
1 parent 28567da commit 8b1fda9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 41 deletions.
57 changes: 42 additions & 15 deletions src/lib/alpha.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
export function removeIgnoredCharacters(str, ignoredCharacters) {
if (!ignoredCharacters) {
return str;
}

if (ignoredCharacters instanceof RegExp) {
return str.replace(ignoredCharacters, '');
}

if (typeof ignoredCharacters === 'string') {
return str.replace(new RegExp(`[${ignoredCharacters.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&')}]`, 'g'), ''); // escape regex for 'ignoredCharacters'
}

throw new Error('"ignore" should be instance of a String or RegExp');
}
import assertString from './util/assertString';

export const ALPHA = {
'en-US': /^[A-Z]+$/i,
Expand Down Expand Up @@ -166,3 +152,44 @@ DECIMAL['pl-Pl'] = DECIMAL['pl-PL'];

// see #1455
ALPHA['fa-AF'] = ALPHA.fa;


function removeIgnoredCharacters(str, ignoredCharacters) {
if (!ignoredCharacters) {
return str;
}

if (ignoredCharacters instanceof RegExp) {
return str.replace(ignoredCharacters, '');
}

if (typeof ignoredCharacters === 'string') {
return str.replace(new RegExp(`[${ignoredCharacters.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&')}]`, 'g'), ''); // escape regex for 'ignoredCharacters'
}

throw new Error('"ignore" should be instance of a String or RegExp');
}

const ALPHA_TYPE_MAP = {
alpha: ALPHA,
alphanumeric: ALPHANUMERIC,
};

function validate(typeKey) {
return (_str, options = {}) => {
assertString(_str);

const { ignore, locale = 'en-US' } = options;
const str = removeIgnoredCharacters(_str, ignore);
const alphaType = ALPHA_TYPE_MAP[typeKey];

if (alphaType[locale]) {
return alphaType[locale].test(str);
}

throw new Error(`Invalid "locale" '${locale}'`);
};
}

export const validateAlpha = validate('alpha');
export const validateAlphanumeric = validate('alphanumeric');
16 changes: 3 additions & 13 deletions src/lib/isAlpha.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import assertString from './util/assertString';
import { ALPHA, removeIgnoredCharacters } from './alpha';
import { ALPHA, validateAlpha } from './alpha';

export default function isAlpha(_str, options = {}) {
assertString(_str);

const { ignore, locale = 'en-US' } = options;
const str = removeIgnoredCharacters(_str, ignore);

if (ALPHA[locale]) {
return ALPHA[locale].test(str);
}

throw new Error(`Invalid "locale" '${locale}'`);
export default function isAlpha(_str, options) {
return validateAlpha(_str, options);
}

export const locales = Object.keys(ALPHA);
16 changes: 3 additions & 13 deletions src/lib/isAlphanumeric.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import assertString from './util/assertString';
import { ALPHANUMERIC, removeIgnoredCharacters } from './alpha';
import { ALPHANUMERIC, validateAlphanumeric } from './alpha';

export default function isAlphanumeric(_str, options = {}) {
assertString(_str);

const { ignore, locale = 'en-US' } = options;
const str = removeIgnoredCharacters(_str, ignore);

if (ALPHANUMERIC[locale]) {
return ALPHANUMERIC[locale].test(str);
}

throw new Error(`Invalid "locale" '${locale}'`);
export default function isAlphanumeric(_str, options) {
return validateAlphanumeric(_str, options);
}

export const locales = Object.keys(ALPHANUMERIC);

0 comments on commit 8b1fda9

Please sign in to comment.