taraskevizer 5.2.6 → 5.3.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.cjs CHANGED
@@ -2003,6 +2003,7 @@ for (const word of gwords)
2003
2003
  wordlist.push([RegExp(word.replace(/ґ/g, "г"), "g"), word]);
2004
2004
 
2005
2005
  // src/tarask.ts
2006
+ var import_diff = require("diff");
2006
2007
  var isUpperCase = (str) => str === str.toUpperCase();
2007
2008
  var getLastLetter = (word, i) => {
2008
2009
  for (let i2 = word.length - 1; i2 >= 0; i2--)
@@ -2094,58 +2095,13 @@ var restoreCase = (text, orig) => {
2094
2095
  }
2095
2096
  return text;
2096
2097
  };
2097
- var highlightChanges = (text, orig, isCyrillic, highlight) => {
2098
- for (let i = 0; i < text.length; i++) {
2099
- const word = text[i];
2100
- const oWord = orig[i];
2101
- if (oWord === word)
2102
- continue;
2103
- const wordH = isCyrillic ? replaceG(word, ($0) => gobj[$0]) : word;
2104
- if (oWord === wordH)
2105
- continue;
2106
- if (!/\(/.test(word)) {
2107
- if (word.length === oWord.length) {
2108
- const wordLetters = word.split("");
2109
- for (let j = 0; j < wordLetters.length; j++) {
2110
- if (wordH[j] !== oWord[j])
2111
- wordLetters[j] = highlight(wordLetters[j]);
2112
- }
2113
- text[i] = wordLetters.join("");
2114
- continue;
2115
- }
2116
- if (isCyrillic) {
2117
- const word1 = word.replace(/ь/g, "");
2118
- switch (oWord) {
2119
- case word1:
2120
- text[i] = word.replace(/ь/g, highlight("ь"));
2121
- continue;
2122
- case word1 + "ь":
2123
- text[i] = word.slice(0, -1).replace(/ь/g, highlight("ь")) + "ь";
2124
- continue;
2125
- }
2126
- }
2127
- }
2128
- const oWordEnd = oWord.length - 1;
2129
- let fromStart = 0;
2130
- let fromWordEnd = word.length - 1;
2131
- let fromOWordEnd = oWordEnd;
2132
- while (wordH[fromStart] === oWord[fromStart])
2133
- ++fromStart;
2134
- while (wordH[fromWordEnd] === oWord[fromOWordEnd]) {
2135
- --fromWordEnd;
2136
- --fromOWordEnd;
2137
- }
2138
- if (oWord.length < word.length) {
2139
- if (fromOWordEnd === oWordEnd) {
2140
- text[i] = highlight(word);
2141
- continue;
2142
- }
2143
- if (fromWordEnd < 0)
2144
- fromWordEnd = 0;
2145
- }
2146
- text[i] = word.slice(0, fromStart) + highlight(word.slice(fromStart, fromWordEnd + 1)) + word.slice(fromWordEnd + 1);
2147
- }
2148
- };
2098
+ var highlightChanges = (text, orig, highlight) => (0, import_diff.diffChars)(orig, text).reduce(
2099
+ (acc, diff) => acc + (diff.removed ? "�" : diff.added ? highlight(diff.value) : diff.value),
2100
+ ""
2101
+ ).replace(
2102
+ /([^<>\x1b])�([^<>\x1b])/g,
2103
+ ($0, $1, $2) => highlight($1 + $2)
2104
+ ).replace(/�/g, "");
2149
2105
  var replaceWithDict = (text, dict = []) => {
2150
2106
  for (const [pattern, result] of dict)
2151
2107
  text = text.replace(
@@ -2199,15 +2155,13 @@ var Taraskevizer = class {
2199
2155
  }
2200
2156
  convert(text) {
2201
2157
  const wrapInColorOf = wrappers.ansiColors;
2202
- const isCyrillic = this.abc === ALPHABET.CYRILLIC;
2203
2158
  const noFixArr = [];
2204
- const { splitted, splittedOrig } = this.process(
2205
- this.prepare(text, noFixArr, "<")
2206
- );
2159
+ text = this.prepare(text, noFixArr, "<");
2160
+ const origInTargetAlphabet = convertAlphabet(text, this.abc);
2161
+ text = this.process(text, origInTargetAlphabet);
2207
2162
  if (this.nonHtml.ansiColors)
2208
- highlightChanges(splitted, splittedOrig, isCyrillic, wrapInColorOf.fix);
2209
- text = join(splitted);
2210
- if (isCyrillic && (this.nonHtml.h || this.nonHtml.ansiColors))
2163
+ text = highlightChanges(text, origInTargetAlphabet, wrapInColorOf.fix);
2164
+ if (this.abc === ALPHABET.CYRILLIC && (this.nonHtml.h || this.nonHtml.ansiColors))
2211
2165
  text = replaceG(
2212
2166
  text,
2213
2167
  this.nonHtml.ansiColors ? this.nonHtml.h ? ($0) => wrapInColorOf.variable(gobj[$0]) : wrapInColorOf.variable("$&") : ($0) => gobj[$0]
@@ -2224,14 +2178,15 @@ var Taraskevizer = class {
2224
2178
  }
2225
2179
  convertToHtml(text) {
2226
2180
  const wrapInTag = wrappers.html;
2227
- const isCyrillic = this.abc === ALPHABET.CYRILLIC;
2228
2181
  const noFixArr = [];
2229
- const { splitted, splittedOrig } = this.process(
2230
- this.prepare(text, noFixArr, "&lt;")
2182
+ text = this.prepare(text, noFixArr, "&lt;");
2183
+ const origInTargetAlphabet = convertAlphabet(text, this.abc);
2184
+ text = highlightChanges(
2185
+ this.process(text, origInTargetAlphabet),
2186
+ origInTargetAlphabet,
2187
+ wrapInTag.fix
2231
2188
  );
2232
- highlightChanges(splitted, splittedOrig, isCyrillic, wrapInTag.fix);
2233
- text = join(splitted);
2234
- if (isCyrillic)
2189
+ if (this.abc === ALPHABET.CYRILLIC)
2235
2190
  text = replaceG(
2236
2191
  text,
2237
2192
  this.html.g ? wrapInTag.letterH("$&") : ($0) => wrapInTag.letterH(gobj[$0])
@@ -2274,9 +2229,9 @@ var Taraskevizer = class {
2274
2229
  "\n"
2275
2230
  );
2276
2231
  }
2277
- process(text) {
2232
+ process(text, textInTargetAlphabet) {
2278
2233
  const { abc, j } = this;
2279
- const splittedOrig = convertAlphabet(text, abc).split(" ");
2234
+ const splittedOrig = textInTargetAlphabet.split(" ");
2280
2235
  text = this.taraskevize(text.toLowerCase());
2281
2236
  if (j && abc !== ALPHABET.LATIN_JI)
2282
2237
  text = replaceIbyJ(text, j === REPLACE_J.ALWAYS);
@@ -2284,7 +2239,7 @@ var Taraskevizer = class {
2284
2239
  let splitted = text.split(" ");
2285
2240
  if (abc !== ALPHABET.ARABIC)
2286
2241
  splitted = restoreCase(splitted, splittedOrig);
2287
- return { splittedOrig, splitted };
2242
+ return join(splitted);
2288
2243
  }
2289
2244
  taraskevize(text) {
2290
2245
  text = replaceWithDict(text, wordlist);
package/dist/index.js CHANGED
@@ -1972,6 +1972,7 @@ for (const word of gwords)
1972
1972
  wordlist.push([RegExp(word.replace(/ґ/g, "г"), "g"), word]);
1973
1973
 
1974
1974
  // src/tarask.ts
1975
+ import { diffChars } from "diff";
1975
1976
  var isUpperCase = (str) => str === str.toUpperCase();
1976
1977
  var getLastLetter = (word, i) => {
1977
1978
  for (let i2 = word.length - 1; i2 >= 0; i2--)
@@ -2063,58 +2064,13 @@ var restoreCase = (text, orig) => {
2063
2064
  }
2064
2065
  return text;
2065
2066
  };
2066
- var highlightChanges = (text, orig, isCyrillic, highlight) => {
2067
- for (let i = 0; i < text.length; i++) {
2068
- const word = text[i];
2069
- const oWord = orig[i];
2070
- if (oWord === word)
2071
- continue;
2072
- const wordH = isCyrillic ? replaceG(word, ($0) => gobj[$0]) : word;
2073
- if (oWord === wordH)
2074
- continue;
2075
- if (!/\(/.test(word)) {
2076
- if (word.length === oWord.length) {
2077
- const wordLetters = word.split("");
2078
- for (let j = 0; j < wordLetters.length; j++) {
2079
- if (wordH[j] !== oWord[j])
2080
- wordLetters[j] = highlight(wordLetters[j]);
2081
- }
2082
- text[i] = wordLetters.join("");
2083
- continue;
2084
- }
2085
- if (isCyrillic) {
2086
- const word1 = word.replace(/ь/g, "");
2087
- switch (oWord) {
2088
- case word1:
2089
- text[i] = word.replace(/ь/g, highlight("ь"));
2090
- continue;
2091
- case word1 + "ь":
2092
- text[i] = word.slice(0, -1).replace(/ь/g, highlight("ь")) + "ь";
2093
- continue;
2094
- }
2095
- }
2096
- }
2097
- const oWordEnd = oWord.length - 1;
2098
- let fromStart = 0;
2099
- let fromWordEnd = word.length - 1;
2100
- let fromOWordEnd = oWordEnd;
2101
- while (wordH[fromStart] === oWord[fromStart])
2102
- ++fromStart;
2103
- while (wordH[fromWordEnd] === oWord[fromOWordEnd]) {
2104
- --fromWordEnd;
2105
- --fromOWordEnd;
2106
- }
2107
- if (oWord.length < word.length) {
2108
- if (fromOWordEnd === oWordEnd) {
2109
- text[i] = highlight(word);
2110
- continue;
2111
- }
2112
- if (fromWordEnd < 0)
2113
- fromWordEnd = 0;
2114
- }
2115
- text[i] = word.slice(0, fromStart) + highlight(word.slice(fromStart, fromWordEnd + 1)) + word.slice(fromWordEnd + 1);
2116
- }
2117
- };
2067
+ var highlightChanges = (text, orig, highlight) => diffChars(orig, text).reduce(
2068
+ (acc, diff) => acc + (diff.removed ? "�" : diff.added ? highlight(diff.value) : diff.value),
2069
+ ""
2070
+ ).replace(
2071
+ /([^<>\x1b])�([^<>\x1b])/g,
2072
+ ($0, $1, $2) => highlight($1 + $2)
2073
+ ).replace(/�/g, "");
2118
2074
  var replaceWithDict = (text, dict = []) => {
2119
2075
  for (const [pattern, result] of dict)
2120
2076
  text = text.replace(
@@ -2168,15 +2124,13 @@ var Taraskevizer = class {
2168
2124
  }
2169
2125
  convert(text) {
2170
2126
  const wrapInColorOf = wrappers.ansiColors;
2171
- const isCyrillic = this.abc === ALPHABET.CYRILLIC;
2172
2127
  const noFixArr = [];
2173
- const { splitted, splittedOrig } = this.process(
2174
- this.prepare(text, noFixArr, "<")
2175
- );
2128
+ text = this.prepare(text, noFixArr, "<");
2129
+ const origInTargetAlphabet = convertAlphabet(text, this.abc);
2130
+ text = this.process(text, origInTargetAlphabet);
2176
2131
  if (this.nonHtml.ansiColors)
2177
- highlightChanges(splitted, splittedOrig, isCyrillic, wrapInColorOf.fix);
2178
- text = join(splitted);
2179
- if (isCyrillic && (this.nonHtml.h || this.nonHtml.ansiColors))
2132
+ text = highlightChanges(text, origInTargetAlphabet, wrapInColorOf.fix);
2133
+ if (this.abc === ALPHABET.CYRILLIC && (this.nonHtml.h || this.nonHtml.ansiColors))
2180
2134
  text = replaceG(
2181
2135
  text,
2182
2136
  this.nonHtml.ansiColors ? this.nonHtml.h ? ($0) => wrapInColorOf.variable(gobj[$0]) : wrapInColorOf.variable("$&") : ($0) => gobj[$0]
@@ -2193,14 +2147,15 @@ var Taraskevizer = class {
2193
2147
  }
2194
2148
  convertToHtml(text) {
2195
2149
  const wrapInTag = wrappers.html;
2196
- const isCyrillic = this.abc === ALPHABET.CYRILLIC;
2197
2150
  const noFixArr = [];
2198
- const { splitted, splittedOrig } = this.process(
2199
- this.prepare(text, noFixArr, "&lt;")
2151
+ text = this.prepare(text, noFixArr, "&lt;");
2152
+ const origInTargetAlphabet = convertAlphabet(text, this.abc);
2153
+ text = highlightChanges(
2154
+ this.process(text, origInTargetAlphabet),
2155
+ origInTargetAlphabet,
2156
+ wrapInTag.fix
2200
2157
  );
2201
- highlightChanges(splitted, splittedOrig, isCyrillic, wrapInTag.fix);
2202
- text = join(splitted);
2203
- if (isCyrillic)
2158
+ if (this.abc === ALPHABET.CYRILLIC)
2204
2159
  text = replaceG(
2205
2160
  text,
2206
2161
  this.html.g ? wrapInTag.letterH("$&") : ($0) => wrapInTag.letterH(gobj[$0])
@@ -2243,9 +2198,9 @@ var Taraskevizer = class {
2243
2198
  "\n"
2244
2199
  );
2245
2200
  }
2246
- process(text) {
2201
+ process(text, textInTargetAlphabet) {
2247
2202
  const { abc, j } = this;
2248
- const splittedOrig = convertAlphabet(text, abc).split(" ");
2203
+ const splittedOrig = textInTargetAlphabet.split(" ");
2249
2204
  text = this.taraskevize(text.toLowerCase());
2250
2205
  if (j && abc !== ALPHABET.LATIN_JI)
2251
2206
  text = replaceIbyJ(text, j === REPLACE_J.ALWAYS);
@@ -2253,7 +2208,7 @@ var Taraskevizer = class {
2253
2208
  let splitted = text.split(" ");
2254
2209
  if (abc !== ALPHABET.ARABIC)
2255
2210
  splitted = restoreCase(splitted, splittedOrig);
2256
- return { splittedOrig, splitted };
2211
+ return join(splitted);
2257
2212
  }
2258
2213
  taraskevize(text) {
2259
2214
  text = replaceWithDict(text, wordlist);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "taraskevizer",
3
- "version": "5.2.6",
3
+ "version": "5.3.0",
4
4
  "author": "GooseOb",
5
5
  "repository": {
6
6
  "type": "git",
@@ -8,14 +8,18 @@
8
8
  },
9
9
  "main": "dist/index.js",
10
10
  "module": "dist/index.js",
11
+ "dependencies": {
12
+ "diff": "^5.2.0"
13
+ },
11
14
  "devDependencies": {
12
- "@digitak/esrun": "^3.2.25",
13
- "@types/node": "^20.10.4",
14
- "bun-types": "^1.0.17",
15
- "husky": "^8.0.3",
16
- "prettier": "^3.1.1",
17
- "tsup": "^7.2.0",
18
- "typescript": "^5.3.3"
15
+ "@digitak/esrun": "^3.2.26",
16
+ "@types/diff": "^5.2.0",
17
+ "@types/node": "^20.12.7",
18
+ "bun-types": "^1.1.4",
19
+ "husky": "^9.0.11",
20
+ "prettier": "^3.2.5",
21
+ "tsup": "^8.0.2",
22
+ "typescript": "^5.4.5"
19
23
  },
20
24
  "bin": {
21
25
  "tarask": "dist/bin.js"
@@ -46,7 +50,7 @@
46
50
  "dev-bun": "bun test --watch",
47
51
  "test": "esrun --send-code-mode=temporaryFile test",
48
52
  "test-cli": "bun run build && esrun --send-code-mode=temporaryFile test",
49
- "prepare": "husky install",
53
+ "prepare": "husky",
50
54
  "typecheck": "tsc --project src/tsconfig.json"
51
55
  },
52
56
  "type": "module",