Closed holyjak closed 8 years ago
RegExp
is immutable by default as far as I understand it.
var a = new RegExp('a')
a.flags
// ""
a.flags = 'g'
// "g"
a.flags
// "" (still empty)
a.source
// "a"
a.source = 'b'
// "b"
a.source
// "a" (not modified)
This issue is also present with Error
s - which is how I found my way here.
Perhaps a solution like that in #31?
I just looked into this in more depth and I realized it should be closed as a duplicate of https://github.com/rtfeldman/seamless-immutable/issues/124
@rtfeldman So this is duplicate of #124 but that was closed with "won't fix" with the explanation that Immutable should only store plain old data - i.e. that regular expressions, Errors etc. can not be included in an Immutable data structure. Correct?
Perhaps, if that is the case, it should be clearly documented in the README and, even better, an exception should be thrown when trying to include any other then supported type of data (i.e. fail quickly and with a clear error message then later with a mysterious error). What do you think?
Also, according to the README, it is possible to include "mutable" elements of type Date, function, React component. That provides a simple workaround: instead of using prop: RegExp(..)
, I could use () => new RegExp()
. But wouldn't it be even better to expand this and allow the user to define what "mutable" data can be part of an Immutable data structure? Something like I proposed in 4.
above or something similar (a list of types/paths to leave as they are ...).
Thoughts?
I expect these two pieces of code to yield the same result:
but they do not.
Ideally, seamless-immutable should not break regular expressions. If that is not possible because they are inherently mutable (as I suspect) that it should perhaps not just silently break them.
Possible courses of action (not complete, not exclusive):
https.Agent
)Immutable(objWithRegExps, { "RegExp": (v) => v /* return unmodified, i.e. mutable*/})
and then, somewhere in the code,if (customConverters[current.constructor.name]) return customConverters[current.constructor.name](current);
Thoughts / advice?