Last active
November 25, 2017 02:40
-
-
Save idettman/e6177efb4473c7882427509f0c57839c to your computer and use it in GitHub Desktop.
es6 utils
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
| /** | |
| * @param obj {Object} | |
| * @returns {string} Object's type {string} value | |
| */ | |
| export function getType (obj) { | |
| return Object.prototype.toString.call(obj); | |
| }; | |
| /** | |
| * @param {string} url | |
| * @returns {boolean} true if valid url or false if not valid | |
| */ | |
| export function isValidURL(url) { | |
| const RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; | |
| if (RegExp.test(url)) { | |
| return true | |
| } | |
| else { | |
| return false | |
| } | |
| return ( | |
| }; | |
| /** | |
| * @param arrayLikeObject {NodeList|Arguments} | |
| * @returns {Array} | |
| */ | |
| export function convertToArray (arrayLikeObject) { | |
| return Array.prototype.slice.call(arrayLikeObject); | |
| }; | |
| /** | |
| * @param element {HTMLElement} | |
| */ | |
| export function removeFromParent (element) { | |
| element.parentNode.removeChild(element); | |
| }; | |
| /** | |
| * @param element {HTMLElement} element to remove children from | |
| */ | |
| export function removeChildren(element) { | |
| if (element.hasChildNodes()) { | |
| let child = element.firstElementChild; | |
| while (child) { | |
| removeFromParent(child); | |
| child = element.firstElementChild; | |
| } | |
| } | |
| } | |
| /** | |
| * @param element {HTMLElement} | |
| */ | |
| export function removeChildrenV2(element) { | |
| if (element.hasChildNodes()) { | |
| while (element.children.length) { | |
| removeFromParent(element.children[0]); | |
| } | |
| } | |
| } | |
| /** | |
| * @param obj {*} | |
| * @param callback {function} | |
| */ | |
| export function maybe (obj, callback) { | |
| if (obj) callback(); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment