Skip to content

Commit

Permalink
apply prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
aborazmeh committed Jul 13, 2024
1 parent 6c8f869 commit e23dba0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 44 deletions.
48 changes: 25 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,75 @@
import type { TextlintRuleModule } from "@textlint/types";

export interface Options {
default_numbers?: 'arabic' | 'indic' | 'persian';
default_numbers?: "arabic" | "indic" | "persian";
}

const numbers = [
{
arabic: "0",
indic: "٠",
persian: "۰"
persian: "۰",
},
{
arabic: "1",
indic: "١",
persian: "۱"
persian: "۱",
},
{
arabic: "2",
indic: "٢",
persian: "۲"
persian: "۲",
},
{
arabic: "3",
indic: "٣",
persian: "۳"
persian: "۳",
},
{
arabic: "4",
indic: "٤",
persian: "۴"
persian: "۴",
},
{
arabic: "5",
indic: "٥",
persian: "۵"
persian: "۵",
},
{
arabic: "6",
indic: "٦",
persian: "۶"
persian: "۶",
},
{
arabic: "7",
indic: "٧",
persian: "۷"
persian: "۷",
},
{
arabic: "8",
indic: "٨",
persian: "۸"
persian: "۸",
},
{
arabic: "9",
indic: "٩",
persian: "۹"
}
persian: "۹",
},
];

const report: TextlintRuleModule<Options> = (context, options = {}) => {
const replaceTo = options.default_numbers ?? 'arabic';
const toReplace = ['indic', 'arabic', 'persian'].filter(o => o !== replaceTo);
const replaceTo = options.default_numbers ?? "arabic";
const toReplace = ["indic", "arabic", "persian"].filter((o) => o !== replaceTo);

const { Syntax, RuleError, fixer, report, getSource, locator } = context;
return {
[Syntax.Str](node) { // "Str" node
[Syntax.Str](node) {
// "Str" node
const text = getSource(node); // Get text

numbers.forEach(n => {
toReplace.forEach((numeric_system:string) => {
const matches = text.matchAll(new RegExp(n[numeric_system as keyof typeof n], 'g'));
numbers.forEach((n) => {
toReplace.forEach((numeric_system: string) => {
const matches = text.matchAll(new RegExp(n[numeric_system as keyof typeof n], "g"));
for (const match of matches) {
const index = match.index ?? 0;
const matchRange = [index, index + match[0].length] as const;
Expand All @@ -78,13 +80,13 @@ const report: TextlintRuleModule<Options> = (context, options = {}) => {
});
report(node, ruleError);
}
})
})
}
}
});
});
},
};
};

export default {
linter: report,
fixer: report
fixer: report,
};
42 changes: 21 additions & 21 deletions test/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ tester.run("rule", rule, {
{
text: "Call me on ٥٥٥-٤٣١",
options: {
default_numbers: "indic"
}
default_numbers: "indic",
},
},
{
text: "My number in Persian numbers is ۵۵۵-۴۳۲",
options: {
default_numbers: "persian"
}
}
default_numbers: "persian",
},
},
],
invalid: [
{
Expand All @@ -26,65 +26,65 @@ tester.run("rule", rule, {
errors: [
{
message: "Found indic numbers.",
range: [6, 7]
range: [6, 7],
},
{
message: "Found indic numbers.",
range: [7, 8]
range: [7, 8],
},
]
],
},
{
text: "Call ۴۴۶ for more information",
output: "Call 446 for more information",
errors: [
{
message: "Found persian numbers.",
range: [5, 6]
range: [5, 6],
},
{
message: "Found persian numbers.",
range: [6, 7]
range: [6, 7],
},
{
message: "Found persian numbers.",
range: [7, 8]
range: [7, 8],
},
]
],
},
{
text: "binaries are 1s and 0s",
output: "binaries are ١s and ٠s",
options: {
default_numbers: "indic"
default_numbers: "indic",
},
errors: [
{
message: "Found arabic numbers.",
range: [13, 14]
range: [13, 14],
},
{
message: "Found arabic numbers.",
range: [20, 21]
range: [20, 21],
},
]
],
},
{
text: "binaries are 1s and 0s",
output: "binaries are ۱s and ۰s",
options: {
default_numbers: "persian"
default_numbers: "persian",
},
errors: [
{
message: "Found arabic numbers.",
range: [13, 14]
range: [13, 14],
},
{
message: "Found arabic numbers.",
range: [20, 21]
range: [20, 21],
},
]
],
},
]
],
});

0 comments on commit e23dba0

Please sign in to comment.