-
-
Notifications
You must be signed in to change notification settings - Fork 321
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), {}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where the old code does its 'magic'. |
||
}; | ||
|
||
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; |
There was a problem hiding this comment.
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)