ts: Convert rtl.js to TypeScript.

Co-authored-by: Satyam Bansal <sbansal1999@gmail.com>
This commit is contained in:
xoldyckk 2023-03-25 02:28:50 +05:30 committed by Tim Abbott
parent 6d6a335e32
commit 6f009c5e65
1 changed files with 7 additions and 5 deletions

View File

@ -1,4 +1,5 @@
import _ from "lodash";
import assert from "minimalistic-assert";
// How to determine the direction of a paragraph (P1-P3): https://www.unicode.org/reports/tr9/tr9-35.html#The_Paragraph_Level
// Embedding level: https://www.unicode.org/reports/tr9/tr9-35.html#BD2
@ -21,7 +22,7 @@ import _ from "lodash";
* @param {string} raw
* @returns {number[]}
*/
function convert_from_raw(digits, part_length, raw) {
function convert_from_raw(digits: string, part_length: number, raw: string): number[] {
const result = [];
for (let i = 0; i < raw.length; ) {
let t = 0;
@ -86,7 +87,7 @@ const lr_ranges = [
* @param {number} ch A character to get its bidirectional class.
* @returns {'I' | 'PDI' | 'R' | 'L' | 'Other'}
*/
function get_bidi_class(ch) {
function get_bidi_class(ch: number): "I" | "PDI" | "R" | "L" | "Other" {
if (i_chars.has(ch)) {
return "I"; // LRI, RLI, FSI
}
@ -109,10 +110,10 @@ function get_bidi_class(ch) {
* @param {string} str The string to get its direction.
* @returns {'ltr' | 'rtl'}
*/
export function get_direction(str) {
export function get_direction(str: string): "ltr" | "rtl" {
let isolations = 0;
for (const ch of str) {
const bidi_class = get_bidi_class(ch.codePointAt(0));
const bidi_class = get_bidi_class(ch.codePointAt(0)!);
switch (bidi_class) {
case "I":
// LRI, RLI, FSI
@ -139,9 +140,10 @@ export function get_direction(str) {
return "ltr";
}
export function set_rtl_class_for_textarea($textarea) {
export function set_rtl_class_for_textarea($textarea: JQuery<HTMLTextAreaElement>): void {
// Set the rtl class if the text has an rtl direction, remove it otherwise
let text = $textarea.val();
assert(typeof text === "string", "Passed HTML element must be a textarea.");
if (text.startsWith("```quote")) {
text = text.slice(8);
}