index.d.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. declare const dotProp: {
  2. /**
  3. Get the value of the property at the given path.
  4. @param object - Object to get the `path` value.
  5. @param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key.
  6. @param defaultValue - Default value.
  7. @example
  8. ```
  9. import dotProp = require('dot-prop');
  10. dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar');
  11. //=> 'unicorn'
  12. dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep');
  13. //=> undefined
  14. dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');
  15. //=> 'default value'
  16. dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot');
  17. //=> 'unicorn'
  18. ```
  19. */
  20. get<T>(
  21. object: {[key: string]: any} | undefined,
  22. path: string
  23. ): T | undefined;
  24. get<T>(
  25. object: {[key: string]: any} | undefined,
  26. path: string,
  27. defaultValue: T
  28. ): T;
  29. /**
  30. Set the property at the given path to the given value.
  31. @param object - Object to set the `path` value.
  32. @param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key.
  33. @param value - Value to set at `path`.
  34. @returns The object.
  35. @example
  36. ```
  37. import dotProp = require('dot-prop');
  38. const object = {foo: {bar: 'a'}};
  39. dotProp.set(object, 'foo.bar', 'b');
  40. console.log(object);
  41. //=> {foo: {bar: 'b'}}
  42. const foo = dotProp.set({}, 'foo.bar', 'c');
  43. console.log(foo);
  44. //=> {foo: {bar: 'c'}}
  45. dotProp.set(object, 'foo.baz', 'x');
  46. console.log(object);
  47. //=> {foo: {bar: 'b', baz: 'x'}}
  48. ```
  49. */
  50. set<T extends {[key: string]: any}>(
  51. object: T,
  52. path: string,
  53. value: unknown
  54. ): T;
  55. /**
  56. Check whether the property at the given path exists.
  57. @param object - Object to test the `path` value.
  58. @param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key.
  59. @example
  60. ```
  61. import dotProp = require('dot-prop');
  62. dotProp.has({foo: {bar: 'unicorn'}}, 'foo.bar');
  63. //=> true
  64. ```
  65. */
  66. has(object: {[key: string]: any} | undefined, path: string): boolean;
  67. /**
  68. Delete the property at the given path.
  69. @param object - Object to delete the `path` value.
  70. @param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key.
  71. @returns A boolean of whether the property existed before being deleted.
  72. @example
  73. ```
  74. import dotProp = require('dot-prop');
  75. const object = {foo: {bar: 'a'}};
  76. dotProp.delete(object, 'foo.bar');
  77. console.log(object);
  78. //=> {foo: {}}
  79. object.foo.bar = {x: 'y', y: 'x'};
  80. dotProp.delete(object, 'foo.bar.x');
  81. console.log(object);
  82. //=> {foo: {bar: {y: 'x'}}}
  83. ```
  84. */
  85. delete(object: {[key: string]: any}, path: string): boolean;
  86. };
  87. export = dotProp;