Skip to content

Instantly share code, notes, and snippets.

View vincentorback's full-sized avatar
🌻

Vincent Orback vincentorback

🌻
View GitHub Profile
@vincentorback
vincentorback / urlParameters.js
Last active June 1, 2021 11:22
Set and get URL parameters
/**
* Set URL Parameter
* @param { String } key
* @param { String } value
* @param { String } url
*/
export function setUrlParameter(key, value, url = window.location.href) {
let reg = new RegExp("([?&])" + key + "=.*?(&|$)", "i")
let separator = url.indexOf('?') !== -1 ? "&" : "?"
@vincentorback
vincentorback / nodesToArray.js
Created March 20, 2016 12:00
Converting a NodeList to an Array
/**
* Turns a list of nodes into an array
* @param { NodeList } nodeList
* @return { Array }
*/
export function nodesToArray(nodeList) {
return [...nodeList];
return Array.from(nodeList);
return [].slice.call(nodeList);
return Array.prototype.slice.call(nodeList);
@vincentorback
vincentorback / breakpoints.js
Last active February 21, 2017 14:18
Sync resizing event and breakpoints
/**
* Dependencies
*/
const breakpoints = {
xs: 300,
sm: 600,
md: 800,
lg: 1200,
xl: 1600
};
@vincentorback
vincentorback / getClosest.js
Last active June 8, 2016 11:57
Get closest parent element matching selector
/**
* Get closest DOM element up the tree that contains a class, ID, or data attribute
* @param {Node} elem The base element
* @param {String} selector The class, id, data attribute, or tag to look for
* @return {Node} Element or Null if no match
*/
export function getClosest (elem, selector) {
let firstChar = selector.charAt(0);
// Get closest match
@vincentorback
vincentorback / elementInViewport.js
Last active June 30, 2019 19:04
Check if element is in viewport
/**
* Check if element is in viewport
* @param {Node} el
* @return {Object}
*/
export function elementInViewport(el) {
let top = el.offsetTop;
let left = el.offsetLeft;
let width = el.offsetWidth;
let height = el.offsetHeight;
@vincentorback
vincentorback / helpers.js
Last active May 27, 2016 11:39
Lots of small javascript helpers that I tend to reuse in projects
/**
* isUndefined
* Check if object is undefined
* @param {Anything} value
* @return {Boolean} Returns `true` if `value` is `undefined`, else `false`.
*/
export function isUndefined(value) {
return value === undefined;
}
@vincentorback
vincentorback / distanceBetween.js
Last active January 10, 2016 14:06
Get distance between to geolocations in kilometers
var distance = distanceBetween({
lat: 59.329323,
lng: 18.068581
}, {
lat: 57.708870,
lng: 11.974560
});
function getRadius(x) {
return x * Math.PI / 180;
@vincentorback
vincentorback / functions.php
Last active May 20, 2016 17:29
Some fun WordPress snippets
<?php
/**
* Check if on posts-page
*/
function is_page_for_posts () {
return ( is_home() || (is_archive() && ! is_post_type_archive() ) );
}
@vincentorback
vincentorback / applyStyle.js
Last active September 6, 2016 11:24
Apply style prefixed with all prefixes
const PREFIXES = ['', 'Moz', 'webkit', 'Webkit', 'O', 'ms'];
function applyStyle(prop, value, element) {
const first = prop[0];
const trail = prop.slice(1);
PREFIXES.forEach(prefix => {
let name = prefix ? (prefix + first.toUpperCase()) : first.toLowerCase();
name += trail;
@vincentorback
vincentorback / stringToElements.js
Last active April 13, 2020 18:39
Creates DOM-elements from a string
function stringToElements (string) {
const div = document.createElement('div')
div.innerHTML = string
if (div.childNodes.length > 1) {
return div.childNodes
}
return div.firstChild
}