All files / text / unstable_reverse.ts

100.00% Branches 3/3
100.00% Functions 1/1
100.00% Lines 18/18
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x3
x3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x3
x3
x3
 
x91
 
x81
 
x81
 
 
x44
x81
 
x81
x81
 
 
x91
x91
x1297
x1297
x91
x91























































































// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.

// Copyright Mathias Bynens <https://mathiasbynens.be/>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

const SYMBOL_WITH_COMBINING_MARKS_REGEXP = /(\P{M})(\p{M}+)/gu;
const SURROGATE_PAIR_REGEXP = /([\uD800-\uDBFF])([\uDC00-\uDFFF])/g;

/** Options for {@linkcode reverse}  */
export type ReverseOptions = {
  /**
   * Whether to handle Unicode symbols such as 🦕 at the cost of ~60% slowdown.
   *
   * Check {@link ./unstable_reverse_bench.ts} for performance comparison.
   *
   * @default {true}
   */
  handleUnicode: boolean;
};

/**
 * Performs a Unicode-aware string reversal.
 *
 * @experimental **UNSTABLE**: New API, yet to be vetted.
 *
 * @param input - The input string to be reversed.
 * @param options The options for the reverse function.
 * @returns The reversed string.
 *
 * @example Standard usage
 * ```ts
 * import { reverse } from "@std/text/unstable-reverse";
 * import { assertEquals } from "@std/assert";
 *
 * assertEquals(reverse("Hello, world!"), "!dlrow ,olleH");
 * assertEquals(reverse("🦕Deno♥"), "♥oneD🦕");
 * ```
 *
 * @example Performance optimization with disabled Unicode handling
 * ```ts
 * import { reverse } from "@std/text/unstable-reverse";
 * import { assertEquals } from "@std/assert";
 *
 * assertEquals(reverse("Hello, world!", { handleUnicode: false }), "!dlrow ,olleH");
 * ```
 */
export function reverse(
  input: string,
  options?: Partial<ReverseOptions>,
): string {
  if (options?.handleUnicode !== false) {
    // Step 1: deal with combining marks and astral symbols (surrogate pairs)
    input = input
      // Swap symbols with their combining marks so the combining marks go first
      .replace(SYMBOL_WITH_COMBINING_MARKS_REGEXP, (_, $1, $2) => {
        // Reverse the combining marks so they will end up in the same order
        // later on (after another round of reversing)
        return reverse($2) + $1;
      })
      // Swap high and low surrogates so the low surrogates go first
      .replace(SURROGATE_PAIR_REGEXP, "$2$1");
  }

  // Step 2: reverse the code units in the string
  let result = "";
  for (let index = input.length; index--;) {
    result += input.charAt(index);
  }
  return result;
}