maraj 1.0.0 → 1.0.1

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.
Files changed (3) hide show
  1. package/README.md +129 -0
  2. package/index.js +1 -1
  3. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,129 @@
1
+
2
+ # Immutable Maraj
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.
7
+ Features:
8
+ - Can accept multiple updates while mantaining the order of execution.
9
+ - Easy syntax for conditional updates.
10
+ - Functional updates.
11
+
12
+ ## Creating an Immutable Update
13
+
14
+ The createImmutableUpdate function has two arguments:
15
+ - First one: The original object we want to update non-destructively
16
+ - Second one: An update object with key-value pairs representing the updates we want to make
17
+
18
+ The updateObject has the following syntax:
19
+
20
+ - It can accept multiple key-value pairs and the order of execution will be preserved.
21
+
22
+ - Key: path name of the fiels, separated by commas (same syntax you use to access object properties)
23
+ (for arrays just use its index value)
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
+
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
+
28
+
29
+ ## Example
30
+
31
+ import createImmutableUpdate from 'maraj'
32
+
33
+ const person = {
34
+ name: 'Lukasz',
35
+ country: 'Spain',
36
+ additionalInfo:{
37
+ info1: null,
38
+ info2: null,
39
+ info3: null,
40
+ info4: null
41
+ }
42
+ projects: [
43
+ {
44
+ title: 'portfolio',
45
+ languages: ['esp','eng'],
46
+ description: 'lorem ipsum',
47
+ available: false,
48
+ data:{
49
+ createdAt: '01-01-2000'
50
+ size: 20000
51
+ }
52
+ },
53
+ {
54
+ title: 'asia',
55
+ languages: ['esp','eng'],
56
+ description: 'lorem ipsum',
57
+ available: true,
58
+ data:{
59
+ createdAt: '01-01-2000'
60
+ size: 40000
61
+ }
62
+ },
63
+ {
64
+ title: 'pos',
65
+ languages: ['esp','eng'],
66
+ description: 'lorem ipsum',
67
+ available: false,
68
+ data:{
69
+ createdAt: '01-01-2000'
70
+ size: 100000
71
+ }
72
+ }
73
+ ]
74
+ }
75
+
76
+ createImmutableUpdate(person,{
77
+ 'name': Maraj //normal update
78
+ 'projects.0.title': 'personal website' //nested update
79
+ 'projects.1.available': true //nested update
80
+ 'projects.1.available': (currentValue) => !currentValue //functional update
81
+ 'projects': (currentValue) => currentValue.filter(project => project.available) //functional update
82
+ [condition && 'additionalInfo.info1']: 'some info' //conditional update, only if the key is not false/undefined/null
83
+ })
84
+
85
+
86
+ ## Other Utilities
87
+
88
+ ### spread
89
+
90
+ Spreads its argument into the key provided in the createImmutableUpdate.
91
+ Works with objects(updates its keys) and arrays(adds new values to the array)
92
+
93
+
94
+ import {spread} from 'maraj'
95
+
96
+ createImmutableUpdate(person,{
97
+ 'additionalInfo.info1': spread({info1: 'new Info'}) //same as (newValue) => (value) => ({...value, ...newValue})
98
+ 'projects': spread({title: 'new project'}) //same as (newValueToAdd) => (value) => ([...value, ...newValueToAdd])
99
+ })
100
+
101
+
102
+ ### remove
103
+
104
+ Removes the key of an object or the index of an array in the key provided in the createImmutableUpdate.
105
+
106
+ import {spread} from 'maraj'
107
+
108
+ createImmutableUpdate(person,{
109
+ 'additionalInfo': remove('info1') // returns new value (in this case "additionalInfo") without the provided key ("info1")
110
+ 'projects': remove(1) // returns new array (in this case "projects") without the provided index (1)
111
+ })
112
+
113
+
114
+ ### createSelectorFromStringPath
115
+
116
+ Creates a store selector from provided string path. Usefull in cases like getting data from Redux, Zustand, and so on. stores.
117
+
118
+ const stringPath = 'projects.1.data.size'
119
+
120
+ createSelectorFromStringPath(stringPath) // will behave as (store) => store[projects][1][data][size]
121
+
122
+
123
+ ### splitPath
124
+
125
+ Usefull when you want to spread multiple values into objects dynamically
126
+
127
+ Splits string path (example:'projects.1.data.size' ) into two:
128
+ - id: the last key in the path (in this case 'size')
129
+ - remainingPath: the remaining path (in this case 'projects.1.data')
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- //creates
1
+ //creates a shallow deep copy with the only needed updated fields
2
2
  const createImmutableUpdate = (state, update = {}) => {
3
3
  const newState = Array.isArray(state) ? [...state] : { ...state }
4
4
  const visited = {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maraj",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A lightweight 500 bytes alternative for immutable updates while dealing with objects and arrays",
5
5
  "main": "index.js",
6
6
  "scripts": {