Skip to content

Transform

James edited this page May 30, 2017 · 3 revisions

🤖 transform 🎼

👆 tap

tap a value with a function

new Chain()
  .set('eh', 'eh')
  .tap('eh', x => x + '!')
  .get('eh') === 'eh!'

this replaced the previous .concat and .append in this simple example, when the existing value for key is an array or a string, append val to it

const {str, arr} = new Chain()
  .set('str', 'emptyish')
  .tap('str', str => str + '+')
  .set('arr', [1])
  .tap('arr', arr => arr.concat([2]))
  .entries()

str == 'emptyish+'
arr == [1, 2]

🗺 remap

const chain = new Chain()
  .remapKeys()
  .remapKey('dis', 'dat')      // dis -> dat
  .from({dis: 1, other: true})
  .get('dat') === 1

👣 traverse

traverse any data type deeply, match values &|| keys, optional callback .onMatch

const eh = {
  notme: 1,
  nested: {
    really: {
      deep: {
        super: false,
        canada: true,
        modules: [{parser: 'moose'}],
      },
      matchme: 'minime',
    },
  },
}

const cleaned = new Chain()
  .merge(eh)
  .traverse(false)
  .keys([/super/, /parser/])
  .vals([/minime/])
  .call(true)

cleaned === {
  notme: 1,
  nested: {
    really: {
      // stripped matchme: minime
      deep: {
        // stripped out super
        canada: true,
        modules: [], // stripped out parser
      },
    },
  },
}

🔗 related

Clone this wiki locally