Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor tweaks #91

Merged
merged 1 commit into from Jan 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 24 additions & 27 deletions index.js
Expand Up @@ -180,11 +180,11 @@ export function getProperty(object, path, value) {
return value;
}

for (let i = 0; i < pathArray.length; i++) {
const key = pathArray[i];
for (let index = 0; index < pathArray.length; index++) {
const key = pathArray[index];

if (isStringIndex(object, key)) {
object = i === pathArray.length - 1 ? undefined : null;
object = index === pathArray.length - 1 ? undefined : null;
} else {
object = object[key];
}
Expand All @@ -195,7 +195,7 @@ export function getProperty(object, path, value) {
// 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 (i !== pathArray.length - 1) {
if (index !== pathArray.length - 1) {
return value;
}

Expand All @@ -214,19 +214,20 @@ export function setProperty(object, path, value) {
const root = object;
const pathArray = getPathSegments(path);

for (let i = 0; i < pathArray.length; i++) {
const p = pathArray[i];
assertNotStringIndex(object, p);
for (let index = 0; index < pathArray.length; index++) {
const key = pathArray[index];

if (!isObject(object[p])) {
object[p] = Number.isInteger(pathArray[i + 1]) ? [] : {};
assertNotStringIndex(object, key);

if (!isObject(object[key])) {
object[key] = typeof pathArray[index + 1] === 'number' ? [] : {};
}

if (i === pathArray.length - 1) {
object[p] = value;
if (index === pathArray.length - 1) {
object[key] = value;
}

object = object[p];
object = object[key];
}

return root;
Expand All @@ -239,16 +240,17 @@ export function deleteProperty(object, path) {

const pathArray = getPathSegments(path);

for (let i = 0; i < pathArray.length; i++) {
const p = pathArray[i];
assertNotStringIndex(object, p);
for (let index = 0; index < pathArray.length; index++) {
const key = pathArray[index];

assertNotStringIndex(object, key);

if (i === pathArray.length - 1) {
delete object[p];
if (index === pathArray.length - 1) {
delete object[key];
return true;
}

object = object[p];
object = object[key];

if (!isObject(object)) {
return false;
Expand All @@ -266,17 +268,12 @@ export function hasProperty(object, path) {
return false;
}

// eslint-disable-next-line unicorn/no-for-loop
for (let i = 0; i < pathArray.length; i++) {
if (isObject(object)) {
if (!(pathArray[i] in object && !isStringIndex(object, pathArray[i]))) {
return false;
}

object = object[pathArray[i]];
} else {
for (const key of pathArray) {
if (!isObject(object) || !(key in object) || isStringIndex(object, key)) {
return false;
}

object = object[key];
}

return true;
Expand Down