unctionjs / computedProp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@unction/computedProp

Tests Stability Dependencies

MapperFunctionType<A, B> => KeyChainType => Array | Set | RecordType<C | B, A> | string => Array | Set | RecordType<C | B, A> | string

Given an object this function will return that object but with a new property, where the value is computed. The computation is given the object you'll be copying.

const computer = ({id, attributes: {username}}) => `${username}#${id}`
const key = "tag"
const payload = {
  id: "1",
  attributes: {
    username: "krainboltgreene"
  }
}

computedProp(computer)(key)(payload)

Would return:

{
  id: "1",
  tag: "krainboltgreene#1",
  attributes: {
    username: "krainboltgreene"
  }
}
const multiKey = ["attributes", "tag"]

computedProp(computer)(key)(payload)

Would return:

{
  id: "1",
  attributes: {
    tag: "krainboltgreene#1",
    username: "krainboltgreene"
  }
}

About

License:Other


Languages

Language:JavaScript 95.2%Language:TypeScript 4.8%