Moved functions from objectUtils.js to immutabilityHelpers.js
This commit is contained in:
parent
24ab2899dc
commit
ebca411384
|
@ -19,7 +19,7 @@
|
|||
"bugs": "https://github.com/josdejong/jsoneditor/issues",
|
||||
"scripts": {
|
||||
"build": "gulp",
|
||||
"test": "ava test/*.test.js test/**/*.test.js",
|
||||
"test": "ava test/*.test.js test/**/*.test.js --verbose",
|
||||
"watch": "gulp watch"
|
||||
},
|
||||
"dependencies": {
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import { h, Component } from 'preact'
|
||||
|
||||
import { setIn, updateIn, getIn, deleteIn, cloneDeep } from './utils/objectUtils'
|
||||
import { compareAsc, compareDesc, last } from './utils/arrayUtils'
|
||||
import { cloneDeep } from './utils/objectUtils'
|
||||
import { setIn, updateIn, getIn, deleteIn } from './utils/immutabilityHelpers'
|
||||
import { compareAsc, compareDesc } from './utils/arrayUtils'
|
||||
import { stringConvert } from './utils/typeUtils'
|
||||
import { isObject } from './utils/typeUtils'
|
||||
import bindMethods from './utils/bindMethods'
|
||||
|
|
|
@ -1,5 +1,16 @@
|
|||
'use strict';
|
||||
|
||||
/**
|
||||
* Immutability helpers
|
||||
*
|
||||
* inspiration:
|
||||
*
|
||||
* https://www.npmjs.com/package/seamless-immutable
|
||||
* https://www.npmjs.com/package/ih
|
||||
* https://www.npmjs.com/package/mutatis
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* helper function to get a nested property in an object or array
|
||||
*
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
import { isObject } from './typeUtils'
|
||||
|
||||
|
||||
// inspiration:
|
||||
//
|
||||
// https://www.npmjs.com/package/seamless-immutable
|
||||
// https://www.npmjs.com/package/ih
|
||||
// https://www.npmjs.com/package/mutatis
|
||||
|
||||
// TODO: unit test clone
|
||||
|
||||
/**
|
||||
|
@ -58,138 +52,3 @@ export function cloneDeep (value) {
|
|||
return value
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: unit test getIn
|
||||
|
||||
/**
|
||||
* helper function to get a nested property in an object or array
|
||||
*
|
||||
* @param {Object | Array} object
|
||||
* @param {Array.<string | number>} path
|
||||
* @return {* | undefined} Returns the field when found, or undefined when the
|
||||
* path doesn't exist
|
||||
*/
|
||||
export function getIn (object, path) {
|
||||
let value = object
|
||||
let i = 0
|
||||
|
||||
while(i < path.length) {
|
||||
if (Array.isArray(value) || isObject(value)) {
|
||||
value = value[path[i]]
|
||||
}
|
||||
else {
|
||||
value = undefined
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// TODO: unit test setIn
|
||||
|
||||
/**
|
||||
* helper function to replace a nested property in an object with a new value
|
||||
* without mutating the object itself.
|
||||
*
|
||||
* @param {Object | Array} object
|
||||
* @param {Array.<string | number>} path
|
||||
* @param {*} value
|
||||
* @return {Object | Array} Returns a new, updated object or array
|
||||
*/
|
||||
export function setIn (object, path, value) {
|
||||
if (path.length === 0) {
|
||||
return value
|
||||
}
|
||||
|
||||
const key = path[0]
|
||||
let updated
|
||||
if (typeof key === 'string' && !isObject(object)) {
|
||||
updated = {} // change into an object
|
||||
}
|
||||
else if (typeof key === 'number' && !Array.isArray(object)) {
|
||||
updated = [] // change into an array
|
||||
}
|
||||
else {
|
||||
updated = clone(object)
|
||||
}
|
||||
|
||||
updated[key] = setIn(updated[key], path.slice(1), value)
|
||||
|
||||
return updated
|
||||
}
|
||||
|
||||
// TODO: unit test updateIn
|
||||
|
||||
/**
|
||||
* helper function to replace a nested property in an object with a new value
|
||||
* without mutating the object itself.
|
||||
*
|
||||
* @param {Object | Array} object
|
||||
* @param {Array.<string | number>} path
|
||||
* @param {function} callback
|
||||
* @return {Object | Array} Returns a new, updated object or array
|
||||
*/
|
||||
export function updateIn (object, path, callback) {
|
||||
if (path.length === 0) {
|
||||
return callback(object)
|
||||
}
|
||||
|
||||
const key = path[0]
|
||||
let updated
|
||||
if (typeof key === 'string' && !isObject(object)) {
|
||||
updated = {} // change into an object
|
||||
}
|
||||
else if (typeof key === 'number' && !Array.isArray(object)) {
|
||||
updated = [] // change into an array
|
||||
}
|
||||
else {
|
||||
updated = clone(object)
|
||||
}
|
||||
|
||||
updated[key] = updateIn(updated[key], path.slice(1), callback)
|
||||
|
||||
return updated
|
||||
}
|
||||
|
||||
// TODO: unit test deleteIn
|
||||
|
||||
/**
|
||||
* helper function to delete a nested property in an object
|
||||
* without mutating the object itself.
|
||||
*
|
||||
* @param {Object | Array} object
|
||||
* @param {Array.<string | number>} path
|
||||
* @return {Object | Array} Returns a new, updated object or array
|
||||
*/
|
||||
export function deleteIn (object, path) {
|
||||
if (path.length === 0) {
|
||||
return object
|
||||
}
|
||||
|
||||
if (path.length === 1) {
|
||||
const key = path[0]
|
||||
const updated = clone(object)
|
||||
if (Array.isArray(updated)) {
|
||||
updated.splice(key, 1)
|
||||
}
|
||||
else {
|
||||
delete updated[key]
|
||||
}
|
||||
|
||||
return updated
|
||||
}
|
||||
|
||||
const key = path[0]
|
||||
const child = object[key]
|
||||
if (Array.isArray(child) || isObject(child)) {
|
||||
const updated = clone(object)
|
||||
updated[key] = deleteIn(child, path.slice(1))
|
||||
return updated
|
||||
}
|
||||
else {
|
||||
// child property doesn't exist. just do nothing
|
||||
return object
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue