Skip to content

Instantly share code, notes, and snippets.

View greyaperez's full-sized avatar

Grey Perez greyaperez

View GitHub Profile
@greyaperez
greyaperez / expose-angular-class-members.ts
Created December 1, 2017 22:04
Exposes Angular class members to the global namespace.
/**
* Expose Angular Methods
* @desc Exposes
* @private
* @param {string} namespace - Namespace to append to window.ng (ex: ng.WidgetComponent)
* @param {...string[]} member
* @example
* ////////////////
* // Usage Example
* ////////////////
@greyaperez
greyaperez / select-to-collection.js
Last active November 22, 2017 04:14
Get collection of label / value from form select element.
// Pure JavaScript
function selectToCollection(selector, asJson) {
var options = Array.from(document.querySelector(selector).querySelectorAll('option'));
var collection = options.reduce((prev, curr) => {
return [...prev, { label: curr.label, value: curr.value}];
}, []);
return (asJson === true) ? JSON.stringify(collection) : collection;
}
@greyaperez
greyaperez / JS-Quick-Debug.txt
Created July 13, 2017 18:21
Auto Debug JS (Regex Replace) - Automatically inserts
// FIND REGEX
(\s+)\w+ (\w+)\(.*\).*{
// REPLACE
$0\n$1 console.log('METHOD:: $2 Invoked');
@greyaperez
greyaperez / boilerplate-angular.domain-model.js
Created April 7, 2016 20:59
Angular - Domain Model Objects
/**
* Domain Model: Boilerplate Example for Angular
* @description Class Model through Composition (looser coupling, higher cohesion)
* @note $cacheFactory mimics proper inheritance through uniquely indexed references
* @author Timothy A. Perez <tim.adam.perez@gmail.com>
*/
'use strict';
(function () {
@greyaperez
greyaperez / isset.mixin.js
Created March 1, 2016 14:40
lodash mixin: _.isSet() - Checks whether a value is undefined, null, empty string, empty array, or empty object.
/**
* Lodash Mixin Util: isSet
* @description Returns flag whether value is - undefined null, empty string, empty array, or empty object
* @param {*} val - Value to Check
* @author Timothy A. Perez <tim.adam.perez@gmail.com>
*/
(function () {
function isSet(val) {
return (! ((_.isUndefined(val) || val === null) || (_.isArray(val) && val.length === 0) || (_.isObject(val) && _.isEmpty(val)) || (_.isString(val) && val === '')) );
}
@greyaperez
greyaperez / predictable-random.js
Created February 10, 2016 19:13
Predictable Random - Is Random Truly Random?
/**
* Predictable Random - Is Random Truly Random?
* @description - Not the cleanest code, but more of a proof of concept quick flesh out.
* @author Timothy A. Perez <tim.adam.perez@gmail.com>
*/
///////////////////////////////////////////////////
var sampleTest = (function a(limit, last, r, i, repeatLog) {
if (!limit) {
console.log('Result: ', repeatLog);
repeatLog = null;
@greyaperez
greyaperez / salesforce-utils-angular.js
Last active January 7, 2016 18:13
Salesforce Utils Angular App. Includes query service which allows async queries to SF objects.
///////////////////
// INITIAL SETUP
///////////////////
/*
// On VF page, pass Session_ID to Client
var SESSION_ID = '{!$Api.Session_ID}';
*/
///////////////////
// Angular Module
@greyaperez
greyaperez / addSingletonGetter.js
Created December 9, 2015 19:58
Create Singleton Class Instances without Polluting the global scope.
/**
* Adds a {@code getInstance} static method that always return the same instance
* object.
* @param {!Function} ctor The constructor for the class to add the static
* method to.
*/
function addSingletonGetter(ctor) {
ctor.getInstance = function() {
if (ctor.instance_) {
return ctor.instance_;
@greyaperez
greyaperez / switch-enhanced.js
Created December 9, 2015 19:31
Enhanced version of switch
var x = 'b';
var y = 'd';
var caseHandlers = {
'a': caseHandlerMethod1,
'b': caseHandlerMethod2,
'c': caseHandlerMethod3,
'default': defaultHandlerMethod
};
switch2(caseHandlers,x);
@greyaperez
greyaperez / dev-console-inject-js-lib.js
Last active December 9, 2015 15:57
Inject jQuery from Dev Console on any Page
var JS_LIB = {
ANGULAR: 'https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js',
JQUERY: 'https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js',
LODASH: 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js'
}
// Inject jQuery & Lodash
function injectAndRun(lib, callback) {
(function(d, script) {
script = d.createElement('script');