postcss-normalize-repeat-style 5.0.3 → 5.1.1

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.3",
3
+ "version": "5.1.1",
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": {
@@ -29,10 +31,5 @@
29
31
  },
30
32
  "peerDependencies": {
31
33
  "postcss": "^8.2.15"
32
- },
33
- "scripts": {
34
- "prebuild": "rimraf dist",
35
- "build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
36
- },
37
- "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"
34
+ }
38
35
  }
@@ -1,83 +1,98 @@
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 = new Set(_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
+ const variableFunctions = new Set(['var', 'env', 'constant']);
25
+ /**
26
+ * @param {valueParser.Node} node
27
+ * @return {boolean}
28
+ */
24
29
  function isVariableFunctionNode(node) {
25
30
  if (node.type !== 'function') {
26
31
  return false;
27
32
  }
28
33
 
29
- return ['var', 'env'].includes(node.value.toLowerCase());
34
+ return variableFunctions.has(node.value.toLowerCase());
30
35
  }
31
36
 
37
+ /**
38
+ * @param {string} value
39
+ * @return {string}
40
+ */
32
41
  function transform(value) {
33
- const parsed = (0, _postcssValueParser.default)(value);
42
+ const parsed = valueParser(value);
34
43
 
35
44
  if (parsed.nodes.length === 1) {
36
45
  return value;
37
46
  }
38
-
47
+ /** @type {{start: number?, end: number?}[]} */
39
48
  const ranges = [];
40
49
  let rangeIndex = 0;
41
50
  let shouldContinue = true;
51
+
42
52
  parsed.nodes.forEach((node, index) => {
43
53
  // After comma (`,`) follows next background
44
54
  if (isCommaNode(node)) {
45
55
  rangeIndex += 1;
46
56
  shouldContinue = true;
57
+
47
58
  return;
48
59
  }
49
60
 
50
61
  if (!shouldContinue) {
51
62
  return;
52
- } // After separator (`/`) follows `background-size` values
53
- // Avoid them
54
-
63
+ }
55
64
 
65
+ // After separator (`/`) follows `background-size` values
66
+ // Avoid them
56
67
  if (node.type === 'div' && node.value === '/') {
57
68
  shouldContinue = false;
69
+
58
70
  return;
59
71
  }
60
72
 
61
73
  if (!ranges[rangeIndex]) {
62
74
  ranges[rangeIndex] = {
63
75
  start: null,
64
- end: null
76
+ end: null,
65
77
  };
66
- } // Do not try to be processed `var and `env` function inside background
67
-
78
+ }
68
79
 
80
+ // Do not try to be processed `var and `env` function inside background
69
81
  if (isVariableFunctionNode(node)) {
70
82
  shouldContinue = false;
71
83
  ranges[rangeIndex].start = null;
72
84
  ranges[rangeIndex].end = null;
85
+
73
86
  return;
74
87
  }
75
88
 
76
- const isRepeatKeyword = node.type === 'word' && repeatKeywords.has(node.value.toLowerCase());
89
+ const isRepeatKeyword =
90
+ node.type === 'word' && repeatKeywords.has(node.value.toLowerCase());
77
91
 
78
92
  if (ranges[rangeIndex].start === null && isRepeatKeyword) {
79
93
  ranges[rangeIndex].start = index;
80
94
  ranges[rangeIndex].end = index;
95
+
81
96
  return;
82
97
  }
83
98
 
@@ -86,68 +101,80 @@ function transform(value) {
86
101
  return;
87
102
  } else if (isRepeatKeyword) {
88
103
  ranges[rangeIndex].end = index;
104
+
89
105
  return;
90
106
  }
91
107
 
92
108
  return;
93
109
  }
94
110
  });
95
- ranges.forEach(range => {
111
+
112
+ ranges.forEach((range) => {
96
113
  if (range.start === null) {
97
114
  return;
98
115
  }
99
116
 
100
- const nodes = parsed.nodes.slice(range.start, range.end + 1);
117
+ const nodes = parsed.nodes.slice(
118
+ range.start,
119
+ /** @type {number} */ (range.end) + 1
120
+ );
101
121
 
102
122
  if (nodes.length !== 3) {
103
123
  return;
104
124
  }
125
+ const key = nodes
126
+ .filter(evenValues)
127
+ .map((n) => n.value.toLowerCase())
128
+ .toString();
105
129
 
106
- const key = nodes.filter(evenValues).map(n => n.value.toLowerCase()).toString();
107
-
108
- const match = _map.default.get(key);
130
+ const match = mappings.get(key);
109
131
 
110
132
  if (match) {
111
133
  nodes[0].value = match;
112
134
  nodes[1].value = nodes[2].value = '';
113
135
  }
114
136
  });
137
+
115
138
  return parsed.toString();
116
139
  }
117
140
 
141
+ /**
142
+ * @type {import('postcss').PluginCreator<void>}
143
+ * @return {import('postcss').Plugin}
144
+ */
118
145
  function pluginCreator() {
119
146
  return {
120
147
  postcssPlugin: 'postcss-normalize-repeat-style',
121
-
122
148
  prepare() {
123
149
  const cache = new Map();
124
150
  return {
125
151
  OnceExit(css) {
126
- css.walkDecls(/^(background(-repeat)?|(-\w+-)?mask-repeat)$/i, decl => {
127
- const value = decl.value;
152
+ css.walkDecls(
153
+ /^(background(-repeat)?|(-\w+-)?mask-repeat)$/i,
154
+ (decl) => {
155
+ const value = decl.value;
128
156
 
129
- if (!value) {
130
- return;
131
- }
157
+ if (!value) {
158
+ return;
159
+ }
132
160
 
133
- if (cache.has(value)) {
134
- decl.value = cache.get(value);
135
- return;
136
- }
161
+ if (cache.has(value)) {
162
+ decl.value = cache.get(value);
137
163
 
138
- const result = transform(value);
139
- decl.value = result;
140
- cache.set(value, result);
141
- });
142
- }
164
+ return;
165
+ }
143
166
 
144
- };
145
- }
167
+ const result = transform(value);
146
168
 
169
+ decl.value = result;
170
+ cache.set(value, result);
171
+ }
172
+ );
173
+ },
174
+ };
175
+ },
147
176
  };
148
177
  }
149
178
 
150
179
  pluginCreator.postcss = true;
151
- var _default = pluginCreator;
152
- exports.default = _default;
153
- module.exports = exports.default;
180
+ 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;