@kashi-ui/persian 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +6 -2
- package/dist/index.js +4 -1
- package/dist/keyboard.d.ts +10 -0
- package/dist/keyboard.js +30 -0
- package/dist/legal-id.d.ts +5 -0
- package/dist/legal-id.js +18 -0
- package/dist/money.d.ts +21 -0
- package/dist/money.js +28 -0
- package/dist/text.d.ts +21 -0
- package/dist/text.js +28 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -9,8 +9,8 @@ export { isValidIban, getBankByIban, formatIban, ibanDigits } from "./iban.js";
|
|
|
9
9
|
export { normalizeMobile, isValidMobile, getMobileOperator, formatMobile } from "./mobile.js";
|
|
10
10
|
export type { Operator } from "./mobile.js";
|
|
11
11
|
export { numberToWords } from "./words.js";
|
|
12
|
-
export { normalizePersian } from "./text.js";
|
|
13
|
-
export type { NormalizeOptions } from "./text.js";
|
|
12
|
+
export { normalizePersian, fixPersianText } from "./text.js";
|
|
13
|
+
export type { NormalizeOptions, FixPersianResult } from "./text.js";
|
|
14
14
|
export { formatRelative } from "./relative.js";
|
|
15
15
|
export type { RelativeOptions } from "./relative.js";
|
|
16
16
|
export { isValidPostalCode, formatPostalCode } from "./postal.js";
|
|
@@ -23,3 +23,7 @@ export { PROVINCES } from "./provinces.js";
|
|
|
23
23
|
export type { Province } from "./provinces.js";
|
|
24
24
|
export { IRAN_FIXED_HOLIDAYS, getIranHoliday } from "./holidays.js";
|
|
25
25
|
export type { HolidayOptions } from "./holidays.js";
|
|
26
|
+
export { isValidLegalId } from "./legal-id.js";
|
|
27
|
+
export { fixKeyboardLayout, looksLikeWrongLayout } from "./keyboard.js";
|
|
28
|
+
export { formatMoney, rialToToman, tomanToRial } from "./money.js";
|
|
29
|
+
export type { FormatMoneyOptions } from "./money.js";
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ export { isValidCardNumber, getBankByCard, formatCardNumber } from "./card.js";
|
|
|
5
5
|
export { isValidIban, getBankByIban, formatIban, ibanDigits } from "./iban.js";
|
|
6
6
|
export { normalizeMobile, isValidMobile, getMobileOperator, formatMobile } from "./mobile.js";
|
|
7
7
|
export { numberToWords } from "./words.js";
|
|
8
|
-
export { normalizePersian } from "./text.js";
|
|
8
|
+
export { normalizePersian, fixPersianText } from "./text.js";
|
|
9
9
|
export { formatRelative } from "./relative.js";
|
|
10
10
|
export { isValidPostalCode, formatPostalCode } from "./postal.js";
|
|
11
11
|
export { PLATE_LETTERS, isValidPlate, formatPlate, plateType } from "./plate.js";
|
|
@@ -13,3 +13,6 @@ export { parseTime, formatTime } from "./time.js";
|
|
|
13
13
|
export { normalizeLandline, getLandlineProvince, isValidLandline, formatLandline } from "./landline.js";
|
|
14
14
|
export { PROVINCES } from "./provinces.js";
|
|
15
15
|
export { IRAN_FIXED_HOLIDAYS, getIranHoliday } from "./holidays.js";
|
|
16
|
+
export { isValidLegalId } from "./legal-id.js";
|
|
17
|
+
export { fixKeyboardLayout, looksLikeWrongLayout } from "./keyboard.js";
|
|
18
|
+
export { formatMoney, rialToToman, tomanToRial } from "./money.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* متنی که با زبان اشتباه صفحهکلید تایپ شده را برمیگرداند: «sghl» → «سلام».
|
|
3
|
+
* to: "fa" کلیدهای لاتین را فارسی میکند، "en" برعکس (مثلاً «لخخلمث» → «google»).
|
|
4
|
+
*/
|
|
5
|
+
export declare function fixKeyboardLayout(text: string, to?: "fa" | "en"): string;
|
|
6
|
+
/**
|
|
7
|
+
* حدس میزند متن لاتین در اصل فارسی بوده که با صفحهکلید انگلیسی تایپ شده است.
|
|
8
|
+
* حدسی است، نه قطعی: کلمههای بدون حرف صدادار یا با علامتهایی مثل ; و ' و [ در وسط کلمه مشکوکاند.
|
|
9
|
+
*/
|
|
10
|
+
export declare function looksLikeWrongLayout(text: string): boolean;
|
package/dist/keyboard.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// چیدمان استاندارد صفحهکلید فارسی (ISIRI 9147) روی کلیدهای QWERTY
|
|
2
|
+
const EN = "qwertyuiop[]\\asdfghjkl;'zxcvbnm,C?H";
|
|
3
|
+
const FA = "ضصثقفغعهخحجچپشسیبلاتنمکگظطزرذدپوژ؟آ";
|
|
4
|
+
const toFa = new Map();
|
|
5
|
+
const toEn = new Map();
|
|
6
|
+
[...EN].forEach((ch, i) => {
|
|
7
|
+
toFa.set(ch, FA[i]);
|
|
8
|
+
if (!toEn.has(FA[i]))
|
|
9
|
+
toEn.set(FA[i], ch);
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* متنی که با زبان اشتباه صفحهکلید تایپ شده را برمیگرداند: «sghl» → «سلام».
|
|
13
|
+
* to: "fa" کلیدهای لاتین را فارسی میکند، "en" برعکس (مثلاً «لخخلمث» → «google»).
|
|
14
|
+
*/
|
|
15
|
+
export function fixKeyboardLayout(text, to = "fa") {
|
|
16
|
+
const map = to === "fa" ? toFa : toEn;
|
|
17
|
+
return [...text].map((ch) => map.get(to === "fa" && ch !== "C" && ch !== "H" ? ch.toLowerCase() : ch) ?? ch).join("");
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* حدس میزند متن لاتین در اصل فارسی بوده که با صفحهکلید انگلیسی تایپ شده است.
|
|
21
|
+
* حدسی است، نه قطعی: کلمههای بدون حرف صدادار یا با علامتهایی مثل ; و ' و [ در وسط کلمه مشکوکاند.
|
|
22
|
+
*/
|
|
23
|
+
export function looksLikeWrongLayout(text) {
|
|
24
|
+
const t = text.trim();
|
|
25
|
+
if (!t || /[\u0600-\u06FF]/.test(t) || !/[a-z]/i.test(t))
|
|
26
|
+
return false;
|
|
27
|
+
const words = t.split(/\s+/).filter((w) => /[a-z]/i.test(w));
|
|
28
|
+
const suspicious = words.filter((w) => /[a-z][;'[\],\\]|[;'[\],\\][a-z]/i.test(w) || (w.length >= 3 && !/[aeiouy]/i.test(w)));
|
|
29
|
+
return suspicious.length > 0 && suspicious.length * 2 >= words.length;
|
|
30
|
+
}
|
package/dist/legal-id.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { onlyDigits } from "./digits.js";
|
|
2
|
+
const COEF = [29, 27, 23, 19, 17, 29, 27, 23, 19, 17];
|
|
3
|
+
/**
|
|
4
|
+
* اعتبارسنجی شناسهٔ ملی اشخاص حقوقی (شرکتها و مؤسسات)، ۱۱ رقم:
|
|
5
|
+
* رقم دهم + ۲ با هر رقم جمع و در ضریبهای ۲۹، ۲۷، ۲۳، ۱۹، ۱۷ ضرب میشود؛ باقیماندهٔ تقسیم بر ۱۱ رقم کنترل است.
|
|
6
|
+
*/
|
|
7
|
+
export function isValidLegalId(value) {
|
|
8
|
+
const n = onlyDigits(value);
|
|
9
|
+
if (!/^\d{11}$/.test(n) || /^0+$/.test(n.slice(3, 9)))
|
|
10
|
+
return false;
|
|
11
|
+
const d = [...n].map(Number);
|
|
12
|
+
const dec = d[9] + 2;
|
|
13
|
+
let sum = 0;
|
|
14
|
+
for (let i = 0; i < 10; i++)
|
|
15
|
+
sum += (d[i] + dec) * COEF[i];
|
|
16
|
+
const r = sum % 11;
|
|
17
|
+
return (r === 10 ? 0 : r) === d[10];
|
|
18
|
+
}
|
package/dist/money.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface FormatMoneyOptions {
|
|
2
|
+
/** واحد نمایش @default "toman" */
|
|
3
|
+
unit?: "toman" | "rial";
|
|
4
|
+
/** واحد مقدار ورودی؛ اگر با unit فرق کند تبدیل میشود (۱ تومان = ۱۰ ریال) @default "toman" */
|
|
5
|
+
from?: "toman" | "rial";
|
|
6
|
+
/** خلاصه با هزار، میلیون، میلیارد و همت: ۱٫۲ میلیون تومان */
|
|
7
|
+
compact?: boolean;
|
|
8
|
+
/** نوشتن واحد پول @default true */
|
|
9
|
+
showUnit?: boolean;
|
|
10
|
+
/** @default "fa" */
|
|
11
|
+
digits?: "fa" | "latin";
|
|
12
|
+
}
|
|
13
|
+
/** ریال به تومان (گرد شده) */
|
|
14
|
+
export declare const rialToToman: (rial: number) => number;
|
|
15
|
+
/** تومان به ریال */
|
|
16
|
+
export declare const tomanToRial: (toman: number) => number;
|
|
17
|
+
/**
|
|
18
|
+
* قالب مبلغ: 1250000 → «۱٬۲۵۰٬۰۰۰ تومان»، و با compact → «۱٫۲۵ میلیون تومان».
|
|
19
|
+
* «همت» (هزار میلیارد تومان) فقط برای تومان استفاده میشود.
|
|
20
|
+
*/
|
|
21
|
+
export declare function formatMoney(amount: number, { unit, from, compact, showUnit, digits }?: FormatMoneyOptions): string;
|
package/dist/money.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { formatNumber, toFaDigits } from "./digits.js";
|
|
2
|
+
const SCALES = [[1e12, "همت"], [1e9, "میلیارد"], [1e6, "میلیون"], [1e3, "هزار"]];
|
|
3
|
+
/** ریال به تومان (گرد شده) */
|
|
4
|
+
export const rialToToman = (rial) => Math.round(rial / 10);
|
|
5
|
+
/** تومان به ریال */
|
|
6
|
+
export const tomanToRial = (toman) => toman * 10;
|
|
7
|
+
/**
|
|
8
|
+
* قالب مبلغ: 1250000 → «۱٬۲۵۰٬۰۰۰ تومان»، و با compact → «۱٫۲۵ میلیون تومان».
|
|
9
|
+
* «همت» (هزار میلیارد تومان) فقط برای تومان استفاده میشود.
|
|
10
|
+
*/
|
|
11
|
+
export function formatMoney(amount, { unit = "toman", from = "toman", compact = false, showUnit = true, digits = "fa" } = {}) {
|
|
12
|
+
let n = from === unit ? amount : from === "rial" ? rialToToman(amount) : tomanToRial(amount);
|
|
13
|
+
const sign = n < 0 ? "−" : "";
|
|
14
|
+
n = Math.abs(n);
|
|
15
|
+
let body = formatNumber(Math.round(n), { digits });
|
|
16
|
+
let unitText = unit === "toman" ? "تومان" : "ریال";
|
|
17
|
+
if (compact) {
|
|
18
|
+
const scale = SCALES.find(([v, name]) => n >= v && (name !== "همت" || unit === "toman"));
|
|
19
|
+
if (scale) {
|
|
20
|
+
const v = Math.round((n / scale[0]) * 100) / 100;
|
|
21
|
+
const text = String(v).replace(".", digits === "fa" ? "٫" : ".");
|
|
22
|
+
body = `${digits === "fa" ? toFaDigits(text) : text} ${scale[1]}`;
|
|
23
|
+
if (scale[1] === "همت")
|
|
24
|
+
unitText = ""; // همت خودش یعنی هزار میلیارد تومان
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return `${sign}${body}${showUnit && unitText ? ` ${unitText}` : ""}`;
|
|
28
|
+
}
|
package/dist/text.d.ts
CHANGED
|
@@ -8,3 +8,24 @@ export interface NormalizeOptions {
|
|
|
8
8
|
* و جمع کردن فاصلهها و نیمفاصلههای تکراری.
|
|
9
9
|
*/
|
|
10
10
|
export declare function normalizePersian(text: string, { digits }?: NormalizeOptions): string;
|
|
11
|
+
export interface FixPersianResult {
|
|
12
|
+
text: string;
|
|
13
|
+
/** نوع اصلاحها با تعداد هر کدام */
|
|
14
|
+
fixes: {
|
|
15
|
+
arabic: number;
|
|
16
|
+
zwnj: number;
|
|
17
|
+
punctuation: number;
|
|
18
|
+
digits: number;
|
|
19
|
+
spacing: number;
|
|
20
|
+
};
|
|
21
|
+
/** جمع همهٔ اصلاحها */
|
|
22
|
+
count: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* ویرایش نگارشی متن فارسی و گزارش تعداد اصلاحها:
|
|
26
|
+
* «ي/ك» عربی، نیمفاصلهٔ «می/نمی» و «ها/های/تر/ترین»، ویرگول و علامت سؤال فارسی،
|
|
27
|
+
* ارقام لاتین داخل متن فارسی و فاصلهٔ اضافهٔ پیش از علامتها.
|
|
28
|
+
*/
|
|
29
|
+
export declare function fixPersianText(text: string, { digits }?: {
|
|
30
|
+
digits?: boolean;
|
|
31
|
+
}): FixPersianResult;
|
package/dist/text.js
CHANGED
|
@@ -21,3 +21,31 @@ export function normalizePersian(text, { digits } = {}) {
|
|
|
21
21
|
out = toEnDigits(out);
|
|
22
22
|
return out;
|
|
23
23
|
}
|
|
24
|
+
const FA_LETTER = "[\\u0621-\\u063A\\u0641-\\u064A\\u067E\\u0686\\u0698\\u06A9\\u06AF\\u06CC]";
|
|
25
|
+
/**
|
|
26
|
+
* ویرایش نگارشی متن فارسی و گزارش تعداد اصلاحها:
|
|
27
|
+
* «ي/ك» عربی، نیمفاصلهٔ «می/نمی» و «ها/های/تر/ترین»، ویرگول و علامت سؤال فارسی،
|
|
28
|
+
* ارقام لاتین داخل متن فارسی و فاصلهٔ اضافهٔ پیش از علامتها.
|
|
29
|
+
*/
|
|
30
|
+
export function fixPersianText(text, { digits = true } = {}) {
|
|
31
|
+
const fixes = { arabic: 0, zwnj: 0, punctuation: 0, digits: 0, spacing: 0 };
|
|
32
|
+
const sub = (s, re, to, key) => s.replace(re, (...m) => {
|
|
33
|
+
fixes[key]++;
|
|
34
|
+
return typeof to === "string" ? to.replace(/\$(\d)/g, (_, i) => m[Number(i)] ?? "") : to(...m);
|
|
35
|
+
});
|
|
36
|
+
const L = FA_LETTER;
|
|
37
|
+
let out = text;
|
|
38
|
+
out = sub(out, /[يى]/g, "ی", "arabic");
|
|
39
|
+
out = sub(out, /ك/g, "ک", "arabic");
|
|
40
|
+
out = sub(out, new RegExp(`(^|[\\s(«])(ن?می) (?=${L})`, "g"), "$1$2\u200c", "zwnj");
|
|
41
|
+
out = sub(out, new RegExp(`(${L}) (ها|های|هایی|هایم|هایت|هایش|تر|ترین)(?=$|[\\s.،؛؟!»)])`, "g"), "$1\u200c$2", "zwnj");
|
|
42
|
+
out = sub(out, new RegExp(`(${L}\\s*),(?=\\s)`, "g"), "$1،", "punctuation");
|
|
43
|
+
out = sub(out, new RegExp(`(${L}\\s*)\\?`, "g"), "$1؟", "punctuation");
|
|
44
|
+
out = sub(out, new RegExp(`(${L}\\s*);`, "g"), "$1؛", "punctuation");
|
|
45
|
+
out = sub(out, / +([،؛؟!.:])(?=\s|$)/g, "$1", "spacing");
|
|
46
|
+
out = sub(out, / {2,}/g, " ", "spacing");
|
|
47
|
+
if (digits)
|
|
48
|
+
out = sub(out, new RegExp(`(${L}[\\s\\u200c]*)(\\d+)|(\\d+)(?=[\\s\\u200c]*${L})`, "g"), (m, pre, d1, d2) => (pre ? pre + toFaDigits(d1) : toFaDigits(d2)), "digits");
|
|
49
|
+
const count = Object.values(fixes).reduce((a, b) => a + b, 0);
|
|
50
|
+
return { text: out, fixes, count };
|
|
51
|
+
}
|