maraj 2.5.0 → 2.5.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 (2) hide show
  1. package/README.md +61 -21
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,16 +1,18 @@
1
1
 
2
2
  # Maraj
3
3
 
4
- A fast lightweight utility for creating **immutable updates**. Full typescript support. Only 600 bytes gziped. Includes usefull utilities for manipulating updates (like remove, select).
4
+ A fast lightweight utility for creating **immutable updates**. Fully typed. Only 600 bytes gziped. Includes usefull utilities for manipulating updates (like remove, select).
5
5
 
6
6
  ### Features
7
7
 
8
+ - updateImmutably and modifyImmutably.
8
9
  - Full typescript support.
9
10
  - Correct type inference.
10
- - Multiple updates at same time mantaining the order of execution.
11
+ - Multiple and concurrent updates.
11
12
  - Functional updates.
13
+ - Includes usefull utilities like "remove", "select".
12
14
 
13
- **<u>For Typescript:</u>** in tsconfig.json set **"strictNullChecks"** and **"noUncheckedIndexedAccess"** to true for correct type inference of optional properties (so it can correctly differentiate between optional props and props with possible undefined value)
15
+ **<u>_For Typescript:_</u>** in tsconfig.json set **"strictNullChecks"** and **"noUncheckedIndexedAccess"** to **true** to be able to infer correctly the type of optional properties and properties with posible undefined value. ({key?: ''} !== {key: '' | undefined})
14
16
 
15
17
  ```ts
16
18
  const originalValue = { name: 'one', lastname: 'two' modified: false, files: [{ filename: 'one' }] }
@@ -20,22 +22,33 @@ const updatedValue = updateImmutably(orignalValue, {
20
22
  'modified': (value, fullObject) => !value,
21
23
  'files.0.filename': 'zero',
22
24
  })
23
-
24
25
  // updatedValue = { name: 'updated', lastname: 'two' modified: true, files: [{ filename: 'zero' }] }
25
-
26
26
  originalValue.files[1] === updatedValue.files[1] // true
27
+
28
+ const newValue = modifyImmutably(orignalValue, {
29
+ 'newProp': 'new',
30
+ 'data.0.name': 'dataName'
31
+ })
32
+ // updatedValue = { ... , newProp: 'new', data: [{ name: 'dataName' }] }
33
+ originalValue.files[1] === newValue.files[1] // true
27
34
  ```
28
35
 
29
36
  ### Limitations
30
37
 
31
- - If "addNonExistentPropsAndIndexes" is enabled (3rd parameter in the updateImmutably function), **numbers** will **always** create an **array** if the array/object doesnt exist beforehand.
38
+ - In "modifyImmutably", **numbers** inside path will **always** create an **array** if the array/object doesnt exist beforehand. If you want an object instead of an array, add it beforehand.
32
39
 
33
40
  ```ts
34
- const updatedValue = updateImmutably({}, {
41
+ const updatedValue = modifyImmutably({}, {
35
42
  'files.1.name': 'new name',
36
- }, true)
37
- // updatedValue = { files: [<empty>, { name: 'new name' }] }
38
- // the "files" prop is an array.
43
+ })
44
+ // The "files" prop is an array. updatedValue = { files: [<empty>, { name: 'new name' }] }
45
+
46
+ // To get an object instead of an array create an object beforehand
47
+ const updatedValue2 = modifyImmutably({}, {
48
+ 'files': {}, // add an object first, so then i can add props as numbers after it
49
+ 'files.1.name': 'new name',
50
+ })
51
+ // updatedValue2 = { files: { '1': { name: 'new name' } } }
39
52
  ```
40
53
 
41
54
  - The type "UpdateDotPathObject" (the type used as second parameter in the updateImmutably function) if used to create new update objects using computed properties { [key]: value } will always throw a ts error even if the typing is correct. [Playground simple explanation](https://www.typescriptlang.org/play?ts=5.3.2#code/LAKALgngDgpgBAGQJYGcxwLxwOQENtwA+cABgCQDeAdgK4C2ARjAE4C+JooksiqYASjADGAe2YATTHApwA2gGk4SKrzQBdAPwAuOItacQoqmjhDmMXGBiDREqQB4AKnBgAPK1XEpVYAHwAKAGsYCB1HAEpMX2lQODijE3MhHWQ0GzFJLApYuNyAejy4AD0wbxZmMR0UZSF4ADcAFgA6AGYmgFYldAB3JAAbPrgwAAsK7rhcFXKxCfQhYeFA5QBzUxE6KBorSWCIb0nJOtw+mhhvGmqqVasNvst4PqQrZmOyupgVJAAzIYWh6BWSm8tiSYByuTkuzUOl24P0IFy5jANGYKiSoFYQA)
@@ -53,24 +66,23 @@ const updateFieldStore = <TPath extends DotPaths<Store>>(
53
66
  }
54
67
  ```
55
68
 
56
- - Use the "set" utility if typescript doesn't distinguish properly _"an optional field"_ vs a _"field which can have 'undefined' as type"_. Usually happens in optional nested props inside arrays.
69
+ - Use the "set" utility if typescript doesn't infer properly _"an optional field"_ vs a _"field which can have 'undefined' as type"_. Usually happens in optional nested props inside arrays.
57
70
 
58
71
  ```ts
59
72
  const updatedValue = updateImmutably(data, {
60
- 'files.0.optionalProperty': 'new name',
61
- 'files.1.optionalProperty': undefined, //no ts error
62
- 'files.0.optionalProperty': set('new name'),
63
- 'files.1.optionalProperty': set(undefined), //throws ts error, undefined cant be a value.
73
+ 'files.0.nonOptionalProperty': 'new name',
74
+ 'files.1.nonOptionalProperty': undefined, //no ts error
75
+ 'files.0.nonOptionalProperty': set('new name'),
76
+ 'files.1.nonOptionalProperty': set(undefined), //throws ts error, undefined cant be a value.
64
77
  })
65
78
  ```
66
79
 
67
- ## Creating an Immutable Update
80
+ ## Creating a Strict Immutable Update
68
81
 
69
82
  Arguments:
70
83
 
71
84
  - Original Object
72
85
  - Update Object
73
- - If it should add non-existent properties/indexes
74
86
 
75
87
  ## Example
76
88
 
@@ -87,7 +99,6 @@ const data = {
87
99
  }
88
100
 
89
101
  const newUpdatedObject = updateImmutably(data, {
90
- "profile.age": 28, //add new properties if "addNonExistentPropsAndIndexes" is enabled
91
102
  "profile.name": "Lucas", //update values
92
103
  "works.1": "gs", //nested array update
93
104
  "history.1.available": (currentValue) => (!currentValue), //functional update
@@ -95,6 +106,34 @@ const newUpdatedObject = updateImmutably(data, {
95
106
  })
96
107
  ```
97
108
 
109
+ ## Creating an Immutable Update allowing new props to be added
110
+
111
+ Arguments:
112
+
113
+ - Original Object
114
+ - Update Object
115
+
116
+ ## Example
117
+
118
+ ```js
119
+ import { modifyImmutably } from "maraj";
120
+
121
+ const data = {
122
+ profile: { name: "Lukasz" },
123
+ works: ["soum", "fi"],
124
+ history: [
125
+ { title: "soum", size: 90, available: true, info: ["info1"] },
126
+ { title: "fi", size: 30, available: false, info: ["infoA"] },
127
+ ]
128
+ }
129
+
130
+ const newUpdatedObject = modifyImmutably(data, {
131
+ "profile.age": 28, //add new property
132
+ "profile.name": "Lucas", //update values
133
+ "works.10": "gs", //increases the array (filling it up with empty/undefined) and sets the value in the index of 10 to "gs"
134
+ })
135
+ ```
136
+
98
137
  ## Remove Utility
99
138
 
100
139
  Removes the **key of an object** or the **index of an array** in the key provided in the **updateImmutably.**
@@ -104,8 +143,9 @@ import {remove} from 'maraj'
104
143
 
105
144
  const newUpdatedObject = updateImmutably(person,{
106
145
  'profile': (value) => remove(value, 'name'), // returns new value without the "name" prop
146
+ 'profile': (value) => remove(value, ['name', 'lastname']), // returns new value without the "name" and "lastname" prop
107
147
  'works': (value) => remove(value, 1), // returns new array without the provided index (1)
108
- 'works': (value) => remove(value, [1, "0"]), // returns new array without the provided index (0,1)
148
+ 'works': (value) => remove(value, [1, "0"]), // returns new array without the provided index "0" and "1"
109
149
  })
110
150
  ```
111
151
 
@@ -135,8 +175,8 @@ import { splitPathAtLastKey } from 'maraj'
135
175
 
136
176
  const stringPath = 'works.1.size'
137
177
 
138
- const [remainingPath, last] = splitPathAtLastKey(stringPath)
139
- // last = 'size'
178
+ const [remainingPath, lastKey] = splitPathAtLastKey(stringPath)
179
+ // lastKey = 'size'
140
180
  // remainingPath = 'works.1'
141
181
  ```
142
182
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "maraj",
3
- "version": "2.5.0",
4
- "description": "A lightweight 500 bytes gzipped alternative for immutable updates while dealing with objects and arrays",
3
+ "version": "2.5.1",
4
+ "description": "A lightweight 600 bytes gzipped alternative for immutable updates while dealing with objects and arrays",
5
5
  "keywords": [
6
6
  "shallow",
7
7
  "shallow copy",