lodash#isRegExp TypeScript Examples

The following examples show how to use lodash#isRegExp. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: helpers.ts    From leda with MIT License 6 votes vote down vote up
getArrayValidator = (
  validator: ValidatorObject[],
  customMessage?: string,
): NormalizedValidatorObject[] => validator
  .map((validatorItem) => {
    if (!isObject(validatorItem)) throw new Error(`L.Validation: type of validator ${JSON.stringify(validator)} is incorrect!`);
    // { function, message? }
    if (isFunction(validatorItem.validator)) {
      return { validator: validatorItem.validator, invalidMessage: validatorItem.invalidMessage };
    }

    // { predefinedValidator, message? }
    if (isString(validatorItem.validator)) {
      const predefinedValidator = getPredefinedValidator(validatorItem.validator as PredefinedValidator);

      return customMessage ? { validator: predefinedValidator.validator, invalidMessage: customMessage } : predefinedValidator;
    }

    // { regexp, message? }
    if (isRegExp(validatorItem.validator)) {
      return {
        validator: (value: string) => !!(value).match(validatorItem.validator as RegExp),
        invalidMessage: validatorItem.invalidMessage,
      };
    }

    throw new Error(`L.Validation: type of validator ${JSON.stringify(validator)} is incorrect!`);
  })
Example #2
Source File: helpers.ts    From leda with MIT License 6 votes vote down vote up
getValidators = (
  validator?: Validator | PredefinedValidator | RegExp | ValidatorObject[],
  invalidMessage?: string,
): NormalizedValidatorObject[] => {
  if (!validator) return [];

  if (isFunction(validator)) return [{ validator, invalidMessage }];

  if (isString(validator)) return [getPredefinedValidator(validator as PredefinedValidator, invalidMessage)];

  if (isRegExp(validator)) return [getRegExpValidator(validator, invalidMessage)];

  if (Array.isArray(validator)) return getArrayValidator(validator, invalidMessage);

  throw new Error(`L.Validation: type of validator ${JSON.stringify(validator)} is incorrect!`);
}
Example #3
Source File: helpers.ts    From leda with MIT License 6 votes vote down vote up
externalToSingleFunctionValidator = (externalValidator: ExternalValidator): SingleFunctionValidator => {
  const {
    validator,
  } = externalValidator;
  const singleFunctionValidator: SingleFunctionValidator = {
    validate: () => true,
  };
  if (Array.isArray(validator)) {
    singleFunctionValidator.validate = (value) => validator.every((element) => {
      const validate = externalToSingleFunctionValidator({
        validator: element,
      });
      return validate.validate(value);
    });
  } else if (isFunction(validator)) {
    singleFunctionValidator.validate = validator;
  } else if (isRegExp(validator)) {
    singleFunctionValidator.validate = (value: string) => value.match(validator) != null;
  } else if (isString(validator)) {
    const predefinedValidator = PREDEFINED_VALIDATORS[validator];
    singleFunctionValidator.validate = predefinedValidator.validator;
    if (externalValidator.invalidMessage == null && predefinedValidator.invalidMessage) {
      singleFunctionValidator.invalidMessage = predefinedValidator.invalidMessage;
    }
  }
  if (singleFunctionValidator.invalidMessage == null && externalValidator.invalidMessage) {
    singleFunctionValidator.invalidMessage = externalValidator.invalidMessage;
  }
  return singleFunctionValidator;
}
Example #4
Source File: regex.ts    From js-client with MIT License 5 votes vote down vote up
isRegex = (value: any): value is Regex => isRegExp(value)