@adviser/cement 0.4.3 → 0.4.4

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/index.cjs CHANGED
@@ -722,6 +722,15 @@ function coerceKey(key, def) {
722
722
  }
723
723
  return { key, def };
724
724
  }
725
+ function resolveHash(hash) {
726
+ const searchParams = new URLSearchParams(hash.replace(/^#/, ""));
727
+ return {
728
+ getParam: (k) => {
729
+ const ret = searchParams.get(k);
730
+ return ret === null ? void 0 : ret;
731
+ }
732
+ };
733
+ }
725
734
  function falsy2undef(value) {
726
735
  return value === void 0 || value === null ? void 0 : value;
727
736
  }
@@ -744,6 +753,7 @@ function isURL(value) {
744
753
  }
745
754
  var customInspectSymbol = Symbol.for("nodejs.util.inspect.custom");
746
755
  var MutableURL = class _MutableURL extends URL {
756
+ // override readonly hash: string;
747
757
  constructor(urlStr) {
748
758
  super("defect://does.not.exist");
749
759
  const partedURL = urlStr.split(":");
@@ -766,7 +776,6 @@ var MutableURL = class _MutableURL extends URL {
766
776
  } else {
767
777
  this._pathname = urlStr.replace(new RegExp(`^${this._protocol}//`), "").replace(/[#?].*$/, "");
768
778
  }
769
- this.hash = this._sysURL.hash;
770
779
  }
771
780
  [customInspectSymbol]() {
772
781
  return this.toString();
@@ -821,16 +830,26 @@ var MutableURL = class _MutableURL extends URL {
821
830
  }
822
831
  this._protocol = p;
823
832
  }
833
+ get hash() {
834
+ return this._sysURL.hash;
835
+ }
836
+ set hash(h) {
837
+ this._sysURL.hash = h;
838
+ }
824
839
  get searchParams() {
825
840
  return this._sysURL.searchParams;
826
841
  }
827
- toString() {
842
+ get search() {
828
843
  let search = "";
829
844
  if (this._sysURL.searchParams.size) {
830
845
  for (const [key, value] of Array.from(this._sysURL.searchParams.entries()).sort((a, b) => a[0].localeCompare(b[0]))) {
831
846
  search += `${!search.length ? "?" : "&"}${key}=${encodeURIComponent(value)}`;
832
847
  }
833
848
  }
849
+ return search;
850
+ }
851
+ toString() {
852
+ const search = this.search;
834
853
  let hostpart = "";
835
854
  if (this._hasHostpart) {
836
855
  hostpart = this._sysURL.hostname;
@@ -841,7 +860,7 @@ var MutableURL = class _MutableURL extends URL {
841
860
  hostpart += "/";
842
861
  }
843
862
  }
844
- return `${this._protocol}//${hostpart}${this._pathname}${search}`;
863
+ return `${this._protocol}//${hostpart}${this._pathname}${search}${this.hash}`;
845
864
  }
846
865
  };
847
866
  function from(fac, strURLUri, defaultProtocol) {
@@ -904,6 +923,10 @@ var BuildURI = class _BuildURI {
904
923
  this._url.pathname = p;
905
924
  return this;
906
925
  }
926
+ hash(h) {
927
+ this._url.hash = h;
928
+ return this;
929
+ }
907
930
  // could pass a relative path or a full URL
908
931
  // if relative path, it will be appended to the current path
909
932
  resolve(p) {
@@ -932,12 +955,51 @@ var BuildURI = class _BuildURI {
932
955
  }
933
956
  return this;
934
957
  }
935
- cleanParams() {
958
+ cleanParams(...remove) {
959
+ const keys = new Set(remove.flat());
936
960
  for (const key of Array.from(this._url.searchParams.keys())) {
937
- this._url.searchParams.delete(key);
961
+ if (keys.size === 0 || keys.has(key)) {
962
+ this._url.searchParams.delete(key);
963
+ }
938
964
  }
939
965
  return this;
940
966
  }
967
+ hashParams(val, mode = "reset") {
968
+ let preset;
969
+ switch (mode) {
970
+ case "reset":
971
+ this._url.hash = "";
972
+ preset = {};
973
+ break;
974
+ case "merge":
975
+ default:
976
+ preset = Object.fromEntries(new URLSearchParams(this._url.hash.replace(/^#/, "")).entries());
977
+ break;
978
+ }
979
+ const out = new URLSearchParams("");
980
+ for (const [key, value] of Object.entries({ ...preset, ...val }).sort((a, b) => a[0].localeCompare(b[0]))) {
981
+ switch (typeof value) {
982
+ case "string":
983
+ out.set(key, value);
984
+ break;
985
+ case "number":
986
+ out.set(key, value.toString());
987
+ break;
988
+ case "boolean":
989
+ out.set(key, value ? "true" : "false");
990
+ break;
991
+ default:
992
+ if (value instanceof Date) {
993
+ out.set(key, value.toISOString());
994
+ } else {
995
+ console.error(`unsupported type: ${typeof value} ignore key: ${key}`);
996
+ }
997
+ break;
998
+ }
999
+ }
1000
+ this._url.hash = out.toString();
1001
+ return this;
1002
+ }
941
1003
  delParam(key) {
942
1004
  this._url.searchParams.delete(key);
943
1005
  return this;
@@ -972,6 +1034,9 @@ var BuildURI = class _BuildURI {
972
1034
  getParamsResult(...keys) {
973
1035
  return getParamsResult(keys, this);
974
1036
  }
1037
+ getHashParams(...keys) {
1038
+ return getParamsResult(keys, resolveHash(this._url.hash));
1039
+ }
975
1040
  toString() {
976
1041
  this._url.searchParams.sort();
977
1042
  return this._url.toString();
@@ -1055,6 +1120,9 @@ var URI = class _URI {
1055
1120
  get hostname() {
1056
1121
  return this._url.hostname;
1057
1122
  }
1123
+ get local() {
1124
+ return this._url.pathname + this._url.search;
1125
+ }
1058
1126
  // get password(): string {
1059
1127
  // return this._url.password;
1060
1128
  // }
@@ -1076,9 +1144,9 @@ var URI = class _URI {
1076
1144
  get pathname() {
1077
1145
  return this._url.pathname;
1078
1146
  }
1079
- // get hash(): string {
1080
- // return this._url.hash;
1081
- // }
1147
+ get hash() {
1148
+ return this._url.hash;
1149
+ }
1082
1150
  // get host(): string {
1083
1151
  // return this._url.host;
1084
1152
  // }
@@ -1102,6 +1170,9 @@ var URI = class _URI {
1102
1170
  getParamsResult(...keys) {
1103
1171
  return getParamsResult(keys, this);
1104
1172
  }
1173
+ getHashParams(...keys) {
1174
+ return getParamsResult(keys, resolveHash(this._url.hash));
1175
+ }
1105
1176
  clone() {
1106
1177
  return new _URI(this._url);
1107
1178
  }