Code Monkey home page Code Monkey logo

flutate-js's Introduction

flutate

A TypeScript library, that aggregates record changes, into a single stream.

Installation

npm install flutate
yarn add flutate

Usage

const newState = flutate(state)
  .clone()
  .update('prop.name', 'new value')
  .update('prop.rows', (rows) => rows.add(item))
  .update('prop.rows', (rows) => rows.replace({ id: '123' }, newItem))
  .update('prop.rows', (rows) =>
    rows.at({ id: '123' }, (row) => row.update('note', 'new note value'))
  )
  .done()

APIs

Record

const record = flutate(source)

.update(path, value)

Update specified parameter.

The path can specify the hierarchy of the object by separating them with dots.

flutate({
  value1: 'example_1',
  value2: {
    value21: 'example_2_1'
  }
})
  .update('value1', 'new value')
  .update('value2.value21', 'new value')

For array manipulation.

flutate({
  values: [
    { id: 1, value: 'item_1' },
    { id: 2, value: 'item_2' }
  ]
}).update('values', (items) => items.add({ id: 3, value: 'item_3' }))

.clone()

Deep copy record value, and return new instance.

const newRecord = record.clone()

.get(path)

Get value from record, by dot's path.

const value = flutate({
  path: {
    to: 'example'
  }
}).get('path.to')
// return "example"

.done()

Return mutated result value.

const result = record.done()

Collection

For use in record.update method's mutation function.

record.update('array', (collection) => {
  // manipulate array by collection methods
  collection.add(...)

  return collation
})

For finding item by index and partial match of properties.

// by index
collection.remove(1)

// by partial match
collection.remove({ id: 1 })

.add(item)

Add item in collection the last.

collection.add({
  id: 1,
  value: 'item_1'
})

.remove(condition)

Remove item in collection.

collection.remove({ id: 1 })

.replace(condition, item)

Replace item in collection.

collection.replace({ id: 1 }, { id: 1, value: 'new value' })

.move(condition, direction)

Move item position by direction.

If you specify "forward" as the direction, the element will move forward. If you specify "backward", the element will move backward.

collection.move({ id: 1 }, 'forward').move({ id: 1 }, 'backward')

.at(condition, recordMutation)

Mutate element value by mutate function with FlutateRecord.

collection.at({ id: 1 }, (record) => record.update('value', 'new value'))

flutate-js's People

Contributors

rymizuki avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.