@adviser/cement 0.3.19 → 0.3.20

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/src/uri.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { DeepWritable } from "ts-essentials";
1
2
  import { exception2Result, Result } from "./result.js";
2
3
  import { getParamsResult, KeysParam } from "./utils/get-params-result.js";
3
4
  import { relativePath } from "./utils/relative-path.js";
@@ -38,6 +39,7 @@ export interface URIInterface<R extends URIInterface<R>> {
38
39
  getParam<T extends string | undefined>(key: string | OneKey<string>, def?: T): T extends string ? string : string | undefined;
39
40
  getParamResult(key: string, msgFn?: (key: string) => string): Result<string>;
40
41
  getParamsResult(...keys: KeysParam): Result<Record<string, string>>;
42
+ match(other: CoerceURI): MatchResult;
41
43
  clone(): R;
42
44
  asURL(): URL;
43
45
  toString(): string;
@@ -45,6 +47,67 @@ export interface URIInterface<R extends URIInterface<R>> {
45
47
  asObj(...strips: StripCommand[]): Partial<HostURIObject | PathURIObject>;
46
48
  }
47
49
 
50
+ export interface MatchResult {
51
+ readonly score: number;
52
+ readonly protocol: boolean;
53
+ readonly hostname: boolean;
54
+ readonly port: boolean;
55
+ readonly pathname: boolean;
56
+ readonly pathParts: string[];
57
+ readonly params: Record<string, string>;
58
+ }
59
+
60
+ function match(iref: CoerceURI, ioth: CoerceURI): MatchResult {
61
+ const mr: DeepWritable<MatchResult> = {
62
+ score: 0,
63
+ protocol: false,
64
+ hostname: false,
65
+ port: false,
66
+ pathname: false,
67
+ pathParts: [],
68
+ params: {},
69
+ };
70
+ const ref = URI.from(iref);
71
+ const oth = URI.from(ioth);
72
+ if (ref.protocol === oth.protocol) {
73
+ mr.score += 1;
74
+ mr.protocol = true;
75
+ }
76
+ try {
77
+ const refH = ref.hostname;
78
+ const refP = ref.port;
79
+ if (refH === oth.hostname) {
80
+ mr.score += 1;
81
+ mr.hostname = true;
82
+ }
83
+ if (refP.length && refP === oth.port) {
84
+ mr.score += 1;
85
+ mr.port = true;
86
+ }
87
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
88
+ } catch (e) {
89
+ // ignore
90
+ }
91
+ if (ref.pathname.length && ref.pathname !== "/") {
92
+ const pref = ref.pathname.split("/").filter((p) => p.length);
93
+ const poth = oth.pathname.split("/").filter((p) => p.length);
94
+ for (let i = 0; i < pref.length && i < poth.length; i++) {
95
+ if (poth[i] === pref[i]) {
96
+ mr.score += 1;
97
+ mr.pathname = true;
98
+ mr.pathParts.push(pref[i]);
99
+ }
100
+ }
101
+ }
102
+ for (const [key, value] of ref.getParams) {
103
+ if (oth.getParam(key) === value) {
104
+ mr.score += 1;
105
+ mr.params[key] = value;
106
+ }
107
+ }
108
+ return mr;
109
+ }
110
+
48
111
  function coerceKey(key: string | OneKey<string>, def?: string): { key: string; def?: string } {
49
112
  if (typeof key === "object") {
50
113
  const keys = Object.keys(key);
@@ -274,6 +337,10 @@ export class BuildURI implements URIInterface<BuildURI> {
274
337
  return from((url) => new BuildURI(url), strURLUri, defaultProtocol);
275
338
  }
276
339
 
340
+ match(other: CoerceURI): MatchResult {
341
+ return match(this.URI(), URI.from(other));
342
+ }
343
+
277
344
  port(p: string): BuildURI {
278
345
  this._url.port = p;
279
346
  return this;
@@ -454,6 +521,10 @@ export class URI implements URIInterface<URI> {
454
521
  };
455
522
  }
456
523
 
524
+ match(other: CoerceURI): MatchResult {
525
+ return match(this, other);
526
+ }
527
+
457
528
  // if no protocol is provided, default to file:
458
529
  static merge(into: CoerceURI, from: CoerceURI, defaultProtocol = "file:"): URI {
459
530
  const intoUrl = BuildURI.from(into, defaultProtocol);
package/test/index.cjs CHANGED
@@ -1784,6 +1784,54 @@ function localStripper(path, restrips, obj) {
1784
1784
  }
1785
1785
 
1786
1786
  // src/uri.ts
1787
+ function match(iref, ioth) {
1788
+ const mr = {
1789
+ score: 0,
1790
+ protocol: false,
1791
+ hostname: false,
1792
+ port: false,
1793
+ pathname: false,
1794
+ pathParts: [],
1795
+ params: {}
1796
+ };
1797
+ const ref = URI.from(iref);
1798
+ const oth = URI.from(ioth);
1799
+ if (ref.protocol === oth.protocol) {
1800
+ mr.score += 1;
1801
+ mr.protocol = true;
1802
+ }
1803
+ try {
1804
+ const refH = ref.hostname;
1805
+ const refP = ref.port;
1806
+ if (refH === oth.hostname) {
1807
+ mr.score += 1;
1808
+ mr.hostname = true;
1809
+ }
1810
+ if (refP.length && refP === oth.port) {
1811
+ mr.score += 1;
1812
+ mr.port = true;
1813
+ }
1814
+ } catch (e) {
1815
+ }
1816
+ if (ref.pathname.length && ref.pathname !== "/") {
1817
+ const pref = ref.pathname.split("/").filter((p) => p.length);
1818
+ const poth = oth.pathname.split("/").filter((p) => p.length);
1819
+ for (let i = 0; i < pref.length && i < poth.length; i++) {
1820
+ if (poth[i] === pref[i]) {
1821
+ mr.score += 1;
1822
+ mr.pathname = true;
1823
+ mr.pathParts.push(pref[i]);
1824
+ }
1825
+ }
1826
+ }
1827
+ for (const [key, value] of ref.getParams) {
1828
+ if (oth.getParam(key) === value) {
1829
+ mr.score += 1;
1830
+ mr.params[key] = value;
1831
+ }
1832
+ }
1833
+ return mr;
1834
+ }
1787
1835
  function coerceKey(key, def) {
1788
1836
  if (typeof key === "object") {
1789
1837
  const keys = Object.keys(key);
@@ -1950,6 +1998,9 @@ var BuildURI = class _BuildURI {
1950
1998
  static from(strURLUri, defaultProtocol = "file:") {
1951
1999
  return from((url) => new _BuildURI(url), strURLUri, defaultProtocol);
1952
2000
  }
2001
+ match(other) {
2002
+ return match(this.URI(), URI.from(other));
2003
+ }
1953
2004
  port(p) {
1954
2005
  this._url.port = p;
1955
2006
  return this;
@@ -2066,6 +2117,9 @@ var URI = class _URI {
2066
2117
  hasHostPartProtocols.delete(protocol);
2067
2118
  };
2068
2119
  }
2120
+ match(other) {
2121
+ return match(this, other);
2122
+ }
2069
2123
  // if no protocol is provided, default to file:
2070
2124
  static merge(into, from2, defaultProtocol = "file:") {
2071
2125
  const intoUrl = BuildURI.from(into, defaultProtocol);