@jiakun-zhao/utils 1.3.5 → 1.3.7

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/dist/index.d.mts CHANGED
@@ -4,6 +4,10 @@ declare const isArray: (arg: any) => arg is any[];
4
4
  declare function singleOrNull<T>(arr: T[]): T | null;
5
5
  declare function createArray<T = number>(length: number, mapFn?: (index: number) => T): T[];
6
6
 
7
+ type Unit = 'year' | 'month' | 'dayOfMonth' | 'hour' | 'minute' | 'second' | 'millisecond' | 'dayOfWeek';
8
+ declare function isSameDay(date: Date, anotherDate?: Date): boolean;
9
+ declare function dateCalculate(date: Date, value: number, unit?: Unit): Date;
10
+
7
11
  declare function cleanText(str: string, searchValue: RegExp | string): string;
8
12
 
9
13
  declare function getValueOrUndefined<T>(condition: any, value: T): T | undefined;
@@ -12,4 +16,4 @@ declare function from<T>(value: T): {
12
16
  to: <R>(fn: (value: T) => R) => ReturnType<typeof from<R>>;
13
17
  };
14
18
 
15
- export { cleanText, createArray, from, getValueOrUndefined, isArray, singleOrNull };
19
+ export { cleanText, createArray, dateCalculate, from, getValueOrUndefined, isArray, isSameDay, singleOrNull };
package/dist/index.d.ts CHANGED
@@ -4,6 +4,10 @@ declare const isArray: (arg: any) => arg is any[];
4
4
  declare function singleOrNull<T>(arr: T[]): T | null;
5
5
  declare function createArray<T = number>(length: number, mapFn?: (index: number) => T): T[];
6
6
 
7
+ type Unit = 'year' | 'month' | 'dayOfMonth' | 'hour' | 'minute' | 'second' | 'millisecond' | 'dayOfWeek';
8
+ declare function isSameDay(date: Date, anotherDate?: Date): boolean;
9
+ declare function dateCalculate(date: Date, value: number, unit?: Unit): Date;
10
+
7
11
  declare function cleanText(str: string, searchValue: RegExp | string): string;
8
12
 
9
13
  declare function getValueOrUndefined<T>(condition: any, value: T): T | undefined;
@@ -12,4 +16,4 @@ declare function from<T>(value: T): {
12
16
  to: <R>(fn: (value: T) => R) => ReturnType<typeof from<R>>;
13
17
  };
14
18
 
15
- export { cleanText, createArray, from, getValueOrUndefined, isArray, singleOrNull };
19
+ export { cleanText, createArray, dateCalculate, from, getValueOrUndefined, isArray, isSameDay, singleOrNull };
package/dist/index.mjs CHANGED
@@ -8,6 +8,51 @@ function createArray(length, mapFn) {
8
8
  return Array.from({ length }, (_, index) => mapFn?.(index) ?? index);
9
9
  }
10
10
 
11
+ function isSameDay(date, anotherDate = /* @__PURE__ */ new Date()) {
12
+ return date.getDate() === anotherDate.getDate() && date.getMonth() === anotherDate.getMonth() && date.getFullYear() === anotherDate.getFullYear();
13
+ }
14
+ function dateCalculate(date, value, unit = "dayOfMonth") {
15
+ const result = new Date(date);
16
+ switch (unit) {
17
+ case "year": {
18
+ result.setFullYear(date.getFullYear() + value);
19
+ break;
20
+ }
21
+ case "month": {
22
+ result.setMonth(date.getMonth() + value);
23
+ break;
24
+ }
25
+ case "dayOfMonth": {
26
+ result.setDate(date.getDate() + value);
27
+ break;
28
+ }
29
+ case "dayOfWeek": {
30
+ result.setDate(date.getDay() + value);
31
+ break;
32
+ }
33
+ case "hour": {
34
+ result.setHours(date.getHours() + value);
35
+ break;
36
+ }
37
+ case "minute": {
38
+ result.setMinutes(date.getMinutes() + value);
39
+ break;
40
+ }
41
+ case "second": {
42
+ result.setSeconds(date.getSeconds() + value);
43
+ break;
44
+ }
45
+ case "millisecond": {
46
+ result.setMilliseconds(date.getMilliseconds() + value);
47
+ break;
48
+ }
49
+ default: {
50
+ return date;
51
+ }
52
+ }
53
+ return result;
54
+ }
55
+
11
56
  function cleanText(str, searchValue) {
12
57
  return str.replace(searchValue, "");
13
58
  }
@@ -22,4 +67,4 @@ function from(value) {
22
67
  };
23
68
  }
24
69
 
25
- export { cleanText, createArray, from, getValueOrUndefined, isArray, singleOrNull };
70
+ export { cleanText, createArray, dateCalculate, from, getValueOrUndefined, isArray, isSameDay, singleOrNull };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jiakun-zhao/utils",
3
3
  "type": "module",
4
- "version": "1.3.5",
4
+ "version": "1.3.7",
5
5
  "description": "Utils.",
6
6
  "author": "Jiakun Zhao <hi@zhaojiakun.com>",
7
7
  "license": "MIT",