Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
赵勇刚
immutability-helper
Commits
989b4b6c
Commit
989b4b6c
authored
8 years ago
by
Moshe Kolodny
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add $unset directive
parent
f0de94a9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
2 deletions
+48
-2
index.js
index.js
+17
-0
package.json
package.json
+1
-1
test.js
test.js
+30
-1
No files found.
index.js
View file @
989b4b6c
...
...
@@ -98,6 +98,23 @@ var defaultCommands = {
invariantSet
(
spec
);
return
value
;
},
$unset
:
function
(
value
,
nextObject
,
spec
,
object
)
{
invariant
(
Array
.
isArray
(
value
),
'
update(): expected spec of $unset to be an array; got %s.
'
+
'
Did you forget to wrap the key(s) in an array?
'
,
value
);
var
originalValue
=
nextObject
;
for
(
var
i
=
0
;
i
<
value
.
length
;
i
++
)
{
var
key
=
value
[
i
];
if
(
Object
.
hasOwnProperty
.
call
(
originalValue
,
key
))
{
originalValue
=
nextObject
===
object
?
copy
(
object
)
:
nextObject
;
delete
originalValue
[
key
];
}
}
return
originalValue
;
},
$merge
:
function
(
value
,
nextObject
,
spec
,
object
)
{
var
originalValue
=
nextObject
===
object
?
copy
(
object
)
:
nextObject
;
invariantMerge
(
originalValue
,
value
);
...
...
This diff is collapsed.
Click to expand it.
package.json
View file @
989b4b6c
{
"name"
:
"immutability-helper"
,
"version"
:
"2.
1.2
"
,
"version"
:
"2.
2.0
"
,
"description"
:
"mutate a copy of data without changing the original source"
,
"main"
:
"index.js"
,
"scripts"
:
{
...
...
This diff is collapsed.
Click to expand it.
test.js
View file @
989b4b6c
...
...
@@ -110,6 +110,28 @@ describe('update', function() {
});
});
describe
(
'
$unset
'
,
function
()
{
it
(
'
unsets
'
,
function
()
{
expect
(
update
({
a
:
'
b
'
},
{
$unset
:
[
'
a
'
]}).
a
).
toBe
(
undefined
);
});
it
(
'
removes the key from the object
'
,
function
()
{
var
removed
=
update
({
a
:
'
b
'
},
{
$unset
:
[
'
a
'
]});
expect
(
'
a
'
in
removed
).
toBe
(
false
);
});
it
(
'
does not remove keys from the inherited properties
'
,
function
()
{
function
Parent
()
{
this
.
foo
=
'
Parent
'
;
}
function
Child
()
{}
Child
.
prototype
=
new
Parent
()
var
child
=
new
Child
();
expect
(
update
(
child
,
{
$unset
:
[
'
foo
'
]}).
foo
).
toEqual
(
'
Parent
'
);
});
it
(
'
keeps reference equality when possible
'
,
function
()
{
var
original
=
{
a
:
1
};
expect
(
update
(
original
,
{
$unset
:
[
'
b
'
]})).
toBe
(
original
);
expect
(
update
(
original
,
{
$unset
:
[
'
a
'
]})).
toNotBe
(
original
);
});
});
describe
(
'
$apply
'
,
function
()
{
var
applier
=
function
(
node
)
{
return
{
v
:
node
.
v
*
2
};
...
...
@@ -233,6 +255,13 @@ describe('update', function() {
});
});
it
(
'
should reject non arrays from $unset
'
,
function
()
{
expect
(
update
.
bind
(
null
,
{
a
:
'
b
'
},
{
$unset
:
'
a
'
})).
toThrow
(
'
update(): expected spec of $unset to be an array; got a.
'
+
'
Did you forget to wrap the key(s) in an array?
'
);
});
it
(
'
should require a plain object spec containing command(s)
'
,
function
()
{
var
specs
=
[
null
,
...
...
@@ -244,7 +273,7 @@ describe('update', function() {
expect
(
update
.
bind
(
null
,
{
a
:
'
b
'
},
spec
)).
toThrow
(
'
update(): You provided an invalid spec to update(). The spec
'
+
'
and every included key path must be plain objects containing one
'
+
'
of the following commands: $push, $unshift, $splice, $set,
'
+
'
of the following commands: $push, $unshift, $splice, $set,
$unset,
'
+
'
$merge, $apply.
'
);
});
...
...
This diff is collapsed.
Click to expand it.
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