Last active
March 30, 2017 19:27
-
-
Save marcoslhc/d57356f0d07ff42c09a7cfc543f9aeda to your computer and use it in GitHub Desktop.
ClassList 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ClassList.prototype.reduce = function (reducer, initialValue) { | |
| return ClassList.of(this._classes.reduce(reducer, initialValue)); | |
| } | |
| // Notice how we can define "filter" by reducing the previous array | |
| // into a new array with only the values that satisfies the predicate | |
| ClassList.prototype.filter = function (predicate) { | |
| return ClassList.of(this.getClasses()).reduce((newList, currentClass) => { | |
| if (predicate(currentClass)) newList.push(currentClass); | |
| return newList; | |
| }, []); | |
| } | |
| // These are derivation and mostly for convenience | |
| ClassList.prototype.mapFilter = function(predicate, fn) { | |
| return ClassList.of(this.getClasses()).filter(predicate).map(fn); | |
| }; | |
| ClassList.prototype.filterReduce = function (predicate, fn) { | |
| return ClassList.of(this.getClasses()).filter(predicate).reduce(fn); | |
| }; | |
| // this is wrong | |
| ClassList.prototype.concat = function(otherArray) { | |
| // here is what is wrong | |
| // Can you tell why? | |
| // --------------------- | |
| // || | |
| // \/ | |
| return ClassList.of(this.getClasses().concat(otherArray)); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const onlyBtn = (className) => /^btn(-|.)*/.test(className); | |
| const solarizedBtnCls = ClassList.of(['btn-solarized--active', 'btn-solarized']); | |
| const btnCls = ClassList.of([ | |
| 'btn', | |
| 'active', | |
| 'btn-default' | |
| ]).filter(onlyBtn) | |
| // ClassList(['btn', 'btn-default']) | |
| // Trying to do btnCls + solarizedBtnCls will break | |
| ClassList.of([ | |
| 'btn', | |
| 'active', | |
| 'btn-default' | |
| ]).concat(solarizedBtnCls); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment