@marchingy/lunar
Version:
Chinese calendar with the 24 solar terms.
65 lines (64 loc) • 2.66 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.printDebug = exports.sameDate = exports.compareDate = exports.coerceInteger = exports.isNumberValue = exports.toPrecision = void 0;
function toPrecision(value, precision) {
const p = Math.pow(10, precision);
return Math.round(value * p) / p;
}
exports.toPrecision = toPrecision;
/**
* Whether the provided value is considered a number.
* @docs-private
*/
function isNumberValue(value) {
// parseFloat(value) handles most of the cases we're interested in (it treats null, empty string,
// and other non-number values as NaN, where Number just uses 0) but it considers the string
// '123hello' to be a valid number. Therefore we also check if Number(value) is NaN.
return !isNaN(parseFloat(value)) && !isNaN(Number(value));
}
exports.isNumberValue = isNumberValue;
function coerceInteger(value, fallbackValue = 0) {
return isNumberValue(value) ? (value > 0 ? Math.floor(value) : Math.ceil(value)) : fallbackValue;
}
exports.coerceInteger = coerceInteger;
/**
* Compares two dates.
* @param first The first date to compare.
* @param second The second date to compare.
* @returns 0 if the dates are equal, a number less than 0 if the first date is earlier,
* a number greater than 0 if the first date is later.
*/
function compareDate(first, second) {
return (first.getFullYear() - second.getFullYear() ||
first.getMonth() - second.getMonth() ||
first.getDate() - second.getDate());
}
exports.compareDate = compareDate;
/**
* Checks if two dates are equal.
* @param first The first date to check.
* @param second The second date to check.
* @returns Whether the two dates are equal.
* Null dates are considered equal to other null dates.
*/
function sameDate(first, second) {
if (first && second) {
return !compareDate(first, second);
}
return first == second;
}
exports.sameDate = sameDate;
function printDebug(fun, on) {
on && console.info(typeof fun === 'function' ? fun() : fun);
}
exports.printDebug = printDebug;
});