UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.

58 lines (57 loc) 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.zodiacPlugin = void 0; const constants_1 = require("../constants"); const helpers_1 = require("../helpers"); const zodiacPlugin = ($Chronos) => { function _toHundreds(range) { return range[0] * 100 + range[1]; } function _resolveSigns(options) { const { preset = 'western', custom } = options ?? {}; const sorted = [...(custom ?? constants_1.ZODIAC_PRESETS[preset])].sort((a, b) => _toHundreds(a[1]) - _toHundreds(b[1])); return sorted; } $Chronos.prototype.getZodiacSign = function (options) { const { birthDate } = options ?? {}; let month; let date; if (birthDate?.includes('-')) { [month, date] = birthDate.split('-').map(Number); } else { month = this.isoMonth; date = this.date; } const sortedSigns = _resolveSigns(options); for (let i = sortedSigns.length - 1; i >= 0; i--) { const [sign, [m, d]] = sortedSigns[i]; if (month > m || (month === m && date >= d)) { return sign; } if (i === 0) { return sortedSigns.at(-1)?.[0]; } } return sortedSigns[0][0]; }; $Chronos.prototype.zodiac = function (options) { return this.getZodiacSign(options); }; $Chronos.prototype.getZodiacMeta = (sign, options) => { const sortedSigns = _resolveSigns(options); const index = sortedSigns.findIndex(([s]) => s === sign); if (index === -1) { throw new RangeError(`Invalid zodiac sign: "${sign}"`); } const [startMonth, startDate] = sortedSigns[index][1]; const [endMonth, endDate] = (sortedSigns[index + 1] ?? sortedSigns[0])[1]; return { index, sign, start: `${(0, helpers_1._padZero)(startMonth)}-${(0, helpers_1._padZero)(startDate)}`, end: `${(0, helpers_1._padZero)(endMonth)}-${(0, helpers_1._padZero)(endDate - 1)}`, }; }; }; exports.zodiacPlugin = zodiacPlugin;