Skip to content

Instantly share code, notes, and snippets.

Avatar
🎮

Chris Yip chrisyip

🎮
View GitHub Profile
View progress-bar.js
class ProgressBar {
constructor (viewSize) {
this.viewSize = viewSize
this.circleStrokeColor = '#249fff'
this.circleStrokeWidth = 5
this.circleFillColor = 'transparent'
let svg = this.svg = this.createSVG()
svg.setAttribute('width', viewSize)
View .editorconfig
# http://EditorConfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
@chrisyip
chrisyip / promisified-generator.js
Last active Aug 29, 2015
Promise with ES6 generator
View promisified-generator.js
// https://www.promisejs.org/generators/
function async(makeGenerator) {
return function () {
var generator = makeGenerator.apply(this, arguments);
function handle(result){
if (result.done) return Promise.resolve(result.value);
return Promise.resolve(result.value).then(function (res){
return handle(generator.next(res));
View isGenerator.js
function isGenerator (input) {
return typeof input === 'function' && input.constructor.name === 'GeneratorFunction'
}
@chrisyip
chrisyip / lodash_baseSlice.js
Last active Aug 29, 2015
faster array-like slicer, aimed to replace [].slice.call(arguments). jsperf http://jsperf.com/from-slice-vs-slice-call
View lodash_baseSlice.js
// https://github.com/lodash/lodash/blob/51e459b386f8301c803442f7fc722e46fc192d35/lodash.js#L2573
// 2 times faster!
function baseSlice(array, start, end) {
var index = -1,
length = array.length;
start = start == null ? 0 : (+start || 0);
if (start < 0) {
start = -start > length ? 0 : (length + start);
}
@chrisyip
chrisyip / tower
Created Apr 20, 2015
Script for opening Git Tower
View tower
#!/usr/bin/env bash
if [ -x "/Applications/Tower.app" ] || [ -x "$HOME/Applications/Tower.app" ]; then
tower="Tower"
else
echo "Tower not found!"
exit 2
fi
if [ -z "$1" ]; then
View console.trace.js
// http://stackoverflow.com/questions/6715571/how-to-get-result-of-console-trace-as-string-in-javascript-with-chrome-or-fire
function getStackTrace () {
var obj = {}
// Remove arguments.callee from trace list
Error.captureStackTrace(obj, getStackTrace)
return obj.stack
}
console.log(getStackTrace())
@chrisyip
chrisyip / safeApply.js
Last active Aug 29, 2015
Safer $apply for angular, suppress $digest already in progress error
View safeApply.js
angular.module('myApp', [])
.run(function ($rootScope, $exceptionHandler) {
$rootScope.$safeApply = function (exp) {
var phase = (this.$root || this).$$phase
if (phase !== '$apply' && phase !== '$digest') {
this.$apply(exp)
} else {
try {
this.$eval(exp)
} catch (ex) {
@chrisyip
chrisyip / Monokai.tmTheme
Created May 7, 2012
Monokai for Sublime Text: markdown supported
View Monokai.tmTheme
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>Monokai</string>
<key>settings</key>
<array>
<dict>
<key>settings</key>
@chrisyip
chrisyip / Date Validation Expression.js
Created Oct 11, 2012
A regular expression that validates date.
View Date Validation Expression.js
var re = /^(\d{4})[-\/\.]{0,1}(?:(?:(11|0?[469])[-\/\.]?([12]\d|30|0?[1-9]))|(?:(1[02]|0?[13578])[-\/\.]{0,1}([1-2]\d|3[01]|0?[1-9]))|(?:(0?2)[-\/\.]{0,1}(2[1-9]|1\d|0?[1-9])))$/;
// test
[
// valid dates
'2012-4-21',
'2012-04-21',
'2012-5-1',
'2012-05-31',
'2012-11-21',