{"name":"dot-prop","version":"10.2.0","description":"Get, set, or delete a property from a nested object using a dot path","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/dot-prop.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{"types":"./index.d.ts","default":"./index.js"},"sideEffects":false,"engines":{"node":">=20"},"scripts":{"test":"xo && ava && tsc","bench":"node benchmark.js","coverage":"c8 ava"},"keywords":["object","prop","property","dot","path","get","set","delete","access","notation","dotty","dottie","unflatten","expand"],"dependencies":{"type-fest":"^5.0.0"},"devDependencies":{"ava":"^6.4.1","benchmark":"^2.1.4","c8":"^10.1.3","expect-type":"^1.2.2","typescript":"^5.9.2","xo":"^1.2.2"},"gitHead":"d5d11c71a70bfb643a45d22821ed6d284240fce5","types":"./index.d.ts","_id":"dot-prop@10.2.0","bugs":{"url":"https://github.com/sindresorhus/dot-prop/issues"},"homepage":"https://github.com/sindresorhus/dot-prop#readme","_nodeVersion":"22.23.1","_npmVersion":"11.18.0","dist":{"integrity":"sha512-BTJ9aZYL3vCfZlZOBLy9v8TUqWGQ0pzFnygKwFZt5udj6viBoFIBviKPUoZLDCPn1FoXffv6McQFDenrm5Krfw==","shasum":"c2689cc5991a31b4c807f0d36af683fee333db8f","tarball":"http://registry-npm.ywwl.com/dot-prop/download/dot-prop-10.2.0.tgz","fileCount":5,"unpackedSize":33768,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQDDcJRHiBmn2vLzZD9Dm8jq1JAktnrUHjcqqckS4hwtqAIhAJHKst9ED4+mHjMdd4meLanZNXrNvy9/IAYyVcd8WyCv"}],"size":8781,"noattachment":false,"key":"/dot-prop/-/dot-prop-10.2.0.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/dot-prop_10.2.0_1784636376951_0.9516703075003654"},"_hasShrinkwrap":false,"readme":"# dot-prop\n\n> Get, set, or delete a property from a nested object using a dot path\n\n## Install\n\n```sh\nnpm install dot-prop\n```\n\n## Usage\n\n```js\nimport {getProperty, setProperty, hasProperty, deleteProperty} from 'dot-prop';\n\n// Getter\ngetProperty({foo: {bar: 'unicorn'}}, 'foo.bar');\n//=> 'unicorn'\n\ngetProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep');\n//=> undefined\n\ngetProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');\n//=> 'default value'\n\ngetProperty({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\\\.dot');\n//=> 'unicorn'\n\ngetProperty({foo: [{bar: 'unicorn'}]}, 'foo[0].bar');\n//=> 'unicorn'\n\ngetProperty({foo: [{bar: 'unicorn'}]}, 'foo.0.bar');\n//=> 'unicorn'\n\n// Setter\nconst object = {foo: {bar: 'a'}};\nsetProperty(object, 'foo.bar', 'b');\nconsole.log(object);\n//=> {foo: {bar: 'b'}}\n\nconst foo = setProperty({}, 'foo.bar', 'c');\nconsole.log(foo);\n//=> {foo: {bar: 'c'}}\n\nsetProperty(object, 'foo.baz', 'x');\nconsole.log(object);\n//=> {foo: {bar: 'b', baz: 'x'}}\n\nsetProperty(object, 'foo.biz[0]', 'a');\nconsole.log(object);\n//=> {foo: {bar: 'b', baz: 'x', biz: ['a']}}\n\nsetProperty(object, 'foo.items.0', 'first');\nconsole.log(object);\n//=> {foo: {bar: 'b', baz: 'x', biz: ['a'], items: ['first']}}\n\n// Has\nhasProperty({foo: {bar: 'unicorn'}}, 'foo.bar');\n//=> true\n\n// Deleter\nconst object = {foo: {bar: 'a'}};\ndeleteProperty(object, 'foo.bar');\nconsole.log(object);\n//=> {foo: {}}\n\nobject.foo.bar = {x: 'y', y: 'x'};\ndeleteProperty(object, 'foo.bar.x');\nconsole.log(object);\n//=> {foo: {bar: {y: 'x'}}}\n```\n\n### Array paths\n\nFor improved performance and interoperability with other libraries, you can also pass paths as arrays instead of strings. This avoids the overhead of parsing string paths.\n\n```js\nimport {getProperty, setProperty} from 'dot-prop';\n\nconst object = {\n\tusers: [\n\t\t{name: 'Alice', role: 'admin'},\n\t\t{name: 'Bob', role: 'user'}\n\t]\n};\n\n// Using array paths - no parsing overhead\ngetProperty(object, ['users', 0, 'name']);\n//=> 'Alice'\n\nsetProperty(object, ['users', 1, 'role'], 'moderator');\nconsole.log(object.users[1].role);\n//=> 'moderator'\n\n// Useful for interoperability with libraries that return paths as arrays\nconst pathFromOtherLib = ['users', 0, 'profile', 'settings'];\nsetProperty(object, pathFromOtherLib, {theme: 'dark'});\n```\n\nArray paths:\n- Avoid the parse/stringify cycle when you already have path segments\n- Work with all functions: `getProperty`, `setProperty`, `hasProperty`, `deleteProperty`\n- Numeric strings are automatically normalized to numbers for simplicity\n\n## API\n\n### getProperty(object, path, defaultValue?)\n\nGet the value of the property at the given path.\n\nReturns the value if any.\n\n### setProperty(object, path, value)\n\nSet the property at the given path to the given value.\n\nReturns the object.\n\n### hasProperty(object, path)\n\nCheck whether the property at the given path exists.\n\nReturns a boolean.\n\n### deleteProperty(object, path)\n\nDelete the property at the given path.\n\nReturns a boolean of whether the property existed before being deleted.\n\n### escapePath(path)\n\nEscape special characters in a path. Useful for sanitizing user input.\n\n```js\nimport {getProperty, escapePath} from 'dot-prop';\n\nconst object = {\n\tfoo: {\n\t\tbar: '👸🏻 You found me Mario!',\n\t},\n\t'foo.bar' : '🍄 The princess is in another castle!',\n};\nconst escapedPath = escapePath('foo.bar');\n\nconsole.log(getProperty(object, escapedPath));\n//=> '🍄 The princess is in another castle!'\n```\n\n### parsePath(path)\n\nParse a dot path into an array of path segments.\n\nReturns an array of path segments where numbers represent array indices and strings represent object keys.\n\n```js\nimport {parsePath} from 'dot-prop';\n\nparsePath('foo.bar');\n//=> ['foo', 'bar']\n\nparsePath('foo[0].bar');\n//=> ['foo', 0, 'bar']\n\nparsePath('foo.0.bar');\n//=> ['foo', 0, 'bar']\n\nparsePath('foo\\\\.bar');\n//=> ['foo.bar']\n\n// Use case: Iterate over path segments to build up a nested object\nconst path = 'users[0].profile.settings.theme';\nconst segments = parsePath(path);\n//=> ['users', 0, 'profile', 'settings', 'theme']\n```\n\n### stringifyPath(pathSegments, options?)\n\nConvert an array of path segments back into a path string.\n\nReturns a string path that can be used with other dot-prop functions.\n\n```js\nimport {stringifyPath} from 'dot-prop';\n\nstringifyPath(['foo', 'bar']);\n//=> 'foo.bar'\n\nstringifyPath(['foo', 0, 'bar']);\n//=> 'foo[0].bar'\n\nstringifyPath(['foo', '0', 'bar']);\n//=> 'foo[0].bar'\n\n// With preferDotForIndices option\nstringifyPath(['foo', 0, 'bar'], {preferDotForIndices: true});\n//=> 'foo.0.bar'\n```\n\n#### pathSegments\n\nType: `Array<string | number>`\n\nArray of path segments where numbers represent array indices and strings represent object keys.\n\n#### options\n\nType: `object`\n\n##### preferDotForIndices\n\nType: `boolean`\\\nDefault: `false`\n\nWhen `true`, numeric indices will use dot notation instead of bracket notation when not the first segment.\n\n### deepKeys(object)\n\nReturns an array of every path. Non-empty plain objects and arrays are deeply recursed and are not themselves included.\n\nThis can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal.\n\n```js\nimport {getProperty, deepKeys} from 'dot-prop';\n\nconst user = {\n\tname: {\n\t\tfirst: 'Richie',\n\t\tlast: 'Bendall',\n\t},\n\tactiveTasks: [],\n\tcurrentProject: null\n};\n\nfor (const property of deepKeys(user)) {\n\tconsole.log(`${property}: ${getProperty(user, property)}`);\n\t//=> name.first: Richie\n\t//=> name.last: Bendall\n\t//=> activeTasks: []\n\t//=> currentProject: null\n}\n```\n\nSparse arrays are supported. In general, [avoid using sparse arrays](https://github.com/sindresorhus/dot-prop/issues/109#issuecomment-1614819869).\n\n#### object\n\nType: `object | array`\n\nObject or array to get, set, or delete the `path` value.\n\nYou are allowed to pass in `undefined` as the object to the `get` and `has` functions.\n\n#### path\n\nType: `string | Array<string | number>`\n\nPath of the property in the object.\n\n**String paths**: Use `.` to separate each nested key. Use `\\\\.` if you have a `.` in the key. Array indices can be accessed using bracket notation (like `'users[0].name'`) or dot notation (like `'users.0.name'`). Both syntaxes are equivalent and will create arrays when setting values. Numeric strings in dot notation (like `'users.0.name'`) are automatically coerced to numbers.\n\n**Array paths**: Pass an array of path segments for better performance and interoperability. Numbers create arrays (like `['users', 0, 'name']`). Numeric strings are normalized to numbers for simplicity. No parsing overhead.\n\nThe following path components are invalid and result in `undefined` being returned: `__proto__`, `prototype`, `constructor`.\n\n> [!IMPORTANT]\n> Sanitizing untrusted `path` input is your responsibility. Use [`escapePath`](#escapepathpath) to treat it as a literal key.\n>\n> Apart from the components above, no protection against attacker-controlled paths is attempted. A path can reach anything reachable from the object, including inherited properties and built-ins. Only you know which parts of your object graph are safe to expose, so guard that where the path enters your program.\n\n#### value\n\nType: `unknown`\n\nValue to set at `path`.\n\n#### defaultValue\n\nType: `unknown`\n\nDefault value.\n\n### unflatten(object)\n\nConvert an object with dot paths into a nested object.\n\nUses the same path rules and escaping as the rest of the API.\n\n```js\nimport {unflatten} from 'dot-prop';\n\nconst flat = {\n\t'unicorn.name': 'Rainbow Dash',\n\t'unicorn.color': '🦄',\n\t'unicorn.treasures[0]': 'sparkles',\n\t'unicorn.treasures[1]': 'glitter',\n};\n\nunflatten(flat);\n/*\n{\n\tunicorn: {\n\t\tname: 'Rainbow Dash',\n\t\tcolor: '🦄',\n\t\ttreasures: ['sparkles', 'glitter']\n\t}\n}\n*/\n```\n\n> [!IMPORTANT]\n> Numeric keys become array indices, so nodes with numeric keys turn into arrays. The `deepKeys` → `unflatten` round-trip is therefore lossy for such objects (`{a: {0: 'x', name: 'y'}}` → `{a: ['x']}`), and untrusted input can yield arrays where you expected objects.\n","publish_time":1784636377088,"_cnpm_publish_time":1784636377088,"dist-tags":{"latest":"10.2.0","legacy":"4.2.1"}}