@h3ravel/support 0.16.1 → 0.17.0-alpha.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/dist/index.js CHANGED
@@ -1,18 +1,10 @@
1
- import { t as __export } from "./chunk-NmTyJVUt.js";
2
- import { t as RuntimeException } from "./RuntimeException-CrNX0B-p.js";
1
+ import { t as __export } from "./chunk-CGA-kHjv.js";
2
+ import { t as RuntimeException } from "./RuntimeException-Bt4SxSOi.js";
3
+ import { n as format, t as DateTime } from "./Time-DXrqE9Zj.js";
3
4
  import { Collection as Collection$1 } from "@h3ravel/collect.js";
4
5
  import { createHash, createHmac, randomBytes, randomUUID } from "crypto";
5
6
  import process from "process";
6
7
  import util from "util";
7
- import dayjs from "dayjs";
8
- import advancedFormat from "dayjs/plugin/advancedFormat.js";
9
- import customParseFormat from "dayjs/plugin/customParseFormat.js";
10
- import dayOfYear from "dayjs/plugin/dayOfYear.js";
11
- import isBetween from "dayjs/plugin/isBetween.js";
12
- import isLeapYear from "dayjs/plugin/isLeapYear.js";
13
- import relativeTime from "dayjs/plugin/relativeTime.js";
14
- import timezone from "dayjs/plugin/timezone.js";
15
- import utc from "dayjs/plugin/utc.js";
16
8
  import { isIP } from "node:net";
17
9
  import { Logger, trait } from "@h3ravel/shared";
18
10
  import { readFile, stat } from "node:fs/promises";
@@ -868,6 +860,24 @@ var Obj = class Obj {
868
860
  });
869
861
  }
870
862
  /**
863
+ * Checks if an object is not empty
864
+ *
865
+ * @param obj
866
+ * @returns
867
+ */
868
+ static isNotEmpty(obj) {
869
+ return Object.keys(obj).length >= 1;
870
+ }
871
+ /**
872
+ * Checks if an object is empty
873
+ *
874
+ * @param obj
875
+ * @returns
876
+ */
877
+ static isEmpty(obj) {
878
+ return !this.isNotEmpty(obj);
879
+ }
880
+ /**
871
881
  * Check if an object is associative (has at least one non-numeric key).
872
882
  *
873
883
  * @param obj
@@ -888,6 +898,36 @@ var Obj = class Obj {
888
898
  return isPlainObject(value, allowArray);
889
899
  }
890
900
  /**
901
+ * Removes the last element from an object and returns it.
902
+ * If the object is empty, undefined is returned and the object is not modified.
903
+ *
904
+ * @param obj
905
+ * @returns
906
+ */
907
+ static pop(obj) {
908
+ const keys = Object.keys(obj);
909
+ if (!keys.length) return void 0;
910
+ const lastKey = keys[keys.length - 1];
911
+ const value = obj[lastKey];
912
+ delete obj[lastKey];
913
+ return value;
914
+ }
915
+ /**
916
+ * Removes the first element from an array and returns it.
917
+ * If the array is empty, undefined is returned and the array is not modified.
918
+ *
919
+ * @param obj
920
+ * @returns
921
+ */
922
+ static shift(obj) {
923
+ const keys = Object.keys(obj);
924
+ if (!keys.length) return void 0;
925
+ const firstKey = keys[0];
926
+ const value = obj[firstKey];
927
+ delete obj[firstKey];
928
+ return value;
929
+ }
930
+ /**
891
931
  * Add a prefix to all keys of the object.
892
932
  *
893
933
  * @param obj
@@ -920,6 +960,16 @@ var Obj = class Obj {
920
960
  return parts.join("&");
921
961
  }
922
962
  /**
963
+ * If the given value is not an associative object, wrap it in one.
964
+ *
965
+ * @param value
966
+ */
967
+ static wrap(value) {
968
+ value = typeof value === "string" || typeof value === "number" ? this.arrayWrap(value) : value;
969
+ if (Array.isArray(value)) value = Object.fromEntries(value.map((e, i) => [i, e]));
970
+ return value;
971
+ }
972
+ /**
923
973
  * undot
924
974
  *
925
975
  * Convert a dot-notated object back into nested structure.
@@ -930,9 +980,21 @@ var Obj = class Obj {
930
980
  * @param obj
931
981
  * @returns
932
982
  */
