index.d.ts 2.4 KB

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