Skip to content

feat: add isModified helper - #1328

Open
saadkhantareen wants to merge 1 commit into
immerjs:mainfrom
saadkhantareen:feat/is-modified
Open

saadkhantareen wants to merge 1 commit into
immerjs:mainfrom
saadkhantareen:feat/is-modified

Conversation

@saadkhantareen

Copy link
Copy Markdown

Summary

Adds a public isModified(value) helper, as proposed in #1153. Inside a recipe there is currently no cheap way to ask whether a draft subtree was changed: the available workarounds either snapshot the draft with current() and compare it against original(), or finalize and re-create the draft. isModified answers the question directly from the draft's internal state.

produce(base, draft => {
	performOpaqueUpdates(draft)
	if (isModified(draft.item)) {
		draft.item.modifiedOn = new Date()
	}
})

Changes

  • src/utils/common.ts: implement isModified. It returns the draft state's modified_ flag, which markChanged already propagates from a modified nested draft to all of its parent drafts, so modifications anywhere inside the given draft are reported. Values that are not drafts return false.
  • src/immer.ts: export isModified from the public entry point.
  • src/types/index.js.flow: add the Flow declaration next to isDraft.
  • website/docs/api.md: add isModified to the API overview table.
  • __tests__/isModified.js: new tests covering non-draft values, unmodified drafts, direct property writes, additions and deletions, same-value assignments (not a modification), nested drafts and untouched siblings, arrays, manual createDraft/finishDraft drafts, Map and Set drafts, and the derived-data use case from isModified helper #1153.

Tests

  • npx vitest run __tests__/isModified.js --maxWorkers=2 — 1 file passed, 10 tests passed.
  • npx vitest run __tests__/current.js __tests__/original.js __tests__/base.js --maxWorkers=2 — 3 files passed, 3367 tests passed.
  • npx tsc -p tsconfig.json — no diagnostics in src/; the only diagnostics reported are inside node_modules/@types/node and are unchanged by this change.

Fixes #1153

Copilot AI balanced review requested due to automatic review settings October 15, 2026 10:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

isModified helper

2 participants