How to use is-regex - 6 common examples

To help you get started, we’ve selected a few is-regex examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github airbnb / prop-types / src / componentWithName.js View on Github external
) || null;
  }

  if (!React.isValidElement(propValue)) {
    return new TypeError(
      `${componentName}.${propName} is not a valid React element`,
    );
  }

  const { type } = propValue;
  const componentNameFromType = getComponentName(type);
  const innerComponentName = namesOfHOCsToStrip.length > 0
    ? stripHOCs(componentNameFromType, namesOfHOCsToStrip)
    : componentNameFromType;

  if (isRegex(name) && !name.test(innerComponentName)) {
    return new TypeError(
      `\`${componentName}.${propName}\` only accepts components matching the regular expression ${name}`,
    );
  }

  if (!isRegex(name) && innerComponentName !== name) {
    return new TypeError(
      `\`${componentName}.${propName}\` only accepts components named ${name}, got ${innerComponentName}`,
    );
  }

  return null;
}
github airbnb / prop-types / src / componentWithName.js View on Github external
);
  }

  const { type } = propValue;
  const componentNameFromType = getComponentName(type);
  const innerComponentName = namesOfHOCsToStrip.length > 0
    ? stripHOCs(componentNameFromType, namesOfHOCsToStrip)
    : componentNameFromType;

  if (isRegex(name) && !name.test(innerComponentName)) {
    return new TypeError(
      `\`${componentName}.${propName}\` only accepts components matching the regular expression ${name}`,
    );
  }

  if (!isRegex(name) && innerComponentName !== name) {
    return new TypeError(
      `\`${componentName}.${propName}\` only accepts components named ${name}, got ${innerComponentName}`,
    );
  }

  return null;
}
github airbnb / enzyme / packages / enzyme / src / RSTTraversal.js View on Github external
export function hasClassName(node, className) {
  let classes = propsOfNode(node).className || '';
  classes = String(classes).replace(/\s/g, ' ');
  if (isRegex(className)) return className.test(classes);
  return ` ${classes} `.indexOf(` ${className} `) > -1;
}
github mjackson / expect / modules / TestUtils.js View on Github external
export const functionThrows = (fn, context, args, value) => {
  try {
    fn.apply(context, args)
  } catch (error) {
    if (value == null)
      return true

    if (isFunction(value) && error instanceof value)
      return true

    const message = error.message || error

    if (typeof message === 'string') {
      if (isRegExp(value) && value.test(error.message))
        return true

      if (typeof value === 'string' && message.indexOf(value) !== -1)
        return true
    }
  }

  return false
}
github airbnb / prop-types / src / componentWithName.js View on Github external
export default function componentWithName(
  name,
  options = {},
) {
  if (typeof name !== 'string' && !isRegex(name)) {
    throw new TypeError('name must be a string or a regex');
  }

  const passedOptions = Object.keys(options);
  if (passedOptions.length > 1 || (passedOptions.length === 1 && passedOptions[0] !== 'stripHOCs')) {
    throw new TypeError(`The only options supported are: “stripHOCs”, got: “${passedOptions.join('”, “')}”`);
  }
  const { stripHOCs: namesOfHOCsToStrip = [] } = options;

  const allHOCNamesAreValid = namesOfHOCsToStrip.every((x) => {
    if (typeof x !== 'string' || /[()]/g.test(x)) {
      return false;
    }
    return /^(?:[a-z][a-zA-Z0-9]+|[A-Z][a-z][a-zA-Z0-9]+)$/.test(x);
  });
  if (!allHOCNamesAreValid) {
github komachi / usedcss / src / index.js View on Github external
pr = Promise.map(options.ignoreRegexp, (item) => {
                      if (!isRegex(item)) {
                        item = new RegExp(item);
                      }
                      if (item.test(selector)) {
                        return Promise.reject();
                      }
                    });
                  } else {

is-regex

Is this value a JS regex? Works cross-realm/iframe, and despite ES6 @@toStringTag

MIT
Latest version published 3 years ago

Package Health Score

67 / 100
Full package analysis

Popular is-regex functions