Socket
Socket
Sign inDemoInstall

@forcloud/date-json

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @forcloud/date-json

A lightweight (794 bytes) library to add Date support to JSON.


Version published
Maintainers
1
Install size
12.7 kB
Created

Readme

Source

DateJson

A lightweight (794 bytes) library to add Date support to JSON APIs.

Enables APIs to send database records that contain dates without having to manually transform them.

import { DateJson } from "@forcloud/date-json"

const json = DateJson.toJSON({ createdAt: new Date() })
// --> { createdAt: { $date: 1519211811670 } }

const jsonWithDates = DateJson.fromJSON(json)
// --> { createdAt: anInstanceOfADateObject }

Features

  • Lightweight: 795 bytes
  • JSON: Transform to/from JSON
  • String: Transform to/from string (stringify/parse)
  • TypeScript: Keeps type integrity through transformations

TypeScript

Keeps type integrity during encode/decode enabling type safety in API calls.

const json = DateJson.toJSON({ createdAt: new Date() })
// --> { createdAt: { $date: 1519211811670 } }
type MyJson = typeof json
// --> type: { createdAt: { $date: number }}

const jsonWithDates = DateJson.fromJSON(json)
// --> { createdAt: anInstanceOfADateObject }
type MyJsonWithDates = typeof jsonWithDates
// --> type: { createdAt: Date }

No $date Key Allowed

The key $date is prohibited in toJSON like in DateJson.toJSON({ $date: 'today' }). This throws the error Object with key $date is invalid.

The design is intentional and is there to prevent confusion. Am I looking at a DateJson object or a plain JSON object with $date in it?

Keywords

FAQs

Last updated on 12 Sep 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc