-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as test from "tape"; | ||
import assoc from "./"; | ||
|
||
test("assoc", t => { | ||
t.throws( | ||
() => assoc("d", 1, undefined), | ||
"should throw error if object is undefined" | ||
); | ||
|
||
t.equals(assoc("d", 1, {}).d, 1, "should set the value to a key in object"); | ||
|
||
const o = {}; | ||
t.notEquals(assoc("d", 1, o), o, "should not mutate original object"); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import curry from "../curry"; | ||
import objectDeepClone from "../deepClone"; | ||
|
||
function assoc<A>(key: string, val: A, obj: any): any { | ||
if (!obj || typeof obj !== "object") | ||
throw new Error("assoc: expects an object"); | ||
const o = objectDeepClone(obj); | ||
o[key] = val; | ||
return o; | ||
} | ||
export default curry(assoc); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as test from "tape"; | ||
import deepClone from "./"; | ||
|
||
test("deep clone", t => { | ||
t.equals(deepClone(1), 1, "should return value if type is not object"); | ||
const o1 = { d: 1 }; | ||
t.notEquals(deepClone(o1), o1, "should not mutate original object"); | ||
const s = { p: 1 }; | ||
const o2 = { d: s }; | ||
t.notEquals( | ||
deepClone(o2).d, | ||
o2.d, | ||
"returned value should not be deep equal to original value" | ||
); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default function objectDeepClone(o: any) { | ||
if (o == null || typeof o !== "object") return o; | ||
const clone: any = Array.isArray(o) ? [] : {}; | ||
Object.keys(o).map(k => { | ||
if (o.hasOwnProperty(k)) { | ||
clone[k] = objectDeepClone(o[k]); | ||
} | ||
}); | ||
|
||
return clone; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters