rtfeldman / seamless-immutable

Immutable data structures for JavaScript which are backwards-compatible with normal JS Arrays and Objects.
BSD 3-Clause "New" or "Revised" License
5.37k stars 194 forks source link

Possibility to deactivate helper methods? #170

Closed ghost closed 7 years ago

ghost commented 7 years ago

I really like the project. One thing that I don't find ideal is, that it adds new methods to the object. This way we'd tightly couple our whole app to this lib to perform mutations.

We'd rather just have the benefit of immutable data structure and operate on the data for instance with lodash/ramda/whatever.

In the future, will there be a possibility to deactivate the methods?

theogravity commented 7 years ago

this would be also useful for redux-devtools where the log monitor is also picking up the methods, making the redux log difficult to read through

rtfeldman commented 7 years ago

I'See https://github.com/rtfeldman/seamless-immutable/pull/160

theogravity commented 7 years ago

In the meantime, I've done the following for using seamless-immutable with redux-devtools and redux-devtools-log-monitor (it's not ideal perf-wise, but does the job):

import React from 'react'

import { createDevTools } from 'redux-devtools'
import LogMonitor from 'redux-devtools-log-monitor'
import DockMonitor from 'redux-devtools-dock-monitor'
import MultipleMonitors from 'redux-devtools-multiple-monitors'

// clone without prototype methods so the seamless-immutable methods aren't exposed in the logs
// http://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object
function clone(obj) {
  var copy

  // Handle the 3 simple types, and null or undefined
  if (null == obj || "object" != typeof obj) return obj

  // Handle Date
  if (obj instanceof Date) {
    copy = new Date()
    copy.setTime(obj.getTime())
    return copy
  }

  // Handle Array
  if (obj instanceof Array) {
    copy = []
    for (var i = 0, len = obj.length; i < len; i++) {
      copy[i] = clone(obj[i])
    }
    return copy
  }

  // Handle Object
  if (obj instanceof Object) {
    copy = {}
    for (var attr in obj) {
      if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr])
    }
    return copy
  }
}

export default createDevTools(
  <DockMonitor toggleVisibilityKey='ctrl-h' changePositionKey='ctrl-q' defaultIsVisible={false}>
    <MultipleMonitors>
      <LogMonitor select={clone} />
    </MultipleMonitors>
  </DockMonitor>
)
ghost1542 commented 7 years ago

FWIW, this would be pretty useful to me as well! I am often debugging objects in the console, and the instance methods often make it harder, so I end up using JSON.stringify() which isn't as easily browseable.

ghost1542 commented 7 years ago

This issue can be closed - Fixed as of 7.0.0