933
- undot(obj) {
983
+ static undot(obj) {
934
984
  return undot(obj);
935
985
  }
986
+ /**
987
+ * If the given value is not an array and not null, wrap it in one.
988
+ *
989
+ * Non-array values become [value]; null/undefined becomes [].
990
+ *
991
+ * @param value
992
+ * @returns
993
+ */
994
+ static arrayWrap(value) {
995
+ if (value === null || value === void 0) return [];
996
+ return Array.isArray(value) ? value : [value];
997
+ }
936
998
  };
937
999
 
938
1000
  //#endregion
@@ -1619,173 +1681,6 @@ var Arr = class Arr {
1619
1681
  }
1620
1682
  };
1621
1683
 
1622
- //#endregion
1623
- //#region src/Helpers/Time.ts
1624
- dayjs.extend(utc);
1625
- dayjs.extend(timezone);
1626
- dayjs.extend(dayOfYear);
1627
- dayjs.extend(isBetween);
1628
- dayjs.extend(isLeapYear);
1629
- dayjs.extend(relativeTime);
1630
- dayjs.extend(advancedFormat);
1631
- dayjs.extend(customParseFormat);
1632
- const phpToDayjsTokens = (format$1) => format$1.replace(/Y/g, "YYYY").replace(/m/g, "MM").replace(/d/g, "DD").replace(/H/g, "HH").replace(/i/g, "mm").replace(/s/g, "ss");
1633
- function format(date, fmt) {
1634
- return dayjs(date).format(phpToDayjsTokens(fmt));
1635
- }
1636
- const TimeClass = class {};
1637
- var DateTime = class DateTime extends TimeClass {
1638
- instance;
1639
- constructor(config, format$1, locale, strict) {
1640
- super(config);
1641
- this.instance = dayjs(config, format$1, locale, strict);
1642
- return new Proxy(this, { get: (target, prop, receiver) => {
1643
- if (prop in target) return Reflect.get(target, prop, receiver);
1644
- const value = Reflect.get(this.instance, prop, receiver);
1645
- if (typeof value === "function") return (...args) => {
1646
- const result = value.apply(this.instance, args);
1647
- return dayjs.isDayjs(result) ? new DateTime(result) : result;
1648
- };
1649
- return value;
1650
- } });
1651
- }
1652
- /**
1653
- * Start time of a specific unit.
1654
- *
1655
- * @returns
1656
- */
1657
- start(unit = "days") {
1658
- return this.startOf(unit);
1659
- }
1660
- /**
1661
- * Set the timezone for the instance
1662
- *
1663
- * @param timezone
1664
- * @returns
1665
- */
1666
- setTimezone(timezone$1, keepLocalTime) {
1667
- return new DateTime(this.tz(timezone$1, keepLocalTime));
1668
- }
1669
- /**
1670
- * End time of a specific unit.
1671
- *
1672
- * @returns
1673
- */
1674
- end(unit = "days") {
1675
- return this.endOf(unit);
1676
- }
1677
- /**
1678
- * Get the first day of the month of the given date
1679
- *
1680
- * @returns
1681
- */
1682
- firstDayOfMonth() {
1683
- return new DateTime(new Date(Date.UTC(this.year(), this.month(), 1)));
1684
- }
1685
- carbonFormat(template) {
1686
- return template ? this.format(phpToDayjsTokens(template)) : this.format();
1687
- }
1688
- /**
1689
- * Get the last day of the month of the given date
1690
- *
1691
- * @returns
1692
- */
1693
- lastDayOfMonth() {
1694
- return new DateTime(new Date(Date.UTC(this.year(), this.month() + 1, 0)));
1695
- }
1696
- /**
1697
- * Get a random time between the specified hour and minute.
1698
- *
1699
- * @param startHour
1700
- * @param startMinute
1701
- * @param endHour
1702
- * @param endMinute
1703
- * @returns
1704
- */
1705
- randomTime(startHour = 9, startMinute = 0, endHour = 17, endMinute = 0) {
1706
- const today = /* @__PURE__ */ new Date();
1707
- const startMinutes = startHour * 60 + startMinute;
1708
- const endMinutes = endHour * 60 + endMinute;
1709
- const randomMinutes = Math.floor(Math.random() * (endMinutes - startMinutes)) + startMinutes;
1710
- const hour = Math.floor(randomMinutes / 60);
1711
- const minute = randomMinutes % 60;
1712
- const date = new Date(today);
1713
- date.setHours(hour, minute, 0, 0);
1714
- return new DateTime(date);
1715
- }
1716
- /**
1717
- * Create a date for a given timestamp.
1718
- *
1719
- * @param timestamp - Unix timestamp
1720
- *
1721
- * @return {Date} object
1722
- */
1723
- static fromTimestamp(timestamp) {
1724
- return new DateTime(timestamp * 1e3);
1725
- }
1726
- /**
1727
- * Get current time instance.
1728
- *
1729
- * @returns Current time
1730
- */
1731
- static now() {
1732
- return new DateTime();
1733
- }
1734
- /**
1735
- * Parse the time
1736
- *
1737
- * @param date
1738
- * @returns
1739
- */
1740
- static parse(date) {
1741
- return new DateTime(date);
1742
- }
1743
- /**
1744
- * Get the formatted date according to the string of tokens passed in.
1745
- *
1746
- * To escape characters, wrap them in square brackets (e.g. [MM]).
1747
- *
1748
- * @param time - current time
1749
- * @param template - time format
1750
- */
1751
- static format(time, template) {
1752
- return new DateTime(time).format(template);
1753
- }
1754
- /**
1755
- * Get the difference in days from today.
1756
- *
1757
- * @param time
1758
- * @param startHour
1759
- * @param startMinute
1760
- * @param endHour
1761
- * @param endMinute
1762
- * @returns
1763
- */
1764
- static randomTime(time, startHour, startMinute, endHour, endMinute) {
1765
- return new DateTime(time).randomTime(startHour, startMinute, endHour, endMinute);
1766
- }
1767
- /**
1768
- * Get the first day of the month of the given date
1769
- *
1770
- * @param time
1771
- *
1772
- * @returns
1773
- */
1774
- static firstDayOfMonth(time) {
1775
- return new DateTime(time).firstDayOfMonth();
1776
- }
1777
- /**
1778
- * Get the last day of the month of the given date
1779
- *
1780
- * @param time
1781
- *
1782
- * @returns
1783
- */
1784
- static lastDayOfMonth(time) {
1785
- return new DateTime(time).lastDayOfMonth();
1786
- }
1787
- };
1788
-
1789
1684
  //#endregion
