/** * @file Shared utilities for JSON formatting preservation and manipulation. * Provides functions for detecting and preserving indentation, line endings, * and determining when JSON files should be saved based on content changes. */ /** * Symbols used to store formatting metadata in JSON objects. */ export declare const INDENT_SYMBOL: unique symbol; export declare const NEWLINE_SYMBOL: unique symbol; /** * Formatting metadata for JSON files. */ export interface JsonFormatting { indent: string | number; newline: string; } /** * Options for determining if a save should occur. */ export interface ShouldSaveOptions { ignoreWhitespace?: boolean | undefined; sort?: boolean | undefined; sortFn?: ((obj: Record) => Record) | undefined; } /** * Detect indentation from a JSON string. Space-based indentation returns a * count; mixed indentation returns the string. * * @example * ;```ts * detectIndent('{\n "key": "value"\n}') // => 2 * detectIndent('{\n "key": "value"\n}') // => 4 * detectIndent('{\n\t"key": "value"\n}') // => '\t' * ``` * * @param json - JSON string to analyze. * * @returns Number of spaces or indentation string, defaults to 2 if not * detected. */ export declare function detectIndent(json: string): string | number; /** * Detect newline character(s) from a JSON string. Supports LF (\n) and CRLF * (\r\n) line endings. * * @example * ;```ts * detectNewline('{\n "key": "value"\n}') // => '\n' * detectNewline('{\r\n "key": "value"\r\n}') // => '\r\n' * ``` * * @param json - JSON string to analyze. * * @returns Line ending string ('\n' or '\r\n'), defaults to '\n' if not * detected. */ export declare function detectNewline(json: string): string; /** * Extract formatting metadata from a JSON string. * * @example * ;```ts * const formatting = extractFormatting('{\n "key": "value"\n}') * // => { indent: 2, newline: '\n' } * ``` * * @param json - JSON string to analyze. * * @returns Object containing indent and newline formatting */ export declare function extractFormatting(json: string): JsonFormatting; /** * Get default formatting for JSON files. * * @example * ;```typescript * const fmt = getDefaultFormatting() * // { indent: 2, newline: '\n' } * ``` * * @returns Default formatting (2 spaces, LF line endings) */ export declare function getDefaultFormatting(): JsonFormatting; /** * Extract formatting from content object that has symbol-based metadata. * * @example * ;```typescript * const content = { * [Symbol.for('indent')]: 4, * [Symbol.for('newline')]: '\r\n', * } * getFormattingFromContent(content) // { indent: 4, newline: "\r\n" } * ``` * * @param content - Content object with Symbol.for('indent') and * Symbol.for('newline') * * @returns Formatting metadata, or defaults if symbols not present */ export declare function getFormattingFromContent(content: Record): JsonFormatting; /** * Determine if content should be saved based on changes and options. Compares * current content with original content and respects options like * ignoreWhitespace and sort. * * @example * ;```ts * const current = { key: 'new-value', [Symbol.for('indent')]: 2 } * const original = { key: 'old-value', [Symbol.for('indent')]: 2 } * shouldSave(current, original, '{\n "key": "old-value"\n}\n') * // => true * ``` * * @param currentContent - Current content object (may include formatting * symbols) * @param originalContent - Original content for comparison (may include * formatting symbols) * @param originalFileContent - Original file content as string (for whitespace * comparison) * @param options - Options controlling save behavior. * * @returns True if content should be saved, false otherwise */ export declare function shouldSave(currentContent: Record, originalContent: Record | undefined, originalFileContent: string, options?: ShouldSaveOptions | undefined): boolean; /** * Sort object keys alphabetically. Creates a new object with sorted keys (does * not mutate input). * * @example * ;```ts * sortKeys({ z: 3, a: 1, m: 2 }) * // => { a: 1, m: 2, z: 3 } * ``` * * @param obj - Object to sort. * * @returns New object with alphabetically sorted keys */ export declare function sortKeys(obj: Record): Record; /** * Stringify JSON with specific formatting. Applies indentation and line ending * preferences. * * @example * ;```ts * stringifyWithFormatting({ key: 'value' }, { indent: 4, newline: '\r\n' }) * // => '{\r\n "key": "value"\r\n}\r\n' * ``` * * @param content - Object to stringify. * @param formatting - Formatting preferences: indent and newline. * * @returns Formatted JSON string with trailing newline */ export declare function stringifyWithFormatting(content: Record, formatting: JsonFormatting): string; /** * Strip formatting symbols from content object. Removes Symbol.for('indent') * and Symbol.for('newline') from the object. * * @example * ;```typescript * const obj = { key: 'value', [Symbol.for('indent')]: 2 } * stripFormattingSymbols(obj) // { key: "value" } * ``` * * @param content - Content object with potential symbol properties. * * @returns Object with symbols removed */ export declare function stripFormattingSymbols(content: Record): Record;