UNPKG

wellwish

Version:

Generates a nuanced well-wishing phrase based on English cultural norms.

92 lines (86 loc) 3.21 kB
/* ******************************************************* * wellwish * * @license * * Apache-2.0 * * Copyright 2015-2025 Alex Stevovich * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * @meta * * package_name: wellwish * file_name: src/index.js * purpose: Core functionality and exports combined. * * @system * * generated_on: 2025-03-15T06:47:01.389Z * certified_version: 1.0.0 * file_uuid: 1276971e-8a19-42c4-80dd-637a650466d9 * file_size: 3213 bytes * file_hash: 586a411a75e6df586d5e920c045e5ba85fe45f061437ff8d25909e0647f75c37 * mast_hash: 68eca7ee87a5bcd73a22a31c13cc0bb36955c1daec3cf3da0ae0d6b1270cf34d * generated_by: preamble on npm! * * [Preamble Metadata] ********************************************************/ import dayphase from 'dayphase'; /** * Generates a wellwish phrase based on the current time and custom-defined phases. * * @param {Object} options - Configuration options. * @param {string} [options.day="day"] - The phrase for the "day" phase. * @param {string} [options.evening="evening"] - The phrase for the "evening" phase. * @param {string} [options.night="night"] - The phrase for the "night" phase. * @param {number} [options.dayStart=4] - The hour at which "day" starts. * @param {number} [options.eveningStart=17] - The hour at which "evening" starts. * @param {number} [options.nightStart=21] - The hour at which "night" starts. * @param {number} [options.hour=new Date().getHours()] - The hour to evaluate (default: current hour). * @returns {string} - The appropriate wellwish phrase. * @throws {Error} - If input values are invalid. */ export function wellwish({ day = 'day', evening = 'evening', night = 'night', dayStart = 4, eveningStart = 17, nightStart = 21, hour = new Date().getHours(), } = {}) { // Validate phase start times const phases = [dayStart, eveningStart, nightStart].filter( (n) => typeof n === 'number' && Number.isInteger(n) && n >= 0 && n <= 24, ); if (dayStart < 0 || dayStart > 24 || !Number.isInteger(dayStart)) { throw new Error('dayStart must be an integer between 0 and 24.'); } if ( eveningStart < 0 || eveningStart > 24 || !Number.isInteger(eveningStart) ) { throw new Error('eveningStart must be an integer between 0 and 24.'); } if (nightStart < 0 || nightStart > 24 || !Number.isInteger(nightStart)) { throw new Error('nightStart must be an integer between 0 and 24.'); } const phaseIndex = dayphase(phases, hour); // Correct mapping for phaseIndex to phrase return [night, day, evening][phaseIndex] || night; } export default wellwish;