@naverpay/hidash
Version:
improved lodash
39 lines (38 loc) • 1.16 kB
JavaScript
import { isArray as o } from "./isArray.mjs";
const l = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/, y = /^\w*$/;
function u(r, t) {
return Array.isArray(r) || typeof r != "string" ? !1 : y.test(r) || !l.test(r) || t != null && r in Object(t);
}
function c(r) {
return Array.isArray(r) ? r.map(String) : typeof r == "string" ? r.split(".") : [String(r)];
}
function g(r, t) {
if (r == null)
return !1;
const n = u(t, r) ? [t] : c(t), f = n.length;
let e = r;
for (let s = 0; s < f; s++) {
const i = n[s];
if (e == null || typeof e != "object" || typeof i != "string" || !Object.prototype.hasOwnProperty.call(e, i))
return !1;
if (e = e[i], s === n.length - 1)
return !0;
}
return !0;
}
function p(r, t) {
if (t == null || Array.isArray(t) && !t.length)
return !1;
if (typeof t == "string") {
if (!t || t === "." || t === "..")
return !1;
if (!t.includes("."))
return r != null && typeof r == "object" && Object.prototype.hasOwnProperty.call(r, t);
} else if (o(t) && !t.every((n) => typeof n == "string"))
return !1;
return g(r, t);
}
export {
p as default,
p as has
};