lodash#mapKeys JavaScript Examples

The following examples show how to use lodash#mapKeys. 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: requests.js    From holo-schedule with MIT License 6 votes vote down vote up
async function* pagedItemsFetcher(endpoint, params = {}, paramEntries = []) {
  const safeParams = mapKeys(params, (_, key) => snakeCase(key))
  const { limit = 50 } = safeParams

  const searchParams = new URLSearchParams({ limit, ...safeParams })
  paramEntries.forEach(entry => searchParams.append(...entry))

  let page = 0
  let shouldContinue = true

  do {
    page += 1
    searchParams.set('page', page.toString())

    const { [endpoint.split('/')[0]]: items } = await fetchData(
      `${TARGET}api/v1/${endpoint}?${searchParams.toString()}`,
    )

    yield items

    if (items.length < limit) {
      shouldContinue = false
    }
  } while (shouldContinue)
}
Example #2
Source File: index.js    From datapass with GNU Affero General Public License v3.0 6 votes vote down vote up
function flattenDiffTransformer(accumulatorObject, fullObjectDiff, objectKey) {
  if (!isObject(fullObjectDiff[0])) {
    accumulatorObject[objectKey] = fullObjectDiff;

    return accumulatorObject;
  }
  // {contacts: [[{'name': 'c', email: 'd', work_email: 'a'}], [{'name': 'e', email: 'd'}]]}
  const objectBefore = flatten(fullObjectDiff[0], objectKey);
  const objectAfter = flatten(fullObjectDiff[1], objectKey);
  const objectDiff = mergeWith(
    objectBefore,
    objectAfter,
    (valueBefore, valueAfter) => [valueBefore, valueAfter]
  );
  // {0.name: ['c', 'e'], 0.email: ['d', 'd'], 0.work_email: 'a'}
  const objectDiffNoUnchangedNoDeprecated = omitBy(
    objectDiff,
    (value) => !isArray(value) || value[0] === value[1]
  );
  // {0.name: ['c', 'e']}
  const objectDiffPrefixedKey = mapKeys(
    objectDiffNoUnchangedNoDeprecated,
    (value, flatKey) => `${objectKey}.${flatKey}`
  );
  // {contacts.0.name: ['c', 'e']}
  Object.assign(accumulatorObject, objectDiffPrefixedKey);

  return accumulatorObject;
}
Example #3
Source File: ValidateInput.js    From SESTA-FMS with GNU Affero General Public License v3.0 5 votes vote down vote up
validateInput = (value, inputValidations = {}, type = "text") => {
  let errors = [];

  const emailRegex = new RegExp(
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  );
  const phoneRegex = new RegExp(
    /^\s*(?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *[x/#]{1}(\d+))?\s*$/
  );
  const passRegx = new RegExp("^(?=.*[a-zA-Z])(?=.*[0-9])(?=.{8,})");

  // handle i18n
  const requiredError = { id: "components.Input.error.validation.required" };
  mapKeys(inputValidations, (validationValue, validationKey) => {
    switch (validationKey) {
      case "max":
        if (parseInt(value, 10) > validationValue.value) {
          errors.push(validationValue.message);
        }
        break;
      case "maxLength":
        if (value.length > validationValue.value) {
          errors.push(validationValue.message);
        }
        break;
      case "min":
        if (parseInt(value, 10) < validationValue.value) {
          errors.push(validationValue.message);
        }
        break;
      case "minLength":
        if (value.length < validationValue.value) {
          errors.push(validationValue.message);
        }
        break;
      case "required":
        if (value.length === 0) {
          errors.push(validationValue.message);
        }
        break;
      case "regex":
        if (!new RegExp(validationValue.value).test(value)) {
          errors.push(validationValue.message);
        }
        break;
      case "validatePasswordRegex":
        if (value.length !== 0 && !passRegx.test(value)) {
          errors.push(validationValue.message);
        }
        break;
      case "email":
        if (value.length !== 0 && !emailRegex.test(value)) {
          errors.push(validationValue.message);
        }
        break;
      case "phone":
        if (value.length !== 0 && !phoneRegex.test(value)) {
          errors.push(validationValue.message);
        }
        break;
      default:
        errors = [];
    }
  });

  if (type === "email" && !emailRegex.test(value)) {
    errors.push("Not an email");
  }

  if (includes(errors, requiredError)) {
    errors = reject(errors, (error) => error !== requiredError);
  }

  return errors;
}
Example #4
Source File: Validation.js    From medha-STPC with GNU Affero General Public License v3.0 5 votes vote down vote up
validation = (value, inputValidations, type = "text") => {
  let errors = [];
  const emailRegex = new RegExp(
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  );
  const phonenoRegx = /^\d{10}$/;
  // handle i18n
  mapKeys(inputValidations, (validationValue, validationKey) => {
    switch (validationKey) {
      case "required":
        if (value !== undefined && value !== null && value.length === 0) {
          errors.push(validationValue.message);
        }
        break;
      case "validateEmailRegex":
        if (value !== null && value.length !== 0 && !emailRegex.test(value)) {
          errors.push(validationValue.message);
        }
        break;
      case "validatePasswordMinLength":
        if (value !== undefined && value.length !== 0 && value.length < 5) {
          errors.push(validationValue.message);
        }
        break;
      case "validateMobileNumber":
        if (value.length !== 0 && !phonenoRegx.test(value)) {
          errors.push(validationValue.message);
        }
        break;
      case "validateOtp":
        //change the value and condition for length after length of otp is decided
        if (value.length !== 0 && value.length !== validationValue.value) {
          errors.push(validationValue.message);
        }
        break;
      case "validateOtpForForgotPassword":
        //change the value and condition for length after length of otp is decided
        if (value.length !== 0 && value.length < validationValue.value) {
          errors.push(validationValue.message);
        }
        break;
      default:
        errors = [];
    }
  });

  if (type === "email" && !emailRegex.test(value)) {
    errors.push("Not an email");
  }

  return errors;
}