Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(postcss-zindex): refactor to drop dependencies #1096

Merged
merged 3 commits into from May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/postcss-zindex/package.json
Expand Up @@ -22,10 +22,7 @@
"z-index"
],
"license": "MIT",
"dependencies": {
"has": "^1.0.3",
"uniqs": "^2.0.0"
},
"dependencies": {},
"homepage": "https://github.com/cssnano/cssnano",
"author": {
"name": "Ben Briggs",
Expand Down
2 changes: 1 addition & 1 deletion packages/postcss-zindex/src/index.js
Expand Up @@ -29,7 +29,7 @@ function pluginCreator(opts = {}) {
return;
}

cache.optimizeValues();
cache.optimizeValues(opts.startIndex || 1);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The start index is only used inside optimizeValues() so I find passing it as an argument reduces indirection (instead of first storing it as an instance variable)


// Second pass; optimize
nodes.forEach((decl) => {
Expand Down
33 changes: 8 additions & 25 deletions packages/postcss-zindex/src/lib/layerCache.js
@@ -1,33 +1,16 @@
import has from 'has';
import uniq from 'uniqs';

function LayerCache(opts) {
this._values = [];
this._startIndex = opts.startIndex || 1;
function LayerCache() {
this._values = new Map();
}

function ascending(a, b) {
return a - b;
}

function reduceValues(list, value, index) {
list[value] = index + this._startIndex;

return list;
}

LayerCache.prototype._findValue = function (value) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole method is not needed anymore, since we can access the map by key directly.

if (has(this._values, value)) {
return this._values[value];
LayerCache.prototype.optimizeValues = function (startIndex) {
const sortedValues = Array.from(this._values.keys()).sort(ascending);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assign to each z-index its rank in the sorted array.

for (let i = 0; i < sortedValues.length; i++) {
this._values.set(sortedValues[i], i + startIndex);
}

return false;
};

LayerCache.prototype.optimizeValues = function () {
this._values = uniq(this._values)
.sort(ascending)
.reduce(reduceValues.bind(this), {});
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where the old code does its 'magic'. this._values is initially an array, which then gets assigned to an object here.

};

LayerCache.prototype.addValue = function (value) {
Expand All @@ -38,13 +21,13 @@ LayerCache.prototype.addValue = function (value) {
return;
}

this._values.push(parsedValue);
this._values.set(parsedValue, parsedValue);
};

LayerCache.prototype.getValue = function (value) {
let parsedValue = parseInt(value, 10);

return this._findValue(parsedValue) || value;
return this._values.get(parsedValue) || value;
};

export default LayerCache;
17 changes: 0 additions & 17 deletions packages/postcss-zindex/yarn.lock
Expand Up @@ -7,18 +7,6 @@ colorette@^1.2.2:
resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.2.2.tgz#cbcc79d5e99caea2dbf10eb3a26fd8b3e6acfa94"
integrity sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w==

function-bind@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d"
integrity sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==

has@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/has/-/has-1.0.3.tgz#722d7cbfc1f6aa8241f16dd814e011e1f41e8796"
integrity sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==
dependencies:
function-bind "^1.1.1"

nanoid@^3.1.23:
version "3.1.23"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.23.tgz#f744086ce7c2bc47ee0a8472574d5c78e4183a81"
Expand All @@ -37,8 +25,3 @@ source-map@^0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==

uniqs@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/uniqs/-/uniqs-2.0.0.tgz#ffede4b36b25290696e6e165d4a59edb998e6b02"
integrity sha1-/+3ks2slKQaW5uFl1KWe25mOawI=