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 getDaysDiffBetweenDates = (dateInitial, dateFinal) => | |
| (dateFinal - dateInitial) / (1000 * 3600 * 24); | |
| // Example | |
| getDaysDiffBetweenDates(new Date('2017-12-13'), new Date('2017-12-22')); // 9 |
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 formatDuration = ms => { | |
| if (ms < 0) ms = -ms; | |
| const time = { | |
| day: Math.floor(ms / 86400000), | |
| hour: Math.floor(ms / 3600000) % 24, | |
| minute: Math.floor(ms / 60000) % 60, | |
| second: Math.floor(ms / 1000) % 60, | |
| millisecond: Math.floor(ms) % 1000 | |
| }; | |
| return Object.entries(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
| const triggerEvent = (el, eventType, detail) => | |
| el.dispatchEvent(new CustomEvent(eventType, { detail })); | |
| // Examples | |
| triggerEvent(document.getElementById('myId'), 'click'); | |
| triggerEvent(document.getElementById('myId'), 'click', { username: 'bob' }); |
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 elementIsVisibleInViewport = (el, partiallyVisible = false) => { | |
| const { top, left, bottom, right } = el.getBoundingClientRect(); | |
| const { innerHeight, innerWidth } = window; | |
| return partiallyVisible | |
| ? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) && | |
| ((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth)) | |
| : top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth; | |
| }; | |
| // Examples |
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 elementContains = (parent, child) => parent !== child && parent.contains(child); | |
| // Examples | |
| elementContains(document.querySelector('head'), document.querySelector('title')); | |
| // true | |
| elementContains(document.querySelector('body'), document.querySelector('body')); // false |
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 scrollToTop = () => { | |
| const c = document.documentElement.scrollTop || document.body.scrollTop; | |
| if (c > 0) { | |
| window.requestAnimationFrame(scrollToTop); | |
| window.scrollTo(0, c - c / 8); | |
| } | |
| }; | |
| // Example | |
| scrollToTop(); |
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 getScrollPosition = (el = window) => ({ | |
| x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft, | |
| y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop | |
| }); | |
| // Example | |
| getScrollPosition(); // {x: 0, y: 200} |
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
| // The Calculator | |
| const betaCalc = { | |
| currentValue: 0, | |
| setValue(value) { | |
| this.currentValue = value; | |
| console.log(this.currentValue); | |
| }, | |
| core: { |
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 addProperty(objList, newProperty) { | |
| return objList.map((obj) => ({ | |
| ...obj, | |
| ...newProperty, | |
| })); | |
| } | |
| // use case | |
| const sectionList = [ | |
| { |
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
| class Deque { | |
| constructor() { | |
| this.items = []; | |
| } | |
| addFront(item) { | |
| this.items.unshift(item); | |
| } | |
| addBack(item) { | |
| return this.items.push(item); | |
| } |