isAlphanumeric.js 950 B

1234567891011121314151617181920212223242526
  1. import assertString from './util/assertString';
  2. import { alphanumeric } from './alpha';
  3. export default function isAlphanumeric(_str) {
  4. var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';
  5. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  6. assertString(_str);
  7. var str = _str;
  8. var ignore = options.ignore;
  9. if (ignore) {
  10. if (ignore instanceof RegExp) {
  11. str = str.replace(ignore, '');
  12. } else if (typeof ignore === 'string') {
  13. str = str.replace(new RegExp("[".concat(ignore.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&'), "]"), 'g'), ''); // escape regex for ignore
  14. } else {
  15. throw new Error('ignore should be instance of a String or RegExp');
  16. }
  17. }
  18. if (locale in alphanumeric) {
  19. return alphanumeric[locale].test(str);
  20. }
  21. throw new Error("Invalid locale '".concat(locale, "'"));
  22. }
  23. export var locales = Object.keys(alphanumeric);