@visulima/fs
Version:
Human friendly file system utilities for Node.js
45 lines (44 loc) • 1.44 kB
TypeScript
/**
* Strips comments from a JSON string.
* Handles both single-line (//) and multi-line (/* ... */) comments.
* @param jsonString The JSON string possibly containing comments.
* @param [options] Optional configuration for stripping comments.
* @param [options.whitespace] If `true` (default), comments are replaced with whitespace to preserve line numbers and character positions. If `false`, comments are removed entirely.
* @returns The JSON string with comments stripped.
* @example
* ```javascript
* import { stripJsonComments } from "@visulima/fs"; // Assuming this util is exported
*
* const jsonWithComments = `{
* // This is a single-line comment
* "name": "John Doe",
* "age": 30, /* This is a
* multi-line comment */
* "city": "New York"
* }`;
*
* const stripped = stripJsonComments(jsonWithComments);
* console.log(stripped);
* // Output (with whitespace=true):
* // {
* //
* // "name": "John Doe",
* // "age": 30, /*
* //
* // "city": "New York"
* // }
*
* const strippedWithoutWhitespace = stripJsonComments(jsonWithComments, { whitespace: false });
* console.log(strippedWithoutWhitespace);
* // Output (with whitespace=false):
* // {
* // "name": "John Doe",
* // "age": 30,
* // "city": "New York"
* // }
* ```
*/
declare const stripJsonComments: (jsonString: string, options?: {
whitespace?: boolean;
}) => string;
export default stripJsonComments;