All files / path / _common / strip_trailing_separators.ts

100.00% Branches 12/12
100.00% Functions 1/1
100.00% Lines 16/16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
 
 
 
 
x179
x179
x179
 
x813
x87
x87
 
x726
 
x787
x744
x28
x744
x716
x716
x744
 
x726
x813























// Copyright 2018-2026 the Deno authors. MIT license.
// Copyright the Browserify authors. MIT License.
// Ported from https://github.com/browserify/path-browserify/
// This module is browser compatible.

export function stripTrailingSeparators(
  segment: string,
  isSep: (char: number) => boolean,
): string {
  if (segment.length <= 1) {
    return segment;
  }

  let end = segment.length;

  for (let i = segment.length - 1; i > 0; i--) {
    if (isSep(segment.charCodeAt(i))) {
      end = i;
    } else {
      break;
    }
  }

  return segment.slice(0, end);
}