1790
1685
  //#region src/Helpers/Str.ts
1791
1686
  let Mode = /* @__PURE__ */ function(Mode$1) {
@@ -6021,12 +5916,12 @@ var Stringable = class Stringable {
6021
5916
  break;
6022
5917
  }
6023
5918
  case "Z": {
6024
- const timezone$1 = now.toLocaleDateString("en-us", {
5919
+ const timezone = now.toLocaleDateString("en-us", {
6025
5920
  timeZoneName: "longOffset",
6026
5921
  timeZone: tz ?? void 0
6027
5922
  });
6028
- const symbol = timezone$1.match(/[+-]/);
6029
- const data = timezone$1.split(/[+-]/);
5923
+ const symbol = timezone.match(/[+-]/);
5924
+ const data = timezone.split(/[+-]/);
6030
5925
  const sign = symbol ? symbol.pop() : "+";
6031
5926
  const offset = data.length === 2 ? data[1] : "0:00";
6032
5927
  const hours$1 = parseInt(offset.split(":")[0]);
@@ -0,0 +1,56 @@
1
+ const require_Time = require('./Time-H1gLMvCJ.cjs');
2
+ let __h3ravel_shared = require("@h3ravel/shared");
3
+ let dayjs_plugin_duration_js = require("dayjs/plugin/duration.js");
4
+ dayjs_plugin_duration_js = require_Time.__toESM(dayjs_plugin_duration_js);
5
+
6
+ //#region src/Traits/InteractsWithTime.ts
7
+ const InteractsWithTime = (0, __h3ravel_shared.trait)((Base) => class InteractsWithTime$1 extends Base {
8
+ /**
9
+ * Get the number of seconds until the given DateTime.
10
+ *
11
+ * @param delay
12
+ */
13
+ secondsUntil(delay) {
14
+ delay = this.parseDateInterval(delay);
15
+ return delay instanceof require_Time.DateTime ? Math.max(0, delay.getTimestamp() - this.currentTime()) : Number(delay);
16
+ }
17
+ /**
18
+ * Get the "available at" UNIX timestamp.
19
+ *
20
+ * @param delay
21
+ */
22
+ availableAt(delay = 0) {
23
+ delay = this.parseDateInterval(delay);
24
+ return delay instanceof require_Time.DateTime ? delay.getTimestamp() : require_Time.DateTime.now().add(delay, "seconds").getTimestamp();
25
+ }
26
+ /**
27
+ * If the given value is an interval, convert it to a DateTime instance.
28
+ *
29
+ * @param delay
30
+ */
31
+ parseDateInterval(delay) {
32
+ if (typeof delay === "number") delay = require_Time.DateTime.now().add(delay);
33
+ return delay;
34
+ }
35
+ /**
36
+ * Get the current system time as a UNIX timestamp.
37
+ */
38
+ currentTime() {
39
+ return require_Time.DateTime.now().getTimestamp();
40
+ }
41
+ /**
42
+ * Given a start time, format the total run time for human readability.
43
+ *
44
+ * @param startTime
45
+ * @param endTime
46
+ */
47
+ runTimeForHumans(startTime, endTime) {
48
+ endTime ??= Date.now();
49
+ const runTime = endTime - startTime;
50
+ if (runTime < 1e3) return `${runTime.toFixed(2)}ms`;
51
+ return require_Time.DateTime.plugin(dayjs_plugin_duration_js.default).duration(runTime).humanize();
52
+ }
53
+ });
54
+
55
+ //#endregion
56
+ exports.InteractsWithTime = InteractsWithTime;
@@ -0,0 +1,41 @@
1
+ import { t as DateTime } from "./Time-DY_lqAIR.js";
2
+ import * as _h3ravel_shared0 from "@h3ravel/shared";
3
+
4
+ //#region src/Traits/InteractsWithTime.d.ts
5
+ declare const InteractsWithTime: _h3ravel_shared0.Trait<(Base: any) => {
6
+ new (): {
7
+ [x: string]: any;
8
+ /**
9
+ * Get the number of seconds until the given DateTime.
10
+ *
11
+ * @param delay
12
+ */
13
+ secondsUntil(delay: DateTime | number): number;
14
+ /**
15
+ * Get the "available at" UNIX timestamp.
16
+ *
17
+ * @param delay
18
+ */
19
+ availableAt(delay?: DateTime | number): number;
20
+ /**
21
+ * If the given value is an interval, convert it to a DateTime instance.
22
+ *
23
+ * @param delay
24
+ */
25
+ parseDateInterval(delay: DateTime | number): DateTime;
26
+ /**
27
+ * Get the current system time as a UNIX timestamp.
28
+ */
29
+ currentTime(): number;
30
+ /**
31
+ * Given a start time, format the total run time for human readability.
32
+ *
33
+ * @param startTime
34
+ * @param endTime
35
+ */
36
+ runTimeForHumans(startTime: number, endTime?: number): string;
37
+ };
38
+ [x: string]: any;
39
+ }, undefined>;
40
+ //#endregion
41
+ export { InteractsWithTime };
package/dist/traits.js ADDED
@@ -0,0 +1,55 @@
1
+ import { t as DateTime } from "./Time-DXrqE9Zj.js";
2
+ import { trait } from "@h3ravel/shared";
3
+ import duration from "dayjs/plugin/duration.js";
4
+
5
+ //#region src/Traits/InteractsWithTime.ts
6
+ const InteractsWithTime = trait((Base) => class InteractsWithTime$1 extends Base {
7
+ /**
8
+ * Get the number of seconds until the given DateTime.
9
+ *
10
+ * @param delay
11
+ */
12
+ secondsUntil(delay) {
13
+ delay = this.parseDateInterval(delay);
14
+ return delay instanceof DateTime ? Math.max(0, delay.getTimestamp() - this.currentTime()) : Number(delay);
15
+ }
16
+ /**
17
+ * Get the "available at" UNIX timestamp.
18
+ *
19
+ * @param delay
20
+ */
21
+ availableAt(delay = 0) {
22
+ delay = this.parseDateInterval(delay);
23
+ return delay instanceof DateTime ? delay.getTimestamp() : DateTime.now().add(delay, "seconds").getTimestamp();
24
+ }
25
+ /**
26
+ * If the given value is an interval, convert it to a DateTime instance.
27
+ *
28
+ * @param delay
29
+ */
30
+ parseDateInterval(delay) {
31
+ if (typeof delay === "number") delay = DateTime.now().add(delay);
32
+ return delay;
33
+ }
34
+ /**
35
+ * Get the current system time as a UNIX timestamp.
36
+ */
37
+ currentTime() {
38
+ return DateTime.now().getTimestamp();
39
+ }
40
+ /**
41
+ * Given a start time, format the total run time for human readability.
42
+ *
43
+ * @param startTime
44
+ * @param endTime
45
+ */
46
+ runTimeForHumans(startTime, endTime) {
47
+ endTime ??= Date.now();
48
+ const runTime = endTime - startTime;
49
+ if (runTime < 1e3) return `${runTime.toFixed(2)}ms`;
50
+ return DateTime.plugin(duration).duration(runTime).humanize();
51
+ }
52
+ });
53
+
54
+ //#endregion
55
+ export { InteractsWithTime };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h3ravel/support",
3
- "version": "0.16.1",
3
+ "version": "0.17.0-alpha.1",
4
4
  "description": "Shared helpers, facades and utilities for H3ravel.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -15,6 +15,10 @@
15
15
  "import": "./dist/facades.js",
16
16
  "require": "./dist/facades.cjs"
17
17
  },
18
+ "./traits": {
19
+ "import": "./dist/traits.js",
20
+ "require": "./dist/traits.cjs"
21
+ },
18
22
  "./package.json": "./package.json"
19
23
  },
20
24
  "files": [
@@ -43,15 +47,14 @@
43
47
  "@h3ravel/collect.js": "^5.3.3",
44
48
  "@types/luxon": "^3.7.1",
45
49
  "typescript": "^5.4.0",
46
- "@h3ravel/shared": "^0.28.1"
50
+ "@h3ravel/shared": "^0.29.0-alpha.1"
47
51
  },
48
52
  "dependencies": {
49
53
  "dayjs": "^1.11.18",
50
54
  "luxon": "^3.7.2",
51
- "@h3ravel/contracts": "^0.28.0"
55
+ "@h3ravel/contracts": "^0.29.0-alpha.1"
52
56
  },
53
57
  "scripts": {
54
- "barrel": "barrelsby --directory src --delete --singleQuotes",
55
58
  "build": "tsdown --config-loader unconfig",
56
59
  "dev": "tsx watch src/index.ts",
57
60
  "start": "node dist/index.js",
File without changes