From 454805a9715c33e17bb2bac546375798fd2d335c Mon Sep 17 00:00:00 2001 From: Ali Torki Date: Fri, 7 Feb 2020 14:10:51 +0330 Subject: [PATCH] feat: extracted all test to singel files --- jest.config.js | 2 - src/modules/NumberToWords.ts | 14 +- src/modules/URLfix.ts | 2 +- src/modules/WordsToNumber.ts | 6 +- src/modules/addCommas.ts | 2 +- src/modules/digits.ts | 18 ++- src/modules/getBankNameFromCardNumber.ts | 6 +- src/modules/getPlaceByIranNationalId.ts | 2 +- src/modules/nationalId.ts | 2 +- src/modules/removeCommas.ts | 17 +-- src/modules/verifyCardNumber.ts | 14 +- test/NumberToWords.ts | 8 + test/URLfix.spec.ts | 14 ++ test/WordsToNumber.spec.ts | 15 ++ test/addCommas.spec.ts | 25 ++++ test/digits.spec.ts | 42 ++++++ test/getBankNameFromCardNumber.spec.ts | 9 ++ test/getPlaceByIranNationalId.spec.ts | 18 +++ test/index.spec.js | 177 ----------------------- test/isPersian.spec.ts | 6 + test/removeCommas.spec.ts | 26 ++++ test/sortText.spec.ts | 5 + test/toPersianChars.spec.ts | 5 + test/verifyCardNumber.spec.ts | 7 + test/verifyIranianNationalId.spec.ts | 14 ++ 25 files changed, 239 insertions(+), 217 deletions(-) create mode 100644 test/NumberToWords.ts create mode 100644 test/URLfix.spec.ts create mode 100644 test/WordsToNumber.spec.ts create mode 100644 test/addCommas.spec.ts create mode 100644 test/digits.spec.ts create mode 100644 test/getBankNameFromCardNumber.spec.ts create mode 100644 test/getPlaceByIranNationalId.spec.ts delete mode 100644 test/index.spec.js create mode 100644 test/isPersian.spec.ts create mode 100644 test/removeCommas.spec.ts create mode 100644 test/sortText.spec.ts create mode 100644 test/toPersianChars.spec.ts create mode 100644 test/verifyCardNumber.spec.ts create mode 100644 test/verifyIranianNationalId.spec.ts diff --git a/jest.config.js b/jest.config.js index fa1f7b10..8d0bcb6c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,6 +1,4 @@ module.exports = { preset: "ts-jest", testEnvironment: "node", - modulePathIgnorePatterns: ["dist/.*", "src/.*"], - testPathIgnorePatterns: ["node_modules/", "dist/", "src/"] }; diff --git a/src/modules/NumberToWords.ts b/src/modules/NumberToWords.ts index 0d2773ba..c6c28059 100644 --- a/src/modules/NumberToWords.ts +++ b/src/modules/NumberToWords.ts @@ -76,7 +76,7 @@ class NumberToWords { return result; }; - public convert(number: number): string | undefined { + public convert(number: bigint | number | string): string | undefined { if (!number) return; if (number === 0) { @@ -87,14 +87,14 @@ class NumberToWords { const result: string[] = []; // @ts-ignore - number = removeCommas(number as string); + number = removeCommas(number); const isNegative: boolean = number < 0; - number = isNegative ? number * -1 : number; + number = isNegative ? (number as number) * -1 : number; while (number > 0) { - result.push(this.toWords(number % base)); - number = Math.floor(number / base); + result.push(this.toWords((number as number) % base)); + number = Math.floor((number as number) / base); } if (result.length > 4) { return ""; @@ -116,4 +116,6 @@ class NumberToWords { } } -export default NumberToWords; +const NumberToWordsInstance = new NumberToWords(); + +export default NumberToWordsInstance; diff --git a/src/modules/URLfix.ts b/src/modules/URLfix.ts index 9b8fb58b..7032c325 100644 --- a/src/modules/URLfix.ts +++ b/src/modules/URLfix.ts @@ -5,7 +5,7 @@ * @param {String} value * @return {String} Fixed String */ -export default (value: string): string | undefined => { +export default (value?: string): string | undefined => { if (!value) { return; } diff --git a/src/modules/WordsToNumber.ts b/src/modules/WordsToNumber.ts index 10dc6322..f91db7a0 100644 --- a/src/modules/WordsToNumber.ts +++ b/src/modules/WordsToNumber.ts @@ -10,8 +10,8 @@ interface IUnit { } interface IOption { - digits: string; - addCommas: boolean; + digits?: string; + addCommas?: boolean; } class WordsToNumber { @@ -89,7 +89,7 @@ class WordsToNumber { * @param {String} [digits='en'] [convert number digits to en or fa] * @return {Number} [Result - like: 350000] */ - public convert(words: string, { digits = "en", addCommas = false }: IOption): number | string | undefined { + public convert(words: string, { digits = "en", addCommas = false }: IOption = {}): number | string | undefined { if (!words) return; // @ts-ignore diff --git a/src/modules/addCommas.ts b/src/modules/addCommas.ts index 4e3ed2e3..7075095f 100644 --- a/src/modules/addCommas.ts +++ b/src/modules/addCommas.ts @@ -7,7 +7,7 @@ import { digitsFaToEn } from "./digits"; * @param {number} number Number, like: 300000 * @return {string} Returned String, like: 30,000 */ -const addCommas = (No: number): string | undefined => { +const addCommas = (No?: number): string | undefined => { if (!No) return; const newNo = isPersian("" + No) ? digitsFaToEn("" + No) : No; diff --git a/src/modules/digits.ts b/src/modules/digits.ts index d3d3eb91..a0917983 100644 --- a/src/modules/digits.ts +++ b/src/modules/digits.ts @@ -8,7 +8,7 @@ const arNums = "٠١٢٣٤٥٦٧٨٩"; * Persian digits * */ -export function digitsEnToFa(str: number): string | undefined { +export function digitsEnToFa(str?: number | string): string | undefined { if (!str) return; let result = "" + str; @@ -27,7 +27,7 @@ export function digitsEnToFa(str: number): string | undefined { * Persian digits * */ -export function digitsFaToEn(str: string): number | undefined { +export function digitsFaToEn(str?: string | string): number | undefined { if (!str) return; for (let i = 0; i < 10; i++) { @@ -45,15 +45,16 @@ export function digitsFaToEn(str: string): number | undefined { * digits * */ -export function digitsArToFa(str: string): string | undefined { +export function digitsArToFa(str?: string | number): string | undefined { if (!str) return; + let result = "" + str; for (let i = 0; i < 10; i++) { const replaceArabicToPersian = new RegExp(arNums[i], "g"); - str = str.replace(replaceArabicToPersian, faNums[i]); + result = result.replace(replaceArabicToPersian, faNums[i]); } - return str; + return result; } /** digitsArToEn @@ -63,13 +64,14 @@ export function digitsArToFa(str: string): string | undefined { * digits * */ -export function digitsArToEn(str: string): number | undefined { +export function digitsArToEn(str?: string | number): number | undefined { if (!str) return; + let result = "" + str; for (let i = 0; i < 10; i++) { const replaceArabicToEnglish = new RegExp(arNums[i], "g"); - str = str.replace(replaceArabicToEnglish, "" + i); + result = result.replace(replaceArabicToEnglish, "" + i); } - return Number(str); + return Number(result); } diff --git a/src/modules/getBankNameFromCardNumber.ts b/src/modules/getBankNameFromCardNumber.ts index 19b3461b..a2c181f0 100644 --- a/src/modules/getBankNameFromCardNumber.ts +++ b/src/modules/getBankNameFromCardNumber.ts @@ -5,12 +5,12 @@ interface IBank { name: string; } -function getBankNameFromCardNumber(digits: number): string | null | undefined { +function getBankNameFromCardNumber(digits?: number | string): string | null | undefined { if (!digits) return; if (digits && digits.toString().length === 16) { - let code = digits.toString().substr(0, 6); - let findBank = (banksCode as IBank[]).find(bank => bank.code === code); + const code = digits.toString().substr(0, 6); + const findBank = (banksCode as IBank[]).find(bank => bank.code === code); if (findBank) { return findBank.name; diff --git a/src/modules/getPlaceByIranNationalId.ts b/src/modules/getPlaceByIranNationalId.ts index 3a50f65b..70359031 100644 --- a/src/modules/getPlaceByIranNationalId.ts +++ b/src/modules/getPlaceByIranNationalId.ts @@ -22,7 +22,7 @@ interface IGetPlaceByNationalIId { * @param {String?} nationalId [String of national id - like this: 1111111111] * @return {Object} [If nationalId is valid, function returning an object of details, but nationalId is invalid, return error message] */ -function getPlaceByIranNationalId(nationalId: string): IGetPlaceByNationalIId | null | undefined { +function getPlaceByIranNationalId(nationalId?: string): IGetPlaceByNationalIId | null | undefined { if (!nationalId) return; if (nationalId && nationalId.length === 10) { diff --git a/src/modules/nationalId.ts b/src/modules/nationalId.ts index bae3bd91..868758e4 100644 --- a/src/modules/nationalId.ts +++ b/src/modules/nationalId.ts @@ -4,7 +4,7 @@ * @param {String?} nationalId [String of national id - like this: 1111111111] * @return {Boolean} [valid or no] */ -function verifyIranianNationalId(nationalId: number): boolean | null | undefined { +function verifyIranianNationalId(nationalId?: string | number): boolean | null | undefined { if (!nationalId) return; if (nationalId) { diff --git a/src/modules/removeCommas.ts b/src/modules/removeCommas.ts index 1620c391..fba28245 100644 --- a/src/modules/removeCommas.ts +++ b/src/modules/removeCommas.ts @@ -1,18 +1,17 @@ /** * Remove all commas in String - * @param {[number]} number - * @return {[string]} + * @param {number | string} number + * @return {string | undefined} */ -const removeCommas = (number: string): number | undefined => { - if (!number) { - return; - } +const removeCommas = (number?: string | number): number | undefined => { + if (!number) return; - if (number.toString().indexOf(",") !== -1) { - number = number.replace(/,\s?/g, ""); + let result = "" + number; + if (result.indexOf(",") !== -1) { + result = result.replace(/,\s?/g, ""); } - return typeof number === "number" ? number : parseInt(number, 10); + return typeof result === "number" ? result : parseInt(result, 10); }; export default removeCommas; diff --git a/src/modules/verifyCardNumber.ts b/src/modules/verifyCardNumber.ts index ea18e28a..e4f194e2 100644 --- a/src/modules/verifyCardNumber.ts +++ b/src/modules/verifyCardNumber.ts @@ -1,10 +1,14 @@ -function verifyCardNumber(digits: string): boolean | undefined { +function verifyCardNumber(digits: number): boolean | undefined { if (!digits) return; - digits = digits.toString(); + const digitsResult = "" + digits; - const length = digits.length; - if (length < 16 || parseInt(digits.substr(1, 10), 10) === 0 || parseInt(digits.substr(10, 6), 10) === 0) { + const length = digitsResult.length; + if ( + length < 16 || + parseInt(digitsResult.substr(1, 10), 10) === 0 || + parseInt(digitsResult.substr(10, 6), 10) === 0 + ) { return false; } @@ -12,7 +16,7 @@ function verifyCardNumber(digits: string): boolean | undefined { let even, subDigit; for (let i = 0; i < 16; i++) { even = i % 2 === 0 ? 2 : 1; - subDigit = parseInt(digits.substr(i, 1), 10) * even; + subDigit = parseInt(digitsResult.substr(i, 1), 10) * even; sum += subDigit > 9 ? subDigit - 9 : subDigit; } return sum % 10 === 0; diff --git a/test/NumberToWords.ts b/test/NumberToWords.ts new file mode 100644 index 00000000..fa771121 --- /dev/null +++ b/test/NumberToWords.ts @@ -0,0 +1,8 @@ +import NumberToWords from "../src/modules/NumberToWords"; + +it("NumberToWords", () => { + expect(NumberToWords.convert(500443)).toEqual("پانصد هزار و چهار صد و چهل و سه"); + expect(NumberToWords.convert("500,443")).toEqual("پانصد هزار و چهار صد و چهل و سه"); + expect(NumberToWords.convert(500)).toHaveLength(5); + expect(NumberToWords.convert(30000000000)).toEqual("سی میلیارد"); +}); diff --git a/test/URLfix.spec.ts b/test/URLfix.spec.ts new file mode 100644 index 00000000..c6161e39 --- /dev/null +++ b/test/URLfix.spec.ts @@ -0,0 +1,14 @@ +import URLfix from "../src/modules/URLfix"; + +it("URLfix", () => { + expect( + URLfix( + "https://fa.wikipedia.org/wiki/%D9%85%D8%AF%DB%8C%D8%A7%D9%88%DB%8C%DA%A9%DB%8C:Gadget-Extra-Editbuttons-botworks.js", + ), + ).toEqual("https://fa.wikipedia.org/wiki/مدیاویکی:Gadget-Extra-Editbuttons-botworks.js"); + expect(URLfix("https://en.wikipedia.org/wiki/Persian_alphabet")).toEqual( + "https://en.wikipedia.org/wiki/Persian_alphabet", + ); + expect(URLfix()).toBeUndefined(); + expect(URLfix("Sample Text")).toEqual("Sample Text"); +}); diff --git a/test/WordsToNumber.spec.ts b/test/WordsToNumber.spec.ts new file mode 100644 index 00000000..76676b01 --- /dev/null +++ b/test/WordsToNumber.spec.ts @@ -0,0 +1,15 @@ +import WordsToNumber from "../src/modules/WordsToNumber"; + +it("WordsToNumber", () => { + expect(WordsToNumber.convert("منفی سه هزارمین", { digits: "fa", addCommas: true })).toEqual("-۳,۰۰۰"); + expect(WordsToNumber.convert("منفی سه هزارمین", { digits: "fa" })).toEqual("-۳۰۰۰"); + expect(WordsToNumber.convert("منفی سه هزارمین")).toEqual(-3000); + expect(WordsToNumber.convert("منفی سه هزارم")).toEqual(-3000); + expect(WordsToNumber.convert("منفی سه هزار")).toEqual(-3000); + expect(WordsToNumber.convert("سه هزار دویست و دوازده")).toEqual(3212); + expect(WordsToNumber.convert("منفی سه هزارمین")).not.toEqual("-3000"); + expect(String(WordsToNumber.convert("منفی سه هزارمین"))).toHaveLength(5); + + expect(WordsToNumber.convert("دوازده هزار بیست دو")).toEqual(12022); + expect(WordsToNumber.convert("دوازده هزار بیست دو", { addCommas: true })).toEqual("12,022"); +}); diff --git a/test/addCommas.spec.ts b/test/addCommas.spec.ts new file mode 100644 index 00000000..ffcbbb07 --- /dev/null +++ b/test/addCommas.spec.ts @@ -0,0 +1,25 @@ +import addCommas from "../src/modules/addCommas"; + +expect.extend({ + toBeType(received, argument) { + const initialType = typeof received; + const type = initialType === "object" ? (Array.isArray(received) ? "array" : initialType) : initialType; + return type === argument + ? { + message: () => `expected ${received} to be type ${argument}`, + pass: true, + } + : { + message: () => `expected ${received} to be type ${argument}`, + pass: false, + }; + }, +}); + +it("Add and remove commas", () => { + expect(addCommas(30000000)).toEqual("30,000,000"); + expect(addCommas(300)).toEqual("300"); + // @ts-ignore + expect(addCommas(3000)).toBeType("string"); + expect(addCommas()).toBeUndefined(); +}); diff --git a/test/digits.spec.ts b/test/digits.spec.ts new file mode 100644 index 00000000..ac51c285 --- /dev/null +++ b/test/digits.spec.ts @@ -0,0 +1,42 @@ +import { digitsEnToFa } from "../src/modules/digits"; +import { digitsFaToEn } from "../src/modules/digits"; +import { digitsArToFa } from "../src/modules/digits"; +import { digitsArToEn } from "../src/modules/digits"; + +describe("Digits", () => { + it("digitsArToFa", () => { + expect(digitsArToFa("٠١٢٣٤٥٦٧٨٩")).toEqual("۰۱۲۳۴۵۶۷۸۹"); + expect(digitsArToFa("۸۹123۴۵")).toEqual("۸۹123۴۵"); + expect(digitsArToFa(456128)).toEqual("456128"); + expect(digitsArToFa()).toBeUndefined(); + expect(digitsArToFa("")).toBeUndefined(); + expect(digitsArToFa("Text ٠١٢٣٤٥٦٧٨٩")).toEqual("Text ۰۱۲۳۴۵۶۷۸۹"); + }); + + it("digitsArToEn", () => { + expect(digitsArToEn("٠١٢٣٤٥٦٧٨٩")).toEqual("0123456789"); + expect(digitsArToEn("٨٩123٤٥")).toEqual("8912345"); + expect(digitsArToEn(456128)).toEqual("456128"); + + expect(digitsArToEn()).toBeUndefined(); + expect(digitsArToEn("")).toBeUndefined(); + + expect(digitsArToEn("Text ٠١٢٣٤٥٦٧٨٩")).toEqual("Text 0123456789"); + }); + + it("digitsEnToFa", () => { + expect(digitsEnToFa("123۴۵۶")).toEqual("۱۲۳۴۵۶"); + expect(digitsEnToFa("٤٥٦")).toEqual("٤٥٦"); + expect(digitsEnToFa("123۴۵۶")).toEqual("۱۲۳۴۵۶"); + expect(digitsEnToFa()).toBeUndefined(); + expect(digitsEnToFa("")).toBeUndefined(); + }); + + it("digitsFaToEn", () => { + expect(digitsFaToEn("123۴۵۶")).toEqual("123456"); + expect(digitsFaToEn("۸۹123۴۵")).toEqual("8912345"); + expect(digitsFaToEn("۰۱۲۳۴۵۶۷۸۹")).toEqual("0123456789"); + expect(digitsFaToEn()).toBeUndefined(); + expect(digitsFaToEn("")).toBeUndefined(); + }); +}); diff --git a/test/getBankNameFromCardNumber.spec.ts b/test/getBankNameFromCardNumber.spec.ts new file mode 100644 index 00000000..74259bde --- /dev/null +++ b/test/getBankNameFromCardNumber.spec.ts @@ -0,0 +1,9 @@ +import getBankNameFromCardNumber from "../src/modules/getBankNameFromCardNumber"; + +it("Get the name of the bank by bank account number", () => { + expect(getBankNameFromCardNumber(6037701689095443)).toEqual("بانک کشاورزی"); + expect(getBankNameFromCardNumber(6219861034529007)).toEqual("بانک سامان"); + expect(getBankNameFromCardNumber("6219861034529007")).toEqual("بانک سامان"); + + expect(getBankNameFromCardNumber()).toBeUndefined(); +}); diff --git a/test/getPlaceByIranNationalId.spec.ts b/test/getPlaceByIranNationalId.spec.ts new file mode 100644 index 00000000..f44b1bb1 --- /dev/null +++ b/test/getPlaceByIranNationalId.spec.ts @@ -0,0 +1,18 @@ +import getPlaceByIranNationalId from "../src/modules/getPlaceByIranNationalId"; + +it("Get the city and province name by national code", () => { + expect(getPlaceByIranNationalId("0499370899").city).toEqual("شهرری"); + expect(getPlaceByIranNationalId("0790419904").city).toEqual("سبزوار"); + expect(getPlaceByIranNationalId("0084575948").city).toEqual("تهران مرکزی"); + expect(getPlaceByIranNationalId("0060495219").city).toEqual("تهران مرکزی"); + expect(getPlaceByIranNationalId("0671658506").city).toEqual("بجنورد"); + expect(getPlaceByIranNationalId("0671658506").city).toEqual("بجنورد"); + expect(getPlaceByIranNationalId("0643005846").city).toEqual("بیرجند"); + expect(getPlaceByIranNationalId("0906582709").city).toEqual("کاشمر"); + expect(getPlaceByIranNationalId("0451727304").city).toEqual("شمیران"); + expect(getPlaceByIranNationalId("0371359058").city).toEqual("قم"); + + expect(getPlaceByIranNationalId("0084545943").city).toEqual("تهران مرکزی"); + + expect(getPlaceByIranNationalId()).toBeUndefined(); +}); diff --git a/test/index.spec.js b/test/index.spec.js deleted file mode 100644 index 92e2971a..00000000 --- a/test/index.spec.js +++ /dev/null @@ -1,177 +0,0 @@ -import isPersian from '../src/modules/isPersian'; -import toPersianChars from '../src/modules/toPersianChars'; - -import WordsToNumber from '../src/modules/WordsToNumber'; -import NumberToWords from '../src/modules/NumberToWords'; - -import verifyCardNumber from '../src/modules/verifyCardNumber'; -import getBankNameFromCardNumber from '../src/modules/getBankNameFromCardNumber'; - -import {digitsEnToFa} from '../src/modules/digits'; -import {digitsFaToEn} from '../src/modules/digits'; -import {digitsArToFa} from '../src/modules/digits'; -import {digitsArToEn} from '../src/modules/digits'; - -import addCommas from '../src/modules/addCommas'; -import removeCommas from '../src/modules/removeCommas'; - -import URLfix from '../src/modules/URLfix'; -import SortText from '../src/modules/SortText'; - -import verifyIranianNationalId from '../src/modules/nationalId'; -import getPlaceByIranNationalId from '../src/modules/getPlaceByIranNationalId'; - -expect.extend({ - toBeType(received, argument) { - const initialType = typeof received; - const type = initialType === "object" ? Array.isArray(received) ? "array" : initialType : initialType; - return type === argument ? { - message: () => `expected ${received} to be type ${argument}`, - pass: true - } : { - message: () => `expected ${received} to be type ${argument}`, - pass: false - }; - } -}); - - -describe('Persian Tools', () => { - it('WordsToNumber', () => { - let wordFn = new WordsToNumber(); - - expect(wordFn.convert('منفی سه هزارمین', { digits: 'fa', addCommas: true })).toEqual("-۳,۰۰۰"); - expect(wordFn.convert('منفی سه هزارمین', { digits: 'fa'})).toEqual("-۳۰۰۰"); - expect(wordFn.convert('منفی سه هزارمین')).toEqual(-3000); - expect(wordFn.convert('منفی سه هزارم')).toEqual(-3000); - expect(wordFn.convert('منفی سه هزار')).toEqual(-3000); - expect(wordFn.convert('سه هزار دویست و دوازده')).toEqual(3212); - expect(wordFn.convert('منفی سه هزارمین')).not.toEqual("-3000"); - expect(String(wordFn.convert('منفی سه هزارمین'))).toHaveLength(5); - - expect(wordFn.convert('دوازده هزار بیست دو')).toEqual(12022) - expect(wordFn.convert('دوازده هزار بیست دو', {addCommas: true})).toEqual("12,022") - }); - - it('NumberToWords', () => { - expect(NumberToWords(500443)).toEqual("پانصد هزار و چهار صد و چهل و سه"); - expect(NumberToWords("500,443")).toEqual("پانصد هزار و چهار صد و چهل و سه"); - expect(NumberToWords(500)).toHaveLength(5); - expect(NumberToWords(30000000000)).toEqual("سی میلیارد"); - }); - - it('isPersian', () => { - expect(isPersian("این یک متن فارسی است؟")).not.toBeFalsy(); - expect(isPersian("Lorem Ipsum Test")).toBeFalsy(); - }); - - it('toPersianChars', () => { - expect(toPersianChars("علي")).toEqual("علی"); - }); - - it('digitsArToFa', () => { - expect(digitsArToFa("٠١٢٣٤٥٦٧٨٩")).toEqual("۰۱۲۳۴۵۶۷۸۹"); - expect(digitsArToFa("۸۹123۴۵")).toEqual("۸۹123۴۵"); - expect(digitsArToFa(456128)).toEqual("456128"); - expect(digitsArToFa()).toBeUndefined(); - expect(digitsArToFa("")).toBeUndefined(); - expect(digitsArToFa("Text ٠١٢٣٤٥٦٧٨٩")).toEqual("Text ۰۱۲۳۴۵۶۷۸۹"); - }); - - - it('digitsArToEn', () => { - expect(digitsArToEn("٠١٢٣٤٥٦٧٨٩")).toEqual("0123456789"); - expect(digitsArToEn("٨٩123٤٥")).toEqual("8912345"); - expect(digitsArToEn(456128)).toEqual("456128"); - - expect(digitsArToEn()).toBeUndefined(); - expect(digitsArToEn("")).toBeUndefined(); - - expect(digitsArToEn("Text ٠١٢٣٤٥٦٧٨٩")).toEqual("Text 0123456789"); - }); - - it('digitsEnToFa', () => { - expect(digitsEnToFa("123۴۵۶")).toEqual("۱۲۳۴۵۶"); - expect(digitsEnToFa("٤٥٦")).toEqual("٤٥٦"); - expect(digitsEnToFa("123۴۵۶")).toEqual("۱۲۳۴۵۶"); - expect(digitsEnToFa()).toBeUndefined(); - expect(digitsEnToFa("")).toBeUndefined(); - }); - - it('digitsFaToEn', () => { - expect(digitsFaToEn("123۴۵۶")).toEqual("123456"); - expect(digitsFaToEn("۸۹123۴۵")).toEqual("8912345"); - expect(digitsFaToEn("۰۱۲۳۴۵۶۷۸۹")).toEqual("0123456789"); - expect(digitsFaToEn()).toBeUndefined(); - expect(digitsFaToEn("")).toBeUndefined(); - }); - - it('URLfix', () => { - expect(URLfix("https://fa.wikipedia.org/wiki/%D9%85%D8%AF%DB%8C%D8%A7%D9%88%DB%8C%DA%A9%DB%8C:Gadget-Extra-Editbuttons-botworks.js")).toEqual("https://fa.wikipedia.org/wiki/مدیاویکی:Gadget-Extra-Editbuttons-botworks.js"); - expect(URLfix("https://en.wikipedia.org/wiki/Persian_alphabet")).toEqual("https://en.wikipedia.org/wiki/Persian_alphabet"); - expect(URLfix()).toBeUndefined(); - expect(URLfix("Sample Text")).toEqual("Sample Text"); - }); - - it('SortText', () => { - expect(SortText("سلام علی ترکی")).toEqual(["ترکی", "سلام", "علی"]); - }); - - it('Bank number validation', () => { - expect(verifyCardNumber(6037701689095443)).not.toBeFalsy(); - expect(verifyCardNumber(6219861034529007)).not.toBeFalsy(); - expect(verifyCardNumber(6219861034529008)).toBeFalsy(); - }); - - it("Get the name of the bank by bank account number", () => { - expect(getBankNameFromCardNumber(6037701689095443)).toEqual("بانک کشاورزی"); - expect(getBankNameFromCardNumber(6219861034529007)).toEqual("بانک سامان"); - expect(getBankNameFromCardNumber("6219861034529007")).toEqual("بانک سامان"); - - expect(getBankNameFromCardNumber()).toBeUndefined(); - }); - - it("Validation of Iranian National Number(code-e Melli)", () => { - expect(verifyIranianNationalId("0499370899")).not.toBeFalsy(); - expect(verifyIranianNationalId("0790419904")).not.toBeFalsy(); - expect(verifyIranianNationalId("0084575948")).not.toBeFalsy(); - expect(verifyIranianNationalId("0963695398")).not.toBeFalsy(); - expect(verifyIranianNationalId("0684159414")).not.toBeFalsy(); - expect(verifyIranianNationalId("0067749828")).not.toBeFalsy(); - - expect(verifyIranianNationalId("0684159415")).toBeFalsy(); - - expect(verifyIranianNationalId()).toBeUndefined(); - }); - - it("Get the city and province name by national code", () => { - expect(getPlaceByIranNationalId("0499370899").city).toEqual("شهرری"); - expect(getPlaceByIranNationalId("0790419904").city).toEqual("سبزوار"); - expect(getPlaceByIranNationalId("0084575948").city).toEqual("تهران مرکزی"); - expect(getPlaceByIranNationalId("0060495219").city).toEqual("تهران مرکزی"); - expect(getPlaceByIranNationalId("0671658506").city).toEqual("بجنورد"); - expect(getPlaceByIranNationalId("0671658506").city).toEqual("بجنورد"); - expect(getPlaceByIranNationalId("0643005846").city).toEqual("بیرجند"); - expect(getPlaceByIranNationalId("0906582709").city).toEqual("کاشمر"); - expect(getPlaceByIranNationalId("0451727304").city).toEqual("شمیران"); - expect(getPlaceByIranNationalId("0371359058").city).toEqual("قم"); - - expect(getPlaceByIranNationalId("0084545943").city).toEqual("تهران مرکزی"); - - expect(getPlaceByIranNationalId()).toBeUndefined(); - }); - - - it("Add and remove commas", () => { - expect(addCommas(30000000)).toEqual("30,000,000"); - expect(addCommas(300)).toEqual("300"); - expect(addCommas(3000)).toBeType("string"); - expect(addCommas()).toBeUndefined(); - - expect(removeCommas("30,000,000")).toEqual(30000000); - expect(removeCommas(300)).toEqual(300); - expect(removeCommas("300")).toEqual(300); - expect(removeCommas("3000")).toBeType("number"); - expect(removeCommas()).toBeUndefined(); - }); -}) diff --git a/test/isPersian.spec.ts b/test/isPersian.spec.ts new file mode 100644 index 00000000..95bbcdd5 --- /dev/null +++ b/test/isPersian.spec.ts @@ -0,0 +1,6 @@ +import isPersian from "../src/modules/isPersian"; + +it("isPersian", () => { + expect(isPersian("این یک متن فارسی است؟")).not.toBeFalsy(); + expect(isPersian("Lorem Ipsum Test")).toBeFalsy(); +}); diff --git a/test/removeCommas.spec.ts b/test/removeCommas.spec.ts new file mode 100644 index 00000000..24e053a8 --- /dev/null +++ b/test/removeCommas.spec.ts @@ -0,0 +1,26 @@ +import removeCommas from "../src/modules/removeCommas"; + +expect.extend({ + toBeType(received, argument) { + const initialType = typeof received; + const type = initialType === "object" ? (Array.isArray(received) ? "array" : initialType) : initialType; + return type === argument + ? { + message: () => `expected ${received} to be type ${argument}`, + pass: true, + } + : { + message: () => `expected ${received} to be type ${argument}`, + pass: false, + }; + }, +}); + +it("Remove commas", () => { + expect(removeCommas("30,000,000")).toEqual(30000000); + expect(removeCommas(300)).toEqual(300); + expect(removeCommas("300")).toEqual(300); + // @ts-ignore + expect(removeCommas("3000")).toBeType("number"); + expect(removeCommas()).toBeUndefined(); +}); diff --git a/test/sortText.spec.ts b/test/sortText.spec.ts new file mode 100644 index 00000000..98e125d8 --- /dev/null +++ b/test/sortText.spec.ts @@ -0,0 +1,5 @@ +import SortText from "../src/modules/SortText"; + +it("SortText", () => { + expect(SortText("سلام علی ترکی")).toEqual(["ترکی", "سلام", "علی"]); +}); diff --git a/test/toPersianChars.spec.ts b/test/toPersianChars.spec.ts new file mode 100644 index 00000000..09d1827a --- /dev/null +++ b/test/toPersianChars.spec.ts @@ -0,0 +1,5 @@ +import toPersianChars from "../src/modules/toPersianChars"; + +it("toPersianChars", () => { + expect(toPersianChars("علي")).toEqual("علی"); +}); diff --git a/test/verifyCardNumber.spec.ts b/test/verifyCardNumber.spec.ts new file mode 100644 index 00000000..4506f952 --- /dev/null +++ b/test/verifyCardNumber.spec.ts @@ -0,0 +1,7 @@ +import verifyCardNumber from "../src/modules/verifyCardNumber"; + +it("Bank number validation", () => { + expect(verifyCardNumber(6037701689095443)).not.toBeFalsy(); + expect(verifyCardNumber(6219861034529007)).not.toBeFalsy(); + expect(verifyCardNumber(6219861034529008)).toBeFalsy(); +}); diff --git a/test/verifyIranianNationalId.spec.ts b/test/verifyIranianNationalId.spec.ts new file mode 100644 index 00000000..931ad968 --- /dev/null +++ b/test/verifyIranianNationalId.spec.ts @@ -0,0 +1,14 @@ +import verifyIranianNationalId from "../src/modules/nationalId"; + +it("Validation of Iranian National Number(code-e Melli)", () => { + expect(verifyIranianNationalId("0499370899")).not.toBeFalsy(); + expect(verifyIranianNationalId("0790419904")).not.toBeFalsy(); + expect(verifyIranianNationalId("0084575948")).not.toBeFalsy(); + expect(verifyIranianNationalId("0963695398")).not.toBeFalsy(); + expect(verifyIranianNationalId("0684159414")).not.toBeFalsy(); + expect(verifyIranianNationalId("0067749828")).not.toBeFalsy(); + + expect(verifyIranianNationalId("0684159415")).toBeFalsy(); + + expect(verifyIranianNationalId()).toBeUndefined(); +});