change-case#snakeCase TypeScript Examples

The following examples show how to use change-case#snakeCase. 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: utils.ts    From jsonapi-fractal with MIT License 7 votes vote down vote up
/**
 * Used to change the case (e.g. captalization) of the keys of a object
 *
 * @param originalAttributes
 * @param caseType
 * @param deep
 */
export function changeCase(originalAttributes: AttributesObject, caseType: CaseType, deep = false): AttributesObject {
  const caseTypes: Record<CaseType, CaseFunction> = {
    [CaseType.camelCase]: camelCase,
    [CaseType.snakeCase]: snakeCase,
    [CaseType.kebabCase]: paramCase,
  }

  const caseFunction = caseTypes[caseType]

  if (!caseFunction) {
    throw new Error('Invalid case type: ' + caseType)
  }

  const parsedAttributes: AttributesObject = {}

  for (const key of Object.keys(originalAttributes)) {
    let value = originalAttributes[key]

    if (deep && value) {
      if (Array.isArray(value)) {
        value = value.map((value) => (isObject(value) ? changeCase(value as JsonObject, caseType, deep) : value))
      } else if (isObject(value)) {
        value = changeCase(value as JsonObject, caseType, deep)
      }
    }

    parsedAttributes[caseFunction(key)] = value
  }

  return parsedAttributes
}
Example #2
Source File: Options.ts    From tf2autobot with MIT License 6 votes vote down vote up
function getOption<T>(option: string, def: T, parseFn: (target: string) => T, options?: Options): T {
    try {
        if (options && options[option]) {
            return options[option] as T;
        }
        const envVar = snakeCase(option).toUpperCase();
        return process.env[envVar] ? parseFn(process.env[envVar]) : def;
    } catch {
        return def;
    }
}
Example #3
Source File: index.ts    From graphql-mesh with MIT License 6 votes vote down vote up
NAMING_CONVENTIONS: Record<NamingConventionType, NamingConventionFn> = {
  camelCase,
  capitalCase,
  constantCase,
  dotCase,
  headerCase,
  noCase,
  paramCase,
  pascalCase,
  pathCase,
  sentenceCase,
  snakeCase,
  upperCase,
  lowerCase,
}
Example #4
Source File: api-rest-interceptor.service.ts    From taiga-front-next with GNU Affero General Public License v3.0 6 votes vote down vote up
private snakeCaseRequestInterceptor(request: HttpRequest<any>): HttpRequest<any> {
    let newRequest = request;

    if (newRequest.body) {
      const body = UtilsService.objKeysTransformer(newRequest.body, snakeCase);
      newRequest = newRequest.clone({ body });
    }

    if (newRequest.params) {
      let params = new HttpParams();

      newRequest.params.keys().forEach((key) => {
        const param = newRequest.params.get(key);

        if (param) {
          params = params.append(snakeCase(key), param);
        }
      });

      newRequest = newRequest.clone({ params });
    }

    return newRequest;
  }