UNPKG

nhb-toolbox

Version:

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

54 lines (53 loc) 1.96 kB
import { ZODIAC_PRESETS } from '../constants.js'; import { _padZero } from '../helpers.js'; export const zodiacPlugin = ($Chronos) => { function _toHundreds(range) { return range[0] * 100 + range[1]; } function _resolveSigns(options) { const { preset = 'western', custom } = options ?? {}; const sorted = [...(custom ?? 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: `${_padZero(startMonth)}-${_padZero(startDate)}`, end: `${_padZero(endMonth)}-${_padZero(endDate - 1)}`, }; }; };