@h3ravel/support 0.12.0 → 0.13.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.
package/dist/index.js CHANGED
@@ -3,14 +3,14 @@ import { createHash, createHmac, randomBytes, randomUUID } from "crypto";
3
3
  import process from "process";
4
4
  import util from "util";
5
5
  import dayjs from "dayjs";
6
- import advancedFormat from "dayjs/plugin/advancedFormat";
7
- import customParseFormat from "dayjs/plugin/customParseFormat";
8
- import dayOfYear from "dayjs/plugin/dayOfYear";
9
- import isBetween from "dayjs/plugin/isBetween";
10
- import isLeapYear from "dayjs/plugin/isLeapYear";
11
- import relativeTime from "dayjs/plugin/relativeTime";
12
- import timezone from "dayjs/plugin/timezone";
13
- import utc from "dayjs/plugin/utc";
6
+ import advancedFormat from "dayjs/plugin/advancedFormat.js";
7
+ import customParseFormat from "dayjs/plugin/customParseFormat.js";
8
+ import dayOfYear from "dayjs/plugin/dayOfYear.js";
9
+ import isBetween from "dayjs/plugin/isBetween.js";
10
+ import isLeapYear from "dayjs/plugin/isLeapYear.js";
11
+ import relativeTime from "dayjs/plugin/relativeTime.js";
12
+ import timezone from "dayjs/plugin/timezone.js";
13
+ import utc from "dayjs/plugin/utc.js";
14
14
 
15
15
  //#region src/Helpers/Arr.ts
