react-native-dates-picker 0.1.7 → 0.2.1

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.
package/src/utils.ts CHANGED
@@ -53,7 +53,7 @@ export function isDateBetween(
53
53
  ): boolean {
54
54
  if (!startDate || !endDate) return false;
55
55
 
56
- return dayjs(date) <= endDate && dayjs(date) >= startDate;
56
+ return dayjs(date) <= dayjs(endDate) && dayjs(date) >= dayjs(startDate);
57
57
  }
58
58
 
59
59
  export const getFormattedDate = (date: DateType, format: string) =>
@@ -154,11 +154,11 @@ export function getFirstDayOfMonth(
154
154
  return d.date(1 - firstDayOfWeek).day();
155
155
  }
156
156
 
157
- export function getStartOfDay(date: DateType): DateType {
157
+ export function getStartOfDay(date: DateType): dayjs.Dayjs {
158
158
  return dayjs(date).startOf('day');
159
159
  }
160
160
 
161
- export function getEndOfDay(date: DateType): DateType {
161
+ export function getEndOfDay(date: DateType): dayjs.Dayjs {
162
162
  return dayjs(date).endOf('day');
163
163
  }
164
164
 
@@ -285,25 +285,8 @@ const generateDayObject = (
285
285
  };
286
286
  };
287
287
 
288
- export function addColorAlpha(color: string | undefined, opacity: number) {
289
- //if it has an alpha, remove it
290
- if (!color) color = '#000000';
291
-
292
- if (color.length > 7) color = color.substring(0, color.length - 2);
293
-
294
- // coerce values so ti is between 0 and 1.
295
- const _opacity = Math.round(Math.min(Math.max(opacity, 0), 1) * 255);
296
- let opacityHex = _opacity.toString(16).toUpperCase();
297
-
298
- // opacities near 0 need a trailing 0
299
- if (opacityHex.length === 1) opacityHex = '0' + opacityHex;
300
-
301
- return color + opacityHex;
302
- }
303
-
304
288
  /**
305
-
306
- 深度比较两个值是否相等
289
+ ** 深度比较两个值是否相等
307
290
  @param {any} value
308
291
  @param {any} other
309
292
  @returns {boolean}
@@ -359,21 +342,68 @@ export const isEqual = (value: any, other: any) => {
359
342
  return false;
360
343
  };
361
344
 
362
- export function throttle<T extends (...args: any[]) => any>(
363
- func: T,
364
- limit: number = 200
365
- ): T {
366
- let inThrottle = false;
367
- let lastResult: any;
368
-
369
- return ((...args: Parameters<T>) => {
370
- if (!inThrottle) {
371
- lastResult = func(...args);
372
- inThrottle = true;
373
- setTimeout(() => {
374
- inThrottle = false;
375
- }, limit);
345
+ /**
346
+ ** Adds alpha channel to a color string.
347
+ ** Supports hex, rgb, rgba, hsl, hsla color formats.
348
+ * @param {string} color - The color string.
349
+ * @param {number} alpha - The alpha value (0-1).
350
+ * @returns {string} The color string with alpha channel.
351
+ * @throws {Error} If the color format is not supported.
352
+ */
353
+ export const addColorAlpha = (color: string, alpha: number = 1): string => {
354
+ const a = clamp01(alpha);
355
+ const input = color.trim();
356
+
357
+ // hex: #RGB/#RRGGBB
358
+ const hexMatch = input.match(/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/);
359
+ if (hexMatch?.[1]) {
360
+ const hex = hexMatch[1];
361
+ let r: number, g: number, b: number;
362
+
363
+ if (hex.length === 3) {
364
+ r = parseInt(hex.charAt(0) + hex.charAt(0), 16);
365
+ g = parseInt(hex.charAt(1) + hex.charAt(1), 16);
366
+ b = parseInt(hex.charAt(2) + hex.charAt(2), 16);
367
+ } else {
368
+ r = parseInt(hex.slice(0, 2), 16);
369
+ g = parseInt(hex.slice(2, 4), 16);
370
+ b = parseInt(hex.slice(4, 6), 16);
376
371
  }
377
- return lastResult;
378
- }) as T;
372
+
373
+ return toHex8(r, g, b, a); // #RRGGBBAA
374
+ }
375
+
376
+ // rgb / rgba
377
+ const rgbMatch = input.match(/^rgba?\((.*)\)$/i);
378
+ if (rgbMatch?.[1]) {
379
+ const parts = rgbMatch[1].split(',').map((s) => s.trim());
380
+ if (parts.length < 3) throw new Error('Invalid rgb/rgba color');
381
+ const [r, g, b] = parts;
382
+ return `rgba(${r}, ${g}, ${b}, ${a})`; // rgba(r, g, b, alpha)[web:18]
383
+ }
384
+
385
+ // hsl / hsla
386
+ const hslMatch = input.match(/^hsla?\((.*)\)$/i);
387
+ if (hslMatch?.[1]) {
388
+ const parts = hslMatch[1].split(',').map((s) => s.trim());
389
+ if (parts.length < 3) throw new Error('Invalid hsl/hsla color');
390
+ const [h, s, l] = parts;
391
+ return `hsla(${h}, ${s}, ${l}, ${a})`; // hsla(h, s, l, alpha)[web:22][web:30]
392
+ }
393
+
394
+ throw new Error(`Unsupported color format: ${color}`);
395
+ };
396
+
397
+ function clamp01(n: number): number {
398
+ if (!Number.isFinite(n)) return 1;
399
+ return Math.min(1, Math.max(0, n));
400
+ }
401
+
402
+ function toHex2(n: number): string {
403
+ return Math.round(n).toString(16).padStart(2, '0').toUpperCase();
404
+ }
405
+
406
+ function toHex8(r: number, g: number, b: number, alpha01: number): string {
407
+ const aa = toHex2(alpha01 * 255);
408
+ return `#${toHex2(r)}${toHex2(g)}${toHex2(b)}${aa}`;
379
409
  }