@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/src/uri.ts CHANGED
@@ -119,6 +119,16 @@ function coerceKey(key: string | OneKey<string>, def?: string): { key: string; d
119
119
  return { key, def: def };
120
120
  }
121
121
 
122
+ function resolveHash(hash: string): { getParam: (k: string) => string | undefined } {
123
+ const searchParams = new URLSearchParams(hash.replace(/^#/, ""));
124
+ return {
125
+ getParam: (k): string | undefined => {
126
+ const ret = searchParams.get(k);
127
+ return ret === null ? undefined : ret;
128
+ },
129
+ };
130
+ }
131
+
122
132
  export interface URIObject {
123
133
  readonly style: "host" | "path";
124
134
  readonly protocol: string;
@@ -177,7 +187,7 @@ export class MutableURL extends URL {
177
187
  private _pathname: string;
178
188
  private _hasHostpart: boolean;
179
189
 
180
- override readonly hash: string;
190
+ // override readonly hash: string;
181
191
 
182
192
  constructor(urlStr: string) {
183
193
  super("defect://does.not.exist");
@@ -201,7 +211,7 @@ export class MutableURL extends URL {
201
211
  } else {
202
212
  this._pathname = urlStr.replace(new RegExp(`^${this._protocol}//`), "").replace(/[#?].*$/, "");
203
213
  }
204
- this.hash = this._sysURL.hash;
214
+ // this.hash = this._sysURL.hash;
205
215
  }
206
216
 
207
217
  [customInspectSymbol](): string {
@@ -269,17 +279,30 @@ export class MutableURL extends URL {
269
279
  this._protocol = p;
270
280
  }
271
281
 
282
+ override get hash(): string {
283
+ return this._sysURL.hash;
284
+ }
285
+
286
+ override set hash(h: string) {
287
+ this._sysURL.hash = h;
288
+ }
289
+
272
290
  override get searchParams(): URLSearchParams {
273
291
  return this._sysURL.searchParams;
274
292
  }
275
293
 
276
- override toString(): string {
294
+ override get search(): string {
277
295
  let search = "";
278
296
  if (this._sysURL.searchParams.size) {
279
297
  for (const [key, value] of Array.from(this._sysURL.searchParams.entries()).sort((a, b) => a[0].localeCompare(b[0]))) {
280
298
  search += `${!search.length ? "?" : "&"}${key}=${encodeURIComponent(value)}`;
281
299
  }
282
300
  }
301
+ return search;
302
+ }
303
+
304
+ override toString(): string {
305
+ const search = this.search;
283
306
  let hostpart = "";
284
307
  if (this._hasHostpart) {
285
308
  hostpart = this._sysURL.hostname;
@@ -290,7 +313,7 @@ export class MutableURL extends URL {
290
313
  hostpart += "/";
291
314
  }
292
315
  }
293
- return `${this._protocol}//${hostpart}${this._pathname}${search}`;
316
+ return `${this._protocol}//${hostpart}${this._pathname}${search}${this.hash}`;
294
317
  }
295
318
  }
296
319
 
@@ -370,6 +393,11 @@ export class BuildURI implements URIInterface<BuildURI> {
370
393
  return this;
371
394
  }
372
395
 
396
+ hash(h: string): BuildURI {
397
+ this._url.hash = h;
398
+ return this;
399
+ }
400
+
373
401
  // could pass a relative path or a full URL
374
402
  // if relative path, it will be appended to the current path
375
403
  resolve(p: CoerceURI): BuildURI {
@@ -419,13 +447,57 @@ export class BuildURI implements URIInterface<BuildURI> {
419
447
  return this;
420
448
  }
421
449
 
422
- cleanParams(): BuildURI {
450
+ cleanParams(...remove: (string | string[])[]): BuildURI {
451
+ const keys = new Set(remove.flat());
423
452
  for (const key of Array.from(this._url.searchParams.keys())) {
424
- this._url.searchParams.delete(key);
453
+ if (keys.size === 0 || keys.has(key)) {
454
+ this._url.searchParams.delete(key);
455
+ }
425
456
  }
426
457
  return this;
427
458
  }
428
459
 
460
+ hashParams(
461
+ val: Record<string, string | number | boolean | Date | null | undefined>,
462
+ mode: "reset" | "merge" = "reset",
463
+ ): BuildURI {
464
+ let preset: Record<string, string>;
465
+ switch (mode) {
466
+ case "reset":
467
+ this._url.hash = "";
468
+ preset = {};
469
+ break;
470
+ case "merge":
471
+ default:
472
+ preset = Object.fromEntries(new URLSearchParams(this._url.hash.replace(/^#/, "")).entries());
473
+ break;
474
+ }
475
+ const out = new URLSearchParams("");
476
+ for (const [key, value] of Object.entries({ ...preset, ...val }).sort((a, b) => a[0].localeCompare(b[0]))) {
477
+ switch (typeof value) {
478
+ case "string":
479
+ out.set(key, value);
480
+ break;
481
+ case "number":
482
+ out.set(key, value.toString());
483
+ break;
484
+ case "boolean":
485
+ out.set(key, value ? "true" : "false");
486
+ break;
487
+ default:
488
+ if (value instanceof Date) {
489
+ out.set(key, value.toISOString());
490
+ } else {
491
+ // eslint-disable-next-line no-console
492
+ console.error(`unsupported type: ${typeof value} ignore key: ${key}`);
493
+ }
494
+ break;
495
+ }
496
+ }
497
+ this._url.hash = out.toString();
498
+ return this;
499
+ }
500
+
429
501
  delParam(key: string): BuildURI {
430
502
  this._url.searchParams.delete(key);
431
503
  return this;
@@ -468,6 +540,10 @@ export class BuildURI implements URIInterface<BuildURI> {
468
540
  return getParamsResult(keys, this);
469
541
  }
470
542
 
543
+ getHashParams(...keys: KeysParam): Result<Record<string, string>> {
544
+ return getParamsResult(keys, resolveHash(this._url.hash));
545
+ }
546
+
471
547
  toString(): string {
472
548
  this._url.searchParams.sort();
473
549
  return this._url.toString();
@@ -579,6 +655,10 @@ export class URI implements URIInterface<URI> {
579
655
  return this._url.hostname;
580
656
  }
581
657
 
658
+ get local(): string {
659
+ return this._url.pathname + this._url.search;
660
+ }
661
+
582
662
  // get password(): string {
583
663
  // return this._url.password;
584
664
  // }
@@ -611,9 +691,9 @@ export class URI implements URIInterface<URI> {
611
691
  // .replace(/\?.*$/, "");
612
692
  }
613
693
 
614
- // get hash(): string {
615
- // return this._url.hash;
616
- // }
694
+ get hash(): string {
695
+ return this._url.hash;
696
+ }
617
697
 
618
698
  // get host(): string {
619
699
  // return this._url.host;
@@ -644,6 +724,10 @@ export class URI implements URIInterface<URI> {
644
724
  return getParamsResult(keys, this);
645
725
  }
646
726
 
727
+ getHashParams(...keys: KeysParam): Result<Record<string, string>> {
728
+ return getParamsResult(keys, resolveHash(this._url.hash));
729
+ }
730
+
647
731
  clone(): URI {
648
732
  return new URI(this._url);
649
733
  }
package/test/index.cjs CHANGED
@@ -1211,6 +1211,15 @@ function coerceKey(key, def) {
1211
1211
  }
1212
1212
  return { key, def };
1213
1213
  }
1214
+ function resolveHash(hash) {
1215
+ const searchParams = new URLSearchParams(hash.replace(/^#/, ""));
1216
+ return {
1217
+ getParam: (k) => {
1218
+ const ret = searchParams.get(k);
1219
+ return ret === null ? void 0 : ret;
1220
+ }
1221
+ };
1222
+ }
1214
1223
  function falsy2undef(value) {
1215
1224
  return value === void 0 || value === null ? void 0 : value;
1216
1225
  }
@@ -1233,6 +1242,7 @@ function isURL(value) {
1233
1242
  }
1234
1243
  var customInspectSymbol = Symbol.for("nodejs.util.inspect.custom");
1235
1244
  var MutableURL = class _MutableURL extends URL {
1245
+ // override readonly hash: string;
1236
1246
  constructor(urlStr) {
1237
1247
  super("defect://does.not.exist");
1238
1248
  const partedURL = urlStr.split(":");
@@ -1255,7 +1265,6 @@ var MutableURL = class _MutableURL extends URL {
1255
1265
  } else {
1256
1266
  this._pathname = urlStr.replace(new RegExp(`^${this._protocol}//`), "").replace(/[#?].*$/, "");
1257
1267
  }
1258
- this.hash = this._sysURL.hash;
1259
1268
  }
1260
1269
  [customInspectSymbol]() {
1261
1270
  return this.toString();
@@ -1310,16 +1319,26 @@ var MutableURL = class _MutableURL extends URL {
1310
1319
  }
1311
1320
  this._protocol = p;
1312
1321
  }
1322
+ get hash() {
1323
+ return this._sysURL.hash;
1324
+ }
1325
+ set hash(h) {
1326
+ this._sysURL.hash = h;
1327
+ }
1313
1328
  get searchParams() {
1314
1329
  return this._sysURL.searchParams;
1315
1330
  }
1316
- toString() {
1331
+ get search() {
1317
1332
  let search = "";
1318
1333
  if (this._sysURL.searchParams.size) {
1319
1334
  for (const [key, value] of Array.from(this._sysURL.searchParams.entries()).sort((a, b) => a[0].localeCompare(b[0]))) {
1320
1335
  search += `${!search.length ? "?" : "&"}${key}=${encodeURIComponent(value)}`;
1321
1336
  }
1322
1337
  }
1338
+ return search;
1339
+ }
1340
+ toString() {
1341
+ const search = this.search;
1323
1342
  let hostpart = "";
1324
1343
  if (this._hasHostpart) {
1325
1344
  hostpart = this._sysURL.hostname;
@@ -1330,7 +1349,7 @@ var MutableURL = class _MutableURL extends URL {
1330
1349
  hostpart += "/";
1331
1350
  }
1332
1351
  }
1333
- return `${this._protocol}//${hostpart}${this._pathname}${search}`;
1352
+ return `${this._protocol}//${hostpart}${this._pathname}${search}${this.hash}`;
1334
1353
  }
1335
1354
  };
1336
1355
  function from(fac, strURLUri, defaultProtocol) {
@@ -1393,6 +1412,10 @@ var BuildURI = class _BuildURI {
1393
1412
  this._url.pathname = p;
1394
1413
  return this;
1395
1414
  }
1415
+ hash(h) {
1416
+ this._url.hash = h;
1417
+ return this;
1418
+ }
1396
1419
  // could pass a relative path or a full URL
1397
1420
  // if relative path, it will be appended to the current path
1398
1421
  resolve(p) {
@@ -1421,12 +1444,51 @@ var BuildURI = class _BuildURI {
1421
1444
  }
1422
1445
  return this;
1423
1446
  }
1424
- cleanParams() {
1447
+ cleanParams(...remove) {
1448
+ const keys = new Set(remove.flat());
1425
1449
  for (const key of Array.from(this._url.searchParams.keys())) {
1426
- this._url.searchParams.delete(key);
1450
+ if (keys.size === 0 || keys.has(key)) {
1451
+ this._url.searchParams.delete(key);
1452
+ }
1427
1453
  }
1428
1454
  return this;
1429
1455
  }
1456
+ hashParams(val, mode = "reset") {
1457
+ let preset;
1458
+ switch (mode) {
1459
+ case "reset":
1460
+ this._url.hash = "";
1461
+ preset = {};
1462
+ break;
1463
+ case "merge":
1464
+ default:
1465
+ preset = Object.fromEntries(new URLSearchParams(this._url.hash.replace(/^#/, "")).entries());
1466
+ break;
1467
+ }
1468
+ const out = new URLSearchParams("");
1469
+ for (const [key, value] of Object.entries({ ...preset, ...val }).sort((a, b) => a[0].localeCompare(b[0]))) {
1470
+ switch (typeof value) {
1471
+ case "string":
1472
+ out.set(key, value);
1473
+ break;
1474
+ case "number":
1475
+ out.set(key, value.toString());
1476
+ break;
1477
+ case "boolean":
1478
+ out.set(key, value ? "true" : "false");
1479
+ break;
1480
+ default:
1481
+ if (value instanceof Date) {
1482
+ out.set(key, value.toISOString());
1483
+ } else {
1484
+ console.error(`unsupported type: ${typeof value} ignore key: ${key}`);
1485
+ }
1486
+ break;
1487
+ }
1488
+ }
1489
+ this._url.hash = out.toString();
1490
+ return this;
1491
+ }
1430
1492
  delParam(key) {
1431
1493
  this._url.searchParams.delete(key);
1432
1494
  return this;
@@ -1461,6 +1523,9 @@ var BuildURI = class _BuildURI {
1461
1523
  getParamsResult(...keys) {
1462
1524
  return getParamsResult(keys, this);
1463
1525
  }
1526
+ getHashParams(...keys) {
1527
+ return getParamsResult(keys, resolveHash(this._url.hash));
1528
+ }
1464
1529
  toString() {
1465
1530
  this._url.searchParams.sort();
1466
1531
  return this._url.toString();
@@ -1526,6 +1591,9 @@ var URI = class _URI {
1526
1591
  get hostname() {
1527
1592
  return this._url.hostname;
1528
1593
  }
1594
+ get local() {
1595
+ return this._url.pathname + this._url.search;
1596
+ }
1529
1597
  // get password(): string {
1530
1598
  // return this._url.password;
1531
1599
  // }
@@ -1547,9 +1615,9 @@ var URI = class _URI {
1547
1615
  get pathname() {
1548
1616
  return this._url.pathname;
1549
1617
  }
1550
- // get hash(): string {
1551
- // return this._url.hash;
1552
- // }
1618
+ get hash() {
1619
+ return this._url.hash;
1620
+ }
1553
1621
  // get host(): string {
1554
1622
  // return this._url.host;
1555
1623
  // }
@@ -1573,6 +1641,9 @@ var URI = class _URI {
1573
1641
  getParamsResult(...keys) {
1574
1642
  return getParamsResult(keys, this);
1575
1643
  }
1644
+ getHashParams(...keys) {
1645
+ return getParamsResult(keys, resolveHash(this._url.hash));
1646
+ }
1576
1647
  clone() {
1577
1648
  return new _URI(this._url);
1578
1649
  }