Skip to content

Instantly share code, notes, and snippets.

@lamchau
lamchau / lodash.padding.js
Last active August 29, 2015 14:09
underscore/lodash
_.mixin((function() {
function createPadding(value, pad, character) {
character = _.isValue(character) ? String(character) : " ";
var strPadded = "";
var length = pad - value.length;
while (strPadded.length < length && character.length) {
strPadded += character;
}
@lamchau
lamchau / helpers.js
Created November 17, 2014 04:40
Ember.js - Immutable check for production mode (FF4+, IE9+, Chrome 5+; modern browsers)
if (!Ember.productionMode) {
// relies on ember build tools which turn `Ember.assert` into a NOOP
Object.defineProperty(Ember, 'productionMode', {
value: Ember.assert && Ember.assert.length == 0
});
}
// more correct name name
// if (!Ember.assertionsEnabled) {
// // relies on ember build tools which turn `Ember.assert` into a NOOP
@lamchau
lamchau / fixture-url-parser.js
Last active August 29, 2015 14:10
fixture pattern matcher
function parseUrl(url) {
url = String(url);
// expects api calls to be "/some/api/v(#)/some/path" with optional query params
// examples: http://regex101.com/r/vO8mU5/1
var regex = /.*\/v(\d+)((?:\/\w+)+)\??(.+)?$/i;
var match = url.match(regex);
console.assert("Invalid URL '" + url + "'", match);
var result = {
function nextCssColor() {
function random(min, max) {
min = +min || 0;
max = +max || 0;
return Math.round(Math.random() * (max - min) + min);
}
var colors = [
"aliceblue",
"antiquewhite",
def message(message, tries)
if tries > 0
puts message + " Please try again. You have #{tries} tries remaining."
else
puts "Game over! :("
end
end
min = 1
max = 100
(function(Factory) {
function isInteger(n) {
return typeof n === 'number' &&
isFinite(n) &&
n > -9007199254740992 &&
n < 9007199254740992 &&
Math.floor(n) === n;
}
var SHA_CHARS = '0123456789abcdef';

Code Review

A guide for reviewing code and having your code reviewed.

Everyone

  • Accept that many programming decisions are opinions. Discuss tradeoffs, which you prefer, and reach a resolution quickly.
@lamchau
lamchau / clear-quora-notifications.js
Created February 16, 2015 20:32
Clearing notifications on Quora
var clickElement = function(element) {
if (!element) {
return;
}
var event = document.createEvent('HTMLEvents');
event.initEvent('click', true, true);
event.eventName = 'click';
element.dispatchEvent(event);
};