postcss-kazaksha 1.0.2 → 1.0.3

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.
Files changed (2) hide show
  1. package/index.js +33 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -16,11 +16,40 @@ const translateCSSVariables = (value) =>
16
16
  return translated ? `var(--${translated})` : match;
17
17
  });
18
18
 
19
- const translateValues = (value) =>
20
- Object.keys(dictionary).reduce((acc, kazakh) => {
21
- const regex = new RegExp(`\\b${kazakh}\\b`, 'gi');
22
- return acc.replace(regex, dictionary[kazakh]);
19
+ const isWordChar = (char) => {
20
+ if (!char) return false;
21
+ return /[a-zA-Zа-яА-ЯәіңғүұқөһӘІҢҒҮҰҚӨҺ0-9_-]/.test(char);
22
+ };
23
+
24
+ const isWordBoundary = (str, startPos, endPos) => {
25
+ const before = startPos > 0 ? str[startPos - 1] : '';
26
+ const after = endPos < str.length ? str[endPos] : '';
27
+ return !isWordChar(before) && !isWordChar(after);
28
+ };
29
+
30
+ const translateValues = (value) => {
31
+ const sortedKeys = Object.keys(dictionary).sort((a, b) => b.length - a.length);
32
+
33
+ return sortedKeys.reduce((acc, kazakh) => {
34
+ let result = acc;
35
+ let searchFrom = 0;
36
+
37
+ while (true) {
38
+ const index = result.indexOf(kazakh, searchFrom);
39
+ if (index === -1) break;
40
+
41
+ const endIndex = index + kazakh.length;
42
+ if (isWordBoundary(result, index, endIndex)) {
43
+ result = result.substring(0, index) + dictionary[kazakh] + result.substring(endIndex);
44
+ searchFrom = index + dictionary[kazakh].length;
45
+ } else {
46
+ searchFrom = index + 1;
47
+ }
48
+ }
49
+
50
+ return result;
23
51
  }, value);
52
+ };
24
53
 
25
54
  const translateProperty = (prop) =>
26
55
  dictionary[prop.toLowerCase()] || prop;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-kazaksha",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "PostCSS плагині - қазақша CSS жазып, стандартты CSS-ке аударыңыз",
5
5
  "main": "index.js",
6
6
  "keywords": [