natural-time-js 2.0.6 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,231 +0,0 @@
1
- /**
2
- * @module natural-time-js/validators
3
- * @description Validation utilities for natural time calculations.
4
- * This module provides functions to validate inputs for natural time operations,
5
- * ensuring that values are within acceptable ranges and of the correct types.
6
- */
7
- import { NaturalDate } from '../core/NaturalDate.js';
8
- /**
9
- * Valid range for latitude values in degrees
10
- */
11
- const LATITUDE_RANGE = {
12
- MIN: -90,
13
- MAX: 90
14
- };
15
- /**
16
- * Valid range for longitude values in degrees
17
- */
18
- const LONGITUDE_RANGE = {
19
- MIN: -180,
20
- MAX: 180
21
- };
22
- /**
23
- * Valid range for angle values in degrees
24
- */
25
- const ANGLE_RANGE = {
26
- MIN: 0,
27
- MAX: 360
28
- };
29
- /**
30
- * Valid ranges for natural date components
31
- */
32
- const NATURAL_DATE_RANGES = {
33
- /** Moon (month) number range (1-14) */
34
- MOON: { MIN: 1, MAX: 14 },
35
- /** Week of year range (1-53) */
36
- WEEK: { MIN: 1, MAX: 53 },
37
- /** Week within moon range (1-4) */
38
- WEEK_OF_MOON: { MIN: 1, MAX: 4 },
39
- /** Day of moon range (1-28) */
40
- DAY_OF_MOON: { MIN: 1, MAX: 28 },
41
- /** Day of week range (1-7) */
42
- DAY_OF_WEEK: { MIN: 1, MAX: 7 }
43
- };
44
- /**
45
- * Validates if a value is a finite number.
46
- *
47
- * This is a utility function that checks if a value is a valid number
48
- * (not NaN, not Infinity, and actually a number type).
49
- *
50
- * @param value - Value to validate
51
- * @returns True if value is a valid number
52
- *
53
- * @example
54
- * isValidNumber(42); // true
55
- * isValidNumber('42'); // false (string, not number)
56
- * isValidNumber(NaN); // false
57
- * isValidNumber(Infinity); // false
58
- */
59
- export const isValidNumber = (value) => {
60
- return typeof value === 'number' && Number.isFinite(value) && !Number.isNaN(value);
61
- };
62
- /**
63
- * Validates if a latitude value is within the valid range.
64
- *
65
- * Latitude must be a number between -90° (South Pole) and 90° (North Pole).
66
- *
67
- * @param latitude - Latitude in degrees to validate
68
- * @returns True if latitude is valid
69
- *
70
- * @example
71
- * isValidLatitude(45.5); // true
72
- * isValidLatitude(-90); // true (South Pole)
73
- * isValidLatitude(90); // true (North Pole)
74
- * isValidLatitude(91); // false (out of range)
75
- */
76
- export const isValidLatitude = (latitude) => {
77
- return isValidNumber(latitude) &&
78
- latitude >= LATITUDE_RANGE.MIN &&
79
- latitude <= LATITUDE_RANGE.MAX;
80
- };
81
- /**
82
- * Validates if a longitude value is within the valid range.
83
- *
84
- * Longitude must be a number between -180° and 180°, where:
85
- * - 0° is the Prime Meridian (Greenwich)
86
- * - Positive values are East longitude
87
- * - Negative values are West longitude
88
- *
89
- * @param longitude - Longitude in degrees to validate
90
- * @returns True if longitude is valid
91
- *
92
- * @example
93
- * isValidLongitude(0); // true (Prime Meridian)
94
- * isValidLongitude(180); // true (International Date Line)
95
- * isValidLongitude(-180); // true (International Date Line)
96
- * isValidLongitude(181); // false (out of range)
97
- */
98
- export const isValidLongitude = (longitude) => {
99
- return isValidNumber(longitude) &&
100
- longitude >= LONGITUDE_RANGE.MIN &&
101
- longitude <= LONGITUDE_RANGE.MAX;
102
- };
103
- /**
104
- * Validates if an angle value is within the valid range.
105
- *
106
- * Angles in natural time are measured in degrees from 0° to 360°,
107
- * representing a full circle. This is used for time of day and
108
- * celestial positions.
109
- *
110
- * @param angle - Angle in degrees to validate
111
- * @returns True if angle is valid
112
- *
113
- * @example
114
- * isValidAngle(180); // true (half circle)
115
- * isValidAngle(360); // true (full circle)
116
- * isValidAngle(0); // true (beginning of circle)
117
- * isValidAngle(361); // false (out of range)
118
- * isValidAngle(-1); // false (out of range)
119
- */
120
- export const isValidAngle = (angle) => {
121
- return isValidNumber(angle) &&
122
- angle >= ANGLE_RANGE.MIN &&
123
- angle <= ANGLE_RANGE.MAX;
124
- };
125
- /**
126
- * Validates if a timestamp is a valid Unix time.
127
- *
128
- * Unix timestamps represent the number of milliseconds since
129
- * January 1, 1970, 00:00:00 UTC (the Unix epoch).
130
- * Valid timestamps must be positive numbers.
131
- *
132
- * @param timestamp - Unix timestamp in milliseconds
133
- * @returns True if timestamp is valid
134
- *
135
- * @example
136
- * isValidTimestamp(Date.now()); // true
137
- * isValidTimestamp(0); // false (Unix epoch exactly)
138
- * isValidTimestamp(-1); // false (before Unix epoch)
139
- */
140
- export const isValidTimestamp = (timestamp) => {
141
- return isValidNumber(timestamp) && timestamp > 0;
142
- };
143
- /**
144
- * Validates if an object is a properly initialized NaturalDate instance.
145
- *
146
- * This function performs a comprehensive check of a NaturalDate object,
147
- * verifying that all properties are present and within valid ranges.
148
- * It's used to ensure that only valid NaturalDate objects are used in calculations.
149
- *
150
- * @param date - Value to validate
151
- * @returns True if value is a valid NaturalDate instance
152
- *
153
- * @example
154
- * const naturalDate = new NaturalDate(new Date(), 0);
155
- * isValidNaturalDate(naturalDate); // true
156
- *
157
- * isValidNaturalDate({}); // false (not a NaturalDate instance)
158
- * isValidNaturalDate(null); // false
159
- */
160
- export const isValidNaturalDate = (date) => {
161
- if (!(date instanceof NaturalDate)) {
162
- return false;
163
- }
164
- // Basic property validations
165
- if (!isValidTimestamp(date.unixTime) ||
166
- !isValidLongitude(date.longitude) ||
167
- !isValidNumber(date.year)) {
168
- return false;
169
- }
170
- // Natural calendar validations
171
- return (isValidNumber(date.moon) &&
172
- date.moon >= NATURAL_DATE_RANGES.MOON.MIN &&
173
- date.moon <= NATURAL_DATE_RANGES.MOON.MAX &&
174
- isValidNumber(date.week) &&
175
- date.week >= NATURAL_DATE_RANGES.WEEK.MIN &&
176
- date.week <= NATURAL_DATE_RANGES.WEEK.MAX &&
177
- isValidNumber(date.weekOfMoon) &&
178
- date.weekOfMoon >= NATURAL_DATE_RANGES.WEEK_OF_MOON.MIN &&
179
- date.weekOfMoon <= NATURAL_DATE_RANGES.WEEK_OF_MOON.MAX &&
180
- isValidNumber(date.dayOfMoon) &&
181
- date.dayOfMoon >= NATURAL_DATE_RANGES.DAY_OF_MOON.MIN &&
182
- date.dayOfMoon <= NATURAL_DATE_RANGES.DAY_OF_MOON.MAX &&
183
- isValidNumber(date.dayOfWeek) &&
184
- date.dayOfWeek >= NATURAL_DATE_RANGES.DAY_OF_WEEK.MIN &&
185
- date.dayOfWeek <= NATURAL_DATE_RANGES.DAY_OF_WEEK.MAX &&
186
- isValidNumber(date.dayOfYear) &&
187
- date.dayOfYear >= 1 &&
188
- date.dayOfYear <= date.yearDuration &&
189
- typeof date.isRainbowDay === 'boolean' &&
190
- isValidAngle(date.time) &&
191
- isValidTimestamp(date.yearStart) &&
192
- isValidNumber(date.yearDuration) &&
193
- isValidTimestamp(date.nadir));
194
- };
195
- /**
196
- * Validates if a string is a valid cache key.
197
- *
198
- * Cache keys are used for storing and retrieving calculated values
199
- * to improve performance. Valid keys must be non-empty strings.
200
- *
201
- * @param key - Cache key to validate
202
- * @returns True if key is valid
203
- *
204
- * @example
205
- * isValidCacheKey('SUN_2023_45.5_0'); // true
206
- * isValidCacheKey(''); // false (empty string)
207
- * isValidCacheKey(123); // false (not a string)
208
- */
209
- export const isValidCacheKey = (key) => {
210
- return typeof key === 'string' && key.length > 0;
211
- };
212
- /**
213
- * Throws a formatted error for invalid parameters.
214
- *
215
- * This utility function creates consistent error messages for validation failures,
216
- * making it easier to identify and fix issues with invalid inputs.
217
- *
218
- * @param paramName - Name of the invalid parameter
219
- * @param value - Invalid value
220
- * @param expectedType - Description of expected type/format
221
- * @throws {Error} Formatted validation error
222
- *
223
- * @example
224
- * // If latitude is invalid:
225
- * throwValidationError('latitude', 95, 'number between -90 and 90');
226
- * // Throws: "Error: Invalid latitude: 95. Expected number between -90 and 90"
227
- */
228
- export const throwValidationError = (paramName, value, expectedType) => {
229
- throw new Error(`Invalid ${paramName}: ${value}. Expected ${expectedType}`);
230
- };
231
- //# sourceMappingURL=validators.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"validators.js","sourceRoot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