@mpen/rerouter 0.1.7 → 0.1.9

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/bundle.cjs CHANGED
@@ -227,18 +227,20 @@ class UriTemplate {
227
227
  for (const v of p.vars) {
228
228
  if (Object.hasOwn(variables, v.name)) {
229
229
  const x = variables[v.name];
230
- const esc = x => percentEncodeRegExp(x, ReservedExpansion[p.prefix], v.length);
230
+ if (x == null) continue;
231
+ const sep = v.repeat ? SeparatorMap[p.prefix] : ",";
232
+ const esc = x => percentEncodeRegExp(x, ReservedExpansion[p.prefix], v.length, v.repeat ? "=" : ",");
231
233
  let pre = "";
232
234
  if (Named[p.prefix]) {
233
235
  pre = v.name + (isEmpty(x) ? IfEmp[p.prefix] : "=");
234
236
  }
235
237
  if (Array.isArray(x)) {
236
238
  if (x.length) {
237
- vs.push((v.repeat ? "" : pre) + x.map((z => (v.repeat ? pre : "") + esc(z))).join(v.repeat ? SeparatorMap[p.prefix] : ","));
239
+ vs.push((v.repeat ? "" : pre) + x.map((z => (v.repeat ? pre : "") + esc(z))).join(sep));
238
240
  }
239
- } else if (x != null && typeof x === "object") {
241
+ } else if (typeof x === "object") {
240
242
  if (Object.keys(x).length) {
241
- vs.push((v.repeat ? "" : pre) + Object.entries(x).map((([ok, ov]) => `${esc(ok)}${v.repeat ? "=" : ","}${esc(ov)}`)).join(v.repeat ? SeparatorMap[p.prefix] : ","));
243
+ vs.push((v.repeat ? "" : pre) + Object.entries(x).map((([ok, ov]) => `${esc(ok)}${v.repeat ? "=" : ","}${esc(ov)}`)).join(sep));
242
244
  }
243
245
  } else {
244
246
  vs.push(pre + esc(x));
@@ -375,18 +377,32 @@ function escapeRegExp(string) {
375
377
  return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
376
378
  }
377
379
 
378
- function percentEncodeRegExp(x, reserved, length) {
379
- if (typeof x === "number") {
380
- return String(x);
380
+ function fullWide(n) {
381
+ try {
382
+ return n.toLocaleString("en-US", {
383
+ useGrouping: false,
384
+ maximumFractionDigits: 20
385
+ });
386
+ } catch {
387
+ return n.toFixed(14).replace(/\.?0+$/, "");
388
+ }
389
+ }
390
+
391
+ function percentEncodeRegExp(value, reserved, length, separator) {
392
+ if (typeof value === "number") {
393
+ return fullWide(value);
394
+ }
395
+ if (value === true) return "1";
396
+ if (value === false) return "0";
397
+ if (value == null) return "";
398
+ if (Array.isArray(value)) {
399
+ return value.map((v => percentEncodeRegExp(v, reserved, length, separator))).join(separator);
381
400
  }
382
- if (x === true) return "1";
383
- if (x === false) return "0";
384
- if (x === null) return "";
385
401
  if (length != null) {
386
- x = x.slice(0, length);
402
+ value = value.slice(0, length);
387
403
  }
388
404
  if (reserved) {
389
- return x.replace(/%[0-9a-fA-F]{2}|./gsu, (m => {
405
+ return value.replace(/%[0-9a-fA-F]{2}|./gsu, (m => {
390
406
  let v = PERCENT_RE.test(m) ? decodeURIComponent(m) : m;
391
407
  if (UR_SET.test(v)) {
392
408
  return percentEncode(v);
@@ -394,7 +410,7 @@ function percentEncodeRegExp(x, reserved, length) {
394
410
  return m;
395
411
  }));
396
412
  }
397
- return x.replace(UNRESERVED, percentEncode);
413
+ return value.replace(UNRESERVED, percentEncode);
398
414
  }
399
415
 
400
416
  const UTF8_ENCODER = new TextEncoder;
package/dist/bundle.mjs CHANGED
@@ -225,18 +225,20 @@ class UriTemplate {
225
225
  for (const v of p.vars) {
226
226
  if (Object.hasOwn(variables, v.name)) {
227
227
  const x = variables[v.name];
228
- const esc = x => percentEncodeRegExp(x, ReservedExpansion[p.prefix], v.length);
228
+ if (x == null) continue;
229
+ const sep = v.repeat ? SeparatorMap[p.prefix] : ",";
230
+ const esc = x => percentEncodeRegExp(x, ReservedExpansion[p.prefix], v.length, v.repeat ? "=" : ",");
229
231
  let pre = "";
230
232
  if (Named[p.prefix]) {
231
233
  pre = v.name + (isEmpty(x) ? IfEmp[p.prefix] : "=");
232
234
  }
233
235
  if (Array.isArray(x)) {
234
236
  if (x.length) {
235
- vs.push((v.repeat ? "" : pre) + x.map((z => (v.repeat ? pre : "") + esc(z))).join(v.repeat ? SeparatorMap[p.prefix] : ","));
237
+ vs.push((v.repeat ? "" : pre) + x.map((z => (v.repeat ? pre : "") + esc(z))).join(sep));
236
238
  }
237
- } else if (x != null && typeof x === "object") {
239
+ } else if (typeof x === "object") {
238
240
  if (Object.keys(x).length) {
239
- vs.push((v.repeat ? "" : pre) + Object.entries(x).map((([ok, ov]) => `${esc(ok)}${v.repeat ? "=" : ","}${esc(ov)}`)).join(v.repeat ? SeparatorMap[p.prefix] : ","));
241
+ vs.push((v.repeat ? "" : pre) + Object.entries(x).map((([ok, ov]) => `${esc(ok)}${v.repeat ? "=" : ","}${esc(ov)}`)).join(sep));
240
242
  }
241
243
  } else {
242
244
  vs.push(pre + esc(x));
@@ -373,18 +375,32 @@ function escapeRegExp(string) {
373
375
  return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
374
376
  }
375
377
 
376
- function percentEncodeRegExp(x, reserved, length) {
377
- if (typeof x === "number") {
378
- return String(x);
378
+ function fullWide(n) {
379
+ try {
380
+ return n.toLocaleString("en-US", {
381
+ useGrouping: false,
382
+ maximumFractionDigits: 20
383
+ });
384
+ } catch {
385
+ return n.toFixed(14).replace(/\.?0+$/, "");
386
+ }
387
+ }
388
+
389
+ function percentEncodeRegExp(value, reserved, length, separator) {
390
+ if (typeof value === "number") {
391
+ return fullWide(value);
392
+ }
393
+ if (value === true) return "1";
394
+ if (value === false) return "0";
395
+ if (value == null) return "";
396
+ if (Array.isArray(value)) {
397
+ return value.map((v => percentEncodeRegExp(v, reserved, length, separator))).join(separator);
379
398
  }
380
- if (x === true) return "1";
381
- if (x === false) return "0";
382
- if (x === null) return "";
383
399
  if (length != null) {
384
- x = x.slice(0, length);
400
+ value = value.slice(0, length);
385
401
  }
386
402
  if (reserved) {
387
- return x.replace(/%[0-9a-fA-F]{2}|./gsu, (m => {
403
+ return value.replace(/%[0-9a-fA-F]{2}|./gsu, (m => {
388
404
  let v = PERCENT_RE.test(m) ? decodeURIComponent(m) : m;
389
405
  if (UR_SET.test(v)) {
390
406
  return percentEncode(v);
@@ -392,7 +408,7 @@ function percentEncodeRegExp(x, reserved, length) {
392
408
  return m;
393
409
  }));
394
410
  }
395
- return x.replace(UNRESERVED, percentEncode);
411
+ return value.replace(UNRESERVED, percentEncode);
396
412
  }
397
413
 
398
414
  const UTF8_ENCODER = new TextEncoder;
@@ -12,6 +12,7 @@ interface StringLiteral {
12
12
  type TemplateParts = Array<Placeholder | StringLiteral>;
13
13
  interface VarSpec {
14
14
  name: string;
15
+ /** Max length, in characters. */
15
16
  length: number | null;
16
17
  func: string | null;
17
18
  repeat: boolean;
@@ -40,9 +41,14 @@ export declare class UriTemplate<P extends UriParams> {
40
41
  expand(variables: P): string;
41
42
  match(url: string): UriMatch<P> | null;
42
43
  }
43
- export type UrlParamValue = string | number | boolean | string[] | number[] | Record<string, string | number | boolean | null>;
44
- type NullableUrlParamValue = UrlParamValue | null;
45
- export type UriParams = Record<string, NullableUrlParamValue>;
44
+ type Primitive = string | number | boolean | null;
45
+ type PrimitivePair = [key: string, value: Primitive];
46
+ type PrimitiveMap = {
47
+ [K in string]: Primitive;
48
+ };
49
+ export type UrlParamValue = Primitive | Primitive[] | PrimitivePair[] | PrimitiveMap;
50
+ export declare function fullWide(n: number): string;
51
+ export type UriParams = Record<string, UrlParamValue>;
46
52
  export type UriMatch<P extends UriParams> = {
47
53
  score: number;
48
54
  params: P;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpen/rerouter",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "packageManager": "bun",
5
5
  "exports": "./dist/bundle.cjs",
6
6
  "module": "./dist/bundle.mjs",