maraj 1.0.1 → 1.0.3
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 +33 -28
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
#
|
|
2
|
+
# Maraj
|
|
3
3
|
|
|
4
|
-
A lightweight alternative for creating immutable updates of objects and arrays
|
|
5
|
-
4 times faster than the commonly used alternatives like Immer or ImmutableJS.
|
|
6
|
-
Only 600 bytes gziped with 5 common utilities that you will most likelly need in every project.
|
|
4
|
+
A lightweight alternative for creating **immutable updates of objects and arrays.**
|
|
5
|
+
**4 times faster** than the commonly used alternatives like Immer or ImmutableJS.
|
|
6
|
+
Only **600 bytes** gziped with 5 common utilities that you will most likelly need in every project.
|
|
7
7
|
Features:
|
|
8
8
|
- Can accept multiple updates while mantaining the order of execution.
|
|
9
9
|
- Easy syntax for conditional updates.
|
|
@@ -11,23 +11,24 @@ Features:
|
|
|
11
11
|
|
|
12
12
|
## Creating an Immutable Update
|
|
13
13
|
|
|
14
|
-
The createImmutableUpdate function has two arguments:
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
The **createImmutableUpdate** function has two arguments:
|
|
15
|
+
1. The **original object** we want to update non-destructively
|
|
16
|
+
2. An **update object** with key-value pairs representing the updates we want to make
|
|
17
17
|
|
|
18
|
-
The updateObject has the following syntax:
|
|
18
|
+
The **updateObject** has the following syntax:
|
|
19
19
|
|
|
20
20
|
- It can accept multiple key-value pairs and the order of execution will be preserved.
|
|
21
21
|
|
|
22
|
-
- Key
|
|
22
|
+
- **Key:** path name of the fiels, separated by commas (same syntax you use to access object properties)
|
|
23
23
|
(for arrays just use its index value)
|
|
24
24
|
(can accept conditional statements. If the key is falsy, undefined or null it will not update the new value into the new object.)
|
|
25
25
|
|
|
26
|
-
- Value
|
|
26
|
+
- **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
27
|
|
|
28
28
|
|
|
29
29
|
## Example
|
|
30
30
|
|
|
31
|
+
```js
|
|
31
32
|
import createImmutableUpdate from 'maraj'
|
|
32
33
|
|
|
33
34
|
const person = {
|
|
@@ -74,51 +75,55 @@ const person = {
|
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
createImmutableUpdate(person,{
|
|
77
|
-
'name': Maraj
|
|
78
|
-
'projects.0.title': 'personal website'
|
|
79
|
-
'projects.1.available': true
|
|
80
|
-
'projects.1.available': (currentValue) => !currentValue
|
|
81
|
-
'projects': (currentValue) => currentValue.filter(project => project.available)
|
|
82
|
-
[condition && 'additionalInfo.info1']: 'some info'
|
|
78
|
+
'name': Maraj //normal update
|
|
79
|
+
'projects.0.title': 'personal website' //nested update
|
|
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
|
|
83
|
+
[condition && 'additionalInfo.info1']: 'some info' //conditional update, only if the key is not false/undefined/null
|
|
83
84
|
})
|
|
84
|
-
|
|
85
|
+
```
|
|
85
86
|
|
|
86
87
|
## Other Utilities
|
|
87
88
|
|
|
88
89
|
### spread
|
|
89
90
|
|
|
90
|
-
Spreads its argument into the key provided in the createImmutableUpdate
|
|
91
|
+
Spreads its argument into the key provided in the **createImmutableUpdate.**
|
|
91
92
|
Works with objects(updates its keys) and arrays(adds new values to the array)
|
|
92
93
|
|
|
93
|
-
|
|
94
|
+
```js
|
|
94
95
|
import {spread} from 'maraj'
|
|
95
96
|
|
|
96
97
|
createImmutableUpdate(person,{
|
|
97
|
-
'additionalInfo.info1': spread({info1: 'new Info'})
|
|
98
|
-
'projects': spread({title: 'new project'})
|
|
98
|
+
'additionalInfo.info1': spread({info1: 'new Info'}) //same as (newValue) => (value) => ({...value, ...newValue})
|
|
99
|
+
'projects': spread({title: 'new project'}) //same as (newValueToAdd) => (value) => ([...value, ...newValueToAdd])
|
|
99
100
|
})
|
|
100
|
-
|
|
101
|
+
```
|
|
101
102
|
|
|
102
103
|
### remove
|
|
103
104
|
|
|
104
|
-
Removes the key of an object or the index of an array in the key provided in the createImmutableUpdate
|
|
105
|
+
Removes the key of an object or the index of an array in the key provided in the **createImmutableUpdate.**
|
|
105
106
|
|
|
106
|
-
|
|
107
|
+
```js
|
|
108
|
+
import {remove} from 'maraj'
|
|
107
109
|
|
|
108
110
|
createImmutableUpdate(person,{
|
|
109
|
-
'additionalInfo': remove('info1')
|
|
110
|
-
'projects': remove(1)
|
|
111
|
+
'additionalInfo': remove('info1') // returns new value (in this case "additionalInfo") without the provided key ("info1")
|
|
112
|
+
'projects': remove(1) // returns new array (in this case "projects") without the provided index (1)
|
|
111
113
|
})
|
|
112
|
-
|
|
114
|
+
```
|
|
113
115
|
|
|
114
116
|
### createSelectorFromStringPath
|
|
115
117
|
|
|
116
118
|
Creates a store selector from provided string path. Usefull in cases like getting data from Redux, Zustand, and so on. stores.
|
|
117
119
|
|
|
120
|
+
```js
|
|
121
|
+
import {createSelectorFromStringPath} from 'maraj'
|
|
122
|
+
|
|
118
123
|
const stringPath = 'projects.1.data.size'
|
|
119
124
|
|
|
120
125
|
createSelectorFromStringPath(stringPath) // will behave as (store) => store[projects][1][data][size]
|
|
121
|
-
|
|
126
|
+
```
|
|
122
127
|
|
|
123
128
|
### splitPath
|
|
124
129
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "maraj",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "A lightweight
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "A lightweight 600 bytes gzipped alternative for immutable updates while dealing with objects and arrays",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "echo \"Error: no test specified\" && exit 1"
|