updeep-remeda/src/is.ts

29 lines
646 B
TypeScript
Raw Normal View History

2023-01-03 18:51:35 +00:00
import splitPath from "./util/splitPath.js";
import wrap from "./wrap.js";
function _is(
2023-01-03 19:31:53 +00:00
object,
path: number | string | (number | string)[],
predicate
2023-01-03 18:51:35 +00:00
): boolean {
2023-01-03 19:31:53 +00:00
const parts = splitPath(path);
2023-01-03 18:51:35 +00:00
2023-01-03 19:31:53 +00:00
for (const part of parts) {
if (typeof object === "undefined") return false;
object = object[part];
}
2023-01-03 18:51:35 +00:00
2023-01-03 19:31:53 +00:00
if (typeof predicate === "function") {
return predicate(object);
}
2023-01-03 18:51:35 +00:00
2023-01-03 19:31:53 +00:00
return predicate === object;
2023-01-03 18:51:35 +00:00
}
2023-01-03 19:31:53 +00:00
export interface Is {
(object, path: string | number | (number | string)[], predicate): boolean;
(path: string | number | (number | string)[], predicate): (object) => boolean;
2023-01-03 18:51:35 +00:00
}
export default wrap(_is) as Is;