update-diff

WebJar for update-diff

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

update-diff
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

update-diff
WebJar for update-diff
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/dfcreative/update-diff

Download update-diff

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/update-diff/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>update-diff</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/update-diff/
implementation 'org.webjars.npm:update-diff:1.1.0'
// https://jarcasting.com/artifacts/org.webjars.npm/update-diff/
implementation ("org.webjars.npm:update-diff:1.1.0")
'org.webjars.npm:update-diff:jar:1.1.0'
<dependency org="org.webjars.npm" name="update-diff" rev="1.1.0">
  <artifact name="update-diff" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='update-diff', version='1.1.0')
)
libraryDependencies += "org.webjars.npm" % "update-diff" % "1.1.0"
[org.webjars.npm/update-diff "1.1.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

update-diff unstable

Update object by mapping differences with another object. Comes handy for updating state.

npm install update-diff

let prop = require('update-diff')

let state = {id: 1, values: [1, 2, 3]}

updateDiff(state, {id: 5, values: ['1', '2']}, {
  values: v => {
    if (!v || v.length) throw 'values should be an array-like'
    return Array.from(v, v => parseFloat(v))
  },
  id: id => id
})

// {id: 5, values: [1, 2]}

If property mapper returns any value other than undefined, it is used for new value. undefined keeps state property unchanged. Only primitives get compared in diffing, to compare arrays you have to do it manually:

update(state, opts, {
	positions: (p, state) => {
		if (p.length === state.positions.length) return
		return p
	}
})

To do multipass update, use array of mappers:

let state = {}
let options = {propA: 0, propB: 1, propC: ['foo'], propD: 'bar'}

updateDiff(state, options, [
//first pass mapping
{
  propA: value => value,
  propB: true,
  propC: Array.isArray,
  propD: function () {}
},
//second pass mapping
{
  propX: (x, state) => state.propB + x
},
//third pass mapping
{
  propA: (value, state, options) => state.propA ? 'a' : 'b'
}
])

// {propA: 0, propB: 1, propC: ['foo']}

Related

  • obj-map-prop − map object properties by a dict
  • map-obj − map properties by single function
  • filter-obj − filter properties by single function

Credits

© 2017 Dima Yv. MIT License

Versions

Version
1.1.0