dayphase
Version:
Determines the dayphase based on hour boundaries. It allows flexible time segmentation for different cultural or system-defined day phases.
79 lines (77 loc) • 2.86 kB
JavaScript
/* *******************************************************
* dayphase
*
* @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: dayphase
* file_name: gen/index.cjs
* purpose: {{PURPOSE}}
*
* @system
*
* generated_on: 2025-03-15T05:55:27.593Z
* certified_version: 1.0.0
* file_uuid: 20cc4895-87f4-4045-83dc-4b7e9d310571
* file_size: 2813 bytes
* file_hash: 5feb9c9b78971e685944ea6ea23eaece084444d0fad1b032c86c38fcb70ca5b2
* mast_hash: aad0ab38a0765267e4b2932a1c246dc8ed888080e166ac7fb670a78cbd905aa1
* generated_by: preamble on npm!
*
* [Preamble Metadata]
********************************************************/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var index_exports = {};
__export(index_exports, {
default: () => index_default
});
module.exports = __toCommonJS(index_exports);
function dayphase(phases = [4, 17, 21], hour = (/* @__PURE__ */ new Date()).getHours()) {
if (!Array.isArray(phases) || phases.length === 0 || phases.some((n) => n < 0 || n > 24)) {
throw new Error(
"phases must be an ordered array of numbers between 0 and 24."
);
}
if (typeof hour !== "number" || !Number.isInteger(hour) || hour < 0 || hour > 24) {
throw new Error("hour must be an integer between 0 and 24.");
}
if (hour < phases[0] || hour > phases[phases.length - 1]) {
return 0;
}
const index = phases.findIndex((phase) => hour < phase);
return index !== -1 ? index : phases.length - 1;
}
var index_default = dayphase;