@yuneta/gobj-js 7.28.1 → 7.29.0

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.
@@ -7544,28 +7544,43 @@ function debounce(func, wait = 0, immediate = false) {
7544
7544
  * Build a URL/path from segments, ensuring exactly one '/'
7545
7545
  * between each part.
7546
7546
  *
7547
- * - The first segment keeps its leading '/' (absolute) or
7548
- * not (relative).
7549
- * - Trailing '/' is stripped from every segment.
7550
- * - Leading '/' is stripped from every segment except the first.
7547
+ * - The first segment is the ROOT: it keeps its leading '/'
7548
+ * (absolute) or not (relative), and its trailing '/' are
7549
+ * stripped -- but a root made only of '/' stays "/".
7550
+ * - Leading and trailing '/' are stripped from every later
7551
+ * segment, and duplicate '/' inside it are collapsed.
7552
+ * - '.' and '..' of the later segments are resolved, and '..'
7553
+ * never rises above the root: ("/store", "../../etc") is
7554
+ * "/store/etc".
7551
7555
  * - Empty/null/undefined segments are skipped.
7552
7556
  *
7553
- * Mirrors the C helper build_path() from gobj-c/src/helpers.c.
7557
+ * Mirrors the C helper build_path() from gobj-c/src/helpers.c
7558
+ * (tests/build_path.test.js pins the same cases). Up to 7.28.1
7559
+ * a root "/" was lost (("/", "tmp") gave "tmp") and '.'/'..'
7560
+ * were kept as they came.
7554
7561
  ************************************************************/
7555
7562
  function build_path(...segments) {
7556
- let result = "";
7557
- let i = 0;
7563
+ let root = null;
7564
+ let tail = [];
7558
7565
  for (let segment of segments) {
7559
7566
  if (segment == null || segment === "") continue;
7560
7567
  segment = String(segment);
7561
- if (i === 0) segment = segment.replace(/\/+$/, "");
7562
- else segment = segment.replace(/^\/+/, "").replace(/\/+$/, "");
7563
- if (segment === "") continue;
7564
- if (i > 0) result += "/";
7565
- result += segment;
7566
- i++;
7568
+ if (root === null) {
7569
+ root = segment.replace(/(.)\/+$/, "$1");
7570
+ continue;
7571
+ }
7572
+ for (let component of segment.split("/")) {
7573
+ if (component === "" || component === ".") continue;
7574
+ if (component === "..") {
7575
+ tail.pop();
7576
+ continue;
7577
+ }
7578
+ tail.push(component);
7579
+ }
7567
7580
  }
7568
- return result;
7581
+ if (root === null) return "";
7582
+ if (tail.length === 0) return root;
7583
+ return root + (root.endsWith("/") ? "" : "/") + tail.join("/");
7569
7584
  }
7570
7585
  /***************************************
7571
7586
  * Flat json (id -> value)