maraj 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
# Maraj
|
|
3
3
|
|
|
4
4
|
A lightweight alternative for creating **immutable updates of objects and arrays.**
|
|
5
|
+
|
|
5
6
|
**4 times faster** than the commonly used alternatives like Immer or ImmutableJS.
|
|
7
|
+
|
|
6
8
|
Only **600 bytes** gziped with 5 common utilities that you will most likelly need in every project.
|
|
9
|
+
|
|
7
10
|
Features:
|
|
8
11
|
- Can accept multiple updates while mantaining the order of execution.
|
|
9
12
|
- Easy syntax for conditional updates.
|
|
@@ -19,9 +22,9 @@ The **updateObject** has the following syntax:
|
|
|
19
22
|
|
|
20
23
|
It can accept multiple key-value pairs and the order of execution will be preserved.
|
|
21
24
|
|
|
22
|
-
- **Key:** path name of the fiels, separated by commas (same syntax you use to access object properties)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
- **Key:** path name of the fiels, separated by commas (same syntax you use to access object/array properties)
|
|
26
|
+
- For arrays just use its index value
|
|
27
|
+
- Can accept conditional statements. (If the key is falsy, undefined or null it will not update the new value into the new object.)
|
|
25
28
|
|
|
26
29
|
- **Value:** can be any type, including a function, where its argument will be the current value and its return value has to be the new value to be updated
|
|
27
30
|
|
|
@@ -47,7 +50,7 @@ const person = {
|
|
|
47
50
|
description: 'lorem ipsum',
|
|
48
51
|
available: false,
|
|
49
52
|
data:{
|
|
50
|
-
createdAt: '01-01-2000'
|
|
53
|
+
createdAt: '01-01-2000',
|
|
51
54
|
size: 20000
|
|
52
55
|
}
|
|
53
56
|
},
|
|
@@ -57,7 +60,7 @@ const person = {
|
|
|
57
60
|
description: 'lorem ipsum',
|
|
58
61
|
available: true,
|
|
59
62
|
data:{
|
|
60
|
-
createdAt: '01-01-2000'
|
|
63
|
+
createdAt: '01-01-2000',
|
|
61
64
|
size: 40000
|
|
62
65
|
}
|
|
63
66
|
},
|
|
@@ -67,7 +70,7 @@ const person = {
|
|
|
67
70
|
description: 'lorem ipsum',
|
|
68
71
|
available: false,
|
|
69
72
|
data:{
|
|
70
|
-
createdAt: '01-01-2000'
|
|
73
|
+
createdAt: '01-01-2000',
|
|
71
74
|
size: 100000
|
|
72
75
|
}
|
|
73
76
|
}
|
|
@@ -75,11 +78,11 @@ const person = {
|
|
|
75
78
|
}
|
|
76
79
|
|
|
77
80
|
const newUpdatedObject = createImmutableUpdate(person,{
|
|
78
|
-
'name':
|
|
79
|
-
'projects.0.title': '
|
|
80
|
-
'projects.1.available': true //nested update
|
|
81
|
-
'projects.1.available': (currentValue) => !currentValue //functional update
|
|
82
|
-
'projects': (currentValue) => currentValue.filter(project => project.available) //functional update
|
|
81
|
+
'name': 'maraj', //normal update
|
|
82
|
+
'projects.0.title': 'website', //nested update
|
|
83
|
+
'projects.1.available': true, //nested update
|
|
84
|
+
'projects.1.available': (currentValue) => !currentValue, //functional update
|
|
85
|
+
'projects': (currentValue) => currentValue.filter(project => project.available), //functional update
|
|
83
86
|
[condition && 'additionalInfo.info1']: 'some info' //conditional update, only if the key is not false/undefined/null
|
|
84
87
|
})
|
|
85
88
|
```
|
|
@@ -89,13 +92,14 @@ const newUpdatedObject = createImmutableUpdate(person,{
|
|
|
89
92
|
### spread
|
|
90
93
|
|
|
91
94
|
Spreads its argument into the key provided in the **createImmutableUpdate.**
|
|
95
|
+
|
|
92
96
|
Works with objects(updates its keys) and arrays(adds new values to the array)
|
|
93
97
|
|
|
94
98
|
```js
|
|
95
99
|
import {spread} from 'maraj'
|
|
96
100
|
|
|
97
101
|
const newUpdatedObject = createImmutableUpdate(person,{
|
|
98
|
-
'additionalInfo.info1': spread({info1: 'new Info'}) //same as (newValue) => (value) => ({...value, ...newValue})
|
|
102
|
+
'additionalInfo.info1': spread({info1: 'new Info'}), //same as (newValue) => (value) => ({...value, ...newValue})
|
|
99
103
|
'projects': spread({title: 'new project'}) //same as (newValueToAdd) => (value) => ([...value, ...newValueToAdd])
|
|
100
104
|
})
|
|
101
105
|
```
|
|
@@ -108,14 +112,16 @@ Removes the key of an object or the index of an array in the key provided in the
|
|
|
108
112
|
import {remove} from 'maraj'
|
|
109
113
|
|
|
110
114
|
const newUpdatedObject = createImmutableUpdate(person,{
|
|
111
|
-
'additionalInfo': remove('info1') // returns new value (in this case "additionalInfo") without the provided key ("info1")
|
|
115
|
+
'additionalInfo': remove('info1'), // returns new value (in this case "additionalInfo") without the provided key ("info1")
|
|
112
116
|
'projects': remove(1) // returns new array (in this case "projects") without the provided index (1)
|
|
113
117
|
})
|
|
114
118
|
```
|
|
115
119
|
|
|
116
120
|
### createSelectorFromStringPath
|
|
117
121
|
|
|
118
|
-
Creates a store selector from provided string path.
|
|
122
|
+
Creates a store selector from provided string path.
|
|
123
|
+
|
|
124
|
+
Usefull in cases like getting data from Redux, Zustand, and so on. stores.
|
|
119
125
|
|
|
120
126
|
```js
|
|
121
127
|
import {createSelectorFromStringPath} from 'maraj'
|