Skip to content

Commit 502f02e

Browse files
committed
feature: updateWithKey(f, k, o).
updates the value if 'k' key is in the object. the function 'f' will receive the pair (k, v) as arguments.
1 parent 2588ddc commit 502f02e

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

readme.md

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ Simple objects utilities.
88
update(f : Any -> Any, k : String, o : Object) -> Object
99

1010
// Update the value of key 'k' if present.
11+
12+
13+
updateWithKey(f : (String, Any) -> Any, k : String, o : Object) -> Object
14+
15+
// Update the value of key 'k' if present.
16+
// Function 'f' will receive the pair (k, v) and must return the new value of 'k'.
1117
```
1218

1319
## license

src/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ export function update(f, k, o) {
33
o[k] = f(o[k]);
44
return o;
55
}
6+
7+
export function updateWithKey(f, k, o) {
8+
if (!o[k]) return o;
9+
o[k] = f(k, o[k]);
10+
return o;
11+
}

tests/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ describe("objects", () => {
88
c: 1
99
});
1010
});
11+
12+
it("updateWithKey(f, k, o)", () => {
13+
const obj = { a: "b", c: 1 };
14+
O.updateWithKey((k, v) => k + v, "a", obj).should.be.eql({
15+
a: "ab",
16+
c: 1
17+
});
18+
});
1119
});

0 commit comments

Comments
 (0)