Remove support for Symbol from immutabilityHelpers

This commit is contained in:
josdejong 2020-05-22 10:59:04 +02:00
parent 8392c57a2d
commit e412628b89
1 changed files with 12 additions and 29 deletions

View File

@ -13,33 +13,17 @@ import { isObjectOrArray } from './typeUtils.js'
/** /**
* Shallow clone of an Object, Array, or value * Shallow clone of an Object, Array, or value
* Also copies any symbols on the Objects and Arrays
* @param {*} value * @param {*} value
* @return {*} * @return {*}
*/ */
export function shallowCloneWithSymbols (value) { export function shallowClone (value) {
if (Array.isArray(value)) { if (Array.isArray(value)) {
// copy array items // copy array items
let arr = value.slice() return value.slice()
// copy all symbols
Object.getOwnPropertySymbols(value).forEach(symbol => arr[symbol] = value[symbol])
return arr
} }
else if (typeof value === 'object') { else if (typeof value === 'object') {
// copy properties // copy object properties
let obj = {} return { ...value }
for (let prop in value) {
if (value.hasOwnProperty(prop)) {
obj[prop] = value[prop]
}
}
// copy all symbols
Object.getOwnPropertySymbols(value).forEach(symbol => obj[symbol] = value[symbol])
return obj
} }
else { else {
return value return value
@ -97,7 +81,7 @@ export function setIn (object, path, value) {
return object return object
} }
else { else {
const updatedObject = shallowCloneWithSymbols(object) const updatedObject = shallowClone(object)
updatedObject[key] = updatedValue updatedObject[key] = updatedValue
return updatedObject return updatedObject
} }
@ -129,7 +113,7 @@ export function updateIn (object, path, callback) {
return object return object
} }
else { else {
const updatedObject = shallowCloneWithSymbols(object) const updatedObject = shallowClone(object)
updatedObject[key] = updatedValue updatedObject[key] = updatedValue
return updatedObject return updatedObject
} }
@ -159,9 +143,9 @@ export function deleteIn (object, path) {
return object return object
} }
else { else {
const updatedObject = shallowCloneWithSymbols(object) const updatedObject = shallowClone(object)
if (Array.isArray(updatedObject) && typeof key !== 'symbol') { if (Array.isArray(updatedObject)) {
updatedObject.splice(key, 1) updatedObject.splice(key, 1)
} }
else { else {
@ -179,7 +163,7 @@ export function deleteIn (object, path) {
return object return object
} }
else { else {
const updatedObject = shallowCloneWithSymbols(object) const updatedObject = shallowClone(object)
updatedObject[key] = updatedValue updatedObject[key] = updatedValue
return updatedObject return updatedObject
} }
@ -205,7 +189,7 @@ export function insertAt (object, path, value) {
throw new TypeError('Array expected at path ' + JSON.stringify(parentPath)) throw new TypeError('Array expected at path ' + JSON.stringify(parentPath))
} }
const updatedItems = shallowCloneWithSymbols(items) const updatedItems = shallowClone(items)
updatedItems.splice(index, 0, value) updatedItems.splice(index, 0, value)
return updatedItems return updatedItems
@ -215,7 +199,6 @@ export function insertAt (object, path, value) {
/** /**
* Transform a JSON object, traverse over the whole object, * Transform a JSON object, traverse over the whole object,
* and allow replacing Objects/Arrays/values. * and allow replacing Objects/Arrays/values.
* Does not iterate over symbols.
* @param {JSON} json * @param {JSON} json
* @param {function (json: JSON, path: Path) : JSON} callback * @param {function (json: JSON, path: Path) : JSON} callback
* @param {Path} [path] * @param {Path} [path]
@ -235,7 +218,7 @@ export function transform (json, callback, path = []) {
const after = transform(before, callback, path.concat(i + '')) const after = transform(before, callback, path.concat(i + ''))
if (after !== before) { if (after !== before) {
if (!updated2) { if (!updated2) {
updated2 = shallowCloneWithSymbols(updated1) updated2 = shallowClone(updated1)
} }
updated2[i] = after updated2[i] = after
} }
@ -252,7 +235,7 @@ export function transform (json, callback, path = []) {
const after = transform(before, callback, path.concat(key)) const after = transform(before, callback, path.concat(key))
if (after !== before) { if (after !== before) {
if (!updated2) { if (!updated2) {
updated2 = shallowCloneWithSymbols(updated1) updated2 = shallowClone(updated1)
} }
updated2[key] = after updated2[key] = after
} }