@thejob/util 1.0.17 → 1.0.19

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.
@@ -270,7 +270,7 @@ const displayNameLabel = (name, type = "full", onEmptyLabel = "Not available") =
270
270
  }
271
271
  return (sequence
272
272
  .filter(Boolean)
273
- .filter((fragment) => fragment !== schema_1.EmptyString)
273
+ .filter((fragment) => fragment !== schema_1.EMPTY_STRING)
274
274
  .join(" ") || onEmptyLabel);
275
275
  };
276
276
  exports.displayNameLabel = displayNameLabel;
@@ -1,12 +1,18 @@
1
1
  import { DateRangeOption } from "@thejob/schema";
2
2
  import dayjs, { Dayjs } from "dayjs";
3
+ export type DateAgoUnit = "seconds" | "minutes" | "hours" | "days" | "weeks" | "months" | "years";
4
+ export interface DateAgoResult {
5
+ value: number;
6
+ unit: DateAgoUnit;
7
+ isPast: boolean;
8
+ }
3
9
  export declare const toDayJS: ({ date, dateFormat, strict, utc, }?: {
4
10
  date?: string | number | Dayjs;
5
11
  dateFormat?: string;
6
12
  strict?: boolean;
7
13
  utc?: boolean;
8
14
  }) => dayjs.Dayjs;
9
- export declare const dateAgo: (value: number | string | Date) => string;
15
+ export declare const dateAgo: (value: number | string | Date) => DateAgoResult;
10
16
  export declare const dateRangeOptionToDateRange: (option: DateRangeOption) => {
11
17
  startDate: string | undefined;
12
18
  endDate: string | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/date/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAoB,MAAM,gBAAgB,CAAC;AACnE,OAAO,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAQrC,eAAO,MAAM,OAAO,GAAI,qCAKrB;IACD,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,GAAG,CAAC,EAAE,OAAO,CAAC;CACV,gBASL,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,OAAO,MAAM,GAAG,MAAM,GAAG,IAAI,WAEpD,CAAC;AAEF,eAAO,MAAM,0BAA0B,GAAI,QAAQ,eAAe;;;CA4CjE,CAAC"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/date/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAsB,MAAM,gBAAgB,CAAC;AACrE,OAAO,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAQrC,MAAM,MAAM,WAAW,GACnB,SAAS,GACT,SAAS,GACT,OAAO,GACP,MAAM,GACN,OAAO,GACP,QAAQ,GACR,OAAO,CAAC;AAEZ,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,WAAW,CAAC;IAClB,MAAM,EAAE,OAAO,CAAC;CACjB;AAED,eAAO,MAAM,OAAO,GAAI,qCAKrB;IACD,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,GAAG,CAAC,EAAE,OAAO,CAAC;CACV,gBASL,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,OAAO,MAAM,GAAG,MAAM,GAAG,IAAI,KAAG,aA6BvD,CAAC;AAEF,eAAO,MAAM,0BAA0B,GAAI,QAAQ,eAAe;;;CA4CjE,CAAC"}
