Skip to content

Instantly share code, notes, and snippets.

@tobiasdalhof
Created May 7, 2018 08:22
Show Gist options
  • Save tobiasdalhof/d54082e56d2ea49408f3d42c5ac59d52 to your computer and use it in GitHub Desktop.
Save tobiasdalhof/d54082e56d2ea49408f3d42c5ac59d52 to your computer and use it in GitHub Desktop.
Helper class
export default class DeliveryMessageHelper {
/**
* Get current URL without trailing slash.
* @returns {string}
*/
static getCurrentURL () {
return window.location.origin + window.location.pathname
}
/**
* Remove trailing slash from string
* @param {string} string
*/
static removeTrailingSlash (string) {
return string.replace(/\/+$/, '')
}
/**
* HTML to element.
* @param {String} htmlString
*/
static createElementFromHTML (htmlString) {
const div = document.createElement('div')
div.innerHTML = htmlString.trim()
return div.childNodes
}
/**
* Add new element BEFORE another.
* @param {HTMLElement} newElement
* @param {HTMLElement} element
*/
static addElementBefore (newElement, element) {
element.parentNode.insertBefore(newElement, element)
}
/**
* Add new element AFTER another.
* @param {HTMLElement} newElement
* @param {HTMLElement} element
*/
static addElementAfter (newElement, element) {
element.parentNode.insertBefore(newElement, element.nextSibling)
}
/**
* Remove hashtags from string.
* @param {String} string
*/
static removeHashtags (string) {
const regexp = new RegExp('#([^\\s]*)', 'g')
return string.replace(regexp, '')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment