Skip to content

Xcraft-Inc/xcraft-core-converters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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())