💿Trans Functions

A set of functions to processing and converting Arabic text, including functions for language segmentation, and more.

Table of Contents

convert

Syntax

convert(text, codeFrom, codeTo);

Description

Converts the given text from one encoding to another.

Parameters

  • text (string): The input text to be converted.

  • codeFrom (string): The source encoding.

  • codeTo (string): The target encoding.

Returns

  • string: The converted text.

Example

const text = "مرحبا";
const convertedText = convert(text, "utf", "tim");
console.log(convertedText); // "mrHbA"

segmentLanguage

Syntax

segmentLanguage(text);

Description

Segments the given text into Arabic and non-Arabic chunks.

Parameters

  • text (string): The input text to be segmented.

Returns

  • Array: An array of tuples, where the first element of each tuple is the language ("arabic" or "latin") and the second element is the corresponding text chunk.

Example

const text = "Hello, مرحبا!";
const segmentedText = segmentLanguage(text);
console.log(segmentedText); // [[ 'latin', 'Hello, ' ], [ 'arabic', 'مرحبا!' ]]

delimiteLanguage

Syntax

delimiteLanguage(text, language, start, end);

Description

Wraps the text chunks of the specified language in the given start and end delimiters.

Parameters

  • text (string): The input text to be delimited.

  • language (string): The language to wrap ("arabic" or "latin").

  • start (string): The start delimiter.

  • end (string): The end delimiter.

Returns

  • string: The text with specified language chunks wrapped in the delimiters.

Example

const text = "Hello, مرحبا!";
const delimitedText = delimiteLanguage(text, "arabic", "<arabic>", "</arabic>");
console.log(delimitedText); // "Hello, <arabic>مرحبا</arabic>!"

normalizeDigits

Syntax

normalizeDigits(text, source, out);

Description

Normalizes the digits in the given text based on the source and target number systems.

Parameters

  • text (string): The input text containing digits to be normalized.

  • source (string): The source number system ("all", "west", "east", or "persian").

  • out (string): The target number system ("west", "east", or "persian").

Returns

  • string: The text with digits normalized.

Example

const text = "١٢٣";
const normalizedDigits = normalizeDigits(text, "all", "west");
console.log(normalizedDigits); // "123"

Last updated