JKCPR
JKCPR
Reputation Top 1%
Jake Cooper
27 Snippets  (17th place)
Published
1 Channel
Created
21 Channels
Following
Apr 25, 2018
Last Visit
Jul 26, 2014
Registered
3995 points  (23rd place)
Reputation
Top 5% Publisher
Junior Code Generator
Serious Code Generator
Senior Code Generator
Junior Publisher
Serious Publisher
Senior Publisher
Junior Topic Hub
Junior Trend Maker
Serious Trend Maker
Senior Trend Maker
Junior Judge
Serious Judge
Senior Judge
Senior Popular Coder
Junior Autobiographer
Master Autobiographer
Master Famous Coder
Junior Wise Coder

Recent Snippets See all snippets by JKCPR

public by JKCPR created Apr 25, 2018  164  0  4  0

unit testing - mocking server responses with sinonjs

{
    setUp: function () {
        this.server = sinon.fakeServer.create();
    },

    tearDown: function () {
        this.server.restore();
    },

    "test should fetch comments from server" : function () {
        this.server.respondWith("GET", "/some/article/comments.json",
                                [200, { "Content-Type": "application/json" },
                                 '[{ "id": 12, "comment": "Hey there" }]']);

        var callback = sinon.spy();
        myLib.getCommentsFor("/some/article", callback);
        this.server.respond();

        sinon.assert.calledWith(callback, [{ id: 12, comment: "Hey there" }]));
    }
}		
;

public by JKCPR created Mar 31, 2018  349  0  4  0

Fibonacci meh

Loops over the Fibonnaci sequence...might be useful in an interview.
 
function fibonacci(num, memo) {
  memo = memo || {};

  return memo[num]? memo[num]: num < 2? 1: memo[num] = fibonacci(num - 1, memo) + fibonacci(num - 2, memo);
}
    for(var i=0;i<100;i=i+1){
        this.console.log(fibonacci(i));
    }
;

public by JKCPR created May 12, 2017  307  1  5  1

is your DOM classy? Wouldn't your console like to know...

Intended as Dev Tool to be utilised in the browser console so that refactoring your messy CSS can be less hurty.
;( 
  function (){
  "use strict";

  //get ALL DOM elements => BODY > *, BODY *
  var elms = document.body.getElementsByTagName("*"),
      i = elms.length,
      arr = [],
      classArray;
  
  //iterate, isolate, finally indicate
  while( i-- ) {
      classArray = elms[i].className.split(/\s+/);
      if (classArray.length > 1) {
          arr.push( classArray[1] );
      }
  }
  
  console.log( arr.join(',') );
  return arr;
  
})();
;

public by JKCPR created Oct 31, 2016  2023  2  5  1

Matching Height - using CSS @element query

A simple elegant solution to matching element heights using the @element query.
//  Some html setup required
/*
    <div id=thing1>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nemo, temporibus consequuntur velit dolor! Eos animi molestias  found here <a href="http://codepen.io/tomhodgins/pen/yadeXV"> thanks Tommy </a>aliquid eum error officiis vel et saepe a facilis, voluptate dignissimos nam assumenda, deleniti.</div>
    <div id=thing2>This one doesn't have as much text as id=thing1… so it's set to 100eh, or 100 element height units for #thing1, which makes it the same height.</div>
*/

div {
  margin: 1em 0;
  width: 50%;
  float: left;
  color: black;
  background: lime;
}
#thing2 {
  background: red;
}
@element '#thing1' {
  #thing2 {
    height: 100eh;
  }
}
;

public by JKCPR created Sep 20, 2016  8604  1  5  1

AngularJS 'Ctrl as vm' pattern

Within controller use 'var varname' if it is only needed internally, anything which needs to be bound to the ViewModel uses 'vm.varname' because in 'Controller as vm' pattern you forego using $scope to bind to the ViewModel and instead use the vm.object instead. Sometimes $scope is necessary but less so using this pattern....vm is readable and conv
// AngularJS 'Controller As vm' pattern
<script>
(function(){
  "use strict";

  var app = angular.module('app')
    .controller('Customers', [function() {
      var vm = this; //this is implicitly bound to controller's scope
  
      vm.title = 'Customers';
      vm.customers = [
        {name: 'Haley'}, {name: 'Ella'}, {name: 'Landon'}, {name: 'John'}
        ];
    }])
    .controller('Shells', [function() {
      var vm = this; //this need not be named identically as in HTML
  
      vm.title = 'Shells';
      vm.shells = [
        {name: 'Main Shells'}, {name:'Half Shells'},{name: 'Quarter Shells'}
      ];
    }]);
})();
</script>

<div ng-controller="Shell as shellVm">
  <h1>{{  shellVm.title }}</h1>
  <article ng-controller="Customers as customersVm">
    <h2> {{ customersVm.title }} in </h2>
    <ul ng-repeat="c in customersVm.customers">
      <li>{{ c.name }}</li>
    </ul>
  </article>
</div>
;