Skip to content

Commit 0fab520

Browse files
committed
Add RegExp support to immutability middleware
1 parent b27aa9b commit 0fab520

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

docs/api/immutabilityMiddleware.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ interface ImmutableStateInvariantMiddlewareOptions {
2929
*/
3030
isImmutable?: IsImmutableFunc
3131
/**
32-
An array of dot-separated path strings that match named nodes from
32+
An array of dot-separated path strings or RegExps that match named nodes from
3333
the root state to ignore when checking for immutability.
3434
Defaults to undefined
3535
*/
36-
ignoredPaths?: string[]
36+
ignoredPaths?: (string | RegExp)[]
3737
/** Print a warning if checks take longer than N ms. Default: 32ms */
3838
warnAfter?: number
3939
// @deprecated. Use ignoredPaths

packages/toolkit/src/immutableStateInvariantMiddleware.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function isImmutableDefault(value: unknown): boolean {
7272

7373
export function trackForMutations(
7474
isImmutable: IsImmutableFunc,
75-
ignorePaths: string[] | undefined,
75+
ignorePaths: IgnorePaths | undefined,
7676
obj: any
7777
) {
7878
const trackedProperties = trackProperties(isImmutable, ignorePaths, obj)
@@ -116,11 +116,11 @@ function trackProperties(
116116
return tracked as TrackedProperty
117117
}
118118

119-
type IgnorePaths = readonly string[]
119+
type IgnorePaths = readonly (string | RegExp)[]
120120

121121
function detectMutations(
122122
isImmutable: IsImmutableFunc,
123-
ignorePaths: IgnorePaths = [],
123+
ignoredPaths: IgnorePaths = [],
124124
trackedProperty: TrackedProperty,
125125
obj: any,
126126
sameParentRef: boolean = false,
@@ -147,19 +147,30 @@ function detectMutations(
147147
keysToDetect[key] = true
148148
}
149149

150+
const hasIgnoredPaths = ignoredPaths.length > 0
151+
150152
for (let key in keysToDetect) {
151-
const childPath = path ? path + '.' + key : key
152-
if (ignorePaths.length && ignorePaths.indexOf(childPath) !== -1) {
153-
continue
153+
const nestedPath = path ? path + '.' + key : key
154+
155+
if (hasIgnoredPaths) {
156+
const hasMatches = ignoredPaths.some((ignored) => {
157+
if (ignored instanceof RegExp) {
158+
return ignored.test(nestedPath)
159+
}
160+
return nestedPath === ignored
161+
})
162+
if (hasMatches) {
163+
continue
164+
}
154165
}
155166

156167
const result = detectMutations(
157168
isImmutable,
158-
ignorePaths,
169+
ignoredPaths,
159170
trackedProperty.children[key],
160171
obj[key],
161172
sameRef,
162-
childPath
173+
nestedPath
163174
)
164175

165176
if (result.wasMutated) {
@@ -189,7 +200,7 @@ export interface ImmutableStateInvariantMiddlewareOptions {
189200
the root state to ignore when checking for immutability.
190201
Defaults to undefined
191202
*/
192-
ignoredPaths?: string[]
203+
ignoredPaths?: IgnorePaths
193204
/** Print a warning if checks take longer than N ms. Default: 32ms */
194205
warnAfter?: number
195206
// @deprecated. Use ignoredPaths

packages/toolkit/src/serializableStateInvariantMiddleware.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ interface NonSerializableValue {
2828
value: unknown
2929
}
3030

31+
type IgnorePaths = readonly (string | RegExp)[]
32+
3133
/**
3234
* @public
3335
*/
@@ -36,7 +38,7 @@ export function findNonSerializableValue(
3638
path: string = '',
3739
isSerializable: (value: unknown) => boolean = isPlain,
3840
getEntries?: (value: unknown) => [string, any][],
39-
ignoredPaths: readonly (string | RegExp)[] = []
41+
ignoredPaths: IgnorePaths = []
4042
): NonSerializableValue | false {
4143
let foundNestedSerializable: NonSerializableValue | false
4244

@@ -59,7 +61,7 @@ export function findNonSerializableValue(
5961
const nestedPath = path ? path + '.' + key : key
6062

6163
if (hasIgnoredPaths) {
62-
const hasMatches = ignoredPaths.some(ignored => {
64+
const hasMatches = ignoredPaths.some((ignored) => {
6365
if (ignored instanceof RegExp) {
6466
return ignored.test(nestedPath)
6567
}

packages/toolkit/src/tests/immutableStateInvariantMiddleware.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,16 @@ describe('createImmutableStateInvariantMiddleware', () => {
121121
return action
122122
}
123123

124-
const dispatch = middleware({ ignoredPaths: ['foo.bar'] })(next)
124+
const dispatch1 = middleware({ ignoredPaths: ['foo.bar'] })(next)
125125

126126
expect(() => {
127-
dispatch({ type: 'SOME_ACTION' })
127+
dispatch1({ type: 'SOME_ACTION' })
128+
}).not.toThrow()
129+
130+
const dispatch2 = middleware({ ignoredPaths: [/^foo/] })(next)
131+
132+
expect(() => {
133+
dispatch2({ type: 'SOME_ACTION' })
128134
}).not.toThrow()
129135
})
130136

0 commit comments

Comments
 (0)