Skip to content

Instantly share code, notes, and snippets.

@chrisyip
chrisyip / progress-bar.js
Created August 29, 2015 07:53
SVG progress bar
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)
# 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 August 29, 2015 14:02
Promise with ES6 generator
// 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));
function isGenerator (input) {
return typeof input === 'function' && input.constructor.name === 'GeneratorFunction'
}
@chrisyip
chrisyip / lodash_baseSlice.js
Last active August 29, 2015 14:07
faster array-like slicer, aimed to replace [].slice.call(arguments). jsperf http://jsperf.com/from-slice-vs-slice-call
// 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 April 20, 2015 04:35
Script for opening Git 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
@chrisyip
chrisyip / console.trace.js
Created May 16, 2015 16:24
console.trace
// 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 August 29, 2015 14:21
Safer $apply for angular, suppress $digest already in progress error
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 13:36
Monokai for Sublime Text: markdown supported
<?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 October 11, 2012 09:09
A regular expression that validates date.
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',