form-diff-lite 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +37 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +86 -0
- package/dist/index.mjs +58 -0
- package/package.json +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tejas Kadam
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# form-diff-lite
|
|
2
|
+
|
|
3
|
+
Dirty-field tracking and structured diffing for form state — compares initial vs current values field by field, nested objects included.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install form-diff-lite
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { dirtyFields, isDirty, buildPatch } from 'form-diff-lite';
|
|
15
|
+
|
|
16
|
+
const initial = { name: 'Ann', address: { city: 'NYC', zip: '10001' } };
|
|
17
|
+
const current = { name: 'Ann', address: { city: 'LA', zip: '10001' } };
|
|
18
|
+
|
|
19
|
+
isDirty(initial, current); // true
|
|
20
|
+
dirtyFields(initial, current); // ['address.city']
|
|
21
|
+
buildPatch(initial, current); // { address: { city: 'LA' } }
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Why form-diff-lite
|
|
25
|
+
|
|
26
|
+
Knowing exactly which fields a user actually changed — not just "is the form dirty" — is what lets you send a minimal PATCH request, show a targeted "unsaved changes" list, or warn about specific fields before navigating away. form-diff-lite walks two same-shaped objects recursively (following nested plain objects, deep-comparing arrays), and gives you the changed leaf paths, a dirty flag, or a nested patch object containing only what changed — no form-library dependency, works with any state shape.
|
|
27
|
+
|
|
28
|
+
## API
|
|
29
|
+
|
|
30
|
+
- `diffFields(before, after)` — returns `{ path, before, after }` for every changed leaf field.
|
|
31
|
+
- `dirtyFields(before, after)` — just the changed field paths.
|
|
32
|
+
- `isDirty(before, after)` — true if anything changed.
|
|
33
|
+
- `buildPatch(before, after)` — a nested object containing only the changed values.
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
interface FieldChange {
|
|
2
|
+
path: string;
|
|
3
|
+
before: unknown;
|
|
4
|
+
after: unknown;
|
|
5
|
+
}
|
|
6
|
+
/** Walks two objects field-by-field (recursing into nested plain objects) and lists changed leaf paths. */
|
|
7
|
+
declare function diffFields<T extends Record<string, unknown>>(before: T, after: T, prefix?: string): FieldChange[];
|
|
8
|
+
/** Returns just the set of dirty (changed) top-level-or-nested field paths. */
|
|
9
|
+
declare function dirtyFields<T extends Record<string, unknown>>(before: T, after: T): string[];
|
|
10
|
+
/** True if any field differs between before and after. */
|
|
11
|
+
declare function isDirty<T extends Record<string, unknown>>(before: T, after: T): boolean;
|
|
12
|
+
/** Builds a partial "patch" object containing only the changed leaf values, at their original nesting. */
|
|
13
|
+
declare function buildPatch<T extends Record<string, unknown>>(before: T, after: T): Record<string, unknown>;
|
|
14
|
+
|
|
15
|
+
export { type FieldChange, buildPatch, diffFields, dirtyFields, isDirty };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
interface FieldChange {
|
|
2
|
+
path: string;
|
|
3
|
+
before: unknown;
|
|
4
|
+
after: unknown;
|
|
5
|
+
}
|
|
6
|
+
/** Walks two objects field-by-field (recursing into nested plain objects) and lists changed leaf paths. */
|
|
7
|
+
declare function diffFields<T extends Record<string, unknown>>(before: T, after: T, prefix?: string): FieldChange[];
|
|
8
|
+
/** Returns just the set of dirty (changed) top-level-or-nested field paths. */
|
|
9
|
+
declare function dirtyFields<T extends Record<string, unknown>>(before: T, after: T): string[];
|
|
10
|
+
/** True if any field differs between before and after. */
|
|
11
|
+
declare function isDirty<T extends Record<string, unknown>>(before: T, after: T): boolean;
|
|
12
|
+
/** Builds a partial "patch" object containing only the changed leaf values, at their original nesting. */
|
|
13
|
+
declare function buildPatch<T extends Record<string, unknown>>(before: T, after: T): Record<string, unknown>;
|
|
14
|
+
|
|
15
|
+
export { type FieldChange, buildPatch, diffFields, dirtyFields, isDirty };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
buildPatch: () => buildPatch,
|
|
24
|
+
diffFields: () => diffFields,
|
|
25
|
+
dirtyFields: () => dirtyFields,
|
|
26
|
+
isDirty: () => isDirty
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
function isPlainObject(value) {
|
|
30
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
31
|
+
}
|
|
32
|
+
function valuesEqual(a, b) {
|
|
33
|
+
if (a === b) return true;
|
|
34
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
35
|
+
return a.length === b.length && a.every((v, i) => valuesEqual(v, b[i]));
|
|
36
|
+
}
|
|
37
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
38
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
39
|
+
return [...keys].every((k) => valuesEqual(a[k], b[k]));
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
function diffFields(before, after, prefix = "") {
|
|
44
|
+
const changes = [];
|
|
45
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(before ?? {}), ...Object.keys(after ?? {})]);
|
|
46
|
+
for (const key of keys) {
|
|
47
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
48
|
+
const beforeVal = before?.[key];
|
|
49
|
+
const afterVal = after?.[key];
|
|
50
|
+
if (isPlainObject(beforeVal) && isPlainObject(afterVal)) {
|
|
51
|
+
changes.push(...diffFields(beforeVal, afterVal, path));
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (!valuesEqual(beforeVal, afterVal)) {
|
|
55
|
+
changes.push({ path, before: beforeVal, after: afterVal });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return changes;
|
|
59
|
+
}
|
|
60
|
+
function dirtyFields(before, after) {
|
|
61
|
+
return diffFields(before, after).map((c) => c.path);
|
|
62
|
+
}
|
|
63
|
+
function isDirty(before, after) {
|
|
64
|
+
return diffFields(before, after).length > 0;
|
|
65
|
+
}
|
|
66
|
+
function buildPatch(before, after) {
|
|
67
|
+
const patch = {};
|
|
68
|
+
for (const change of diffFields(before, after)) {
|
|
69
|
+
const parts = change.path.split(".");
|
|
70
|
+
let cursor = patch;
|
|
71
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
72
|
+
const key = parts[i];
|
|
73
|
+
if (!isPlainObject(cursor[key])) cursor[key] = {};
|
|
74
|
+
cursor = cursor[key];
|
|
75
|
+
}
|
|
76
|
+
cursor[parts[parts.length - 1]] = change.after;
|
|
77
|
+
}
|
|
78
|
+
return patch;
|
|
79
|
+
}
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
buildPatch,
|
|
83
|
+
diffFields,
|
|
84
|
+
dirtyFields,
|
|
85
|
+
isDirty
|
|
86
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function isPlainObject(value) {
|
|
3
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
function valuesEqual(a, b) {
|
|
6
|
+
if (a === b) return true;
|
|
7
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
8
|
+
return a.length === b.length && a.every((v, i) => valuesEqual(v, b[i]));
|
|
9
|
+
}
|
|
10
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
11
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
12
|
+
return [...keys].every((k) => valuesEqual(a[k], b[k]));
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
function diffFields(before, after, prefix = "") {
|
|
17
|
+
const changes = [];
|
|
18
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(before ?? {}), ...Object.keys(after ?? {})]);
|
|
19
|
+
for (const key of keys) {
|
|
20
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
21
|
+
const beforeVal = before?.[key];
|
|
22
|
+
const afterVal = after?.[key];
|
|
23
|
+
if (isPlainObject(beforeVal) && isPlainObject(afterVal)) {
|
|
24
|
+
changes.push(...diffFields(beforeVal, afterVal, path));
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (!valuesEqual(beforeVal, afterVal)) {
|
|
28
|
+
changes.push({ path, before: beforeVal, after: afterVal });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return changes;
|
|
32
|
+
}
|
|
33
|
+
function dirtyFields(before, after) {
|
|
34
|
+
return diffFields(before, after).map((c) => c.path);
|
|
35
|
+
}
|
|
36
|
+
function isDirty(before, after) {
|
|
37
|
+
return diffFields(before, after).length > 0;
|
|
38
|
+
}
|
|
39
|
+
function buildPatch(before, after) {
|
|
40
|
+
const patch = {};
|
|
41
|
+
for (const change of diffFields(before, after)) {
|
|
42
|
+
const parts = change.path.split(".");
|
|
43
|
+
let cursor = patch;
|
|
44
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
45
|
+
const key = parts[i];
|
|
46
|
+
if (!isPlainObject(cursor[key])) cursor[key] = {};
|
|
47
|
+
cursor = cursor[key];
|
|
48
|
+
}
|
|
49
|
+
cursor[parts[parts.length - 1]] = change.after;
|
|
50
|
+
}
|
|
51
|
+
return patch;
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
buildPatch,
|
|
55
|
+
diffFields,
|
|
56
|
+
dirtyFields,
|
|
57
|
+
isDirty
|
|
58
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "form-diff-lite",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Dirty-field tracking and structured diffing for form state — compares initial vs current values field by field, nested objects included.",
|
|
5
|
+
"main": "dist/index.js", "module": "dist/index.mjs", "types": "dist/index.d.ts", "files": ["dist"],
|
|
6
|
+
"license": "MIT", "author": "Tejas Kadam",
|
|
7
|
+
"repository": { "type": "git", "url": "git+https://github.com/tejas821/form-diff-lite.git" },
|
|
8
|
+
"keywords": ["forms", "dirty-fields", "diff", "typescript"],
|
|
9
|
+
"scripts": { "build": "tsup src/index.ts --format cjs,esm --dts", "test": "jest", "typecheck": "tsc --noEmit" },
|
|
10
|
+
"devDependencies": { "typescript": "^5.5.4", "tsup": "^8.2.4", "jest": "^29.7.0", "ts-jest": "^29.2.5", "@types/jest": "^29.5.12" }
|
|
11
|
+
}
|