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
| function backwardsPath(element, root) { | |
| const path = []; | |
| let current = element; | |
| while (current.parentNode) { | |
| const index = [...current.parentNode.children].indexOf(current); | |
| path.push(index); | |
| current = parentNode; | |
| } |
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
| function throttle(fn, time, ...args) { | |
| let id; | |
| return function() { | |
| if (id) return; | |
| id = setTimeout(() => { | |
| fn.apply(this, args); | |
| id = null; | |
| }, time); | |
| } | |
| } |
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
| function debounce(fn, time, ...args) { | |
| let id; | |
| return function() { | |
| if (id) clearTimeout(id); | |
| id = setTimeout(() => { | |
| fn.apply(this, args); | |
| id = null; | |
| }, time); | |
| } | |
| } |
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
| function bind(fn, context, ...args) { | |
| return function() { | |
| return fn.apply(context, args); | |
| } | |
| } | |
| const foo = function (param) { | |
| console.log('Context:', this.bar); | |
| console.log('Params:', param); | |
| } |
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
| function flatten(arr) { | |
| return arr.reduce((acc, item) => { | |
| if (Array.isArray(item)) { | |
| acc = acc.concat(flatten(item)); | |
| } else { | |
| acc.push(item); | |
| } | |
| return acc; | |
| }, []); | |
| } |
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
| function removeDuplicateWithCaseSensitive(string) { | |
| const arrayOfWords = string.split(' '); | |
| const mapOfWords = {}; | |
| return arrayOfWords.reduce((newString, word) => { | |
| const lowerCasedWord = word.toLowerCase(); | |
| if (mapOfWords[lowerCasedWord]) return newString; | |
| mapOfWords[lowerCasedWord] = word; | |
| return `${newString} ${word}`; | |
| }, ''); | |
| } |
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
| function removeDuplicates(string) { | |
| const arrayOfWords = string.split(' '); | |
| const mapOfWords = {}; | |
| return arrayOfWords.reduce((newString, word) => { | |
| if (mapOfWords[word]) return newString; | |
| mapOfWords[word] = true; | |
| return `${newString} ${word}`; | |
| }); | |
| } |
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
| // reimplemented pull | |
| const without = (arr, ...values) => { | |
| return arr.filter((item) => { | |
| return values.indexOf(item) < 0; | |
| }); | |
| }; | |
| const arr = ['a', 'b', 'c', 'a', 'b', 'c']; | |
| console.log('Lodash pull: ', _.without(['a', 'b', 'c', 'a', 'b', 'c'], 'a', 'c')); |
NewerOlder