UNPKG

iztro-hook

Version:

A hook of iztro。紫微斗数开源库iztro的react钩子函数。

55 lines (54 loc) 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useIztro = void 0; var iztro_1 = require("iztro"); var utils_1 = require("iztro/lib/utils"); var react_1 = require("react"); function useIztro(input) { var _currentDate = new Date(); var _currentHour = _currentDate.getHours(); var _a = (0, react_1.useState)(), astrolabe = _a[0], _setAstrolabe = _a[1]; var _b = (0, react_1.useState)(_currentDate), _horoscopeDate = _b[0], _setHoroscopeDate = _b[1]; var _c = (0, react_1.useState)((0, utils_1.timeToIndex)(_currentHour)), _horoscopeHour = _c[0], _setHoroscopeHour = _c[1]; var _d = (0, react_1.useState)(), horoscope = _d[0], _setHoroscope = _d[1]; var birthTime = input.birthTime, birthday = input.birthday, birthdayType = input.birthdayType, fixLeap = input.fixLeap, isLeapMonth = input.isLeapMonth, gender = input.gender, lang = input.lang, options = input.options, astroType = input.astroType; (0, react_1.useEffect)(function () { var date = new Date(birthday).toString().toLowerCase(); if (!birthday || date === 'invalid date') { return undefined; } var data = iztro_1.astro.withOptions({ dateStr: birthday, timeIndex: birthTime, gender: gender, type: birthdayType, isLeapMonth: isLeapMonth, fixLeap: fixLeap, astroType: astroType, language: lang, config: options, }); _setAstrolabe(data); }, [birthTime, birthday, birthdayType, fixLeap, isLeapMonth, gender, lang, astroType]); (0, react_1.useEffect)(function () { if (astrolabe) { _setHoroscope(astrolabe.horoscope(_horoscopeDate, _horoscopeHour)); } }, [astrolabe, _horoscopeDate, _horoscopeHour]); var setHoroscope = function (date, hour) { _setHoroscopeDate(date); if (typeof hour === 'number') { _setHoroscopeHour(hour); } else { var _hour = (0, utils_1.timeToIndex)(new Date(date).getHours()); _setHoroscopeHour(_hour); } }; return { astrolabe: astrolabe, horoscope: horoscope, setHoroscope: setHoroscope, }; } exports.useIztro = useIztro;