Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
composition / src / camelCaseKeys.ts
Size: Mime:
import { camelCase } from 'chain-able-boost'

// @see https://github.com/Microsoft/TypeScript/issues/5683#issuecomment-376505064
// https://github.com/Microsoft/TypeScript/issues/5683#issuecomment-228497288
// https://github.com/Microsoft/TypeScript/issues/5683#issuecomment-260430159
type LowerCase = Partial<{ [k in 'a' | 'b' | '_']: string }>
interface CamelCasable extends LowerCase {
  // [key: LowerCase]: any
  // [P in LowerCase]: string
}

/**
 * @desc this duplicates keys, is simplest fastest
 * @NOTE mutates obj
 */
function addCamelCaseKeys(obj: CamelCasable) {
  const keys = Object.keys(obj)
  const camelKeys = keys.map(camelCase) as string[]

  for (let i = 0; i < keys.length; i++) {
    const camel = camelKeys[i]
    if (camel.length === 0) continue
    obj[camel] = obj[keys[i]]
  }

  return obj
}

/* eslint-disable max-statements */

/**
 * @NOTE is immutable
 */
function camelCaseKeys(obj) {
  const keys = Object.keys(obj)
  const camelKeys = keys.map(camelCase) as string[]
  const camelized = {}

  for (let index = 0; index < keys.length; index++) {
    const camel = camelKeys[index]
    const key = keys[index]
    const value = obj[key]

    // is not camel
    if (camel.length === 0) {
      camelized[key] = value
    } else {
      obj[camel] = value
    }
  }

  return obj
}

export { addCamelCaseKeys }
export { camelCaseKeys }
export default camelCaseKeys