Skip to content

Instantly share code, notes, and snippets.

@nashvail
nashvail / findPropIn.js
Last active January 2, 2018 05:52
Recursively find prop in nested objects.
function findPropIn(propName, obj) {
if (obj == undefined || typeof obj != 'object') return;
if (propName in obj) { // 🗒️ If propName exists in the prototype chain
return obj[propName];
} else {
for (let prop of Object.keys(obj)) { // 👈🏽 looping through the keys
let ret = findPropIn(propName, obj[prop]);
if (ret !== undefined) {
return ret;
}
@nashvail
nashvail / physics_utils.js
Created October 29, 2017 06:15
General physics simulation utils.
export default {
clamp (n, min, max) {
return Math.min(Math.max(n, min), max);
}
};
@nashvail
nashvail / centerer.sass
Created May 17, 2016 11:19
Sass Mixin to position child in parent, relative to parent's center
@mixin centerer($horizontal: true, $vertical: true, $offsetH: 0%, $offsetV: 0%) {
position: absolute;
@if ($horizontal and $vertical) {
top: 50% + $offsetV;
left: 50% + $offsetH;
transform: translate(-50%, -50%);
} @else if ($horizontal) {
left: 50% + $offsetH;
transform: translate(-50%, 0);
} @else if ($vertical) {
@nashvail
nashvail / sortByProp.js
Created June 27, 2015 06:51
JavaScript function to sort an array of objects in descending order by any one of the property present in all of the objects.
/*
* Function : descBy(one of the property name of objects present in an array)
* Usage : arrayHoldingNames.sort(by(firstName));
* ----------------------------------------------------------------------
* Used to sort any array of objects in descending order by a property name
* of the obejcts inside that array.
*/
function descBy(propertyName) {
return function(m, n) {
if(typeof m === 'object' && typeof n === 'object') {
@nashvail
nashvail / pubsub.js
Created June 26, 2015 07:30
jQuery pub sub API implementation
(function ($) {
var o = $( {} ); // to return an instance of jquery that is to pass an empty object literal to jQuery to get a new
$.each({
trigger : 'publish',
on : 'subscribe',
off : 'unsubscribe'
}, function(key, val) {
jQuery[val] = function() {
o[key].apply(o, arguments);
@nashvail
nashvail / js_debounce.js
Created June 23, 2015 10:52
JavaScript debounce function.
function debounce(fn, duration) {
var timeout;
return function() {
clearTimeout(timeout);
timeout = setTimeout(fn, duration);
};
}