@hybridly/utils 0.2.1 → 0.3.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.
package/dist/index.cjs CHANGED
@@ -346,6 +346,35 @@ function merge(x, y, options = {}) {
346
346
  function removeTrailingSlash(string) {
347
347
  return string.replace(/\/+$/, "");
348
348
  }
349
+ function setValueAtPath(obj, path, value2) {
350
+ if (!path.includes(".")) {
351
+ obj[path] = value2;
352
+ return;
353
+ }
354
+ const segments = path.split(".");
355
+ let nestedObject = obj;
356
+ for (let i = 0; i < segments.length - 1; i++) {
357
+ const key = segments[i];
358
+ nestedObject = nestedObject[key] = nestedObject[key] || {};
359
+ }
360
+ nestedObject[segments[segments.length - 1]] = value2;
361
+ }
362
+ function unsetPropertyAtPath(obj, path) {
363
+ if (!path.includes(".")) {
364
+ delete obj[path];
365
+ return;
366
+ }
367
+ const segments = path.split(".");
368
+ let nestedObject = obj;
369
+ for (let i = 0; i < segments.length - 1; i++) {
370
+ const key = segments[i];
371
+ nestedObject = nestedObject[key] = nestedObject[key] || {};
372
+ }
373
+ delete nestedObject[segments[segments.length - 1]];
374
+ if (Object.keys(nestedObject).length === 0) {
375
+ unsetPropertyAtPath(obj, segments.slice(0, -1).join("."));
376
+ }
377
+ }
349
378
 
350
379
  const debug = {
351
380
  router: makeDebugger__default("hybridly:core:router"),
@@ -368,8 +397,10 @@ exports.merge = merge;
368
397
  exports.objectToFormData = objectToFormData;
369
398
  exports.random = random;
370
399
  exports.removeTrailingSlash = removeTrailingSlash;
400
+ exports.setValueAtPath = setValueAtPath;
371
401
  exports.showDomainsDisabledErrorModal = showDomainsDisabledErrorModal;
372
402
  exports.showPageComponentErrorModal = showPageComponentErrorModal;
373
403
  exports.showResponseErrorModal = showResponseErrorModal;
404
+ exports.unsetPropertyAtPath = unsetPropertyAtPath;
374
405
  exports.value = value;
375
406
  exports.when = when;
package/dist/index.d.ts CHANGED
@@ -48,6 +48,27 @@ interface MergeOptions {
48
48
  }
49
49
  declare function merge<T>(x: Partial<T>, y: Partial<T>, options?: MergeOptions): T;
50
50
  declare function removeTrailingSlash(string: string): string;
51
+ /**
52
+ * Sets a value at a path in an object
53
+ *
54
+ * This function will set a value at a path in an object, creating any missing
55
+ * objects along the way. The object is modified in place.
56
+ *
57
+ * @param obj the object to set the value in
58
+ * @param path a dot-separated path to the property to set
59
+ * @param value the value to set
60
+ */
61
+ declare function setValueAtPath(obj: any, path: string, value: any): void;
62
+ /**
63
+ * Unsets a property at a path in an object
64
+ *
65
+ * This function will unset a property at a path in an object, deleting any
66
+ * objects along the way that are empty. The object is modified in place.
67
+ *
68
+ * @param obj the object to unset the property in
69
+ * @param path a dot-separated path to the property to unset
70
+ */
71
+ declare function unsetPropertyAtPath(obj: any, path: string): void;
51
72
 
52
73
  declare const debug: {
53
74
  router: makeDebugger.Debugger;
@@ -61,4 +82,4 @@ declare const debug: {
61
82
  adapter: (name: string, ...args: any[]) => void;
62
83
  };
63
84
 
64
- export { FormDataConvertible, RequestData, debounce, debug, hasFiles, match, merge, objectToFormData, random, removeTrailingSlash, showDomainsDisabledErrorModal, showPageComponentErrorModal, showResponseErrorModal, value, when };
85
+ export { FormDataConvertible, RequestData, debounce, debug, hasFiles, match, merge, objectToFormData, random, removeTrailingSlash, setValueAtPath, showDomainsDisabledErrorModal, showPageComponentErrorModal, showResponseErrorModal, unsetPropertyAtPath, value, when };
package/dist/index.mjs CHANGED
@@ -336,6 +336,35 @@ function merge(x, y, options = {}) {
336
336
  function removeTrailingSlash(string) {
337
337
  return string.replace(/\/+$/, "");
338
338
  }
339
+ function setValueAtPath(obj, path, value2) {
340
+ if (!path.includes(".")) {
341
+ obj[path] = value2;
342
+ return;
343
+ }
344
+ const segments = path.split(".");
345
+ let nestedObject = obj;
346
+ for (let i = 0; i < segments.length - 1; i++) {
347
+ const key = segments[i];
348
+ nestedObject = nestedObject[key] = nestedObject[key] || {};
349
+ }
350
+ nestedObject[segments[segments.length - 1]] = value2;
351
+ }
352
+ function unsetPropertyAtPath(obj, path) {
353
+ if (!path.includes(".")) {
354
+ delete obj[path];
355
+ return;
356
+ }
357
+ const segments = path.split(".");
358
+ let nestedObject = obj;
359
+ for (let i = 0; i < segments.length - 1; i++) {
360
+ const key = segments[i];
361
+ nestedObject = nestedObject[key] = nestedObject[key] || {};
362
+ }
363
+ delete nestedObject[segments[segments.length - 1]];
364
+ if (Object.keys(nestedObject).length === 0) {
365
+ unsetPropertyAtPath(obj, segments.slice(0, -1).join("."));
366
+ }
367
+ }
339
368
 
340
369
  const debug = {
341
370
  router: makeDebugger("hybridly:core:router"),
@@ -349,4 +378,4 @@ const debug = {
349
378
  adapter: (name, ...args) => makeDebugger("hybridly:adapter").extend(name)(args.shift(), ...args)
350
379
  };
351
380
 
352
- export { debounce, debug, hasFiles, match, merge, objectToFormData, random, removeTrailingSlash, showDomainsDisabledErrorModal, showPageComponentErrorModal, showResponseErrorModal, value, when };
381
+ export { debounce, debug, hasFiles, match, merge, objectToFormData, random, removeTrailingSlash, setValueAtPath, showDomainsDisabledErrorModal, showPageComponentErrorModal, showResponseErrorModal, unsetPropertyAtPath, value, when };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hybridly/utils",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "description": "Utils used in Hybridly packages",
5
5
  "keywords": [
6
6
  "hybridly"