es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
43 lines (40 loc) • 996 B
JavaScript
import { isPlainObject } from './isPlainObject.mjs';
function isJSONValue(value) {
switch (typeof value) {
case 'object': {
return value === null || isJSONArray(value) || isJSONObject(value);
}
case 'string':
case 'number':
case 'boolean': {
return true;
}
default: {
return false;
}
}
}
function isJSONArray(value) {
if (!Array.isArray(value)) {
return false;
}
return value.every(item => isJSONValue(item));
}
function isJSONObject(obj) {
if (!isPlainObject(obj)) {
return false;
}
const keys = Reflect.ownKeys(obj);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = obj[key];
if (typeof key !== 'string') {
return false;
}
if (!isJSONValue(value)) {
return false;
}
}
return true;
}
export { isJSONArray, isJSONObject, isJSONValue };