maraj 1.0.6 → 1.0.8
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 -15
- 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,7 +22,7 @@ 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)
|
|
25
|
+
- **Key:** path name of the fiels, separated by commas (same syntax you use to access object/array properties)
|
|
23
26
|
- For arrays just use its index value
|
|
24
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
|
|
|
@@ -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,33 +92,37 @@ const newUpdatedObject = createImmutableUpdate(person,{
|
|
|
89
92
|
### spread
|
|
90
93
|
|
|
91
94
|
Spreads its argument into the key provided in the **createImmutableUpdate.**
|
|
92
|
-
|
|
95
|
+
|
|
96
|
+
Works with **objects (updates its keys)**, **arrays (adds new values to the array)** and **any other value (sets the new value)**
|
|
93
97
|
|
|
94
98
|
```js
|
|
95
99
|
import {spread} from 'maraj'
|
|
96
100
|
|
|
97
101
|
const newUpdatedObject = createImmutableUpdate(person,{
|
|
98
|
-
'additionalInfo
|
|
102
|
+
'additionalInfo': spread({info1: 'new Info'}), //same as (newValue) => (value) => ({...value, ...newValue})
|
|
99
103
|
'projects': spread({title: 'new project'}) //same as (newValueToAdd) => (value) => ([...value, ...newValueToAdd])
|
|
104
|
+
'additionalInfo.info1': spread('new project') //same as (newValue) => (value) => (newValue)
|
|
100
105
|
})
|
|
101
106
|
```
|
|
102
107
|
|
|
103
108
|
### remove
|
|
104
109
|
|
|
105
|
-
Removes the key of an object or the index of an array in the key provided in the **createImmutableUpdate.**
|
|
110
|
+
Removes the **key of an object** or the **index of an array** in the key provided in the **createImmutableUpdate.**
|
|
106
111
|
|
|
107
112
|
```js
|
|
108
113
|
import {remove} from 'maraj'
|
|
109
114
|
|
|
110
115
|
const newUpdatedObject = createImmutableUpdate(person,{
|
|
111
|
-
'additionalInfo': remove('info1') // returns new value (in this case "additionalInfo") without the provided key ("info1")
|
|
116
|
+
'additionalInfo': remove('info1'), // returns new value (in this case "additionalInfo") without the provided key ("info1")
|
|
112
117
|
'projects': remove(1) // returns new array (in this case "projects") without the provided index (1)
|
|
113
118
|
})
|
|
114
119
|
```
|
|
115
120
|
|
|
116
121
|
### createSelectorFromStringPath
|
|
117
122
|
|
|
118
|
-
Creates a store selector from provided string path.
|
|
123
|
+
**Creates a store selector** from provided string path.
|
|
124
|
+
|
|
125
|
+
Usefull in cases like getting data from Redux, Zustand, and so on. stores.
|
|
119
126
|
|
|
120
127
|
```js
|
|
121
128
|
import {createSelectorFromStringPath} from 'maraj'
|
|
@@ -131,4 +138,15 @@ Usefull when you want to spread multiple values into objects dynamically
|
|
|
131
138
|
|
|
132
139
|
Splits string path (example:'projects.1.data.size' ) into two:
|
|
133
140
|
- id: the last key in the path (in this case 'size')
|
|
134
|
-
- remainingPath: the remaining path (in this case 'projects.1.data')
|
|
141
|
+
- remainingPath: the remaining path (in this case 'projects.1.data')
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
```js
|
|
145
|
+
import {splitPath} from 'maraj'
|
|
146
|
+
|
|
147
|
+
const stringPath = 'projects.1.data.size'
|
|
148
|
+
|
|
149
|
+
const { id, remainingPath } = splitPath(stringPath)
|
|
150
|
+
// id = 'size'
|
|
151
|
+
// remainingPath = 'projects.1.data'
|
|
152
|
+
```
|