postcss-normalize-repeat-style 5.0.2 → 5.1.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/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "postcss-normalize-repeat-style",
3
- "version": "5.0.2",
3
+ "version": "5.1.0",
4
4
  "description": "Convert two value syntax for repeat-style into one value.",
5
- "main": "dist/index.js",
5
+ "main": "src/index.js",
6
+ "types": "types/index.d.ts",
6
7
  "files": [
7
8
  "LICENSE-MIT",
8
- "dist"
9
+ "src",
10
+ "types"
9
11
  ],
10
12
  "license": "MIT",
11
13
  "dependencies": {
@@ -30,9 +32,5 @@
30
32
  "peerDependencies": {
31
33
  "postcss": "^8.2.15"
32
34
  },
33
- "scripts": {
34
- "prebuild": "rimraf dist",
35
- "build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
36
- },
37
35
  "readme": "# [postcss][postcss]-normalize-repeat-style\n\n> Normalize repeat styles with PostCSS.\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-normalize-repeat-style) do:\n\n```\nnpm install postcss-normalize-repeat-style --save\n```\n\n## Example\n\n### Input\n\n```css\nh1 {\n background: url(image.jpg) repeat no-repeat\n}\n```\n\n### Output\n\n```css\nh1 {\n background: url(image.jpg) repeat-x\n}\n```\n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n## License\n\nMIT © [Ben Briggs](http://beneb.info)\n\n[postcss]: https://github.com/postcss/postcss\n"
38
36
  }
@@ -1,26 +1,30 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
9
-
10
- var _map = _interopRequireDefault(require("./lib/map"));
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
- function evenValues(list, index) {
1
+ 'use strict';
2
+ const valueParser = require('postcss-value-parser');
3
+ const mappings = require('./lib/map');
4
+
5
+ /**
6
+ * @param {unknown} item
7
+ * @param {number} index
8
+ * @return {boolean}
9
+ */
10
+ function evenValues(item, index) {
15
11
  return index % 2 === 0;
16
12
  }
17
13
 
18
- const repeatKeywords = [..._map.default.values()];
14
+ const repeatKeywords = new Set(mappings.values());
19
15
 
16
+ /**
17
+ * @param {valueParser.Node} node
18
+ * @return {boolean}
19
+ */
20
20
  function isCommaNode(node) {
21
21
  return node.type === 'div' && node.value === ',';
22
22
  }
23
23
 
24
+ /**
25
+ * @param {valueParser.Node} node
26
+ * @return {boolean}
27
+ */
24
28
  function isVariableFunctionNode(node) {
25
29
  if (node.type !== 'function') {
26
30
  return false;
@@ -29,55 +33,65 @@ function isVariableFunctionNode(node) {
29
33
  return ['var', 'env'].includes(node.value.toLowerCase());
30
34
  }
31
35
 
36
+ /**
37
+ * @param {string} value
38
+ * @return {string}
39
+ */
32
40
  function transform(value) {
33
- const parsed = (0, _postcssValueParser.default)(value);
41
+ const parsed = valueParser(value);
34
42
 
35
43
  if (parsed.nodes.length === 1) {
36
44
  return value;
37
45
  }
38
-
46
+ /** @type {{start: number?, end: number?}[]} */
39
47
  const ranges = [];
40
48
  let rangeIndex = 0;
41
49
  let shouldContinue = true;
50
+
42
51
  parsed.nodes.forEach((node, index) => {
43
52
  // After comma (`,`) follows next background
44
53
  if (isCommaNode(node)) {
45
54
  rangeIndex += 1;
46
55
  shouldContinue = true;
56
+
47
57
  return;
48
58
  }
49
59
 
50
60
  if (!shouldContinue) {
51
61
  return;
52
- } // After separator (`/`) follows `background-size` values
53
- // Avoid them
54
-
62
+ }
55
63
 
64
+ // After separator (`/`) follows `background-size` values
65
+ // Avoid them
56
66
  if (node.type === 'div' && node.value === '/') {
57
67
  shouldContinue = false;
68
+
58
69
  return;
59
70
  }
60
71
 
61
72
  if (!ranges[rangeIndex]) {
62
73
  ranges[rangeIndex] = {
63
74
  start: null,
64
- end: null
75
+ end: null,
65
76
  };
66
- } // Do not try to be processed `var and `env` function inside background
67
-
77
+ }
68
78
 
79
+ // Do not try to be processed `var and `env` function inside background
69
80
  if (isVariableFunctionNode(node)) {
70
81
  shouldContinue = false;
71
82
  ranges[rangeIndex].start = null;
72
83
  ranges[rangeIndex].end = null;
84
+
73
85
  return;
74
86
  }
75
87
 
76
- const isRepeatKeyword = node.type === 'word' && repeatKeywords.includes(node.value.toLowerCase());
88
+ const isRepeatKeyword =
89
+ node.type === 'word' && repeatKeywords.has(node.value.toLowerCase());
77
90
 
78
91
  if (ranges[rangeIndex].start === null && isRepeatKeyword) {
79
92
  ranges[rangeIndex].start = index;
80
93
  ranges[rangeIndex].end = index;
94
+
81
95
  return;
82
96
  }
83
97
 
@@ -86,68 +100,80 @@ function transform(value) {
86
100
  return;
87
101
  } else if (isRepeatKeyword) {
88
102
  ranges[rangeIndex].end = index;
103
+
89
104
  return;
90
105
  }
91
106
 
92
107
  return;
93
108
  }
94
109
  });
95
- ranges.forEach(range => {
110
+
111
+ ranges.forEach((range) => {
96
112
  if (range.start === null) {
97
113
  return;
98
114
  }
99
115
 
100
- const nodes = parsed.nodes.slice(range.start, range.end + 1);
116
+ const nodes = parsed.nodes.slice(
117
+ range.start,
118
+ /** @type {number} */ (range.end) + 1
119
+ );
101
120
 
102
121
  if (nodes.length !== 3) {
103
122
  return;
104
123
  }
124
+ const key = nodes
125
+ .filter(evenValues)
126
+ .map((n) => n.value.toLowerCase())
127
+ .toString();
105
128
 
106
- const key = nodes.filter(evenValues).map(n => n.value.toLowerCase()).toString();
107
-
108
- const match = _map.default.get(key);
129
+ const match = mappings.get(key);
109
130
 
110
131
  if (match) {
111
132
  nodes[0].value = match;
112
133
  nodes[1].value = nodes[2].value = '';
113
134
  }
114
135
  });
136
+
115
137
  return parsed.toString();
116
138
  }
117
139
 
140
+ /**
141
+ * @type {import('postcss').PluginCreator<void>}
142
+ * @return {import('postcss').Plugin}
143
+ */
118
144
  function pluginCreator() {
119
145
  return {
120
146
  postcssPlugin: 'postcss-normalize-repeat-style',
121
-
122
147
  prepare() {
123
148
  const cache = new Map();
124
149
  return {
125
150
  OnceExit(css) {
126
- css.walkDecls(/^(background(-repeat)?|(-\w+-)?mask-repeat)$/i, decl => {
127
- const value = decl.value;
151
+ css.walkDecls(
152
+ /^(background(-repeat)?|(-\w+-)?mask-repeat)$/i,
153
+ (decl) => {
154
+ const value = decl.value;
128
155
 
129
- if (!value) {
130
- return;
131
- }
156
+ if (!value) {
157
+ return;
158
+ }
132
159
 
133
- if (cache.has(value)) {
134
- decl.value = cache.get(value);
135
- return;
136
- }
160
+ if (cache.has(value)) {
161
+ decl.value = cache.get(value);
137
162
 
138
- const result = transform(value);
139
- decl.value = result;
140
- cache.set(value, result);
141
- });
142
- }
163
+ return;
164
+ }
143
165
 
144
- };
145
- }
166
+ const result = transform(value);
146
167
 
168
+ decl.value = result;
169
+ cache.set(value, result);
170
+ }
171
+ );
172
+ },
173
+ };
174
+ },
147
175
  };
