npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@adaskothebeast/hierarchical-convert-to-date

v11.0.1

Published

Deep, in-place conversion of ISO 8601 date strings in JSON payloads into native JavaScript Date objects.

Downloads

339

Readme

📅 @adaskothebeast/hierarchical-convert-to-date

The date-library-free member of date-interceptors: walks a parsed JSON graph and replaces ISO 8601 date strings with native Date instances in place.

npm license

The only runtime dependency is the dependency-free @adaskothebeast/hierarchical-convert-core; no date library, no framework.


📦 Install

npm i @adaskothebeast/hierarchical-convert-to-date

🎯 What it does

JSON.parse gives you strings where your API meant instants. This package fixes that after the fact: it walks the arrays and plain objects of a parsed payload, recognizes ISO 8601 date-time strings, and assigns a Date back into the same slot.

const payload = { createdAt: '2023-07-17T23:06:00.000Z' };
hierarchicalConvertToDate(payload);
payload.createdAt instanceof Date; // true

The traversal is deliberately defensive: own __proto__, constructor and prototype keys are skipped, only arrays and plain objects are entered, cycles terminate, frozen or read-only properties are left alone instead of throwing, and recursion stops past a fixed depth. That makes it safe to point at untrusted response bodies.

fetchJson is a thin convenience wrapper: one fetch call, status and Problem Details handling, JSON parsing, then the conversion.


🧰 API

Conversion

| Symbol | Signature | Notes | | -------------------------------- | ------------------------ | --------------------------------------- | | hierarchicalConvertToDate(obj) | (obj: unknown) => void | Mutates obj in place; returns nothing |

Fetch helper

| Symbol | Signature | Notes | | ---------------------------------------------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ | | fetchJson<T>(input, init?, options?) | (input: RequestInfo \| URL, init?: RequestInit, options?: FetchJsonOptions) => Promise<T \| undefined> | Resolves with converted data, or undefined for an empty successful response | | FetchJsonOptions | { readonly fetch?: typeof globalThis.fetch } | Inject a custom fetch (tests, polyfills, instrumented client) | | FetchJsonError | class FetchJsonError extends Error | name: 'FetchJsonError', plus readonly status, statusText, and response | | isProblemDetailsError, ProblemDetailsError | Re-exported from core | Narrow and inspect JSON Problem Details failures |

FetchJsonError's message reads HTTP request failed with status 503 Service Unavailable. The original, unconsumed Response is available as error.response.


⚡ Usage

Convert a payload you already have:

import { hierarchicalConvertToDate } from '@adaskothebeast/hierarchical-convert-to-date';

interface Order {
  id: string;
  createdAt: Date;
  lines: { shippedAt: Date | null }[];
}

const raw: unknown = JSON.parse(text);
hierarchicalConvertToDate(raw);
const order = raw as Order;

Fetch and convert in one step:

import { FetchJsonError, fetchJson } from '@adaskothebeast/hierarchical-convert-to-date';

try {
  const order = await fetchJson<Order>('/api/orders/1');
  console.log(order?.createdAt.getFullYear());
} catch (e) {
  if (e instanceof FetchJsonError) {
    console.error(e.status, e.statusText);
  }
}

Inject a fetch implementation:

await fetchJson<Order>('/api/orders/1', { method: 'GET' }, { fetch: myInstrumentedFetch });

🎛️ Recognition rules

There is nothing to configure. Recognition and traversal are shared with the other converters through hierarchical-convert-core; the full rules are in the repository conversion contract. In short:

  • A string converts when it has the shape YYYY-MM-DDTHH:mm:ss, an optional fraction of one to nine digits, and an optional Z or ±HH:MM offset.
  • Calendar and clock components are validated first (leap years, days per month, hours below 24, minutes and seconds below 60, offset hours below 24 and offset minutes below 60), so impossible dates never reach new Date.
  • Fractions beyond milliseconds are truncated, never rounded into the next second.
  • The root plus 100 nested levels are walked.

📤 Output examples

| Input | After conversion | | --------------------------------------------------- | ------------------------------------------------- | | { date: '2023-07-17T23:06:00.000Z' } | { date: Date('2023-07-17T23:06:00.000Z') } | | { date: '2023-07-17T23:06:00.000+01:00' } | { date: Date('2023-07-17T23:06:00.000+01:00') } | | { date: '2023-07-17T23:06:00' } | { date: Date(...) } in the local timezone | | { nested: { date: '2023-07-17T23:06:00.000Z' } } | { nested: { date: Date(...) } } | | ['2023-07-17T23:06:00.000Z'] | [Date(...)] | | { text: 'adam', number: 42, flag: true, n: null } | untouched | | { d: '2023/07/17 23:06:00' } | untouched (wrong format) | | { d: '2023-02-30T00:00:00Z' } | untouched (impossible calendar date) | | { d: '2023-07-17' } | untouched (date-only) |

// fetchJson
200 + body                   -> resolved value with Date instances in place of ISO strings
204, 205, or empty body      -> resolves to undefined
503                          -> rejects with FetchJsonError { status: 503, statusText: 'Service Unavailable' }
application/problem+json     -> rejects with a FetchJsonError recognised by isProblemDetailsError, even for HTTP 200

⚠️ Edge cases

  • Mutation in place. The function returns void and rewrites your object. Clone first (structuredClone) if the caller needs the original strings.
  • Only arrays and plain objects are entered (11.0.0). Class instances, Map, Set, Date, Buffers and typed arrays are left untouched, so running the converter twice is a no-op. Before 11.0.0 every non-null object was entered.
  • Prototype pollution is blocked. Own __proto__, constructor and prototype keys are skipped entirely and never entered.
  • Cycles are safe. Shared references and cycles are visited once.
  • Frozen or read-only values stay strings. Frozen objects, non-writable properties, and getter-only properties are skipped rather than throwing mid-walk.
  • Past depth 100 nothing changes. Deeper values stay strings; no error, no warning.
  • Invalid dates stay strings. You never get an Invalid Date object out of this package.
  • Offset-less values are local time, because that is what new Date('2023-07-17T23:06:00') does. Values with Z or an explicit offset are exact instants.
  • Date-only strings are never touched, which avoids the classic "plain date silently became midnight UTC" bug.
  • Durations are not handled. P1Y2M3D stays a string; use the -date-fns, -dayjs, -luxon, -moment or -temporal packages if your payload carries ISO durations.
  • hierarchicalConvertToDate on a primitive, null or undefined is a no-op, so it is safe to call on any unknown.
  • fetchJson never converts problem responses. Non-ok responses reject with FetchJsonError before the body is read; nonempty bodies must contain valid JSON or the call rejects with SyntaxError.

🔗 Related packages

Full matrix and recipes: main README.


📄 License

MIT © Adam Pluciński

JSON Problem Details (application/problem+json) bypass success conversion, including HTTP 200 responses. Use the exported isProblemDetailsError(error) guard to read httpStatus, typed problem members, and the original decoded body. Extension members such as errors and traceId are preserved. See the repository conversion contract for transport-specific behavior.