Skip to content

Instantly share code, notes, and snippets.

@GuyMograbi
GuyMograbi / promiseMock.js
Last active October 10, 2015 14:16
a small snippet to easily mock promises
/**
*
* @description
*
* a utility function to help mock promises. Using $q for mocks in tests is a bad practice. tests should not be asyncronous.
* This mock makes a synchronous promise like mechanism.
* I found it answers at least 80% of my use-cases and reduces code significantly.
*
* Usage:
*
@GuyMograbi
GuyMograbi / one.js
Created October 17, 2015 11:22
Find or create pattern in angular - saw someone implemented it in a wrong way and thought to blog about it
/**
*
* The WRONG way
*
* - requires method to be in Controller - see $scope and $http usage.. lack of separation.
* - copy paste of callback.. lack of use of promise behavior, no proper error handling
*
**/
@GuyMograbi
GuyMograbi / index.js
Created October 25, 2015 10:26
my prefered prototype inheritance pattern
// Code goes here
function Animal(){
this.type = 'abstract';
this.sayGoodbye = function(){
return 'goodbye';
}
}
@GuyMograbi
GuyMograbi / index.js - assume one table in the page
Last active October 30, 2015 05:55
turn table to object -
(function(){
var rows = _.map($('table tr'), function(r){
return _.map($(r).find('td'),function(c){
var $c = $(c);
if ( $c.children().length > 0){ // get only visible text
return $(c).find('*:not(:has(*)):visible').text();
}else{ // no children.. get text
return $(c).text();
}
var logger = require('log4js').getLogger('normalize');
var chalk = require('chalk');
chalk.enabled = true;
beforeEach(function(){
console.log( chalk.bold.blue('currently running :: ' + jasmine.getEnv().currentSpec.getFullName()));
// define new matchers
@GuyMograbi
GuyMograbi / index.js
Last active October 31, 2015 20:50
add a text locator to protractor
by.addLocator('text',
/**
*
* @param {string} text - will be lowercased
* @param {string} selector - to get list of children, css
* @param {object|null} parent - protractor will provide this..
*/
function(text, selector, parent) {
return _.filter($(parent || 'body').find(selector), function(e){
@GuyMograbi
GuyMograbi / post.html
Last active December 12, 2015 00:28
Using Ben Alman's postMessage JQuery plugin. read more at http://blog.mograbi.info/2013/01/postMessage-support-multi-domains-and-messages.html
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Post Message Example</title>
<script src="/assets/javascripts/jquery.min.js"> </script><!-- jquery -->
<script src="/assets/javascripts/jquery.ba-postmessage.min.js"> </script><!-- plugin -->
</head>
<body>
@GuyMograbi
GuyMograbi / smart-table.html
Created June 8, 2013 20:47
The complete code for my AngularJS post serie on my blog at blog.mograbi.info. Coming soon..
<html ng-app="myApp">
<head>
<style>
body {background-color:#cecece; margin:0; padding:0; font-family:Arial; }
body>div:first-child {width:50%; margin:auto; margin-top:40px;}
table {border:none; border-collapse:collapse; width:100%;}
table tr { border:none;}
table tr td { border:none; font-size:37px; font-weight:bolder;}
table tr:first-child{background-color:#00ff00;}
table tr:nth-child(2n){ background-color:blue;}
@GuyMograbi
GuyMograbi / index.js
Created January 9, 2017 01:34
Naive code to indent HTML for readability
var myHtml = `<div billing-total="" quote="quote" payment-frequency="frequency" on-select-frequency="onSelect(frequency)" payment-plan="plan" class="ng-scope ng-isolate-scope"><div class="billing-frequency-wrap container"><div class="billing-total col col-lg-3 col-md-3 col-sm-8 col-xs-4"><div class="frequency"><div class="freq-select"><div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label getmdl-select text-input"><label for="paymentFrequency" class="mdl-textfield-label-right"><i class="material-icons filled">done</i> <i class="mdl-icon-toggle__label material-icons">arrow_drop_down</i></label><span><i class="material-icons filled">done</i></span> <input class="mdl-textfield__input" type="text" id="paymentFrequency" placeholder="Monthly" readonly="readonly" value=""><label for="paymentFrequency" class="mdl-textfield__label"><span>Billing Frequency</span></label><div class="getmdl-select-container" ng-click="setPaymentFrequency($event);"><ul for="paymentFrequency" class="mdl-menu mdl-js-menu m
@GuyMograbi
GuyMograbi / 0_reuse_code.js
Created June 23, 2017 22:00
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console