summaryrefslogtreecommitdiff
path: root/shared/utils/src/is-pojo.ts
blob: 43634544716c632ca5dbc8f43a7dc7b60766e221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * Determine if {@linkcode arg} is a Plain Old JavaScript Object.
 *
 * @see https://masteringjs.io/tutorials/fundamentals/pojo
 *
 * @param arg to test
 * @returns true if {@linkcode arg} is a POJO
 */
export function isPOJO(arg: unknown): arg is Record<string, unknown> {
    if (!arg || typeof arg !== 'object') {
        return false;
    }

    const proto = Object.getPrototypeOf(arg);
    if (!proto) {
        return true; // `Object.create(null)`
    }

    return proto === Object.prototype;
}