Skip to content

Instantly share code, notes, and snippets.

View xeaone's full-sized avatar
:bowtie:

Alexander Elias xeaone

:bowtie:
View GitHub Profile
@xeaone
xeaone / flatten-object.js
Last active October 9, 2016 22:54
Flattens a multi level object to a single level object with paths
function flattenObject (object) {
var flatObject = {};
for (var key in object) {
if (!object.hasOwnProperty(key)) continue;
if (object[key] && object[key].constructor.name === 'Object') {
var childObject = flattenObject(object[key]);
for (var childKey in childObject) {
@xeaone
xeaone / is.js
Last active September 14, 2016 00:41
Primitive Type Check
/*
type: constructor name ('String', 'Array', 'Object', 'Function')
value: variable
*/
function is (type, value) {
return !value ? false : value.constructor.name === type;
}
/*
'use strict';
var object = {
hello: 'blue',
app: {
env: {
port: 7777,
la: 1
}
},
function toCamelCase (string) {
var nextIndex = string.search('-') + 1;
var nextLetter = string.charAt(nextIndex).toString();
var r = '-' + nextLetter;
var n = nextLetter.toUpperCase();
return string.replace(r, n);
}
function fromCamelCase (string) {
@xeaone
xeaone / edit-style-sheet.js
Created September 2, 2016 01:20
Edit Style Sheet Javascript
/*
var name = 'nav-bar';
var selector = '.nav-bar > ul.active';
var key = 'transform';
getRule(name, selector, key, function (sTransform) {
var values = getTranslateValues(sTransform);
var x = values[0] - 100;
var y = values[1];
var z = values[2];
@xeaone
xeaone / each.js
Last active February 7, 2017 22:05
Loop through each an iterable and enables, break, continue, and manipulation.
function each (iterable, callback, scope) {
var statment = null, i = null, l = null, k = null;
if (iterable.constructor.name === 'Number') {
for (i = 0; i < iterable; i++) {
statment = callback.call(scope, i, iterable);
if (statment === 'break') break;
else if (statment === 'continue') continue;
}
} else if (iterable.constructor.name === 'Map') {
@xeaone
xeaone / manipulate-object.js
Created February 8, 2017 23:33
Object Manipulation Object
function getByPath(object, path) {
var keys = path.replace('[', '.').replace(']', '').split('.');
var last = keys.length - 1;
var obj = object;
for (var i = 0; i < last; i++) {
var prop = keys[i];
if (!obj[prop]) return undefined;
obj = obj[prop];
@xeaone
xeaone / umd.js
Created February 16, 2017 19:35
A better way then umd
var NAME = '';
var MODULE = {} || function () {};
(function (t, n, m) {
if (typeof m === 'function') m = m(t);
if (typeof t.define !== 'undefined') t.define([n], m);
else if (typeof t.module !== 'undefined') t.module.exports = m;
else if (typeof t.window !== 'undefined') t.window[n] = m;
else return m;
var object = {}, i = 0;
while (i++ < 100000) {
object['user' + i] = i;
}
// fastest
console.time('keys for');
var result;
var keys = Object.keys(object);
@xeaone
xeaone / promise-async-each.js
Created September 15, 2017 19:51
Promise Async Each
function each (condition, method, context, index) {
index = index === undefined ? 0 : index;
if (condition.call(context, index)) {
return Promise.resolve().then(function () {
return method.call(context, index);
}).then(each.bind(null, condition, method, context, index+1));
} else {
return Promise.resolve();
}