Skip to content

Commit

Permalink
fix(#70): converting 0 to Persian digit returns undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-master committed Apr 18, 2021
1 parent 31dced1 commit a21c69e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
37 changes: 20 additions & 17 deletions src/modules/digits/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ const arNums = "٠١٢٣٤٥٦٧٨٩";
*
*/
export function digitsEnToFa(value?: number | string): string {
if (typeof value !== "number" && typeof value !== "string") {
throw new TypeError("the input must be string or number");
const isString = typeof value === "string";
if (typeof value !== "number" && !isString) {
throw new TypeError("PersianTools: digitsEnToFa - The input must be string or number");
}

let string = typeof value === "number" ? String(value) : value;

let str = (!isString ? `${value}` : value) as string;
for (let i = 0; i < 10; i++) {
const replaceEntoFa = new RegExp("" + i, "g");
string = string.replace(replaceEntoFa, faNums[i]);
const replaceEnToFa = new RegExp(`${i}`, "g");
str = str.replace(replaceEnToFa, faNums[i]);
}

return string;
return str;
}

/** digitsFaToEn
Expand All @@ -32,13 +32,12 @@ export function digitsEnToFa(value?: number | string): string {
*/
export function digitsFaToEn(str: string): string {
if (typeof str !== "string") {
throw new Error("the input must be string");
throw new Error("PersianTools: digitsFaToEn - The input must be string");
}

for (let i = 0; i < 10; i++) {
const replaceFaToEn = new RegExp(faNums[i], "g");
// @ts-ignore
str = str.replace(replaceFaToEn, i);
str = str.replace(replaceFaToEn, `${i}`);
}

return str;
Expand All @@ -51,12 +50,15 @@ export function digitsFaToEn(str: string): string {
* digits
*
*/
export function digitsArToFa(str?: string | number): string | undefined {
if (!str) return;
export function digitsArToFa(str: string): string {
if (typeof str !== "string") {
throw new TypeError("PersianTools: digitsArToFa - The input must be string");
}

let result = "" + str;
let result = `${str}`;
for (let i = 0; i < 10; i++) {
const replaceArabicToPersian = new RegExp(arNums[i], "g");

result = result.replace(replaceArabicToPersian, faNums[i]);
}

Expand All @@ -70,14 +72,15 @@ export function digitsArToFa(str?: string | number): string | undefined {
* digits
*
*/
export function digitsArToEn(str?: string): string | undefined {
if (!str) return;
export function digitsArToEn(str: string): string {
if (typeof str !== "string") {
throw new TypeError("PersianTools: digitsArToEn - The input must be string");
}

let result = str;
for (let i = 0; i < 10; i++) {
const replaceArabicToEnglish = new RegExp(arNums[i], "g");
// @ts-ignore
result = String(result).replace(replaceArabicToEnglish, i);
result = String(result).replace(replaceArabicToEnglish, `${i}`);
}

return result;
Expand Down
44 changes: 20 additions & 24 deletions test/digits.spec.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,64 @@
import { digitsEnToFa, digitsFaToEn, digitsArToFa, digitsArToEn } from "../src";

describe("Digits", () => {
describe("Digits converter", () => {
it("digitsArToFa", () => {
expect(digitsArToFa("٠١٢٣٤٥٦٧٨٩")).toEqual("۰۱۲۳۴۵۶۷۸۹");
expect(digitsArToFa("۸۹123۴۵")).toEqual("۸۹123۴۵");
expect(digitsArToFa(456128)).toEqual("456128");
expect(digitsArToFa()).toBeUndefined();
expect(digitsArToFa("")).toBeUndefined();
try {
//@ts-ignore
digitsArToFa();
} catch (e) {
expect(e.message).toEqual("PersianTools: digitsArToFa - The input must be string");
}
expect(digitsArToFa("")).toEqual("");
expect(digitsArToFa("Text ٠١٢٣٤٥٦٧٨٩")).toEqual("Text ۰۱۲۳۴۵۶۷۸۹");
});

it("digitsArToEn", () => {
expect(digitsArToEn("٠١٢٣٤٥٦٧٨٩")).toEqual("0123456789");
expect(digitsArToEn("٨٩123٤٥")).toEqual("8912345");
// @ts-ignore
expect(digitsArToEn(456128)).toEqual("456128");

expect(digitsArToEn()).toBeUndefined();
expect(digitsArToEn("")).toBeUndefined();
try {
//@ts-ignore
digitsArToEn();
} catch (e) {
expect(e.message).toEqual("PersianTools: digitsArToEn - The input must be string");
}

expect(digitsArToEn("Text ٠١٢٣٤٥٦٧٨٩")).toEqual("Text 0123456789");
});

it("digitsEnToFa", () => {
expect(digitsEnToFa("123۴۵۶")).toEqual("۱۲۳۴۵۶");
expect(digitsEnToFa(123)).toEqual("۱۲۳");
expect(digitsEnToFa(1234567891)).toEqual("۱۲۳۴۵۶۷۸۹۱");
expect(digitsEnToFa(0)).toEqual("۰");
expect(digitsEnToFa("٤٥٦")).toEqual("٤٥٦");
expect(digitsEnToFa("123۴۵۶")).toEqual("۱۲۳۴۵۶");
try {
//@ts-ignore
digitsEnToFa();
} catch (e) {
expect(e.message).toEqual("the input must be string or number");
expect(e.message).toEqual("PersianTools: digitsEnToFa - The input must be string or number");
}

try {
//@ts-ignore
digitsEnToFa(undefined);
} catch (e) {
expect(e.message).toEqual("the input must be string or number");
expect(e.message).toEqual("PersianTools: digitsEnToFa - The input must be string or number");
}
});

it("digitsFaToEn", () => {
expect(digitsFaToEn("123۴۵۶")).toEqual("123456");
expect(digitsFaToEn("۸۹123۴۵")).toEqual("8912345");
expect(digitsFaToEn("۰۱۲۳۴۵۶۷۸۹")).toEqual("0123456789");
try {
//@ts-ignore
digitsFaToEn(undefined);
} catch (e) {
expect(e.message).toEqual("the input must be string");
}
try {
//@ts-ignore
digitsFaToEn();
} catch (e) {
expect(e.message).toEqual("the input must be string");
}

try {
//@ts-ignore
digitsFaToEn({});
} catch (e) {
expect(e.message).toEqual("the input must be string");
expect(e.message).toEqual("PersianTools: digitsFaToEn - The input must be string");
}
});
});

0 comments on commit a21c69e

Please sign in to comment.