|
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:
1.0 KB
|
| Line | |
|---|
| 1 | /**
|
|---|
| 2 | * Checks if a given value is a valid JSON string.
|
|---|
| 3 | *
|
|---|
| 4 | * A valid JSON string is one that can be successfully parsed using `JSON.parse()`. According to JSON
|
|---|
| 5 | * specifications, valid JSON can represent:
|
|---|
| 6 | * - Objects (with string keys and valid JSON values)
|
|---|
| 7 | * - Arrays (containing valid JSON values)
|
|---|
| 8 | * - Strings
|
|---|
| 9 | * - Numbers
|
|---|
| 10 | * - Booleans
|
|---|
| 11 | * - null
|
|---|
| 12 | *
|
|---|
| 13 | * String values like `"null"`, `"true"`, `"false"`, and numeric strings (e.g., `"42"`) are considered
|
|---|
| 14 | * valid JSON and will return true.
|
|---|
| 15 | *
|
|---|
| 16 | * This function serves as a type guard in TypeScript, narrowing the type of the argument to `string`.
|
|---|
| 17 | *
|
|---|
| 18 | * @param {unknown} value The value to check.
|
|---|
| 19 | * @returns {boolean} Returns `true` if `value` is a valid JSON string, else `false`.
|
|---|
| 20 | *
|
|---|
| 21 | * @example
|
|---|
| 22 | * isJSON('{"name":"John","age":30}'); // true
|
|---|
| 23 | * isJSON('[1,2,3]'); // true
|
|---|
| 24 | * isJSON('true'); // true
|
|---|
| 25 | * isJSON('invalid json'); // false
|
|---|
| 26 | * isJSON({ name: 'John' }); // false (not a string)
|
|---|
| 27 | * isJSON(null); // false (not a string)
|
|---|
| 28 | */
|
|---|
| 29 | declare function isJSON(value: unknown): value is string;
|
|---|
| 30 |
|
|---|
| 31 | export { isJSON };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.