Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

skava / chain-able-lego   js

Repository URL to install this package:

Version: 6.0.4 

/ src / makeShape.ts

import { toTestable, keys, preAllocate, forOwn } from 'chain-able-deps'

interface AnyObj {
  [key: string]: any
  [key: number]: any
}

/**
 * @param shape @alias specificationTransformations
 * @return shape creation
 */
const makeShape = (shape: AnyObj): AnyObj => {
  const shapeKeys = keys(shape)
  const specifications = preAllocate(shapeKeys)
  const iteratee = (key, index) => {
    specifications[index] = {
      satisfies: (x, prop) => {
        // shapeKeys[key]
        return toTestable(key)(prop)
      },
      transform: shape[key],
    }
  }

  forOwn(shapeKeys, iteratee)

  const shaped = (value, key) => {
    let evolved = false

    forOwn(specifications, specification => {
      const { transform, satisfies } = specification

      if (satisfies(value, key)) {
        evolved = transform(value, key)

        // stop iterating - unless we want multiple transforms...
        // return false
      }
    })

    return evolved
  }

  return shaped
}

export { makeShape }
export default makeShape