Skip to content

Instantly share code, notes, and snippets.

@ksheedlo
ksheedlo / goto.js
Created March 4, 2014 21:04
goto-in-javascript
function goto (func, line) {
var code = func.toString()
.split('\n')
.slice(line)
.join('\n');
var curlies = 0;
for (var i = 0; i < code.length; i++) {
if (code[i] === '{') {
curlies--;
}

The best place to start learning about how to test your Angular code is the Unit Testing section of the AngularJS Developer Guide. Some of the important takeaways from the guide are that

  • dependency injection is the best way to get ahold of dependencies for testing purposes. Angular has this built in, but you can help by not manipulating the DOM in your application (e.g., controller) logic.
  • use the $compile service to bind scope to a directive's DOM for testing

Some other helpful practices I've discovered through experience:

import random
import string
print ''.join(random.sample(string.letters + string.digits, 20))
@ksheedlo
ksheedlo / CLI output
Last active August 29, 2015 14:13
Typescript-require relative paths bug
$ node explode.js
fs.js:427
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT, no such file or directory '/Users/ken/typescript-require-min-bug/tmp/tsreq/ship.js'
at Object.fs.openSync (fs.js:427:18)
at Object.fs.readFileSync (fs.js:284:15)
at runJS (/Users/ken/typescript-require-min-bug/node_modules/typescript-require/index.js:91:20)
at Object.require.extensions..ts (/Users/ken/typescript-require-min-bug/node_modules/typescript-require/index.js:22:3)
'use strict';
// ksheedlo: I'm working on a node/web micro library for this, but it's not published yet
angular.module('myApp')
.factory('Types', function () {
return {
toInt: function (x) {
return parseInt(x, 10);
},
toFloating: function (x) {
@ksheedlo
ksheedlo / gist:3045659
Created July 4, 2012 06:07
Miller-Rabin primality in Python
import random
_rng = random.SystemRandom()
def modexp(base, exp, mod):
'''
Classic modular exponentiation by repeated squaring.
'''
acc = 1
@ksheedlo
ksheedlo / lights.c
Created October 10, 2012 08:31
Solver code for a puzzle in Bloodshot Stronghold - Borderlands 2
#include<stdint.h>
#include<stdio.h>
#define MAX_LEVEL 15
int32_t search(int32_t state, int32_t l, int32_t p, int32_t w, int32_t level) {
const int32_t table[3][4] = {
/* Pattern of XOR operations for the lever */
{ 20, 3, 20, 3 },
/* Pattern of XOR operations for the switch */
@ksheedlo
ksheedlo / decorators.py
Created December 3, 2012 18:25
Fun with Python decorators
HANDLERS = {}
def handler_decorator(*dargs, **dkwargs):
'''
This function is actually a decorator factory.
It makes decorators that take arguments.
'''
print "Ermuhgerd! Erm uh dehcruter!"
@ksheedlo
ksheedlo / demo.js
Last active October 24, 2015 06:26
'use strict';
global.Promise = global.Promise || require('bluebird');
var request = require('request'),
fetch = require('node-fetch');
exports.getRequest = function getRequest() {
return new Promise(function (resolve, reject) {
request({
@ksheedlo
ksheedlo / Output
Created March 5, 2013 20:16
Reasons to switch to Python 3
$ python look.py
File "look.py", line 3
ಠ_ಠ = 42
^
SyntaxError: invalid syntax
$ ./look.py
42