16
16
  var Arr_exports = /* @__PURE__ */ __export({
@@ -30,7 +30,8 @@ var Arr_exports = /* @__PURE__ */ __export({
30
30
  range: () => range,
31
31
  reverse: () => reverse,
32
32
  shift: () => shift,
33
- take: () => take
33
+ take: () => take,
34
+ wrap: () => wrap
34
35
  });
35
36
  /**
36
37
  * Splits an array into chunks of a specified size.
@@ -128,6 +129,16 @@ const flatten = (arr) => {
128
129
  recurse(arr);
129
130
  return result;
130
131
  };
132
+ /**
133
+ * If the given value is not an array and not null, wrap it in one.
134
+ *
135
+ * @param {Array} value
136
+ * @return array
137
+ */
138
+ const wrap = (value) => {
139
+ if (value === null) return [];
140
+ return Array.isArray(value) ? value : [value];
141
+ };
131
142
 
132
143
  //#endregion
133
144
  //#region src/Helpers/Crypto.ts
@@ -667,6 +678,164 @@ const slugifyKeys = (obj, only = [], separator = "_") => {
667
678
  return Object.fromEntries(entries.map(([key, value]) => [slugify(key), value]));
668
679
  };
669
680
 
681
+ //#endregion
682
+ //#region src/Helpers/Time.ts
683
+ dayjs.extend(utc);
684
+ dayjs.extend(timezone);
685
+ dayjs.extend(dayOfYear);
686
+ dayjs.extend(isBetween);
687
+ dayjs.extend(isLeapYear);
688
+ dayjs.extend(relativeTime);
689
+ dayjs.extend(advancedFormat);
690
+ dayjs.extend(customParseFormat);
691
+ 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");
692
+ function format(date, fmt) {
693
+ return dayjs(date).format(phpToDayjsTokens(fmt));
694
+ }
695
+ const TimeClass = class {};
696
+ var DateTime = class DateTime extends TimeClass {
697
+ instance;
698
+ constructor(config) {
699
+ super(config);
700
+ this.instance = dayjs(config);
701
+ return new Proxy(this, { get: (target, prop, receiver) => {
702
+ if (prop in target) return Reflect.get(target, prop, receiver);
703
+ const value = Reflect.get(this.instance, prop, receiver);
704
+ if (typeof value === "function") return (...args) => {
705
+ const result = value.apply(this.instance, args);
706
+ return dayjs.isDayjs(result) ? new DateTime(result) : result;
707
+ };
708
+ return value;
709
+ } });
710
+ }
711
+ /**
712
+ * Start time of a specific unit.
713
+ *
714
+ * @returns
715
+ */
716
+ start(unit = "days") {
717
+ return this.startOf(unit);
718
+ }
719
+ /**
720
+ * End time of a specific unit.
721
+ *
722
+ * @returns
723
+ */
724
+ end(unit = "days") {
725
+ return this.endOf(unit);
726
+ }
727
+ /**
728
+ * Get the first day of the month of the given date
729
+ *
730
+ * @returns
731
+ */
732
+ firstDayOfMonth() {
733
+ return new DateTime(new Date(Date.UTC(this.year(), this.month(), 1)));
734
+ }
735
+ carbonFormat(template) {
736
+ return template ? this.format(phpToDayjsTokens(template)) : this.format();
737
+ }
738
+ /**
739
+ * Get the last day of the month of the given date
740
+ *
741
+ * @returns
742
+ */
743
+ lastDayOfMonth() {
744
+ return new DateTime(new Date(Date.UTC(this.year(), this.month() + 1, 0)));
745
+ }
746
+ /**
747
+ * Get a random time between the specified hour and minute.
748
+ *
749
+ * @param startHour
750
+ * @param startMinute
751
+ * @param endHour
752
+ * @param endMinute
753
+ * @returns
754
+ */
755
+ randomTime(startHour = 9, startMinute = 0, endHour = 17, endMinute = 0) {
756
+ const today = /* @__PURE__ */ new Date();
757
+ const startMinutes = startHour * 60 + startMinute;
758
+ const endMinutes = endHour * 60 + endMinute;
759
+ const randomMinutes = Math.floor(Math.random() * (endMinutes - startMinutes)) + startMinutes;
760
+ const hour = Math.floor(randomMinutes / 60);
761
+ const minute = randomMinutes % 60;
762
+ const date = new Date(today);
763
+ date.setHours(hour, minute, 0, 0);
764
+ return new DateTime(date);
765
+ }
766
+ /**
767
+ * Create a date for a given timestamp.
768
+ *
769
+ * @param timestamp - Unix timestamp
770
+ *
771
+ * @return {Date} object
772
+ */
773
+ static fromTimestamp(timestamp) {
774
+ return /* @__PURE__ */ new Date(timestamp * 1e3);
775
+ }
776
+ /**
777
+ * Get current time instance.
778
+ *
779
+ * @returns Current time
780
+ */
781
+ static now() {
782
+ return new DateTime();
783
+ }
784
+ /**
785
+ * Parse the time
786
+ *
787
+ * @param date
788
+ * @returns
789
+ */
790
+ static parse(date) {
791
+ return new DateTime(date);
792
+ }
793
+ /**
794
+ * Get the formatted date according to the string of tokens passed in.
795
+ *
796
+ * To escape characters, wrap them in square brackets (e.g. [MM]).
797
+ *
798
+ * @param time - current time
799
+ * @param template - time format
800
+ */
801
+ static format(time, template) {
802
+ return new DateTime(time).format(template);
803
+ }
804
+ /**
805
+ * Get the difference in days from today.
806
+ *
807
+ * @param time
808
+ * @param startHour
809
+ * @param startMinute
810
+ * @param endHour
811
+ * @param endMinute
812
+ * @returns
813
+ */
814
+ static randomTime(time, startHour, startMinute, endHour, endMinute) {
815
+ return new DateTime(time).randomTime(startHour, startMinute, endHour, endMinute);
816
+ }
817
+ /**
818
+ * Get the first day of the month of the given date
819
+ *
820
+ * @param time
821
+ *
822
+ * @returns
823
+ */
824
+ static firstDayOfMonth(time) {
825
+ return new DateTime(time).firstDayOfMonth();
826
+ }
827
+ /**
828
+ * Get the last day of the month of the given date
829
+ *
830
+ * @param time
831
+ *
832
+ * @returns
833
+ */
834
+ static lastDayOfMonth(time) {
835
+ return new DateTime(time).lastDayOfMonth();
836
+ }
837
+ };
838
+
670
839
  //#endregion
671
840
  //#region src/Helpers/Str.ts
672
841
  let Mode = /* @__PURE__ */ function(Mode$1) {
@@ -5006,164 +5175,6 @@ function matchCase(value, comparison) {
5006
5175
  return value;
5007
5176
  }
5008
5177
 
5009
- //#endregion
5010
- //#region src/Helpers/Time.ts
5011
- dayjs.extend(utc);
5012
- dayjs.extend(timezone);
5013
- dayjs.extend(dayOfYear);
5014
- dayjs.extend(isBetween);
5015
- dayjs.extend(isLeapYear);
5016
- dayjs.extend(relativeTime);
5017
- dayjs.extend(advancedFormat);
5018
- dayjs.extend(customParseFormat);
5019
- 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");
5020
- function format(date, fmt) {
5021
- return dayjs(date).format(phpToDayjsTokens(fmt));
5022
- }
5023
- const TimeClass = class {};
5024
- var Time = class Time extends TimeClass {
5025
- instance;
5026
- constructor(config) {
5027
- super(config);
5028
- this.instance = dayjs(config);
5029
- return new Proxy(this, { get: (target, prop, receiver) => {
5030
- if (prop in target) return Reflect.get(target, prop, receiver);
5031
- const value = Reflect.get(this.instance, prop, receiver);
5032
- if (typeof value === "function") return (...args) => {
5033
- const result = value.apply(this.instance, args);
5034
- return dayjs.isDayjs(result) ? new Time(result) : result;
5035
- };
5036
- return value;
5037
- } });
5038
- }
5039
- /**
5040
- * Start time of a specific unit.
5041
- *
5042
- * @returns
5043
- */
5044
- start(unit = "days") {
5045
- return this.startOf(unit);
5046
- }
5047
- /**
5048
- * End time of a specific unit.
5049
- *
5050
- * @returns
5051
- */
5052
- end(unit = "days") {
5053
- return this.endOf(unit);
5054
- }
5055
- /**
5056
- * Get the first day of the month of the given date
5057
- *
5058
- * @returns
5059
- */
5060
- firstDayOfMonth() {
5061
- return new Time(new Date(Date.UTC(this.year(), this.month(), 1)));
5062
- }
5063
- carbonFormat(template) {
5064
- return template ? this.format(phpToDayjsTokens(template)) : this.format();
5065
- }
5066
- /**
5067
- * Get the last day of the month of the given date
5068
- *
5069
- * @returns
5070
- */
5071
- lastDayOfMonth() {
5072
- return new Time(new Date(Date.UTC(this.year(), this.month() + 1, 0)));
5073
- }
5074
- /**
5075
- * Get a random time between the specified hour and minute.
5076
- *
5077
- * @param startHour
5078
- * @param startMinute
5079
- * @param endHour
5080
- * @param endMinute
5081
- * @returns
5082
- */
5083
- randomTime(startHour = 9, startMinute = 0, endHour = 17, endMinute = 0) {
5084
- const today = /* @__PURE__ */ new Date();
5085
- const startMinutes = startHour * 60 + startMinute;
5086
- const endMinutes = endHour * 60 + endMinute;
5087
- const randomMinutes = Math.floor(Math.random() * (endMinutes - startMinutes)) + startMinutes;
5088
- const hour = Math.floor(randomMinutes / 60);
5089
- const minute = randomMinutes % 60;
5090
- const date = new Date(today);
5091
- date.setHours(hour, minute, 0, 0);
5092
- return new Time(date);
5093
- }
5094
- /**
5095
- * Create a date for a given timestamp.
5096
- *
5097
- * @param timestamp - Unix timestamp
5098
- *
5099
- * @return {Date} object
5100
- */
5101
- static fromTimestamp(timestamp) {
5102
- return /* @__PURE__ */ new Date(timestamp * 1e3);
5103
- }
5104
- /**
5105
- * Get current time instance.
5106
- *
5107
- * @returns Current time
5108
- */
5109
- static now() {
5110
- return new Time();
5111
- }
5112
- /**
5113
- * Parse the time
5114
- *
5115
- * @param date
5116
- * @returns
5117
- */
5118
- static parse(date) {
5119
- return new Time(date);
5120
- }
5121
- /**
5122
- * Get the formatted date according to the string of tokens passed in.
5123
- *
5124
- * To escape characters, wrap them in square brackets (e.g. [MM]).
5125
- *
5126
- * @param time - current time
5127
- * @param template - time format
5128
- */
5129
- static format(time, template) {
5130
- return new Time(time).format(template);
5131
- }
5132
- /**
5133
- * Get the difference in days from today.
5134
- *
5135
- * @param time
5136
- * @param startHour
5137
- * @param startMinute
5138
- * @param endHour
5139
- * @param endMinute
5140
- * @returns
5141
- */
5142
- static randomTime(time, startHour, startMinute, endHour, endMinute) {
5143
- return new Time(time).randomTime(startHour, startMinute, endHour, endMinute);
5144
- }
5145
- /**
5146
- * Get the first day of the month of the given date
5147
- *
5148
- * @param time
5149
- *
5150
- * @returns
5151
- */
5152
- static firstDayOfMonth(time) {
5153
- return new Time(time).firstDayOfMonth();
5154
- }
5155
- /**
5156
- * Get the last day of the month of the given date
5157
- *
5158
- * @param time
5159
- *
5160
- * @returns
5161
- */
5162
- static lastDayOfMonth(time) {
5163
- return new Time(time).lastDayOfMonth();
5164
- }
5165
- };
5166
-
5167
5178
  //#endregion
5168
5179
  //#region src/GlobalBootstrap.ts
5169
5180
  /**
@@ -5195,7 +5206,7 @@ function loadHelpers(target = globalThis) {
5195
5206
  Number: Number_exports,
5196
5207
  Obj: Obj_exports,
5197
5208
  Str,
5198
- Time,
5209
+ DateTime,
5199
5210
  DumpDie: DumpDie_exports,
5200
5211
  apa: Str.apa,
5201
5212
  title: Str.title,
@@ -5241,13 +5252,13 @@ function loadHelpers(target = globalThis) {
5241
5252
  checksum,
5242
5253
  verifyChecksum,
5243
5254
  caesarCipher,
5244
- now: Time.now,
5245
- format: Time.format,
5246
- fromTimestamp: Time.fromTimestamp,
5247
- parse: Time.parse,
5248
- randomTime: Time.randomTime,
5249
- firstDayOfMonth: Time.firstDayOfMonth,
5250
- lastDayOfMonth: Time.lastDayOfMonth,
5255
+ now: DateTime.now,
5256
+ format: DateTime.format,
5257
+ fromTimestamp: DateTime.fromTimestamp,
5258
+ parse: DateTime.parse,
5259
+ randomTime: DateTime.randomTime,
5260
+ firstDayOfMonth: DateTime.firstDayOfMonth,
5261
+ lastDayOfMonth: DateTime.lastDayOfMonth,
5251
5262
  abbreviate,
5252
5263
  humanize,
5253
5264
  toBytes,
@@ -5345,5 +5356,5 @@ function cleanHelpers(target = globalThis) {
5345
5356
  }
5346
5357
 
5347
5358
  //#endregion
5348
- export { Arr_exports as Arr, Crypto_exports as Crypto, DumpDie_exports as DumpDie, HtmlString, Mode, Number_exports as Number, Obj_exports as Obj, Str, Stringable, Time, abbreviate, alternate, base64Decode, base64Encode, caesarCipher, checksum, chunk, cleanHelpers, collapse, combine, dd, dot, dump, extractProperties, find, first, flatten, forget, format, getValue, hash, hmac, humanize, isEmpty, isNotEmpty, last, loadHelpers, modObj, pop, prepend, random, randomColor, randomPassword, randomSecure, range, reverse, safeDot, secureToken, setNested, shift, slugifyKeys, str, take, toBytes, toHumanTime, uuid, verifyChecksum, xor };
5359
+ export { Arr_exports as Arr, Crypto_exports as Crypto, DateTime, DumpDie_exports as DumpDie, HtmlString, Mode, Number_exports as Number, Obj_exports as Obj, Str, Stringable, abbreviate, alternate, base64Decode, base64Encode, caesarCipher, checksum, chunk, cleanHelpers, collapse, combine, dd, dot, dump, extractProperties, find, first, flatten, forget, format, getValue, hash, hmac, humanize, isEmpty, isNotEmpty, last, loadHelpers, modObj, pop, prepend, random, randomColor, randomPassword, randomSecure, range, reverse, safeDot, secureToken, setNested, shift, slugifyKeys, str, take, toBytes, toHumanTime, uuid, verifyChecksum, wrap, xor };
5349
5360
  //# sourceMappingURL=index.js.map