1 | import assocPath from 'ramda/src/assocPath'
|
2 | import clone from './utils/clone'
|
3 | import path from 'ramda/src/path'
|
4 |
|
5 | class Map {
|
6 | constructor (obj) {
|
7 | if (typeof obj !== 'object') {
|
8 | throw new TypeError(obj, 'is not an object')
|
9 | }
|
10 |
|
11 | this.obj = obj
|
12 | this['@@Plait/Map'] = 1
|
13 | }
|
14 |
|
15 | clone () {
|
16 | return new Map(this.toObject())
|
17 | }
|
18 |
|
19 | toObject () {
|
20 | return clone(this.obj)
|
21 | }
|
22 |
|
23 | set (prop, val) {
|
24 | const obj = this.toObject()
|
25 |
|
26 | obj[prop] = val
|
27 |
|
28 | return new Map(obj)
|
29 | }
|
30 |
|
31 | get (prop) {
|
32 | const obj = this.toObject()
|
33 |
|
34 | return obj[prop]
|
35 | }
|
36 |
|
37 | update (prop, updater) {
|
38 | return this.set(prop, updater(this.get(prop)))
|
39 | }
|
40 |
|
41 | setIn(propPath, val) {
|
42 | const obj = assocPath(propPath, val, this.obj)
|
43 |
|
44 | return new Map(obj)
|
45 | }
|
46 |
|
47 | getIn(propPath) {
|
48 | return path(propPath, this.obj)
|
49 | }
|
50 | }
|
51 |
|
52 | export default Map
|