Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!isObj(obj) || !path) {
return
}
path = toString(path)
if (path in obj) {
obj[path as string] = value
return
}
const pathArr = getPathSegments(path)
for (let i = 0; i < pathArr.length; i++) {
const p = pathArr[i]
if (!isObj(obj[p])) {
if (obj[p] === undefined && value === undefined) {
return
}
if (/^\d+$/.test(pathArr[i + 1 + ''])) {
if (getSchema) {
const schema = getSchema(pathArr.slice(0, i) as string[])
if (!schema || schema.type === 'array') {
obj[p] = []
} else {
obj[p] = {}
}
} else {
obj[p] = []
}
} else {
function _existIn(obj: any, path: Path) {
if (!isObj(obj) || !path) {
return false
}
path = toString(path)
if (path in obj) {
return true
}
const pathArr = getPathSegments(path)
for (let i = 0; i < pathArr.length; i++) {
if (isObj(obj)) {
if (!(pathArr[i] in obj)) {
return false
}
obj = obj[pathArr[i]]
} else {
return false
}
}
return true
}
export const getIn = resolveGetIn(_getIn)
function _getIn(obj: any, path: Path, value: any) {
if (!isObj(obj) || !path) {
return obj
}
path = toString(path)
if (path in obj) {
return obj[path as string]
}
const pathArr = getPathSegments(path)
for (let i = 0; i < pathArr.length; i++) {
if (!Object.prototype.propertyIsEnumerable.call(obj, pathArr[i])) {
return value
}
function _existIn(obj: any, path: Path) {
if (!isObj(obj) || !path) {
return false
}
path = toString(path)
if (path in obj) {
return true
}
const pathArr = getPathSegments(path)
for (let i = 0; i < pathArr.length; i++) {
if (isObj(obj)) {
if (!(pathArr[i] in obj)) {
return false
}
function _deleteIn(obj: any, path: Path) {
if (!isObj(obj) || !path) {
return
}
path = toString(path)
if (path in obj) {
delete obj[path as string]
return
}
const pathArr = getPathSegments(path)
for (let i = 0; i < pathArr.length; i++) {
const p = pathArr[i]
if (i === pathArr.length - 1) {
for (let i = 0; i < pathArr.length; i++) {
const p = pathArr[i]
if (i === pathArr.length - 1) {
if (isArr(obj)) {
obj.splice(p as number, 1)
} else {
delete obj[p]
}
return
}
obj = obj[p]
if (!isObj(obj)) {
return
}
}
}
function _setIn(
obj: any,
path: Path,
value: any,
getSchema?: (path: string[] | number[]) => any
) {
if (!isObj(obj) || !path) {
return
}
path = toString(path)
if (path in obj) {
obj[path as string] = value
return
}
const pathArr = getPathSegments(path)
for (let i = 0; i < pathArr.length; i++) {
const p = pathArr[i]
if (!isObj(obj[p])) {
if (obj[p] === undefined && value === undefined) {