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

xcraft-core-converters

v4.4.0

Published

Xcraft converters

Downloads

221

Readme

Xcraft Converters

To run test:

  • npm test xcraft-core-converters

To debug test:

  • Open the file xcraft-core-converters/lib/\*.js or xcraft-core-converters/test/\*.js to debug
  • Set a breakpoint
  • Run "Test current file (mocha)"

Canonical

Canonical values are used by the computer. These are the values that are persisted in RethinkDB.

Here is the definition given by Wikipedia:

In computer science, canonicalization (sometimes standardization or normalization) is a process for converting data that has more than one possible representation into a "standard", "normal", or canonical form. This can be done to compare different representations for equivalence, to count the number of distinct data structures, to improve the efficiency of various algorithms by eliminating repeated calculations, or to make it possible to impose a meaningful sorting order.

Generally, canonical values are string, except for number and bool, which use JS native types. For each type, the string containing the canonical value respects a precise syntax (see tests in lib\xcraft-core-converters\test for documentation).

Examples of canonical values:

  • date : "2020-03-31"
  • time : "11:30:00"
  • datetime : "2019-01-18T23:59:59.000Z"
  • price : "100", "49.95"
  • delay : "* * * 30 * * *" (30 days)
  • color : "#FF000" (red in rgb), "HSL(40,100,100)", "CMYK(100,0,0,50)", G(50) ( medium gray)

number and bool use JS native types:

  • number : 50, 0.02
  • bool : true, false

The function parseEdited(edited) parse a free text entered by the user. Some flexibility allows the user to enter data in various formats, possibly incomplete, with a minimum of intelligence. For example:

  • date : parseEdited("25")"2020-03-25" (completed by current month and year)
  • time : parseEdited("12")"12:00:00" (completed with zeros)
  • delay : parseEdited("20j")"* * * 20 * * *"
  • delay : parseEdited("4h")"* * 4 * * * *"
  • color : parseEdited("#12F")"#1122FF"

The function parseEdited return a map with {value, error}. If everything is ok, error === null. In the event of an error, the value comes as close as possible to something plausible.

Displayed

Displayed values are for human users. A canonical value has several possible representations, more or less long.

The function getDisplayed(canonical, format) format a canonical value to a string for the human user (the parameter format is optional). For example:

  • date : getDisplayed("2020-03-31")"31.03.2020"
  • date : getDisplayed("2020-03-31", "dMy")"31 mars 2020" (with parameter format)

See in lib\xcraft-core-converters\lib for documentation of each type.

Many types

There are many types, for all uses:

  • date : A date with day, month and year.
  • time : A time with hours, minutes and seconds.
  • datetime : A date and time.
  • integer : An integer, therefore without fractional part.
  • number : A real number, therefore with a fractional part.
  • price : A price in Swiss francs, with 2 decimal places for cents (accept big numbers).
  • percent : A percentage.
  • delay : Duration in minutes, hours, days, months or years.
  • length : A length with different units ("km", "m", "cm", "mm", usw.). The canonical value is in meters.
  • weight : A weight with different units ("t", "kg", "g", "mg"). The canonical value is in kilogram.
  • volume : A volume defined by 3 lengths, or a number of liters, with different units ("m", "cm", "l", "dm3"). The function getDisplayedIATA format a dimensional weight.

Common mistakes

To get today's canonical date, don't do this:

  • const canonicalNow = date.now()

Instead, write this:

  • const canonicalNow = DateConverters.getNowCanonical()

Or, to display the current date in plain text:

  • const displayedNow = DateConverters.getDisplayed(DateConverters.getNowCanonical())