From 1edf1f8cc0f0885a4fab12980bc0d384b804bb59 Mon Sep 17 00:00:00 2001 From: Ludovico Fischer Date: Sat, 8 May 2021 22:26:10 +0200 Subject: [PATCH] chore(postcss-zindex): refactor to drop depenencies Replace uniqs and has with a map. --- packages/postcss-zindex/package.json | 5 +-- packages/postcss-zindex/src/index.js | 2 +- packages/postcss-zindex/src/lib/layerCache.js | 33 +++++-------------- packages/postcss-zindex/yarn.lock | 17 ---------- 4 files changed, 10 insertions(+), 47 deletions(-) diff --git a/packages/postcss-zindex/package.json b/packages/postcss-zindex/package.json index 1476d39e5..a4c9a65ca 100644 --- a/packages/postcss-zindex/package.json +++ b/packages/postcss-zindex/package.json @@ -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", diff --git a/packages/postcss-zindex/src/index.js b/packages/postcss-zindex/src/index.js index 9c0bd04d7..965e6f2c1 100644 --- a/packages/postcss-zindex/src/index.js +++ b/packages/postcss-zindex/src/index.js @@ -29,7 +29,7 @@ function pluginCreator(opts = {}) { return; } - cache.optimizeValues(); + cache.optimizeValues(opts.startIndex || 1); // Second pass; optimize nodes.forEach((decl) => { diff --git a/packages/postcss-zindex/src/lib/layerCache.js b/packages/postcss-zindex/src/lib/layerCache.js index 3268bcece..c1c45ad3a 100644 --- a/packages/postcss-zindex/src/lib/layerCache.js +++ b/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) { @@ -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; diff --git a/packages/postcss-zindex/yarn.lock b/packages/postcss-zindex/yarn.lock index 1525ba84f..360fc7ea6 100644 --- a/packages/postcss-zindex/yarn.lock +++ b/packages/postcss-zindex/yarn.lock @@ -7,18 +7,6 @@ colorette@^1.2.1: resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.2.1.tgz#4d0b921325c14faf92633086a536db6e89564b1b" integrity sha512-puCDz0CzydiSYOrnXpz/PKd69zRrribezjtE9yd4zvytoRc8+RY/KJPvtPFKZS3E3wP6neGyMe0vOTlHO5L3Pw== -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.20: version "3.1.20" resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.20.tgz#badc263c6b1dcf14b71efaa85f6ab4c1d6cfc788" @@ -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=