1 | import { errorTransform } from '../../transform/utils/errorTransform'
|
2 | import { setSafeProperty } from '../../../utils/customs'
|
3 |
|
4 | export function assignFactory ({ subset, matrix }) {
|
5 | |
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | return function assign (object, index, value) {
|
20 | try {
|
21 | if (Array.isArray(object)) {
|
22 |
|
23 | return matrix(object).subset(index, value).valueOf()
|
24 | } else if (object && typeof object.subset === 'function') {
|
25 | return object.subset(index, value)
|
26 | } else if (typeof object === 'string') {
|
27 |
|
28 | return subset(object, index, value)
|
29 | } else if (typeof object === 'object') {
|
30 | if (!index.isObjectProperty()) {
|
31 | throw TypeError('Cannot apply a numeric index as object property')
|
32 | }
|
33 | setSafeProperty(object, index.getObjectProperty(), value)
|
34 | return object
|
35 | } else {
|
36 | throw new TypeError('Cannot apply index: unsupported type of object')
|
37 | }
|
38 | } catch (err) {
|
39 | throw errorTransform(err)
|
40 | }
|
41 | }
|
42 | }
|