148
176
  }
149
177
 
150
178
  pluginCreator.postcss = true;
151
- var _default = pluginCreator;
152
- exports.default = _default;
153
- module.exports = exports.default;
179
+ module.exports = pluginCreator;
package/src/lib/map.js ADDED
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+ module.exports = new Map([
3
+ [['repeat', 'no-repeat'].toString(), 'repeat-x'],
4
+ [['no-repeat', 'repeat'].toString(), 'repeat-y'],
5
+ [['repeat', 'repeat'].toString(), 'repeat'],
6
+ [['space', 'space'].toString(), 'space'],
7
+ [['round', 'round'].toString(), 'round'],
8
+ [['no-repeat', 'no-repeat'].toString(), 'no-repeat'],
9
+ ]);
@@ -0,0 +1,9 @@
1
+ export = pluginCreator;
2
+ /**
3
+ * @type {import('postcss').PluginCreator<void>}
4
+ * @return {import('postcss').Plugin}
5
+ */
6
+ declare function pluginCreator(): import('postcss').Plugin;
7
+ declare namespace pluginCreator {
8
+ const postcss: true;
9
+ }
@@ -0,0 +1,15 @@
1
+ declare const _exports: {
2
+ clear(): void;
3
+ delete(key: string): boolean;
4
+ forEach(callbackfn: (value: string, key: string, map: Map<string, string>) => void, thisArg?: any): void;
5
+ get(key: string): string | undefined;
6
+ has(key: string): boolean;
7
+ set(key: string, value: string): Map<string, string>;
8
+ readonly size: number;
9
+ entries(): IterableIterator<[string, string]>;
10
+ keys(): IterableIterator<string>;
11
+ values(): IterableIterator<string>;
12
+ [Symbol.iterator](): IterableIterator<[string, string]>;
13
+ readonly [Symbol.toStringTag]: string;
14
+ };
15
+ export = _exports;
package/dist/lib/map.js DELETED
@@ -1,11 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _default = new Map([[['repeat', 'no-repeat'].toString(), 'repeat-x'], [['no-repeat', 'repeat'].toString(), 'repeat-y'], [['repeat', 'repeat'].toString(), 'repeat'], [['space', 'space'].toString(), 'space'], [['round', 'round'].toString(), 'round'], [['no-repeat', 'no-repeat'].toString(), 'no-repeat']]);
9
-
10
- exports.default = _default;
11
- module.exports = exports.default;