@heyform-inc/utils 1.3.0 → 1.3.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.mjs CHANGED
@@ -541,6 +541,217 @@ function datePeriod(start, value = 1, unit = "month") {
541
541
  return unixDate(start).add(value, unit).unix();
542
542
  }
543
543
 
544
+ // src/error.ts
545
+ var HttpStatus = /* @__PURE__ */ ((HttpStatus2) => {
546
+ HttpStatus2[HttpStatus2["CONTINUE"] = 100] = "CONTINUE";
547
+ HttpStatus2[HttpStatus2["SWITCHING_PROTOCOLS"] = 101] = "SWITCHING_PROTOCOLS";
548
+ HttpStatus2[HttpStatus2["PROCESSING"] = 102] = "PROCESSING";
549
+ HttpStatus2[HttpStatus2["EARLYHINTS"] = 103] = "EARLYHINTS";
550
+ HttpStatus2[HttpStatus2["OK"] = 200] = "OK";
551
+ HttpStatus2[HttpStatus2["CREATED"] = 201] = "CREATED";
552
+ HttpStatus2[HttpStatus2["ACCEPTED"] = 202] = "ACCEPTED";
553
+ HttpStatus2[HttpStatus2["NON_AUTHORITATIVE_INFORMATION"] = 203] = "NON_AUTHORITATIVE_INFORMATION";
554
+ HttpStatus2[HttpStatus2["NO_CONTENT"] = 204] = "NO_CONTENT";
555
+ HttpStatus2[HttpStatus2["RESET_CONTENT"] = 205] = "RESET_CONTENT";
556
+ HttpStatus2[HttpStatus2["PARTIAL_CONTENT"] = 206] = "PARTIAL_CONTENT";
557
+ HttpStatus2[HttpStatus2["MULTI_STATUS"] = 207] = "MULTI_STATUS";
558
+ HttpStatus2[HttpStatus2["ALREADY_REPORTED"] = 208] = "ALREADY_REPORTED";
559
+ HttpStatus2[HttpStatus2["CONTENT_DIFFERENT"] = 210] = "CONTENT_DIFFERENT";
560
+ HttpStatus2[HttpStatus2["AMBIGUOUS"] = 300] = "AMBIGUOUS";
561
+ HttpStatus2[HttpStatus2["MOVED_PERMANENTLY"] = 301] = "MOVED_PERMANENTLY";
562
+ HttpStatus2[HttpStatus2["FOUND"] = 302] = "FOUND";
563
+ HttpStatus2[HttpStatus2["SEE_OTHER"] = 303] = "SEE_OTHER";
564
+ HttpStatus2[HttpStatus2["NOT_MODIFIED"] = 304] = "NOT_MODIFIED";
565
+ HttpStatus2[HttpStatus2["TEMPORARY_REDIRECT"] = 307] = "TEMPORARY_REDIRECT";
566
+ HttpStatus2[HttpStatus2["PERMANENT_REDIRECT"] = 308] = "PERMANENT_REDIRECT";
567
+ HttpStatus2[HttpStatus2["BAD_REQUEST"] = 400] = "BAD_REQUEST";
568
+ HttpStatus2[HttpStatus2["UNAUTHORIZED"] = 401] = "UNAUTHORIZED";
569
+ HttpStatus2[HttpStatus2["PAYMENT_REQUIRED"] = 402] = "PAYMENT_REQUIRED";
570
+ HttpStatus2[HttpStatus2["FORBIDDEN"] = 403] = "FORBIDDEN";
571
+ HttpStatus2[HttpStatus2["NOT_FOUND"] = 404] = "NOT_FOUND";
572
+ HttpStatus2[HttpStatus2["METHOD_NOT_ALLOWED"] = 405] = "METHOD_NOT_ALLOWED";
573
+ HttpStatus2[HttpStatus2["NOT_ACCEPTABLE"] = 406] = "NOT_ACCEPTABLE";
574
+ HttpStatus2[HttpStatus2["PROXY_AUTHENTICATION_REQUIRED"] = 407] = "PROXY_AUTHENTICATION_REQUIRED";
575
+ HttpStatus2[HttpStatus2["REQUEST_TIMEOUT"] = 408] = "REQUEST_TIMEOUT";
576
+ HttpStatus2[HttpStatus2["CONFLICT"] = 409] = "CONFLICT";
577
+ HttpStatus2[HttpStatus2["GONE"] = 410] = "GONE";
578
+ HttpStatus2[HttpStatus2["LENGTH_REQUIRED"] = 411] = "LENGTH_REQUIRED";
579
+ HttpStatus2[HttpStatus2["PRECONDITION_FAILED"] = 412] = "PRECONDITION_FAILED";
580
+ HttpStatus2[HttpStatus2["PAYLOAD_TOO_LARGE"] = 413] = "PAYLOAD_TOO_LARGE";
581
+ HttpStatus2[HttpStatus2["URI_TOO_LONG"] = 414] = "URI_TOO_LONG";
582
+ HttpStatus2[HttpStatus2["UNSUPPORTED_MEDIA_TYPE"] = 415] = "UNSUPPORTED_MEDIA_TYPE";
583
+ HttpStatus2[HttpStatus2["REQUESTED_RANGE_NOT_SATISFIABLE"] = 416] = "REQUESTED_RANGE_NOT_SATISFIABLE";
584
+ HttpStatus2[HttpStatus2["EXPECTATION_FAILED"] = 417] = "EXPECTATION_FAILED";
585
+ HttpStatus2[HttpStatus2["I_AM_A_TEAPOT"] = 418] = "I_AM_A_TEAPOT";
586
+ HttpStatus2[HttpStatus2["MISDIRECTED"] = 421] = "MISDIRECTED";
587
+ HttpStatus2[HttpStatus2["UNPROCESSABLE_ENTITY"] = 422] = "UNPROCESSABLE_ENTITY";
588
+ HttpStatus2[HttpStatus2["LOCKED"] = 423] = "LOCKED";
589
+ HttpStatus2[HttpStatus2["FAILED_DEPENDENCY"] = 424] = "FAILED_DEPENDENCY";
590
+ HttpStatus2[HttpStatus2["PRECONDITION_REQUIRED"] = 428] = "PRECONDITION_REQUIRED";
591
+ HttpStatus2[HttpStatus2["TOO_MANY_REQUESTS"] = 429] = "TOO_MANY_REQUESTS";
592
+ HttpStatus2[HttpStatus2["UNRECOVERABLE_ERROR"] = 456] = "UNRECOVERABLE_ERROR";
593
+ HttpStatus2[HttpStatus2["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
594
+ HttpStatus2[HttpStatus2["NOT_IMPLEMENTED"] = 501] = "NOT_IMPLEMENTED";
595
+ HttpStatus2[HttpStatus2["BAD_GATEWAY"] = 502] = "BAD_GATEWAY";
596
+ HttpStatus2[HttpStatus2["SERVICE_UNAVAILABLE"] = 503] = "SERVICE_UNAVAILABLE";
597
+ HttpStatus2[HttpStatus2["GATEWAY_TIMEOUT"] = 504] = "GATEWAY_TIMEOUT";
598
+ HttpStatus2[HttpStatus2["HTTP_VERSION_NOT_SUPPORTED"] = 505] = "HTTP_VERSION_NOT_SUPPORTED";
599
+ HttpStatus2[HttpStatus2["INSUFFICIENT_STORAGE"] = 507] = "INSUFFICIENT_STORAGE";
600
+ HttpStatus2[HttpStatus2["LOOP_DETECTED"] = 508] = "LOOP_DETECTED";
601
+ return HttpStatus2;
602
+ })(HttpStatus || {});
603
+ var HttpError = class extends Error {
604
+ constructor(message, { status, code, errors }) {
605
+ super(message);
606
+ this.name = "HttpError";
607
+ Object.defineProperty(this, "status", {
608
+ value: status ?? 500 /* INTERNAL_SERVER_ERROR */,
609
+ writable: false,
610
+ enumerable: true,
611
+ configurable: false
612
+ });
613
+ Object.defineProperty(this, "code", {
614
+ value: code || "HTTP_ERROR",
615
+ writable: false,
616
+ enumerable: true,
617
+ configurable: false
618
+ });
619
+ Object.defineProperty(this, "errors", {
620
+ value: errors,
621
+ writable: false,
622
+ enumerable: true,
623
+ configurable: false
624
+ });
625
+ }
626
+ getStatus() {
627
+ return this.status;
628
+ }
629
+ getCode() {
630
+ return this.code;
631
+ }
632
+ getErrors() {
633
+ return this.errors;
634
+ }
635
+ toJSON() {
636
+ return {
637
+ status: this.status,
638
+ code: this.code,
639
+ message: this.message,
640
+ errors: this.errors
641
+ };
642
+ }
643
+ };
644
+ var BadGatewayError = class extends HttpError {
645
+ constructor(message = "Bad Gateway", code = "BAD_GATEWAY", errors) {
646
+ super(message, { status: 502 /* BAD_GATEWAY */, code, errors });
647
+ }
648
+ };
649
+ var BadRequestError = class extends HttpError {
650
+ constructor(message = "Bad Request", code = "BAD_REQUEST", errors) {
651
+ super(message, { status: 400 /* BAD_REQUEST */, code, errors });
652
+ }
653
+ };
654
+ var ConflictError = class extends HttpError {
655
+ constructor(message = "Conflict", code = "CONFLICT", errors) {
656
+ super(message, { status: 409 /* CONFLICT */, code, errors });
657
+ }
658
+ };
659
+ var ForbiddenError = class extends HttpError {
660
+ constructor(message = "Forbidden", code = "FORBIDDEN", errors) {
661
+ super(message, { status: 403 /* FORBIDDEN */, code, errors });
662
+ }
663
+ };
664
+ var TooManyRequestsError = class extends HttpError {
665
+ constructor(message = "Too Many Requests", code = "TOO_MANY_REQUESTS", errors) {
666
+ super(message, { status: 429 /* TOO_MANY_REQUESTS */, code, errors });
667
+ }
668
+ };
669
+ var GatewayTimeoutError = class extends HttpError {
670
+ constructor(message = "Gateway Timeout", code = "GATEWAY_TIMEOUT", errors) {
671
+ super(message, { status: 504 /* GATEWAY_TIMEOUT */, code, errors });
672
+ }
673
+ };
674
+ var GoneError = class extends HttpError {
675
+ constructor(message = "Gone", code = "GONE", errors) {
676
+ super(message, { status: 410 /* GONE */, code, errors });
677
+ }
678
+ };
679
+ var HttpVersionNotSupportedError = class extends HttpError {
680
+ constructor(message = "HTTP Version Not Supported", code = "HTTP_VERSION_NOT_SUPPORTED", errors) {
681
+ super(message, { status: 505 /* HTTP_VERSION_NOT_SUPPORTED */, code, errors });
682
+ }
683
+ };
684
+ var ImATeapotError = class extends HttpError {
685
+ constructor(message = "I'm a teapot", code = "I_AM_A_TEAPOT", errors) {
686
+ super(message, { status: 418 /* I_AM_A_TEAPOT */, code, errors });
687
+ }
688
+ };
689
+ var InternalServerError = class extends HttpError {
690
+ constructor(message = "Internal Server Error", code = "INTERNAL_SERVER_ERROR", errors) {
691
+ super(message, { status: 500 /* INTERNAL_SERVER_ERROR */, code, errors });
692
+ }
693
+ };
694
+ var MethodNotAllowedError = class extends HttpError {
695
+ constructor(message = "Method Not Allowed", code = "METHOD_NOT_ALLOWED", errors) {
696
+ super(message, { status: 405 /* METHOD_NOT_ALLOWED */, code, errors });
697
+ }
698
+ };
699
+ var MisdirectedError = class extends HttpError {
700
+ constructor(message = "Misdirected Request", code = "MISDIRECTED", errors) {
701
+ super(message, { status: 421 /* MISDIRECTED */, code, errors });
702
+ }
703
+ };
704
+ var NotAcceptableError = class extends HttpError {
705
+ constructor(message = "Not Acceptable", code = "NOT_ACCEPTABLE", errors) {
706
+ super(message, { status: 406 /* NOT_ACCEPTABLE */, code, errors });
707
+ }
708
+ };
709
+ var NotFoundError = class extends HttpError {
710
+ constructor(message = "Not Found", code = "NOT_FOUND", errors) {
711
+ super(message, { status: 404 /* NOT_FOUND */, code, errors });
712
+ }
713
+ };
714
+ var NotImplementedError = class extends HttpError {
715
+ constructor(message = "Not Implemented", code = "NOT_IMPLEMENTED", errors) {
716
+ super(message, { status: 501 /* NOT_IMPLEMENTED */, code, errors });
717
+ }
718
+ };
719
+ var PayloadTooLargeError = class extends HttpError {
720
+ constructor(message = "Payload Too Large", code = "PAYLOAD_TOO_LARGE", errors) {
721
+ super(message, { status: 413 /* PAYLOAD_TOO_LARGE */, code, errors });
722
+ }
723
+ };
724
+ var PreconditionFailedError = class extends HttpError {
725
+ constructor(message = "Precondition Failed", code = "PRECONDITION_FAILED", errors) {
726
+ super(message, { status: 412 /* PRECONDITION_FAILED */, code, errors });
727
+ }
728
+ };
729
+ var RequestTimeoutError = class extends HttpError {
730
+ constructor(message = "Request Timeout", code = "REQUEST_TIMEOUT", errors) {
731
+ super(message, { status: 408 /* REQUEST_TIMEOUT */, code, errors });
732
+ }
733
+ };
734
+ var ServiceUnavailableError = class extends HttpError {
735
+ constructor(message = "Service Unavailable", code = "SERVICE_UNAVAILABLE", errors) {
736
+ super(message, { status: 503 /* SERVICE_UNAVAILABLE */, code, errors });
737
+ }
738
+ };
739
+ var UnauthorizedError = class extends HttpError {
740
+ constructor(message = "Unauthorized", code = "UNAUTHORIZED", errors) {
741
+ super(message, { status: 401 /* UNAUTHORIZED */, code, errors });
742
+ }
743
+ };
744
+ var UnprocessableEntityError = class extends HttpError {
745
+ constructor(message = "Unprocessable Entity", code = "UNPROCESSABLE_ENTITY", errors) {
746
+ super(message, { status: 422 /* UNPROCESSABLE_ENTITY */, code, errors });
747
+ }
748
+ };
749
+ var UnsupportedMediaTypeError = class extends HttpError {
750
+ constructor(message = "Unsupported Media Type", code = "UNSUPPORTED_MEDIA_TYPE", errors) {
751
+ super(message, { status: 415 /* UNSUPPORTED_MEDIA_TYPE */, code, errors });
752
+ }
753
+ };
754
+
544
755
  // src/mime-db.json
545
756
  var mime_db_default = {
546
757
  "123": "application/vnd.lotus-1-2-3",
@@ -1812,15 +2023,23 @@ function excludeObject(target, excludes = []) {
1812
2023
  return pickObject(target, [], excludes);
1813
2024
  }
1814
2025
  function pickValidValues(target, fields) {
2026
+ if (!isObject(target) || !isArray(fields)) {
2027
+ return {};
2028
+ }
1815
2029
  const dist = {};
1816
2030
  fields.forEach((field) => {
1817
2031
  let key = String(field);
1818
2032
  let alias;
1819
2033
  if (isArray(field)) {
2034
+ if (field.length === 0 || isNil(field[0])) {
2035
+ return;
2036
+ }
1820
2037
  key = field[0];
1821
- if (field.length > 1) {
1822
- alias = field[1];
2038
+ if (field.length > 1 && !isNil(field[1])) {
2039
+ alias = String(field[1]);
1823
2040
  }
2041
+ } else if (isNil(field)) {
2042
+ return;
1824
2043
  }
1825
2044
  let value = target[key];
1826
2045
  if (isValid(value)) {
@@ -1848,16 +2067,22 @@ function removeObjectNil(target) {
1848
2067
  return newObj;
1849
2068
  }
1850
2069
  function copyObjectValues(target, dist, keyMaps) {
1851
- if (!isObject(target) || !isObject(dist)) {
2070
+ if (!isObject(target) || !isObject(dist) || !isArray(keyMaps)) {
1852
2071
  return;
1853
2072
  }
1854
2073
  for (const keys of keyMaps) {
1855
2074
  let targetKey;
1856
2075
  let distKey;
1857
2076
  if (isArray(keys)) {
1858
- targetKey = keys[0];
1859
- distKey = keys[1];
2077
+ if (keys.length === 0 || isNil(keys[0])) {
2078
+ continue;
2079
+ }
2080
+ targetKey = String(keys[0]);
2081
+ distKey = !isNil(keys[1]) ? String(keys[1]) : targetKey;
1860
2082
  } else {
2083
+ if (isNil(keys)) {
2084
+ continue;
2085
+ }
1861
2086
  targetKey = String(keys);
1862
2087
  distKey = targetKey;
1863
2088
  }
@@ -1872,52 +2097,29 @@ function copyObjectValues(target, dist, keyMaps) {
1872
2097
  }
1873
2098
 
1874
2099
  // src/random.ts
1875
- var numeric = "0123456789";
1876
- var hexic = "0123456789abcdef";
1877
- var lower = "abcdefghijklmnopqrstuvwxyz";
1878
- var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1879
- var length = 6;
1880
- var RandomType = /* @__PURE__ */ ((RandomType2) => {
1881
- RandomType2[RandomType2["LOWER"] = 0] = "LOWER";
1882
- RandomType2[RandomType2["UPPER"] = 1] = "UPPER";
1883
- RandomType2[RandomType2["NUMERIC"] = 2] = "NUMERIC";
1884
- RandomType2[RandomType2["HEXIC"] = 3] = "HEXIC";
1885
- RandomType2[RandomType2["LOWER_NUMERIC"] = 4] = "LOWER_NUMERIC";
1886
- RandomType2[RandomType2["UPPER_NUMERIC"] = 5] = "UPPER_NUMERIC";
1887
- RandomType2[RandomType2["ALPHANUMERIC"] = 6] = "ALPHANUMERIC";
1888
- return RandomType2;
1889
- })(RandomType || {});
1890
- function random(len = length * 2, type2 = 6 /* ALPHANUMERIC */) {
1891
- let alphabet = numeric + lower + upper;
1892
- switch (type2) {
1893
- case 0 /* LOWER */:
1894
- alphabet = lower;
1895
- break;
1896
- case 1 /* UPPER */:
1897
- alphabet = upper;
1898
- break;
1899
- case 3 /* HEXIC */:
1900
- alphabet = hexic;
1901
- break;
1902
- case 2 /* NUMERIC */:
1903
- alphabet = numeric;
1904
- break;
1905
- case 4 /* LOWER_NUMERIC */:
1906
- alphabet = lower + numeric;
1907
- break;
1908
- case 5 /* UPPER_NUMERIC */:
1909
- alphabet = upper + numeric;
1910
- break;
1911
- case 6 /* ALPHANUMERIC */:
1912
- alphabet = lower + upper + numeric;
1913
- break;
1914
- }
1915
- let str = "";
1916
- const alphabetLength = alphabet.length;
2100
+ var NUMERIC = "0123456789";
2101
+ var HEXIC = "0123456789abcdef";
2102
+ var ALPHA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
2103
+ var ALPHANUMERIC = ALPHA + NUMERIC;
2104
+ function random(len = 6, alphabet = ALPHANUMERIC) {
2105
+ let result = "";
2106
+ const length = alphabet.length;
1917
2107
  for (let i = 0; i < len; i++) {
1918
- str += alphabet.charAt(Math.floor(Math.random() * alphabetLength));
2108
+ result += alphabet.charAt(Math.floor(Math.random() * length));
1919
2109
  }
1920
- return str;
2110
+ return result;
2111
+ }
2112
+ function randomHexic(len) {
2113
+ return random(len, HEXIC);
2114
+ }
2115
+ function randomAlpha(len) {
2116
+ return random(len, ALPHA);
2117
+ }
2118
+ function randomNumeric(len) {
2119
+ return random(len, NUMERIC);
2120
+ }
2121
+ function randomNumber(min, max) {
2122
+ return Math.ceil(Math.random() * (max - min) + min);
1921
2123
  }
1922
2124
  function slugify(text, options) {
1923
2125
  return slug(text, {
@@ -1927,6 +2129,6 @@ function slugify(text, options) {
1927
2129
  });
1928
2130
  }
1929
2131
 
1930
- export { FILE_MIME_TYPES, IMAGE_MIME_TYPES, RandomType, alpha, alphaHexToRgb, bytes, clone, clsx, colorBrightness, commonFileMimeTypes, commonImageMimeTypes, copyObjectValues, darken, date, datePeriod, dayjs, excludeObject, formatBytes, helper_default as helper, hexToRgb, hs, htmlToText, invert, isDarkColor, isDateExpired, isHexColor, isLightColor, isRgb, isRgba, lighten, mime, ms, nanoid, nanoidCustomAlphabet, parseBool, parseBytes, parseJson, parseNumber, pickObject, pickValidValues, random, removeObjectNil, rgbToHex, slugify, timestamp, toBool, toDuration, toFixed, toFloat, toInt, toInteger, toIntlNumber, toJSON, toMillisecond, toSecond, toURLParams, toURLQuery, type, unixDate, unixDiff };
2132
+ export { BadGatewayError, BadRequestError, ConflictError, FILE_MIME_TYPES, ForbiddenError, GatewayTimeoutError, GoneError, HttpError, HttpStatus, HttpVersionNotSupportedError, IMAGE_MIME_TYPES, ImATeapotError, InternalServerError, MethodNotAllowedError, MisdirectedError, NotAcceptableError, NotFoundError, NotImplementedError, PayloadTooLargeError, PreconditionFailedError, RequestTimeoutError, ServiceUnavailableError, TooManyRequestsError, UnauthorizedError, UnprocessableEntityError, UnsupportedMediaTypeError, alpha, alphaHexToRgb, bytes, clone, clsx, colorBrightness, commonFileMimeTypes, commonImageMimeTypes, copyObjectValues, darken, date, datePeriod, dayjs, excludeObject, formatBytes, helper_default as helper, hexToRgb, hs, htmlToText, invert, isDarkColor, isDateExpired, isHexColor, isLightColor, isRgb, isRgba, lighten, mime, ms, nanoid, nanoidCustomAlphabet, parseBool, parseBytes, parseJson, parseNumber, pickObject, pickValidValues, random, randomAlpha, randomHexic, randomNumber, randomNumeric, removeObjectNil, rgbToHex, slugify, timestamp, toBool, toDuration, toFixed, toFloat, toInt, toInteger, toIntlNumber, toJSON, toMillisecond, toSecond, toURLParams, toURLQuery, type, unixDate, unixDiff };
1931
2133
  //# sourceMappingURL=index.mjs.map
1932
2134
  //# sourceMappingURL=index.mjs.map