Skip to content

Instantly share code, notes, and snippets.

View robinweser's full-sized avatar

Robin Weser robinweser

View GitHub Profile
@robinweser
robinweser / Object.sort.js
Last active August 29, 2015 14:23
Adds a sorting algorithm to objects to sort by property or value
/*
Returns an alphabethic or value-base ascending/descending sorted Object (child objects sorted if recursive)
*/
Object.prototype.sort = function(recursive, direction, caseSensitive, sortByValue) {
var dir = (direction ? direction : "ASC");
var me = this;
var tempArray = [];
/*
Sorting function for value-based (sortByValue) sorting
*/
@robinweser
robinweser / getElementsByStyleProperty.js
Last active August 29, 2015 14:23
element selector to get all elements with a given style property value
/*
Warning: This way is for sure not performant
*/
document.getElementsByStyleProperty = function(property, value){
var allBodyElements = document.body.querySelectorAll("*");
var selectedElements = [];
var allBodyElementsLength = allBodyElements.length;
for (var i = 0; i < allBodyElementsLength; ++i) {
if (Extend.getStyleProperty(allBodyElements[i], property) == value) {
selectedElements.push(allBodyElements[i]);
@robinweser
robinweser / isNumeric.js
Last active August 29, 2015 14:23
number validation
/*
Validates if a value actually is a number
*/
function isNumeric(value) {
return !isNaN(parseFloat(value)) && isFinite(value);
}
@robinweser
robinweser / applyStylesheet.js
Last active August 29, 2015 14:23
fastest way to manually apply a stylesheet to your document
//creates a new <style></style>-tag
var style = document.createElement('style');
/*** GENERATE CSS HERE ***/
//adds the whole css rules to your styles tag
style.appendChild(document.createTextNode(CSS));
//applied it to your head which now start effecting your DOM
document.head.appendChild(style)
@robinweser
robinweser / diffMap.js
Last active August 29, 2015 14:24
Compare to Maps and get a map of states and changed values
let Diff = {
CHANGED : 'change',
REMOVED : 'remove',
ADDED : 'add'
}
function diffMap(base, diff) {
let changes = new Map();
for (let [property, value] of base) {
@robinweser
robinweser / mapToObject.js
Last active August 29, 2015 14:24
Transform a ES6 Map into an Object
function mapToObject(map){
let obj = {}
for (let [prop, value] of map){
if (value instanceof Map){
obj[prop] = mapToObject(value);
} else {
obj[prop] = value;
}
}
return obj;
@robinweser
robinweser / objectToMap.js
Last active August 29, 2015 14:24
Transform an Object to an ES6 Map
function objectToMap(obj){
let map = new Map();
let i;
let keys = Object.keys(obj);
let length = keys.length;
for (i = 0; i < length; ++i){
let temp = obj[keys[i]];
if (temp instanceof Object){
map.set(keys[i], objectToMap(temp));
@robinweser
robinweser / inline-style-evaluation.js
Created July 22, 2015 10:17
barely legible inline-style evaluation
<div style={[
styles.box,
this.props.checked && styles.checked,
this.props.editable && this.state.editMode && styles.editable,
...
]}>
</div>
(function(){Array.prototype.slice.call(document.querySelectorAll('[data-reactid]')).forEach(function(ele){ele.style.background = 'rgba(0, 129, 255, 0.05)'})})();
@robinweser
robinweser / atom-styles.less
Last active September 23, 2015 17:34
Personal atom settings
/*
* Your Stylesheet
*
* This stylesheet is loaded when Atom starts up and is reloaded automatically
* when it is changed.
*
* If you are unfamiliar with LESS, you can read more about it here:
* http://www.lesscss.org
*/