Skip to content

Commit

Permalink
chore(postcss-zindex): refactor to drop depenencies (#1096)
Browse files Browse the repository at this point in the history
Replace uniqs and has with a map.
  • Loading branch information
ludofischer committed May 13, 2021
1 parent 4d7fe36 commit 65d8197
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 47 deletions.
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);

// 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) {
if (has(this._values, value)) {
return this._values[value];
LayerCache.prototype.optimizeValues = function (startIndex) {
const sortedValues = Array.from(this._values.keys()).sort(ascending);
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), {});
};

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=

0 comments on commit 65d8197

Please sign in to comment.