All functions in date-fp follow a set of functional programming principles.
Generic Date objects
date.getHour() will return a different result when run in London, or New York. This generally leads to a lot of confusion since running the same function on the server and on the client will likely produce different results. date-fp uses the UTC accessor methods which guarantees function purity and predictable results. When working with local dates make sure you use the
D.of function to construct the date as this removes any unintended timezone conversions.
All functions in date-fp are pure. For a function to be pure it must follow two basic rules.
- Pure functions always produce the same output given the same input.
- Pure functions have no side effects. This means that calling the function will not affect the world outside the function.
A consequence of this is that date-fp will never knowingly throw an error upon receiving invalid input
but it may return an invalid Date object or
NaN. Inspect date-fp's documentation to find out which functions
behave in this manner.
Dates in date-fp are never mutated. All operations that modify a date return a copy with the given changes and leave the original date object intact.
All functions in date-fp use automatic currying, which allows for easy partial application. For more information on currying read: Why Curry Helps
Functions in date-fp take the data (usually a date object) as the last parameter. This, combined with currying, allows for easy function composition.
const tomorrowAsString = compose(D.format('YYY-MM-DD'), D.add('days', 1)); tomorrowAsString(new Date('2015-01-01')); // '2015-01-02';