@@ -12,7 +12,7 @@ const customParseFormat_1 = __importDefault(require("dayjs/plugin/customParseFor
12
12
  dayjs_1.default.extend(relativeTime_1.default);
13
13
  dayjs_1.default.extend(localizedFormat_1.default);
14
14
  dayjs_1.default.extend(customParseFormat_1.default);
15
- const toDayJS = ({ date, dateFormat = schema_1.SystemDateFormat, strict = false, utc = false, } = {}) => {
15
+ const toDayJS = ({ date, dateFormat = schema_1.SYSTEM_DATE_FORMAT, strict = false, utc = false, } = {}) => {
16
16
  return (0, dayjs_1.default)(date, {
17
17
  utc,
18
18
  format: dateFormat,
@@ -20,7 +20,38 @@ const toDayJS = ({ date, dateFormat = schema_1.SystemDateFormat, strict = false,
20
20
  };
21
21
  exports.toDayJS = toDayJS;
22
22
  const dateAgo = (value) => {
23
- return (0, dayjs_1.default)(value).fromNow();
23
+ const now = (0, dayjs_1.default)();
24
+ const date = (0, dayjs_1.default)(value);
25
+ const isPast = date.isBefore(now);
26
+ const diff = isPast ? now.diff(date) : date.diff(now);
27
+ const seconds = Math.floor(diff / 1000);
28
+ const minutes = Math.floor(seconds / 60);
29
+ const hours = Math.floor(minutes / 60);
30
+ const days = Math.floor(hours / 24);
31
+ const weeks = Math.floor(days / 7);
32
+ const months = Math.floor(days / 30);
33
+ const years = Math.floor(days / 365);
34
+ if (years > 0) {
35
+ return { value: years, unit: "years", isPast };
36
+ }
37
+ else if (months > 0) {
38
+ return { value: months, unit: "months", isPast };
39
+ }
40
+ else if (weeks > 0) {
41
+ return { value: weeks, unit: "weeks", isPast };
42
+ }
43
+ else if (days > 0) {
44
+ return { value: days, unit: "days", isPast };
45
+ }
46
+ else if (hours > 0) {
47
+ return { value: hours, unit: "hours", isPast };
48
+ }
49
+ else if (minutes > 0) {
50
+ return { value: minutes, unit: "minutes", isPast };
51
+ }
52
+ else {
53
+ return { value: seconds, unit: "seconds", isPast };
54
+ }
24
55
  };
25
56
  exports.dateAgo = dateAgo;
26
57
  const dateRangeOptionToDateRange = (option) => {
@@ -63,8 +94,8 @@ const dateRangeOptionToDateRange = (option) => {
63
94
  break;
64
95
  }
65
96
  return {
66
- startDate: dateRange.startDate?.format(schema_1.SystemDateFormat),
67
- endDate: dateRange.endDate?.format(schema_1.SystemDateFormat),
97
+ startDate: dateRange.startDate?.format(schema_1.SYSTEM_DATE_FORMAT),
98
+ endDate: dateRange.endDate?.format(schema_1.SYSTEM_DATE_FORMAT),
68
99
  };
69
100
  };
70
101
  exports.dateRangeOptionToDateRange = dateRangeOptionToDateRange;
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { EmptyString, ExperienceLevel, ProficiencyLevel, UserCompletenessSchema, } from "@thejob/schema";
2
+ import { EMPTY_STRING, ExperienceLevel, ProficiencyLevel, UserCompletenessSchema, } from "@thejob/schema";
3
3
  import { customAlphabet } from "nanoid";
4
4
  export const removeEmpty = (obj) => {
5
5
  // Check if the value is an object and not null
@@ -249,7 +249,7 @@ export const displayNameLabel = (name, type = "full", onEmptyLabel = "Not availa
249
249
  }
250
250
  return (sequence
251
251
  .filter(Boolean)
252
- .filter((fragment) => fragment !== EmptyString)
252
+ .filter((fragment) => fragment !== EMPTY_STRING)
253
253
  .join(" ") || onEmptyLabel);
254
254
  };
255
255
  export const displayMobileNumberLabel = (mobileNumber, options) => {
@@ -1,12 +1,18 @@
1
1
  import { DateRangeOption } from "@thejob/schema";
2
2
  import dayjs, { Dayjs } from "dayjs";
3
+ export type DateAgoUnit = "seconds" | "minutes" | "hours" | "days" | "weeks" | "months" | "years";
4
+ export interface DateAgoResult {
5
+ value: number;
6
+ unit: DateAgoUnit;
7
+ isPast: boolean;
8
+ }
3
9
  export declare const toDayJS: ({ date, dateFormat, strict, utc, }?: {
4
10
  date?: string | number | Dayjs;
5
11
  dateFormat?: string;
6
12
  strict?: boolean;
7
13
  utc?: boolean;
8
14
  }) => dayjs.Dayjs;
9
- export declare const dateAgo: (value: number | string | Date) => string;
15
+ export declare const dateAgo: (value: number | string | Date) => DateAgoResult;
10
16
  export declare const dateRangeOptionToDateRange: (option: DateRangeOption) => {
11
17
  startDate: string | undefined;
12
18
  endDate: string | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/date/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAoB,MAAM,gBAAgB,CAAC;AACnE,OAAO,KAAK,EA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, SystemDateFormat } from "@thejob/schema";
1
+ import { DateRangeOption, SYSTEM_DATE_FORMAT } from "@thejob/schema";
2
2
  import dayjs from "dayjs";
3
3
  import localizedFormat from "dayjs/plugin/localizedFormat";
4
4
  import relativeTime from "dayjs/plugin/relativeTime";
@@ -6,14 +6,45 @@ import customParseFormat from "dayjs/plugin/customParseFormat";
6
6
  dayjs.extend(relativeTime);
7
7
  dayjs.extend(localizedFormat);
8
8
  dayjs.extend(customParseFormat);
9
- export const toDayJS = ({ date, dateFormat = SystemDateFormat, strict = false, utc = false, } = {}) => {
9
+ export const toDayJS = ({ date, dateFormat = SYSTEM_DATE_FORMAT, strict = false, utc = false, } = {}) => {
10
10
  return dayjs(date, {
11
11
  utc,
12
12
  format: dateFormat,
13
13
  }, strict);
14
14
  };
15
15
  export const dateAgo = (value) => {
16
- return dayjs(value).fromNow();
16
+ const now = dayjs();
17
+ const date = dayjs(value);
18
+ const isPast = date.isBefore(now);
19
+ const diff = isPast ? now.diff(date) : date.diff(now);
20
+ const seconds = Math.floor(diff / 1000);
21
+ const minutes = Math.floor(seconds / 60);
22
+ const hours = Math.floor(minutes / 60);
23
+ const days = Math.floor(hours / 24);
24
+ const weeks = Math.floor(days / 7);
25
+ const months = Math.floor(days / 30);
26
+ const years = Math.floor(days / 365);
27
+ if (years > 0) {
28
+ return { value: years, unit: "years", isPast };
29
+ }
30
+ else if (months > 0) {
31
+ return { value: months, unit: "months", isPast };
32
+ }
33
+ else if (weeks > 0) {
34
+ return { value: weeks, unit: "weeks", isPast };
35
+ }
36
+ else if (days > 0) {
37
+ return { value: days, unit: "days", isPast };
38
+ }
39
+ else if (hours > 0) {
40
+ return { value: hours, unit: "hours", isPast };
41
+ }
42
+ else if (minutes > 0) {
43
+ return { value: minutes, unit: "minutes", isPast };
44
+ }
45
+ else {
46
+ return { value: seconds, unit: "seconds", isPast };
47
+ }
17
48
  };
18
49
  export const dateRangeOptionToDateRange = (option) => {
19
50
  const today = toDayJS({ utc: true });
@@ -55,7 +86,7 @@ export const dateRangeOptionToDateRange = (option) => {
55
86
  break;
56
87
  }
57
88
  return {
58
- startDate: dateRange.startDate?.format(SystemDateFormat),
59
- endDate: dateRange.endDate?.format(SystemDateFormat),
89
+ startDate: dateRange.startDate?.format(SYSTEM_DATE_FORMAT),
90
+ endDate: dateRange.endDate?.format(SYSTEM_DATE_FORMAT),
60
91
  };
61
92
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thejob/util",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/esm/index.d.ts",
@@ -34,7 +34,7 @@
34
34
  "typescript": "^5.7.3"
35
35
  },
36
36
  "dependencies": {
37
- "@thejob/schema": "^1.0.67",
37
+ "@thejob/schema": "^1.0.69",
38
38
  "dayjs": "^1.11.13",
39
39
  "mongoose": "^8.11.0",
40
40
  "nanoid": "3.3.5",