postcss-kazaksha 1.0.2 → 1.2.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.
Files changed (3) hide show
  1. package/index.js +55 -23
  2. package/package.json +1 -1
  3. package/readme.md +2 -2
package/index.js CHANGED
@@ -16,35 +16,67 @@ 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;
27
56
 
28
- module.exports = postcss.plugin('postcss-kazaksha', () => {
29
- return (root) => {
30
- root.walkDecls((decl) => {
31
- const translatedValue = [translateCSSVariables, translateValues]
32
- .reduce((value, transform) => transform(value), decl.value);
33
-
34
- const translatedProp = decl.prop.startsWith('--')
35
- ? translateCustomProperty(decl.prop)
36
- : translateProperty(decl.prop);
37
-
38
- decl.value = translatedValue;
39
- decl.prop = translatedProp;
40
- });
57
+ const processRoot = (root) => {
58
+ root.walkDecls((decl) => {
59
+ const translatedValue = [translateCSSVariables, translateValues]
60
+ .reduce((value, transform) => transform(value), decl.value);
41
61
 
42
- root.walkAtRules((atRule) => {
43
- if (atRule.params) {
44
- atRule.params = translateValues(atRule.params);
45
- }
46
- });
47
- };
62
+ const translatedProp = decl.prop.startsWith('--')
63
+ ? translateCustomProperty(decl.prop)
64
+ : translateProperty(decl.prop);
65
+
66
+ decl.value = translatedValue;
67
+ decl.prop = translatedProp;
68
+ });
69
+
70
+ root.walkAtRules((atRule) => {
71
+ if (atRule.params) {
72
+ atRule.params = translateValues(atRule.params);
73
+ }
74
+ });
75
+ };
76
+
77
+ module.exports = () => ({
78
+ postcssPlugin: 'postcss-kazaksha',
79
+ Once: processRoot
48
80
  });
49
81
 
50
82
  module.exports.postcss = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-kazaksha",
3
- "version": "1.0.2",
3
+ "version": "1.2.0",
4
4
  "description": "PostCSS плагині - қазақша CSS жазып, стандартты CSS-ке аударыңыз",
5
5
  "main": "index.js",
6
6
  "keywords": [
package/readme.md CHANGED
@@ -27,14 +27,14 @@ module.exports = {
27
27
  --екінші-түс: жасыл;
28
28
  }
29
29
 
30
- .батырма {
30
+ .block {
31
31
  артқы-түс: var(--негізгі-түс);
32
32
  түс: ақ;
33
33
  өту: барлық 0.3s;
34
34
  түрлендіру: масштаб(1.1);
35
35
  }
36
36
 
37
- .тор-контейнер {
37
+ .container {
38
38
  көрсету: тор;
39
39
  тор-үлгі-бағандар: қайталау(3, 1fr);
40
40
  орын: 20px;