-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathisAlphanumeric.js
More file actions
26 lines (21 loc) · 786 Bytes
/
isAlphanumeric.js
File metadata and controls
26 lines (21 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import assertString from './util/assertString';
import { alphanumeric } from './alpha';
export default function isAlphanumeric(_str, locale = 'en-US', options = {}) {
assertString(_str);
let str = _str;
const { ignore } = options;
if (ignore) {
if (ignore instanceof RegExp) {
str = str.replace(ignore, '');
} else if (typeof ignore === 'string') {
str = str.replace(new RegExp(`[${ignore.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&')}]`, 'g'), ''); // escape regex for ignore
} else {
throw new Error('ignore should be instance of a String or RegExp');
}
}
if (locale in alphanumeric) {
return alphanumeric[locale].test(str);
}
throw new Error(`Invalid locale '${locale}'`);
}
export const locales = Object.keys(alphanumeric);