1.0.1 • Published 4 years ago

date-reviver v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

date-reviver

A utility module to parse date strings and objectlike structures with date string properties. Supports type guarding.

Usage

This section will provide infomation on how to use the library.

Strings.

If you want to revive strings, use the reviveString methode.

import dateReviver from 'date-reviver';

const isoDateString = new Date().toISOString();
const revivedString = dateReviver.reviveString(isoDateString);
console.log(revivedString instanceof Date);
// true

Objects

If you want to deeply revive objects, use the reviveArray methode.

import dateReviver from 'date-reviver';

const objectWithDateStrings = {
  numericValue: 1337,
  normalStringValue: 'Hello World',
  isoDateString: new Date().toISOString(),
  booleanValue: true,
  nestedObject: {
    anotherDateString: new Date().toISOString(),
  }
};
const revivedObject = dateReviver.reviveString(isoDateString);
console.log(
  typeof revivedObject.numericValue,
  // 'number'
  typeof revivedObject.normalStringValue,
  // 'string'
  revivedObject.isoDateString instanceof Date,
  // true
  typeof revivedObject.booleanValue,
  // 'boolean'
  typeof revivedObject.nestedObject,
  // 'object'
  revivedObject.nestedObject.anotherDateString instanceof Date,
  // true
);

Arrays

If you want to deeply revive arrays, use the reviveArray methode.

import dateReviver from 'date-reviver';

const arrayWithDateStrings = [
  123,
  'Hello World',
  new Date().toISOString(),
  [
    new Date().toISOString(),
  ],
];
const revivedArray = dateReviver.reviveArray(arrayWithDateStrings);
console.log(
  typeof revivedArray[0],
  // 'number'
  typeof revivedArray[1],
  // 'string'
  revivedArray[2] instanceof Date,
  // true
  revivedArray[3][0] instanceof Date,
  // true
);

Dynamic

If you just want to throw in any value and let it revive recursively if possile, use the basic revive methode.

import dateReviver from 'date-reviver';

const someUnpredictableValue = someMethodeThatReturnsDifferentResultFromTimeToTime();
const possibleRevivedValue = dateReviver.revive(someUnpredictableValue);
1.0.1

4 years ago

1.0.0

4 years ago