Skip to content

Commit

Permalink
Add RegExp support to immutability middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Jan 28, 2023
1 parent b27aa9b commit 0fab520
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs/api/immutabilityMiddleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ interface ImmutableStateInvariantMiddlewareOptions {
*/
isImmutable?: IsImmutableFunc
/**
An array of dot-separated path strings that match named nodes from
An array of dot-separated path strings or RegExps that match named nodes from
the root state to ignore when checking for immutability.
Defaults to undefined
*/
ignoredPaths?: string[]
ignoredPaths?: (string | RegExp)[]
/** Print a warning if checks take longer than N ms. Default: 32ms */
warnAfter?: number
// @deprecated. Use ignoredPaths
Expand Down
29 changes: 20 additions & 9 deletions packages/toolkit/src/immutableStateInvariantMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function isImmutableDefault(value: unknown): boolean {

export function trackForMutations(
isImmutable: IsImmutableFunc,
ignorePaths: string[] | undefined,
ignorePaths: IgnorePaths | undefined,
obj: any
) {
const trackedProperties = trackProperties(isImmutable, ignorePaths, obj)
Expand Down Expand Up @@ -116,11 +116,11 @@ function trackProperties(
return tracked as TrackedProperty
}

type IgnorePaths = readonly string[]
type IgnorePaths = readonly (string | RegExp)[]

function detectMutations(
isImmutable: IsImmutableFunc,
ignorePaths: IgnorePaths = [],
ignoredPaths: IgnorePaths = [],
trackedProperty: TrackedProperty,
obj: any,
sameParentRef: boolean = false,
Expand All @@ -147,19 +147,30 @@ function detectMutations(
keysToDetect[key] = true
}

const hasIgnoredPaths = ignoredPaths.length > 0

for (let key in keysToDetect) {
const childPath = path ? path + '.' + key : key
if (ignorePaths.length && ignorePaths.indexOf(childPath) !== -1) {
continue
const nestedPath = path ? path + '.' + key : key

if (hasIgnoredPaths) {
const hasMatches = ignoredPaths.some((ignored) => {
if (ignored instanceof RegExp) {
return ignored.test(nestedPath)
}
return nestedPath === ignored
})
if (hasMatches) {
continue
}
}

const result = detectMutations(
isImmutable,
ignorePaths,
ignoredPaths,
trackedProperty.children[key],
obj[key],
sameRef,
childPath
nestedPath
)

if (result.wasMutated) {
Expand Down Expand Up @@ -189,7 +200,7 @@ export interface ImmutableStateInvariantMiddlewareOptions {
the root state to ignore when checking for immutability.
Defaults to undefined
*/
ignoredPaths?: string[]
ignoredPaths?: IgnorePaths
/** Print a warning if checks take longer than N ms. Default: 32ms */
warnAfter?: number
// @deprecated. Use ignoredPaths
Expand Down
6 changes: 4 additions & 2 deletions packages/toolkit/src/serializableStateInvariantMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ interface NonSerializableValue {
value: unknown
}

type IgnorePaths = readonly (string | RegExp)[]

/**
* @public
*/
Expand All @@ -36,7 +38,7 @@ export function findNonSerializableValue(
path: string = '',
isSerializable: (value: unknown) => boolean = isPlain,
getEntries?: (value: unknown) => [string, any][],
ignoredPaths: readonly (string | RegExp)[] = []
ignoredPaths: IgnorePaths = []
): NonSerializableValue | false {
let foundNestedSerializable: NonSerializableValue | false

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

if (hasIgnoredPaths) {
const hasMatches = ignoredPaths.some(ignored => {
const hasMatches = ignoredPaths.some((ignored) => {
if (ignored instanceof RegExp) {
return ignored.test(nestedPath)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,16 @@ describe('createImmutableStateInvariantMiddleware', () => {
return action
}

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

expect(() => {
dispatch({ type: 'SOME_ACTION' })
dispatch1({ type: 'SOME_ACTION' })
}).not.toThrow()

const dispatch2 = middleware({ ignoredPaths: [/^foo/] })(next)

expect(() => {
dispatch2({ type: 'SOME_ACTION' })
}).not.toThrow()
})

Expand Down

0 comments on commit 0fab520

Please sign in to comment.