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 🙏

© 2024 – Pkg Stats / Ryan Hefner

parso

v0.1.0

Published

Lightweight & performant date parser for terrible date formats.

Downloads

1

Readme

Parso Build Status codecov

Parso is a lightweight and performant date parser for terrible date formats, it aims to help you with parsing inconsistently formatted dates.

Details TL;DR

  • includes a sane predefined set of parsers for ISO-ish date-time strings
  • supports customization via custom parsers
  • tree shakable structure, so you bundle only what you use

What is not included?

  • modifying Date objects - use date-fns for that!
  • timezone handling - use spacetime for that!

Installation

Install with npm:

npm install --save parso

Install with yarn:

yarn add parso

Usage

import { parse } from 'parso';

const easter = parse('20180401');
const christmas = parse('2018-12-24');
const newYear = parse('2010.01.01');

Documentation

Parsing dates

Parso has a simple API, it exposes two functions - parse and parseOrThrow to parse dateish or date-timeish strings. Neither of them require any default options, but their behaviour can be customized via an optional second settings object. The only difference between them is the latter will throw an error if none of the registered parsers can process the passed in value.

import { parse, parseOrThrow, ParsoParseError } from 'parso';

const validValue = '2023.08-21';
const invalidValue = 'you-will-never-parse-me';

try {
  parse(validValue); // returns new Date('2023-08-21T00:00:00Z')
  parseOrThrow(validValue); // returns new Date('2023-08-21T00:00:00Z')

  parse(invalidValue); // returns null
  parseOrThrow(invalidValue); // throws ParsoParseError
} catch (error) {
  error instanceof ParsoParseError; // true
}

Registering non default parsers

By default only a sane set of parsers for the ISO 8601-ish formats are included in the default registry. You can register non-default or your custom handlers with the defaulParserRegistry.registerParser function.

Extra parsers are included for extreme formats, you can import them from 'parso/parsers'. You can read more about the included parsers in their documentation.

Writing custom parsers

Parsers are simple functions which implement the DateParser type. They must meet the following criteria:

  • must be sync
  • must return undefined when failed to parse the recieved value

Example:

/**
 * Parses a valid date string.
 */
export const validDateParser: DateParser = (value: string | number): Date | undefined => {
  const invalidDate = Number.isNaN(new Date(value).getTime());

  return invalidDate ? undefined : new Date(value);
};

You can read more about custom parsers in their documentation.

API

parse function

Tries to parse the recieved value into a Date object with the registered parsers, returns null when the parsing attempt fails.

Possible return values:

  • Date instance
  • null value when none of the parsers can parse the recieived value

Possible errors:

  • ParsoInvalidInputError when the recieved value is not a string, number or Date type.

Signature:

parse(value: string | number | Date, parseOptions: ParseOptions): Date | null

parseOrThrow function

Tries to parse the recieved value into a Date object with the registered parsers, throws an instance of ParsoParseError error when the parsing attempt fails.

Possible return values:

  • Date instance

Possible errors:

  • ParsoInvalidInputError when the recieved value is not a string, number or Date type.
  • ParsoParseError when none of the registered parsers can parser the recieved value.

Signature:

parseOrThrow(value: string | number | Date, parseOptions: ParseOptions): Date

ParserRegistry class

A ParserRegistry instance can be used to store parsers. Which later can be passed into the parse functions via the parseOptions.customRegistry option.

import { ParserRegistry, parse } from 'parso';
import { myCustomParser } from './my-custom-parser';

const customRegistry = new ParserRegistry();

customRegistry.registerParsers(myCustomParser);

parse('2019_08_01', { customRegistry });

Parso exports a default registry instance named defaulParserRegistry which is used by the parser functions when no custom registry is specified.


DateParser type

See the parser documentation for details.

License

MIT