pax_global_header00006660000000000000000000000064142033422310014504gustar00rootroot0000000000000052 comment=aff8acb5076257d14dd5f9cfefbbe5526b81aa04 dot-prop-7.2.0/000077500000000000000000000000001420334223100132565ustar00rootroot00000000000000dot-prop-7.2.0/.editorconfig000066400000000000000000000002571420334223100157370ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 dot-prop-7.2.0/.gitattributes000066400000000000000000000000231420334223100161440ustar00rootroot00000000000000* text=auto eol=lf dot-prop-7.2.0/.github/000077500000000000000000000000001420334223100146165ustar00rootroot00000000000000dot-prop-7.2.0/.github/funding.yml000066400000000000000000000001621420334223100167720ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/dot-prop custom: https://sindresorhus.com/donate dot-prop-7.2.0/.github/security.md000066400000000000000000000002631420334223100170100ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. dot-prop-7.2.0/.github/workflows/000077500000000000000000000000001420334223100166535ustar00rootroot00000000000000dot-prop-7.2.0/.github/workflows/main.yml000066400000000000000000000006641420334223100203300ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 16 - 14 - 12 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test dot-prop-7.2.0/.gitignore000066400000000000000000000000271420334223100152450ustar00rootroot00000000000000node_modules yarn.lock dot-prop-7.2.0/.npmrc000066400000000000000000000000231420334223100143710ustar00rootroot00000000000000package-lock=false dot-prop-7.2.0/benchmark.js000066400000000000000000000100631420334223100155460ustar00rootroot00000000000000import Benchmark from 'benchmark'; import {getProperty, setProperty, hasProperty, deleteProperty} from './index.js'; const suite = new Benchmark.Suite(); suite .add('getProperty', () => { const fixture1 = {foo: {bar: 1}}; getProperty(fixture1); fixture1[''] = 'foo'; getProperty(fixture1, ''); getProperty(fixture1, 'foo'); getProperty({foo: 1}, 'foo'); getProperty({foo: null}, 'foo'); getProperty({foo: undefined}, 'foo'); getProperty({foo: {bar: true}}, 'foo.bar'); getProperty({foo: {bar: {baz: true}}}, 'foo.bar.baz'); getProperty({foo: {bar: {baz: null}}}, 'foo.bar.baz'); getProperty({foo: {bar: 'a'}}, 'foo.fake'); getProperty({foo: {bar: 'a'}}, 'foo.fake.fake2'); getProperty({'\\': true}, '\\'); getProperty({'\\foo': true}, '\\foo'); getProperty({'bar\\': true}, 'bar\\'); getProperty({'foo\\bar': true}, 'foo\\bar'); getProperty({'\\.foo': true}, '\\\\.foo'); getProperty({'bar\\.': true}, 'bar\\\\.'); getProperty({'foo\\.bar': true}, 'foo\\\\.bar'); const fixture2 = {}; Object.defineProperty(fixture2, 'foo', { value: 'bar', enumerable: false, }); getProperty(fixture2, 'foo'); getProperty({}, 'hasOwnProperty'); function fn() {} fn.foo = {bar: 1}; getProperty(fn); getProperty(fn, 'foo'); getProperty(fn, 'foo.bar'); const fixture3 = {foo: null}; getProperty(fixture3, 'foo.bar'); getProperty({'foo.baz': {bar: true}}, 'foo\\.baz.bar'); getProperty({'fo.ob.az': {bar: true}}, 'fo\\.ob\\.az.bar'); getProperty(null, 'foo.bar', false); getProperty('foo', 'foo.bar', false); getProperty([], 'foo.bar', false); getProperty(undefined, 'foo.bar', false); }) .add('setProperty', () => { const func = () => 'test'; let fixture1 = {}; setProperty(fixture1, 'foo', 2); fixture1 = {foo: {bar: 1}}; setProperty(fixture1, 'foo.bar', 2); setProperty(fixture1, 'foo.bar.baz', 3); setProperty(fixture1, 'foo.bar', 'test'); setProperty(fixture1, 'foo.bar', null); setProperty(fixture1, 'foo.bar', false); setProperty(fixture1, 'foo.bar', undefined); setProperty(fixture1, 'foo.fake.fake2', 'fake'); setProperty(fixture1, 'foo.function', func); function fn() {} setProperty(fn, 'foo.bar', 1); fixture1.fn = fn; setProperty(fixture1, 'fn.bar.baz', 2); const fixture2 = {foo: null}; setProperty(fixture2, 'foo.bar', 2); const fixture3 = {}; setProperty(fixture3, '', 3); setProperty(fixture1, 'foo\\.bar.baz', true); setProperty(fixture1, 'fo\\.ob\\.ar.baz', true); }) .add('hasProperty', () => { const fixture1 = {foo: {bar: 1}}; hasProperty(fixture1); hasProperty(fixture1, 'foo'); hasProperty({foo: 1}, 'foo'); hasProperty({foo: null}, 'foo'); hasProperty({foo: undefined}, 'foo'); hasProperty({foo: {bar: true}}, 'foo.bar'); hasProperty({foo: {bar: {baz: true}}}, 'foo.bar.baz'); hasProperty({foo: {bar: {baz: null}}}, 'foo.bar.baz'); hasProperty({foo: {bar: 'a'}}, 'foo.fake.fake2'); function fn() {} fn.foo = {bar: 1}; hasProperty(fn); hasProperty(fn, 'foo'); hasProperty(fn, 'foo.bar'); hasProperty({'foo.baz': {bar: true}}, 'foo\\.baz.bar'); hasProperty({'fo.ob.az': {bar: true}}, 'fo\\.ob\\.az.bar'); }) .add('deleteProperty', () => { const func = () => 'test'; func.foo = 'bar'; const inner = { a: 'a', b: 'b', c: 'c', func, }; const fixture1 = { foo: { bar: { baz: inner, }, }, top: { dog: 'sindre', }, }; deleteProperty(fixture1, 'foo.bar.baz.c'); deleteProperty(fixture1, 'top'); deleteProperty(fixture1, 'foo.bar.baz.func.foo'); deleteProperty(fixture1, 'foo.bar.baz.func'); setProperty(fixture1, 'foo\\.bar.baz', true); deleteProperty(fixture1, 'foo\\.bar.baz'); const fixture2 = {}; setProperty(fixture2, 'foo.bar\\.baz', true); deleteProperty(fixture2, 'foo.bar\\.baz'); fixture2.dotted = { sub: { 'dotted.prop': 'foo', other: 'prop', }, }; deleteProperty(fixture2, 'dotted.sub.dotted\\.prop'); }) .on('cycle', event => { console.log(String(event.target)); }) .on('complete', () => { console.log('Finished'); }) .run({async: true}); dot-prop-7.2.0/index.d.ts000066400000000000000000000102131420334223100151540ustar00rootroot00000000000000import {Get} from 'type-fest'; /** Get the value of the property at the given path. @param object - Object or array to get the `path` value. @param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key. @param defaultValue - Default value. @example ``` import {getProperty} from 'dot-prop'; getProperty({foo: {bar: 'unicorn'}}, 'foo.bar'); //=> 'unicorn' getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep'); //=> undefined getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value'); //=> 'default value' getProperty({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot'); //=> 'unicorn' getProperty({foo: [{bar: 'unicorn'}]}, 'foo[0].bar'); //=> 'unicorn' ``` */ export function getProperty( object: ObjectType, path: PathType, defaultValue?: DefaultValue ): ObjectType extends Record | unknown[] ? (unknown extends Get ? DefaultValue : Get) : undefined; /** Set the property at the given path to the given value. @param object - Object or array to set the `path` value. @param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key. @param value - Value to set at `path`. @returns The object. @example ``` import {setProperty} from 'dot-prop'; const object = {foo: {bar: 'a'}}; setProperty(object, 'foo.bar', 'b'); console.log(object); //=> {foo: {bar: 'b'}} const foo = setProperty({}, 'foo.bar', 'c'); console.log(foo); //=> {foo: {bar: 'c'}} setProperty(object, 'foo.baz', 'x'); console.log(object); //=> {foo: {bar: 'b', baz: 'x'}} setProperty(object, 'foo.biz[0]', 'a'); console.log(object); //=> {foo: {bar: 'b', baz: 'x', biz: ['a']}} ``` */ export function setProperty>( object: ObjectType, path: string, value: unknown ): ObjectType; /** Check whether the property at the given path exists. @param object - Object or array to test the `path` value. @param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key. @example ``` import {hasProperty} from 'dot-prop'; hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar'); //=> true ``` */ export function hasProperty(object: Record | undefined, path: string): boolean; /** Delete the property at the given path. @param object - Object or array to delete the `path` value. @param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key. @returns A boolean of whether the property existed before being deleted. @example ``` import {deleteProperty} from 'dot-prop'; const object = {foo: {bar: 'a'}}; deleteProperty(object, 'foo.bar'); console.log(object); //=> {foo: {}} object.foo.bar = {x: 'y', y: 'x'}; deleteProperty(object, 'foo.bar.x'); console.log(object); //=> {foo: {bar: {y: 'x'}}} ``` */ export function deleteProperty(object: Record, path: string): boolean; /** Escape special characters in a path. Useful for sanitizing user input. @param path - The dot path to sanitize. @example ``` import {getProperty, escapePath} from 'dot-prop'; const object = { foo: { bar: 'πŸ‘ΈπŸ» You found me Mario!', }, 'foo.bar' : 'πŸ„ The princess is in another castle!', }; const escapedPath = escapePath('foo.bar'); console.log(getProperty(object, escapedPath)); //=> 'πŸ„ The princess is in another castle!' ``` */ export function escapePath(path: string): string; /** Returns an array of every path. Plain objects are deeply recursed and are not themselves included. This can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal. @param object - The object to iterate through. @example ``` import {getProperty, deepKeys} from 'dot-prop'; const user = { name: { first: 'Richie', last: 'Bendall', }, }; for (const property of deepKeys(user)) { console.log(`${property}: ${getProperty(user, property)}`); //=> name.first: Richie //=> name.last: Bendall } ``` */ export function deepKeys(object: unknown): string[]; dot-prop-7.2.0/index.js000066400000000000000000000147411420334223100147320ustar00rootroot00000000000000const isObject = value => { const type = typeof value; return value !== null && (type === 'object' || type === 'function'); }; const disallowedKeys = new Set([ '__proto__', 'prototype', 'constructor', ]); const digits = new Set('0123456789'); function getPathSegments(path) { const parts = []; let currentSegment = ''; let currentPart = 'start'; let isIgnoring = false; for (const character of path) { switch (character) { case '\\': if (currentPart === 'index') { throw new Error('Invalid character in an index'); } if (currentPart === 'indexEnd') { throw new Error('Invalid character after an index'); } if (isIgnoring) { currentSegment += character; } currentPart = 'property'; isIgnoring = !isIgnoring; break; case '.': if (currentPart === 'index') { throw new Error('Invalid character in an index'); } if (currentPart === 'indexEnd') { currentPart = 'property'; break; } if (isIgnoring) { isIgnoring = false; currentSegment += character; break; } if (disallowedKeys.has(currentSegment)) { return []; } parts.push(currentSegment); currentSegment = ''; currentPart = 'property'; break; case '[': if (currentPart === 'index') { throw new Error('Invalid character in an index'); } if (currentPart === 'indexEnd') { currentPart = 'index'; break; } if (isIgnoring) { isIgnoring = false; currentSegment += character; break; } if (currentPart === 'property') { if (disallowedKeys.has(currentSegment)) { return []; } parts.push(currentSegment); currentSegment = ''; } currentPart = 'index'; break; case ']': if (currentPart === 'index') { parts.push(Number.parseInt(currentSegment, 10)); currentSegment = ''; currentPart = 'indexEnd'; break; } if (currentPart === 'indexEnd') { throw new Error('Invalid character after an index'); } // Falls through default: if (currentPart === 'index' && !digits.has(character)) { throw new Error('Invalid character in an index'); } if (currentPart === 'indexEnd') { throw new Error('Invalid character after an index'); } if (currentPart === 'start') { currentPart = 'property'; } if (isIgnoring) { isIgnoring = false; currentSegment += '\\'; } currentSegment += character; } } if (isIgnoring) { currentSegment += '\\'; } switch (currentPart) { case 'property': { if (disallowedKeys.has(currentSegment)) { return []; } parts.push(currentSegment); break; } case 'index': { throw new Error('Index was not closed'); } case 'start': { parts.push(''); break; } // No default } return parts; } function isStringIndex(object, key) { if (typeof key !== 'number' && Array.isArray(object)) { const index = Number.parseInt(key, 10); return Number.isInteger(index) && object[index] === object[key]; } return false; } function assertNotStringIndex(object, key) { if (isStringIndex(object, key)) { throw new Error('Cannot use string index'); } } export function getProperty(object, path, value) { if (!isObject(object) || typeof path !== 'string') { return value === undefined ? object : value; } const pathArray = getPathSegments(path); if (pathArray.length === 0) { return value; } for (let index = 0; index < pathArray.length; index++) { const key = pathArray[index]; if (isStringIndex(object, key)) { object = index === pathArray.length - 1 ? undefined : null; } else { object = object[key]; } if (object === undefined || object === null) { // `object` is either `undefined` or `null` so we want to stop the loop, and // if this is not the last bit of the path, and // if it didn't return `undefined` // it would return `null` if `object` is `null` // but we want `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied value, not `null` if (index !== pathArray.length - 1) { return value; } break; } } return object === undefined ? value : object; } export function setProperty(object, path, value) { if (!isObject(object) || typeof path !== 'string') { return object; } const root = object; const pathArray = getPathSegments(path); for (let index = 0; index < pathArray.length; index++) { const key = pathArray[index]; assertNotStringIndex(object, key); if (index === pathArray.length - 1) { object[key] = value; } else if (!isObject(object[key])) { object[key] = typeof pathArray[index + 1] === 'number' ? [] : {}; } object = object[key]; } return root; } export function deleteProperty(object, path) { if (!isObject(object) || typeof path !== 'string') { return false; } const pathArray = getPathSegments(path); for (let index = 0; index < pathArray.length; index++) { const key = pathArray[index]; assertNotStringIndex(object, key); if (index === pathArray.length - 1) { delete object[key]; return true; } object = object[key]; if (!isObject(object)) { return false; } } } export function hasProperty(object, path) { if (!isObject(object) || typeof path !== 'string') { return false; } const pathArray = getPathSegments(path); if (pathArray.length === 0) { return false; } for (const key of pathArray) { if (!isObject(object) || !(key in object) || isStringIndex(object, key)) { return false; } object = object[key]; } return true; } export function escapePath(path) { if (typeof path !== 'string') { throw new TypeError('Expected a string'); } return path.replace(/[\\.[]/g, '\\$&'); } // The keys returned by Object.entries() for arrays are strings function entries(value) { if (Array.isArray(value)) { return value.map((value, index) => [index, value]); } return Object.entries(value); } function stringifyPath(pathSegments) { let result = ''; for (let [index, segment] of entries(pathSegments)) { if (typeof segment === 'number') { result += `[${segment}]`; } else { segment = escapePath(segment); result += index === 0 ? segment : `.${segment}`; } } return result; } function * deepKeysIterator(object, currentPath = []) { if (!isObject(object)) { if (currentPath.length > 0) { yield stringifyPath(currentPath); } return; } for (const [key, value] of entries(object)) { yield * deepKeysIterator(value, [...currentPath, key]); } } export function deepKeys(object) { return [...deepKeysIterator(object)]; } dot-prop-7.2.0/index.test-d.ts000066400000000000000000000016511420334223100161370ustar00rootroot00000000000000import {expectTypeOf} from 'expect-type'; import {getProperty, setProperty, hasProperty, deleteProperty, deepKeys} from './index.js'; expectTypeOf(getProperty({foo: {bar: 'unicorn'}}, 'foo.bar')).toBeString(); expectTypeOf(getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep')).toBeUndefined(); expectTypeOf( getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value'), ).toBeString(); expectTypeOf( getProperty({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot'), // @ts-expect-error type-fest's `Get` not smart enough to deal with escaped dots ).toEqualTypeOf(); const object = {foo: {bar: 'a'}}; expectTypeOf(setProperty(object, 'foo.bar', 'b')).toEqualTypeOf(object); expectTypeOf(hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar')).toEqualTypeOf(); expectTypeOf(deleteProperty({foo: {bar: 'a'}}, 'foo.bar')).toEqualTypeOf(); expectTypeOf(deepKeys({foo: {bar: 'a'}})).toEqualTypeOf(); dot-prop-7.2.0/license000066400000000000000000000021351420334223100146240ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. dot-prop-7.2.0/package.json000066400000000000000000000016371420334223100155530ustar00rootroot00000000000000{ "name": "dot-prop", "version": "7.2.0", "description": "Get, set, or delete a property from a nested object using a dot path", "license": "MIT", "repository": "sindresorhus/dot-prop", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": "./index.js", "engines": { "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "test": "xo && ava && tsc", "bench": "node benchmark.js" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "object", "prop", "property", "dot", "path", "get", "set", "delete", "access", "notation", "dotty" ], "dependencies": { "type-fest": "^2.11.2" }, "devDependencies": { "ava": "^4.0.1", "benchmark": "^2.1.4", "expect-type": "^0.13.0", "typescript": "^4.5.5", "xo": "^0.48.0" } } dot-prop-7.2.0/readme.md000066400000000000000000000073001420334223100150350ustar00rootroot00000000000000# dot-prop > Get, set, or delete a property from a nested object using a dot path ## Install ```sh npm install dot-prop ``` ## Usage ```js import {getProperty, setProperty, hasProperty, deleteProperty} from 'dot-prop'; // Getter getProperty({foo: {bar: 'unicorn'}}, 'foo.bar'); //=> 'unicorn' getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep'); //=> undefined getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value'); //=> 'default value' getProperty({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot'); //=> 'unicorn' getProperty({foo: [{bar: 'unicorn'}]}, 'foo[0].bar'); //=> 'unicorn' // Setter const object = {foo: {bar: 'a'}}; setProperty(object, 'foo.bar', 'b'); console.log(object); //=> {foo: {bar: 'b'}} const foo = setProperty({}, 'foo.bar', 'c'); console.log(foo); //=> {foo: {bar: 'c'}} setProperty(object, 'foo.baz', 'x'); console.log(object); //=> {foo: {bar: 'b', baz: 'x'}} setProperty(object, 'foo.biz.0', 'a'); console.log(object); //=> {foo: {bar: 'b', baz: 'x', biz: ['a']}} // Has hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar'); //=> true // Deleter const object = {foo: {bar: 'a'}}; deleteProperty(object, 'foo.bar'); console.log(object); //=> {foo: {}} object.foo.bar = {x: 'y', y: 'x'}; deleteProperty(object, 'foo.bar.x'); console.log(object); //=> {foo: {bar: {y: 'x'}}} ``` ## API ### getProperty(object, path, defaultValue?) Get the value of the property at the given path. Returns the value if any. ### setProperty(object, path, value) Set the property at the given path to the given value. Returns the object. ### hasProperty(object, path) Check whether the property at the given path exists. Returns a boolean. ### deleteProperty(object, path) Delete the property at the given path. Returns a boolean of whether the property existed before being deleted. ### escapePath(path) Escape special characters in a path. Useful for sanitizing user input. ```js import {getProperty, escapePath} from 'dot-prop'; const object = { foo: { bar: 'πŸ‘ΈπŸ» You found me Mario!', }, 'foo.bar' : 'πŸ„ The princess is in another castle!', }; const escapedPath = escapePath('foo.bar'); console.log(getProperty(object, escapedPath)); //=> 'πŸ„ The princess is in another castle!' ``` ### deepKeys(object) Returns an array of every path. Plain objects are deeply recursed and are not themselves included. This can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal. ```js import {getProperty, deepKeys} from 'dot-prop'; const user = { name: { first: 'Richie', last: 'Bendall', }, }; for (const property of deepKeys(user)) { console.log(`${property}: ${getProperty(user, property)}`); //=> name.first: Richie //=> name.last: Bendall } ``` #### object Type: `object | array` Object or array to get, set, or delete the `path` value. You are allowed to pass in `undefined` as the object to the `get` and `has` functions. #### path Type: `string` Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key. The following path components are invalid and results in `undefined` being returned: `__proto__`, `prototype`, `constructor`. #### value Type: `unknown` Value to set at `path`. #### defaultValue Type: `unknown` Default value. ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
dot-prop-7.2.0/test.js000066400000000000000000000276371420334223100146120ustar00rootroot00000000000000import test from 'ava'; import {getProperty, setProperty, hasProperty, deleteProperty, escapePath, deepKeys} from './index.js'; test('getProperty', t => { const fixture1 = {foo: {bar: 1}}; t.is(getProperty(fixture1), fixture1); fixture1[''] = 'foo'; t.is(getProperty(fixture1, ''), 'foo'); t.is(getProperty(fixture1, 'foo'), fixture1.foo); t.is(getProperty({foo: 1}, 'foo'), 1); t.is(getProperty({foo: null}, 'foo'), null); t.is(getProperty({foo: undefined}, 'foo'), undefined); t.true(getProperty({foo: {bar: true}}, 'foo.bar')); t.true(getProperty({foo: {bar: {baz: true}}}, 'foo.bar.baz')); t.is(getProperty({foo: {bar: {baz: null}}}, 'foo.bar.baz'), null); t.is(getProperty({foo: {bar: 'a'}}, 'foo.fake'), undefined); t.is(getProperty({foo: {bar: 'a'}}, 'foo.fake.fake2'), undefined); t.is(getProperty({foo: {bar: 'a'}}, 'foo.fake.fake2', 'some value'), 'some value'); t.is(getProperty({foo: {}}, 'foo.fake', 'some value'), 'some value'); t.true(getProperty({'\\': true}, '\\')); t.true(getProperty({'\\foo': true}, '\\foo')); t.true(getProperty({'\\foo': true}, '\\\\foo')); t.true(getProperty({'foo\\': true}, 'foo\\\\')); t.true(getProperty({'bar\\': true}, 'bar\\')); t.true(getProperty({'foo\\bar': true}, 'foo\\bar')); t.true(getProperty({'\\': {foo: true}}, '\\\\.foo')); t.true(getProperty({'bar\\.': true}, 'bar\\\\\\.')); t.true(getProperty({'foo\\': { bar: true, }}, 'foo\\\\.bar')); t.is(getProperty({foo: 1}, 'foo.bar'), undefined); t.true(getProperty({'foo\\': true}, 'foo\\')); const fixture2 = {}; Object.defineProperty(fixture2, 'foo', { value: 'bar', enumerable: false, }); t.is(getProperty(fixture2, 'foo'), 'bar'); t.is(getProperty({}, 'hasOwnProperty'), Object.prototype.hasOwnProperty); function fn() {} fn.foo = {bar: 1}; t.is(getProperty(fn), fn); t.is(getProperty(fn, 'foo'), fn.foo); t.is(getProperty(fn, 'foo.bar'), 1); const f3 = {foo: null}; t.is(getProperty(f3, 'foo.bar'), undefined); t.is(getProperty(f3, 'foo.bar', 'some value'), 'some value'); t.true(getProperty({'foo.baz': {bar: true}}, 'foo\\.baz.bar')); t.true(getProperty({'fo.ob.az': {bar: true}}, 'fo\\.ob\\.az.bar')); t.false(getProperty(null, 'foo.bar', false)); t.false(getProperty('foo', 'foo.bar', false)); t.false(getProperty([], 'foo.bar', false)); t.false(getProperty(undefined, 'foo.bar', false)); class F4Class {} F4Class.prototype.foo = 1; const f4 = new F4Class(); t.is(getProperty(f4, 'foo'), 1); // #46 t.true(getProperty({'': {'': {'': true}}}, '..')); t.true(getProperty({'': {'': true}}, '.')); }); test('getProperty - with array indexes', t => { t.true(getProperty([true, false, false], '[0]')); t.true(getProperty([[false, true, false], false, false], '[0][1]')); t.true(getProperty([{foo: [true]}], '[0].foo[0]')); t.true(getProperty({foo: [0, {bar: true}]}, 'foo[1].bar')); t.false(getProperty(['a', 'b', 'c'], '3', false)); t.false(getProperty([{foo: [1]}], '[0].bar[0]', false)); t.false(getProperty([{foo: [1]}], '[0].foo[1]', false)); t.false(getProperty({foo: [0, {bar: 2}]}, 'foo[0].bar', false)); t.false(getProperty({foo: [0, {bar: 2}]}, 'foo[2].bar', false)); t.false(getProperty({foo: [0, {bar: 2}]}, 'foo[1].biz', false)); t.false(getProperty({foo: [0, {bar: 2}]}, 'bar[0].bar', false)); t.true(getProperty({ bar: { '[0]': true, }, }, 'bar.\\[0]')); t.true(getProperty({ bar: { '': [true], }, }, 'bar.[0]')); t.throws(() => getProperty({ 'foo[5[': true, }, 'foo[5['), { message: 'Invalid character in an index', }); t.throws(() => getProperty({ 'foo[5': { bar: true, }, }, 'foo[5.bar'), { message: 'Invalid character in an index', }); t.true(getProperty({ 'foo[5]': { bar: true, }, }, 'foo\\[5].bar')); t.throws(() => getProperty({ 'foo[5\\]': { bar: true, }, }, 'foo[5\\].bar'), { message: 'Invalid character in an index', }); t.throws(() => getProperty({ 'foo[5': true, }, 'foo[5'), { message: 'Index was not closed', }); t.throws(() => getProperty({ 'foo[bar]': true, }, 'foo[bar]'), { message: 'Invalid character in an index', }); t.false(getProperty({}, 'constructor[0]', false)); t.throws(() => getProperty({}, 'foo[constructor]', false), { message: 'Invalid character in an index', }); t.false(getProperty([], 'foo[0].bar', false)); t.true(getProperty({foo: [{bar: true}]}, 'foo[0].bar')); t.false(getProperty({foo: ['bar']}, 'foo[1]', false)); t.false(getProperty([true], '0', false)); t.false(getProperty({foo: [true]}, 'foo.0', false)); t.true(getProperty({foo: { 0: true, }}, 'foo.0')); t.true(getProperty([{ '[1]': true, }, false, false], '[0].\\[1]')); t.true(getProperty({foo: {'[0]': true}}, 'foo.\\[0]')); t.throws(() => getProperty({foo: {'[0]': true}}, 'foo.[0\\]'), { message: 'Invalid character in an index', }); t.true(getProperty({foo: {'\\': [true]}}, 'foo.\\\\[0]')); t.throws(() => getProperty({foo: {'[0]': true}}, 'foo.[0\\]'), { message: 'Invalid character in an index', }); t.throws(() => getProperty({'foo[0': {'9]': true}}, 'foo[0.9]'), { message: 'Invalid character in an index', }); t.throws(() => getProperty({'foo[-1]': true}, 'foo[-1]'), { message: 'Invalid character in an index', }); }); test('setProperty', t => { const func = () => 'test'; let fixture1 = {}; const o1 = setProperty(fixture1, 'foo', 2); t.is(fixture1.foo, 2); t.is(o1, fixture1); fixture1 = {foo: {bar: 1}}; setProperty(fixture1, 'foo.bar', 2); t.is(fixture1.foo.bar, 2); setProperty(fixture1, 'foo.bar.baz', 3); t.is(fixture1.foo.bar.baz, 3); setProperty(fixture1, 'foo.bar', 'test'); t.is(fixture1.foo.bar, 'test'); setProperty(fixture1, 'foo.bar', null); t.is(fixture1.foo.bar, null); setProperty(fixture1, 'foo.bar', false); t.is(fixture1.foo.bar, false); setProperty(fixture1, 'foo.bar', undefined); t.is(fixture1.foo.bar, undefined); setProperty(fixture1, 'foo.fake.fake2', 'fake'); t.is(fixture1.foo.fake.fake2, 'fake'); setProperty(fixture1, 'foo.function', func); t.is(fixture1.foo.function, func); function fn() {} setProperty(fn, 'foo.bar', 1); t.is(fn.foo.bar, 1); fixture1.fn = fn; setProperty(fixture1, 'fn.bar.baz', 2); t.is(fixture1.fn.bar.baz, 2); const fixture2 = {foo: null}; setProperty(fixture2, 'foo.bar', 2); t.is(fixture2.foo.bar, 2); const fixture3 = {}; setProperty(fixture3, '', 3); t.is(fixture3[''], 3); setProperty(fixture1, 'foo\\.bar.baz', true); t.is(fixture1['foo.bar'].baz, true); setProperty(fixture1, 'fo\\.ob\\.ar.baz', true); t.true(fixture1['fo.ob.ar'].baz); const fixture4 = 'noobject'; const output4 = setProperty(fixture4, 'foo.bar', 2); t.is(fixture4, 'noobject'); t.is(output4, fixture4); const fixture5 = []; setProperty(fixture5, '[1]', true); t.is(fixture5[1], true); setProperty(fixture5, '[0].foo[0]', true); t.is(fixture5[0].foo[0], true); t.throws(() => setProperty(fixture5, '1', true), { message: 'Cannot use string index', }); t.throws(() => setProperty(fixture5, '0.foo.0', true), { message: 'Cannot use string index', }); const fixture6 = {}; setProperty(fixture6, 'foo[0].bar', true); t.true(fixture6.foo[0].bar); t.deepEqual(fixture6, { foo: [{ bar: true, }], }); const fixture7 = {foo: ['bar', 'baz']}; setProperty(fixture7, 'foo.length', 1); t.is(fixture7.foo.length, 1); t.deepEqual(fixture7, {foo: ['bar']}); }); test('deleteProperty', t => { const func = () => 'test'; func.foo = 'bar'; const inner = { a: 'a', b: 'b', c: 'c', func, }; const fixture1 = { foo: { bar: { baz: inner, }, }, top: { dog: 'sindre', }, }; t.is(fixture1.foo.bar.baz.c, 'c'); t.true(deleteProperty(fixture1, 'foo.bar.baz.c')); t.is(fixture1.foo.bar.baz.c, undefined); t.is(fixture1.top.dog, 'sindre'); t.true(deleteProperty(fixture1, 'top')); t.is(fixture1.top, undefined); t.is(fixture1.foo.bar.baz.func.foo, 'bar'); t.true(deleteProperty(fixture1, 'foo.bar.baz.func.foo')); t.is(fixture1.foo.bar.baz.func.foo, undefined); t.is(fixture1.foo.bar.baz.func, func); t.true(deleteProperty(fixture1, 'foo.bar.baz.func')); t.is(fixture1.foo.bar.baz.func, undefined); setProperty(fixture1, 'foo\\.bar.baz', true); t.true(fixture1['foo.bar'].baz); t.true(deleteProperty(fixture1, 'foo\\.bar.baz')); t.is(fixture1['foo.bar'].baz, undefined); const fixture2 = {}; setProperty(fixture2, 'foo.bar\\.baz', true); t.true(fixture2.foo['bar.baz']); t.true(deleteProperty(fixture2, 'foo.bar\\.baz')); t.is(fixture2.foo['bar.baz'], undefined); fixture2.dotted = { sub: { 'dotted.prop': 'foo', other: 'prop', }, }; t.true(deleteProperty(fixture2, 'dotted.sub.dotted\\.prop')); t.is(fixture2.dotted.sub['dotted.prop'], undefined); t.is(fixture2.dotted.sub.other, 'prop'); const fixture3 = {foo: null}; t.false(deleteProperty(fixture3, 'foo.bar')); t.deepEqual(fixture3, {foo: null}); const fixture4 = [{ top: { dog: 'sindre', }, }]; t.throws(() => deleteProperty(fixture4, '0.top.dog'), { message: 'Cannot use string index', }); t.true(deleteProperty(fixture4, '[0].top.dog')); t.deepEqual(fixture4, [{top: {}}]); const fixture5 = { foo: [{ bar: ['foo', 'bar'], }], }; deleteProperty(fixture5, 'foo[0].bar[0]'); const fixtureArray = []; fixtureArray[1] = 'bar'; t.deepEqual(fixture5, { foo: [{ bar: fixtureArray, }], }); const fixture6 = {}; setProperty(fixture6, 'foo.bar.0', 'fizz'); }); test('hasProperty', t => { const fixture1 = {foo: {bar: 1}}; t.false(hasProperty(fixture1)); t.true(hasProperty(fixture1, 'foo')); t.true(hasProperty({foo: 1}, 'foo')); t.true(hasProperty({foo: null}, 'foo')); t.true(hasProperty({foo: undefined}, 'foo')); t.true(hasProperty({foo: {bar: true}}, 'foo.bar')); t.true(hasProperty({foo: {bar: {baz: true}}}, 'foo.bar.baz')); t.true(hasProperty({foo: {bar: {baz: null}}}, 'foo.bar.baz')); t.false(hasProperty({foo: {bar: 'a'}}, 'foo.fake.fake2')); t.false(hasProperty({foo: null}, 'foo.bar')); t.false(hasProperty({foo: ''}, 'foo.bar')); function fn() {} fn.foo = {bar: 1}; t.false(hasProperty(fn)); t.true(hasProperty(fn, 'foo')); t.true(hasProperty(fn, 'foo.bar')); t.true(hasProperty({'foo.baz': {bar: true}}, 'foo\\.baz.bar')); t.true(hasProperty({'fo.ob.az': {bar: true}}, 'fo\\.ob\\.az.bar')); t.false(hasProperty(undefined, 'fo\\.ob\\.az.bar')); t.false(hasProperty({ foo: [{bar: ['bar', 'bizz']}], }, 'foo[0].bar.1')); t.false(hasProperty({ foo: [{bar: ['bar', 'bizz']}], }, 'foo[0].bar.2')); t.false(hasProperty({ foo: [{bar: ['bar', 'bizz']}], }, 'foo[1].bar.1')); t.true(hasProperty({ foo: [{bar: { 1: 'bar', }}], }, 'foo[0].bar.1')); }); test('escapePath', t => { t.is(escapePath('foo.bar[0]'), 'foo\\.bar\\[0]'); t.is(escapePath('foo\\.bar[0]'), 'foo\\\\\\.bar\\[0]'); t.is(escapePath('foo\\\.bar[0]'), 'foo\\\\\\.bar\\[0]'); // eslint-disable-line no-useless-escape t.is(escapePath('foo\\\\.bar[0]'), 'foo\\\\\\\\\\.bar\\[0]'); t.is(escapePath('foo\\\\.bar\\\\[0]'), 'foo\\\\\\\\\\.bar\\\\\\\\\\[0]'); t.is(escapePath('foo[0].bar'), 'foo\\[0]\\.bar'); t.is(escapePath('foo.bar[0].baz'), 'foo\\.bar\\[0]\\.baz'); t.is(escapePath('[0].foo'), '\\[0]\\.foo'); t.is(escapePath(''), ''); t.throws(() => { escapePath(0); }, { instanceOf: TypeError, message: 'Expected a string', }); }); test('deepKeys', t => { const object = { 'a.b': { c: { d: [1, 2, 3], e: 'πŸ¦„', f: 0, }, '': { a: 0, }, }, '': { a: 0, }, }; const keys = deepKeys(object); t.deepEqual(keys, [ 'a\\.b.c.d[0]', 'a\\.b.c.d[1]', 'a\\.b.c.d[2]', 'a\\.b.c.e', 'a\\.b.c.f', 'a\\.b..a', '.a', ]); for (const key of keys) { t.true(hasProperty(object, key)); } t.deepEqual(deepKeys([]), []); t.deepEqual(deepKeys(0), []); }); test('prevent setting/getting `__proto__`', t => { setProperty({}, '__proto__.unicorn', 'πŸ¦„'); t.not({}.unicorn, 'πŸ¦„'); // eslint-disable-line no-use-extend-native/no-use-extend-native t.is(getProperty({}, '__proto__'), undefined); }); test('return default value if path is invalid', t => { t.is(getProperty({}, 'constructor', 'πŸ¦„'), 'πŸ¦„'); }); dot-prop-7.2.0/tsconfig.json000066400000000000000000000001671420334223100157710ustar00rootroot00000000000000{ "compilerOptions": { "lib": [ "es2020" ], "strict": true, "noEmit": true }, "include": [ "*.ts" ] }