@exadel/esl
Version:
Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components
41 lines (40 loc) • 2.28 kB
TypeScript
/**
* Extended object parser for lightweight config syntax (HTML attribute friendly).
*
* This function provides an extended parsing capability for strings representing
* lightweight configuration syntax. It supports both strict JSON and a relaxed JSON-like
* syntax, making it suitable for use in scenarios like HTML attributes or other
* lightweight configuration formats.
*
* Supported features:
* - **Strict JSON**: Fully supports JSON objects, arrays, primitives, and strings.
* - **Relaxed JSON (JSONv5-lite)**:
* - Allows unquoted keys (identifier or numeric).
* - Supports single quotes for keys and string literals.
* - Handles trailing commas gracefully.
* - **Extra sugar**:
* - Allows `;` as an alternative to `,` for entry separation in objects and arrays.
* - Top-level object braces may be omitted (e.g., `a:1; b:2` is valid).
* - **Top-level primitives**: Accepts boolean, null, numbers (including leading zeros or `+` sign), and strings.
*
* Not supported / intentionally omitted:
* - **Expressions or identifiers as values**: For example, `{a: someVar}` is not supported.
* - **Malformed escape sequences**: These may result in JSON parsing errors, consistent with `JSON.parse`.
*/
export declare function parseObject(value: string): any;
/**
* Safely parses a string into an object.
*
* This function is a safe wrapper around the {@link parseObject} function. It attempts
* to parse the provided string using `parseObject`. If parsing fails (e.g., due to invalid
* syntax), it catches the error and returns a fallback value instead of throwing an exception.
*
* @param value - The string to parse. It can represent JSON, relaxed JSON, or
* lightweight config syntax.
* @param fallback - The value to return if parsing fails, or function to execute if parsing fails. Defaults to `undefined`.
* @param allowPrimitive - Whether to allow primitive values (e.g., numbers, strings)
* as valid results. Defaults to `false`.
* @returns The parsed object, array, or primitive value. If parsing fails, the
* fallback value is returned.
*/
export declare function parseObjectSafe(value: string, fallback?: any, allowPrimitive?: boolean): any;