Created
June 12, 2017 19:42
-
-
Save vtellier/97df5fb9c667a5ae25308f946da3b6fc to your computer and use it in GitHub Desktop.
A Polymer 2.0 mixin that enables to factorize basic utilitary functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OhUtils = function(superClass) { | |
return class extends superClass { | |
constructor() { | |
super(); | |
} | |
static get properties() { | |
return { | |
/*bar: { | |
type: Object | |
}*/ | |
}; | |
} | |
/*static get observers() { | |
return [ '_barChanged(bar.*)' ]; | |
} | |
_barChanged(bar) { ... }*/ | |
// Returns true if all arguments are empty or undefined | |
isEmpty() { | |
var ret = true; | |
for (var i = 0; ret && i < arguments.length; i++) { | |
var variable = arguments[i]; | |
if (typeof variable !== 'undefined' && variable != null) { | |
if (Array.isArray(variable)) { | |
ret = variable.length == 0; | |
} | |
else if(!isNaN(variable)) { | |
ret = variable == 0; | |
} | |
else if(typeof variable === 'string' || variable instanceof String) { | |
ret = variable.length == 0; | |
} | |
else { | |
ret = Object.keys(variable).length === 0; | |
} | |
} | |
else | |
ret = true; | |
} | |
return ret; | |
} | |
// Returns true if all arguments are defined and not empty | |
isNotEmpty() { | |
var ret = true; | |
for (var i = 0; ret && i < arguments.length; i++) { | |
var variable = arguments[i]; | |
if (typeof variable !== 'undefined' && variable != null) { | |
if (Array.isArray(variable)) { | |
ret = variable.length > 0; | |
} | |
else if(!isNaN(variable)) { | |
ret = variable > 0; | |
} | |
else if(typeof variable === 'string' || variable instanceof String) { | |
ret = variable.length > 0; | |
} | |
else { | |
ret = Object.keys(variable).length !== 0; | |
} | |
} | |
else | |
ret = false; | |
} | |
return ret; | |
} | |
stringify(obj) { | |
return JSON.stringify(obj, 2); | |
} | |
hideIfEmpty(arrayStar) { | |
return arrayStar.base.length==0 ? "hidden" : ""; | |
} | |
hideIfNotEmpty(arrayStar) { | |
return arrayStar.base.length>0 ? "hidden" : ""; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment