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

@fredlackey/ugly-date

v0.8.0

Published

Discover the format of irregular date strings needed for parsing.

Downloads

29

Readme

ugly-date

Discover the format of irregular date strings needed for parsing.

Installation

npm i @fredlackey/ugly-date

Important

This library is an experiment... something I could not shake out of my head one Friday evening. It is not complete. While it does handle basic formats, containing numerics and such, it does not contain logic to handle words or "week of year" / "day of year" logic.

Background

Countless libraries out there that will parse a date string if you supply a string and a format. Most of them will also try to return a valid date if you do not supply a format. In that scenario, the date value often comes back wrong or incomplete. In fact, it is the reason why moment, the de facto library for date manipulation (that i know of), is pulling out their parse logic for scenarios where a format is not supplied.

Usage

Simply supply a value to the .analyze function for a report on where recognized patterns are found within the string.

const uglyDate = require('@fredlackey/ugly-date');

const value    = 'Screen Shot 2015-07-09 at 1.33.25 PM';

const results  = uglyDate.analyze(value);

Example results:

{
  "date": "2015-07-09T17:33:25.000Z"
  "hasDate": true,
  "hasDay": false,
  "hasTime": true,
  "pattern": "Screen Shot YYYY-MM-DD at hh.mm.ss a",
  "value": "Screen Shot 2015-07-09 at 1.33.25 PM",
  "values": {
    "YYYY": 2015,
    "MM": 7,
    "DD": 9,
    "h": 1,
    "mm": 33,
    "ss": 25,
    "aa": "PM"
  },
  "locations": [
    {
      "formal": "YYYY-MM-DD",
      "pattern": "YYYY-MM-DD",
      "position": 12,
      "type": "DATE",
      "value": "2015-07-09",
      "values": {
        "YYYY": 2015,
        "MM": 7,
        "DD": 9
      }
    },
    {
      "formal": "hh.mm.ss a",
      "pattern": "h:mm:ss aa",
      "position": 26,
      "type": "TIME",
      "value": "1.33.25 PM",
      "values": {
        "h": 1,
        "mm": 33,
        "ss": 25,
        "aa": "PM"
      }
    }
  ]
}

In the example above, the following values are returned:

  • date : Date object, if a full date could be constructed.
  • hasDate : Boolean indicating if a valid date value exists.
  • hasDay : Boolean indicating if a valid day value exists.
  • hasTime : Boolean indicating if a valid time value exists.
  • pattern : Example string including the detected pattern.
  • value : Original string (for verification puposes).
  • values : All detected keys (ie result.values.YYYY).
  • locations : Array of locations where patterns were detected.

And, for each location item, you have the following:

  • pattern : Actual pattern detected in supplied string.
  • position : Index of the substring having the pattern.
  • value : Value of the substring matched on the pattern.
  • formal : The actual formal / proper pattern to parse this pattern.
  • values : Each of the extracted values in their number or string form.

Note:
Pay close attention to the a and aa paterns shown in the formal and pattern values. In this example, he double-character formatted value was detected, however this is not the proper token to use when parsing strings. Intead, the single a is used. While both are provided here, you would want to use the formal value as the actual pattern when parsing this string.

Contact Info

As always, get in touch if you have ideas or feedback ...

Fred Lackey
http://fredlackey.com
[email protected]