diff options
| author | rxliuli <rxliuli@gmail.com> | 2025-11-04 05:03:50 +0800 |
|---|---|---|
| committer | rxliuli <rxliuli@gmail.com> | 2025-11-04 05:03:50 +0800 |
| commit | bce557cc2dc767628bed6aac87301a1be7c5431b (patch) | |
| tree | b51a051228d01fe3306cd7626d4a96768aadb944 /shared/utils/src/is-pojo.ts | |
init commit
Diffstat (limited to 'shared/utils/src/is-pojo.ts')
| -rw-r--r-- | shared/utils/src/is-pojo.ts | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/shared/utils/src/is-pojo.ts b/shared/utils/src/is-pojo.ts new file mode 100644 index 0000000..4363454 --- /dev/null +++ b/shared/utils/src/is-pojo.ts @@ -0,0 +1,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; +} |
