|
Last change
on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
996 bytes
|
| Line | |
|---|
| 1 | import { isPlainObject } from './isPlainObject.mjs';
|
|---|
| 2 |
|
|---|
| 3 | function isJSONValue(value) {
|
|---|
| 4 | switch (typeof value) {
|
|---|
| 5 | case 'object': {
|
|---|
| 6 | return value === null || isJSONArray(value) || isJSONObject(value);
|
|---|
| 7 | }
|
|---|
| 8 | case 'string':
|
|---|
| 9 | case 'number':
|
|---|
| 10 | case 'boolean': {
|
|---|
| 11 | return true;
|
|---|
| 12 | }
|
|---|
| 13 | default: {
|
|---|
| 14 | return false;
|
|---|
| 15 | }
|
|---|
| 16 | }
|
|---|
| 17 | }
|
|---|
| 18 | function isJSONArray(value) {
|
|---|
| 19 | if (!Array.isArray(value)) {
|
|---|
| 20 | return false;
|
|---|
| 21 | }
|
|---|
| 22 | return value.every(item => isJSONValue(item));
|
|---|
| 23 | }
|
|---|
| 24 | function isJSONObject(obj) {
|
|---|
| 25 | if (!isPlainObject(obj)) {
|
|---|
| 26 | return false;
|
|---|
| 27 | }
|
|---|
| 28 | const keys = Reflect.ownKeys(obj);
|
|---|
| 29 | for (let i = 0; i < keys.length; i++) {
|
|---|
| 30 | const key = keys[i];
|
|---|
| 31 | const value = obj[key];
|
|---|
| 32 | if (typeof key !== 'string') {
|
|---|
| 33 | return false;
|
|---|
| 34 | }
|
|---|
| 35 | if (!isJSONValue(value)) {
|
|---|
| 36 | return false;
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 | return true;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | export { isJSONArray, isJSONObject, isJSONValue };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.