Skip to content

Commit 4b9c2ab

Browse files
Merge pull request #23 from Seen-Arabic/22-trim-text-before-converting
resolve #22 Trim text before converting
2 parents c83d43f + 0572c01 commit 4b9c2ab

3 files changed

Lines changed: 44 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,55 @@
11
<h1 align=center>📜 Changelog - سجل التغيير</h1>
22
<p align=center>All notable changes to this project will be documented in this file.</p>
33

4+
## [Unreleased](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/compare/v1.0.5...HEAD)
45

5-
## [Unreleased](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/compare/v1.0.4...HEAD)
6+
## [1.0.5](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/releases/tag/v1.0.5) - 2023-12-03 (19 Jumada al-awwal 1445)
7+
8+
### Updated
9+
10+
- Trim text before converting ([#22](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/22))
11+
12+
[Full Changelog](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/compare/v1.0.3...v1.0.4)
613

714
## [1.0.4](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/releases/tag/v1.0.4) - 2023-11-30 (16 Jumada al-awwal 1445)
15+
816
### Updated
9-
- rename `Panned` to `Banned`: which is the correct translation of `محظور` ([PR #19](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/pull/19))
17+
18+
- rename `Panned` to `Banned`: which is the correct translation of `محظور` ([PR #19](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/pull/19))
1019

1120
[Full Changelog](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/compare/v1.0.3...v1.0.4)
1221

1322
## [1.0.3](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/releases/tag/v1.0.3) - 2023-11-18 (04 Jumada al-awwal 1445)
23+
1424
### Added
15-
- Convert To Old Arabic And Tashfeer Banned Words: Transform Arabic text into old script and replace Banned Arabic text with visually similar characters for encoding purposes. (Banned words are words that considered as hate speech in social media) ([#18](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/18))
25+
26+
- Convert To Old Arabic And Tashfeer Banned Words: Transform Arabic text into old script and replace Banned Arabic text with visually similar characters for encoding purposes. (Banned words are words that considered as hate speech in social media) ([#18](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/18))
1627

1728
[Full Changelog](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/compare/v1.0.2...v1.0.3)
1829

1930
## [1.0.2](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/releases/tag/v1.0.2) - 2023-11-18 (04 Jumada al-awwal 1445)
31+
2032
### Added
21-
- Tashfeer Banned Words: Replaces Banned Arabic text with visually similar characters for encoding purposes. (Banned words are words that considered as hate speech in social media) ([#16](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/16))
22-
- Remove Arabic Affixes: Removes predefined affixes (prefixes and suffixes) from an Arabic word if it starts or ends with those affixes. ([#17](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/17))
33+
34+
- Tashfeer Banned Words: Replaces Banned Arabic text with visually similar characters for encoding purposes. (Banned words are words that considered as hate speech in social media) ([#16](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/16))
35+
- Remove Arabic Affixes: Removes predefined affixes (prefixes and suffixes) from an Arabic word if it starts or ends with those affixes. ([#17](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/17))
2336

2437
[Full Changelog](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/compare/v1.0.1...v1.0.2)
2538

2639
## [1.0.1](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/releases/tag/v1.0.1) - 2023-11-15 (Jumada al-awwal 1445)
40+
2741
### Added
28-
- Word To Letters: Convert Arabic word to its pronounced letters. ([#2](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/2))
42+
43+
- Word To Letters: Convert Arabic word to its pronounced letters. ([#2](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/2))
2944

3045
[Full Changelog](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/compare/1.0.0...v1.0.1)
3146

3247
## [1.0.0](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/releases/tag/1.0.0) - 2023-11-07 (23 Rabi` al Thani 1445)
48+
3349
### Added
34-
- Initial release of the library with the following functionalities:
35-
- Tashkeel Removal: Easily remove Tashkeel from Arabic text. ([#12](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/12))
36-
- Tatweel Removal: Remove Tatweel character from Arabic phrases. ([#8](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/8))
37-
- Convert To Old Arabic: Transform Arabic text into old script. ([#9](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/9))
38-
- Tashfeer: Replaces Arabic text with visually similar characters for encoding purposes. ([#13](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/13))
50+
51+
- Initial release of the library with the following functionalities:
52+
- Tashkeel Removal: Easily remove Tashkeel from Arabic text. ([#12](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/12))
53+
- Tatweel Removal: Remove Tatweel character from Arabic phrases. ([#8](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/8))
54+
- Convert To Old Arabic: Transform Arabic text into old script. ([#9](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/9))
55+
- Tashfeer: Replaces Arabic text with visually similar characters for encoding purposes. ([#13](https://github.com/Seen-Arabic/Arabic-Services-JavaScript/issues/13))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "arabic-services",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "Utility functions on Arabic text",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/scripts/scripts.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import { setCharAt, similarityScore } from '../utils';
2121
* Output: "الخيل والليل والبيداء تعرفني"
2222
*/
2323
export function removeTashkeel(text: string): string {
24-
return text.replace(new RegExp('[' + TASHKEEL.join('') + ']', 'g'), '').replace(/ٱ/g, 'ا');
24+
return text
25+
.trim()
26+
.replace(new RegExp('[' + TASHKEEL.join('') + ']', 'g'), '')
27+
.replace(/ٱ/g, 'ا');
2528
}
2629

2730
/**
@@ -33,7 +36,7 @@ export function removeTashkeel(text: string): string {
3336
* Output: "الحىل واللىل والٮىدا ٮعرڡٮى"
3437
*/
3538
export function toOldArabic(sentence: string): string {
36-
sentence = removeTashkeel(sentence);
39+
sentence = removeTashkeel(sentence.trim());
3740
let newSentence = '';
3841
for (let letter = 0; letter < sentence.length; letter++) {
3942
// if letter is not Arabic letter => append to newSentence
@@ -61,7 +64,8 @@ export function toOldArabic(sentence: string): string {
6164

6265
export function toOldArabicAndTashfeerBannedWords(sentence: string, levelOfTashfeer: number = 2): string {
6366
let new_sentence = '';
64-
for (const word of sentence.split(' ')) {
67+
const words = sentence.trim().split(' ');
68+
for (const word of words) {
6569
if (checkIfBannedWord(word)) {
6670
new_sentence += tashfeerHandler(word, levelOfTashfeer) + ' ';
6771
} else {
@@ -80,7 +84,7 @@ export function toOldArabicAndTashfeerBannedWords(sentence: string, levelOfTashf
8084
* Output: "رائع"
8185
*/
8286
export function removeTatweel(text: string): string {
83-
return text.replace(/ـ/g, '');
87+
return text.trim().replace(/ـ/g, '');
8488
}
8589

8690
/**
@@ -89,18 +93,19 @@ export function removeTatweel(text: string): string {
8993
* @returns {string} The word with pronounced letters separated by spaces.
9094
*/
9195
export function wordToLetters(word: string): string {
96+
const trimmedWord = word.trim();
9297
let newWord = '';
9398

9499
// Loop through each character in the input word
95-
for (let i = 0; i < word.length; i++) {
96-
const letter = word[i];
100+
for (let i = 0; i < trimmedWord.length; i++) {
101+
const letter = trimmedWord[i];
97102

98103
// Check if the current letter has a pronunciation in PRONOUNCED_LETTERS
99104
if (PRONOUNCED_LETTERS.hasOwnProperty(letter)) {
100105
newWord += PRONOUNCED_LETTERS[letter];
101106

102107
// Add a space after the pronounced letter unless it's the last letter in the word
103-
if (i !== word.length - 1) {
108+
if (i !== trimmedWord.length - 1) {
104109
newWord += ' ';
105110
}
106111
} else {
@@ -121,6 +126,7 @@ export function wordToLetters(word: string): string {
121126
* @returns {string} The word after removing any matching affixes. Returns the original word if no affix matches are found.
122127
*/
123128
export function removeArabicAffixes(word: string): string {
129+
word = word.trim();
124130
if (ARABIC_PREFIXES.includes(word.substring(0, 2))) {
125131
// For: ALEF & LAM
126132
word = setCharAt(word, 0, '');
@@ -263,6 +269,7 @@ function tashfeerHandler(word: string, level: number = 0): string {
263269
* @returns {string} The encrypted sentence.
264270
*/
265271
export function tashfeer(sentence: string, levelOfTashfeer: number = 1): string {
272+
sentence = sentence.trim();
266273
let new_sentence = '';
267274
for (const word of sentence.split(' ')) {
268275
new_sentence += tashfeerHandler(word, levelOfTashfeer) + ' ';
@@ -308,6 +315,7 @@ function checkIfBannedWord(string: string): boolean {
308315
*/
309316
export function tashfeerBannedWords(sentence: string, levelOfTashfeer: number = 2): string {
310317
let new_sentence = '';
318+
sentence = sentence.trim();
311319
for (const word of sentence.split(' ')) {
312320
if (checkIfBannedWord(word)) {
313321
new_sentence += tashfeerHandler(word, levelOfTashfeer) + ' ';

0 commit comments

Comments
 (0)