Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
N
nfkit
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
nfkit
Commits
01a1dc19
Commit
01a1dc19
authored
Dec 09, 2025
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add observe-diff
parent
1578b0cc
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
151 additions
and
0 deletions
+151
-0
index.ts
index.ts
+1
-0
src/observe-diff.ts
src/observe-diff.ts
+30
-0
tests/observe-diff.spec.ts
tests/observe-diff.spec.ts
+120
-0
No files found.
index.ts
View file @
01a1dc19
...
...
@@ -7,3 +7,4 @@ export * from './src/types';
export
*
from
'
./src/middleware-dispatcher
'
;
export
*
from
'
./src/i18n
'
;
export
*
from
'
./src/patch-string-in-object
'
;
export
*
from
'
./src/observe-diff
'
;
src/observe-diff.ts
0 → 100644
View file @
01a1dc19
export
const
observeDiff
=
<
T
>
(
obj
:
T
,
cb
:
<
K
extends
keyof
T
>
(
change
:
{
type
:
'
add
'
|
'
update
'
|
'
delete
'
;
key
:
K
;
oldValue
:
T
[
K
]
|
undefined
;
newValue
:
T
[
K
]
|
undefined
;
})
=>
any
,
):
T
=>
{
return
new
Proxy
(
obj
as
any
,
{
set
(
target
,
key
:
string
|
symbol
,
value
)
{
const
oldValue
=
target
[
key
];
const
type
=
Object
.
prototype
.
hasOwnProperty
.
call
(
target
,
key
)
?
'
update
'
:
'
add
'
;
target
[
key
]
=
value
;
cb
({
type
,
key
:
key
as
any
,
oldValue
,
newValue
:
value
});
return
true
;
},
deleteProperty
(
target
,
key
:
string
|
symbol
)
{
if
(
Object
.
prototype
.
hasOwnProperty
.
call
(
target
,
key
))
{
const
oldValue
=
target
[
key
];
delete
target
[
key
];
cb
({
type
:
'
delete
'
,
key
:
key
as
any
,
oldValue
,
newValue
:
undefined
});
return
true
;
}
return
false
;
},
});
};
tests/observe-diff.spec.ts
0 → 100644
View file @
01a1dc19
// observe-diff.spec.ts
import
{
observeDiff
}
from
'
../src/observe-diff
'
;
interface
TestObj
{
foo
?:
number
|
null
;
bar
?:
string
|
null
;
}
type
Change
<
T
>
=
Parameters
<
typeof
observeDiff
<
T
>>
[
1
]
extends
(
change
:
infer
C
,
)
=>
any
?
C
:
never
;
describe
(
'
observeDiff
'
,
()
=>
{
it
(
'
should emit update when existing key is changed
'
,
()
=>
{
const
obj
:
TestObj
=
{
foo
:
1
,
bar
:
'
x
'
};
const
changes
:
Change
<
TestObj
>
[]
=
[];
const
proxy
=
observeDiff
(
obj
,
(
c
)
=>
changes
.
push
(
c
));
proxy
.
foo
=
2
;
proxy
.
bar
=
'
y
'
;
expect
(
changes
).
toEqual
([
{
type
:
'
update
'
,
key
:
'
foo
'
,
oldValue
:
1
,
newValue
:
2
,
},
{
type
:
'
update
'
,
key
:
'
bar
'
,
oldValue
:
'
x
'
,
newValue
:
'
y
'
,
},
]);
});
it
(
'
should emit add when new key is assigned
'
,
()
=>
{
const
obj
=
{}
as
TestObj
;
const
changes
:
Change
<
TestObj
>
[]
=
[];
const
proxy
=
observeDiff
(
obj
,
(
c
)
=>
changes
.
push
(
c
));
proxy
.
foo
=
123
;
proxy
.
bar
=
'
abc
'
;
expect
(
changes
).
toEqual
([
{
type
:
'
add
'
,
key
:
'
foo
'
,
oldValue
:
undefined
,
newValue
:
123
,
},
{
type
:
'
add
'
,
key
:
'
bar
'
,
oldValue
:
undefined
,
newValue
:
'
abc
'
,
},
]);
});
it
(
'
should emit delete when property is deleted
'
,
()
=>
{
const
obj
:
TestObj
=
{
foo
:
1
,
bar
:
'
x
'
};
const
changes
:
Change
<
TestObj
>
[]
=
[];
const
proxy
=
observeDiff
(
obj
,
(
c
)
=>
changes
.
push
(
c
));
delete
proxy
.
foo
;
delete
proxy
.
bar
;
expect
(
changes
).
toEqual
([
{
type
:
'
delete
'
,
key
:
'
foo
'
,
oldValue
:
1
,
newValue
:
undefined
,
},
{
type
:
'
delete
'
,
key
:
'
bar
'
,
oldValue
:
'
x
'
,
newValue
:
undefined
,
},
]);
});
it
(
'
should track multiple changes on the same key in order
'
,
()
=>
{
const
obj
:
TestObj
=
{
foo
:
1
};
const
changes
:
Change
<
TestObj
>
[]
=
[];
const
proxy
=
observeDiff
(
obj
,
(
c
)
=>
changes
.
push
(
c
));
proxy
.
foo
=
2
;
proxy
.
foo
=
3
;
delete
proxy
.
foo
;
expect
(
changes
).
toEqual
([
{
type
:
'
update
'
,
key
:
'
foo
'
,
oldValue
:
1
,
newValue
:
2
,
},
{
type
:
'
update
'
,
key
:
'
foo
'
,
oldValue
:
2
,
newValue
:
3
,
},
{
type
:
'
delete
'
,
key
:
'
foo
'
,
oldValue
:
3
,
newValue
:
undefined
,
},
]);
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment