assign-gingerly 0.0.44 → 0.0.45

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 CHANGED
@@ -751,7 +751,31 @@ console.log(obj);
751
751
  // }
752
752
  ```
753
753
 
754
- The `+=` command syntax is `<path> +=` where the path uses the `?.` nested notation for nested properties, or a plain key for direct properties. The right-hand side value is added to the existing value using `+=`. If the path doesn't exist, it's created and set directly to the value. If the expression is a string, string concatenation is used. If the expression can't be "added to", it allows JavaScript to throw its natural error.
754
+ The `+=` command syntax is `<path> +=` where the path uses the `?.` nested notation for nested properties, or a plain key for direct properties. The right-hand side value is added to the existing value using `+=`. If the path doesn't exist, it's created and set directly to the value.
755
+
756
+ **Behavior by type:**
757
+
758
+ | LHS type | RHS type | Result |
759
+ |----------|----------|--------|
760
+ | number | number | addition (`2 += 3` → `5`) |
761
+ | string | any | string concatenation (`"hello" += 3` → `"hello3"`) |
762
+ | array | array | array concatenation (`[1,2] += [3,4]` → `[1,2,3,4]`) |
763
+ | array | non-array | push single item (`[1,2] += 3` → `[1,2,3]`) |
764
+ | undefined/missing | any | direct assignment |
765
+
766
+ ```TypeScript
767
+ const obj = {
768
+ tags: ['a', 'b'],
769
+ name: 'hello'
770
+ };
771
+ assignGingerly(obj, {
772
+ '?.tags +=': ['c', 'd'], // array concat: ['a', 'b', 'c', 'd']
773
+ '?.name +=': ' world' // string concat: 'hello world'
774
+ });
775
+
776
+ // Push a single item
777
+ assignGingerly(obj, { '?.tags +=': 'e' }); // ['a', 'b', 'c', 'd', 'e']
778
+ ```
755
779
 
756
780
  ## Example 5 - Toggling boolean values and negating
757
781
 
package/assignGingerly.js CHANGED
@@ -616,6 +616,11 @@ export function assignGingerly(target, source, options) {
616
616
  if (!(lastKey in parent)) {
617
617
  parent[lastKey] = value;
618
618
  }
619
+ else if (Array.isArray(parent[lastKey])) {
620
+ parent[lastKey] = Array.isArray(value)
621
+ ? [...parent[lastKey], ...value]
622
+ : [...parent[lastKey], value];
623
+ }
619
624
  else {
620
625
  parent[lastKey] += value;
621
626
  }
@@ -625,6 +630,11 @@ export function assignGingerly(target, source, options) {
625
630
  if (!(path in target)) {
626
631
  target[path] = value;
627
632
  }
633
+ else if (Array.isArray(target[path])) {
634
+ target[path] = Array.isArray(value)
635
+ ? [...target[path], ...value]
636
+ : [...target[path], value];
637
+ }
628
638
  else {
629
639
  target[path] += value;
630
640
  }
package/assignGingerly.ts CHANGED
@@ -801,6 +801,10 @@ export function assignGingerly(
801
801
  const parent = ensureNestedPath(target, pathParts);
802
802
  if (!(lastKey in parent)) {
803
803
  parent[lastKey] = value;
804
+ } else if (Array.isArray(parent[lastKey])) {
805
+ parent[lastKey] = Array.isArray(value)
806
+ ? [...parent[lastKey], ...value]
807
+ : [...parent[lastKey], value];
804
808
  } else {
805
809
  parent[lastKey] += value;
806
810
  }
@@ -808,6 +812,10 @@ export function assignGingerly(
808
812
  // Plain key - direct operation on target
809
813
  if (!(path in target)) {
810
814
  target[path] = value;
815
+ } else if (Array.isArray(target[path])) {
816
+ target[path] = Array.isArray(value)
817
+ ? [...target[path], ...value]
818
+ : [...target[path], value];
811
819
  } else {
812
820
  target[path] += value;
813
821
  }
@@ -124,7 +124,14 @@ export function assignTentatively(target, source, options) {
124
124
  if (!(fullPath in reversal)) {
125
125
  reversal[fullPath] = parent[lastKey];
126
126
  }
127
- parent[lastKey] += value;
127
+ if (Array.isArray(parent[lastKey])) {
128
+ parent[lastKey] = Array.isArray(value)
129
+ ? [...parent[lastKey], ...value]
130
+ : [...parent[lastKey], value];
131
+ }
132
+ else {
133
+ parent[lastKey] += value;
134
+ }
128
135
  }
129
136
  else {
130
137
  // Property doesn't exist, create it with the value
@@ -137,7 +144,14 @@ export function assignTentatively(target, source, options) {
137
144
  if (!(path in reversal)) {
138
145
  reversal[path] = target[path];
139
146
  }
140
- target[path] += value;
147
+ if (Array.isArray(target[path])) {
148
+ target[path] = Array.isArray(value)
149
+ ? [...target[path], ...value]
150
+ : [...target[path], value];
151
+ }
152
+ else {
153
+ target[path] += value;
154
+ }
141
155
  }
142
156
  else {
143
157
  target[path] = value;
@@ -152,7 +152,13 @@ export function assignTentatively(
152
152
  if (!(fullPath in reversal)) {
153
153
  reversal[fullPath] = parent[lastKey];
154
154
  }
155
- parent[lastKey] += value;
155
+ if (Array.isArray(parent[lastKey])) {
156
+ parent[lastKey] = Array.isArray(value)
157
+ ? [...parent[lastKey], ...value]
158
+ : [...parent[lastKey], value];
159
+ } else {
160
+ parent[lastKey] += value;
161
+ }
156
162
  } else {
157
163
  // Property doesn't exist, create it with the value
158
164
  parent[lastKey] = value;
@@ -163,7 +169,13 @@ export function assignTentatively(
163
169
  if (!(path in reversal)) {
164
170
  reversal[path] = target[path];
165
171
  }
166
- target[path] += value;
172
+ if (Array.isArray(target[path])) {
173
+ target[path] = Array.isArray(value)
174
+ ? [...target[path], ...value]
175
+ : [...target[path], value];
176
+ } else {
177
+ target[path] += value;
178
+ }
167
179
  } else {
168
180
  target[path] = value;
169
181
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "assign-gingerly",
3
- "version": "0.0.44",
3
+ "version": "0.0.45",
4
4
  "description": "This package provides a utility function for carefully merging one object into another.",
5
5
  "homepage": "https://github.com/bahrus/assign-gingerly#readme",
6
6
  "bugs": {