Skip to main content

Deep Parse Primitive Values

Recursively parses an input—object, array, or primitive—and converts stringified numbers, booleans, null, and undefined to their proper JavaScript types throughout the structure.


Import

import { deepParsePrimitives } from 'nhb-toolbox';
import { parsePrimitivesDeep } from 'nhb-toolbox';

Function Signature

function deepParsePrimitives<T = unknown>(input: unknown): T

Usage Examples

deepParsePrimitives({ count: "10", enabled: "false" });
// Returns: { count: 10, enabled: false }

API Reference

Parameters

NameTypeDescription
inputunknownAny value: array, object, primitive, etc.

Returns

  • The input structure, with all strings like "true", "42", "null", or "undefined" converted to their actual JS primitive types, recursively.

Key Features

  1. Recursive: Handles any nesting depth—arrays and objects are traversed fully.
  2. Smart Conversion: Converts "true"/"false" (case-insensitive) to booleans, string numbers to numbers, "null" to null, and "undefined" to undefined.
  3. Non-destructive: Already-typed values (booleans, numbers, etc.) remain unchanged.
  4. Works with All Inputs: Supports arrays, objects, primitives, deeply nested mixed structures.

Aliases

Also exported as:

  • parsePrimitivesDeep

Limitations

  1. String Match Only: Only exact matches ("true", not "True" or "TRUE", for booleans; numeric strings must be entirely numeric, etc.).
  2. Falsy Strings Stay Strings: Strings like "0" are converted to 0, but random strings (e.g., "no", "NaN") are left unchanged.
  3. Structural Immutability: Use caution if expecting references to remain unchanged (pure transformation).

Notes

  • Most useful after JSON parsing (see parseJSON) or any time you process loosely typed data (e.g., from APIs, user input, or query strings).
  • For strict root-object-only parsing, see parseJsonToObject.

See Also

  • parseJSON: Complete JSON parsing with deep primitive conversion.
  • parseJsonToObject: Like parseJSON, but requires an object at the root.

  • Cleaning up data from APIs, user forms, or URL query decoding.
  • Ensuring type-safety after parsing loosely-typed or legacy data.
  • Data migration or preprocessing before validation.

Conclusion:
deepParsePrimitives is your go-to utility for guaranteeing primitive types throughout any structured value, especially after parsing JSON or serializing user input. Use it to turn all the "true", "null", or "123" strings in your objects/arrays into proper booleans, nulls, and numbers—automatically!