Skip to content

Instantly share code, notes, and snippets.

View ohgyun's full-sized avatar

Ohgyun Ahn ohgyun

View GitHub Profile
@ohgyun
ohgyun / promises-error-handling
Last active January 3, 2016 10:39
Promises Error Handling
var p = function () {
var d = Q.defer();
d.reject();
return d.promise;
};
# Case 1
p()
.then(function () {
console.log('A');
/**
* 데이터 타입을 미리 정의하고, 전달받은 데이터에서 타입에 맞는 값을 받아 설정한다.
* 타입 정의는 JSON 형태로 하며, 객체에 설정한 키값을 {{ }} 안에 dot으로 패스를 구분해 넣는다.
* {
* '전달받을 데이터의 키': '{{dot으로 구분한 키값}}',
* }
*
* @param {Object} context
* @param {Object} data
* @param {Object} typeDef
@ohgyun
ohgyun / hexstr2binstr.js
Last active December 17, 2015 22:39
Convert hex strings to binary strings.
var str = '0x48 0x65 0x6c 0x6c 0x6f'; // Hello
hexstr2binstr(str); //--> '0100 1000 0110 0101 0110 1100 0110 1100 0110 1111'
function hexstr2binstr(hexstr) {
return hexstr.split(' ').map(function (v) {
var bins = Number(v).toString(2).split('');
while (bins.length < 8) {
bins.unshift('0'); // left padding with '0'
}
@ohgyun
ohgyun / Gruntfile.js
Last active December 16, 2015 13:29
Gruntfile for a JavaScript library.
module.exports = function (grunt) {
// Code snippets for livereload
// https://github.com/gruntjs/grunt-contrib-livereload
var path = require('path');
var lrSnippet = require('grunt-contrib-livereload/lib/utils').livereloadSnippet;
var folderMount = function folderMount(connect, point) {
return connect.static(path.resolve(point));
};
@ohgyun
ohgyun / Gruntfile-buildjs.js
Created April 11, 2013 08:01
Gruntfile for compress javascript.
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
options: {
banner: '/*! <%= pkg.name %> - <%= pkg.version %> (<%= grunt.template.today("yyyy-mm-dd") %>) */\n\n',
separator: '\n'
},
@ohgyun
ohgyun / Gruntfile-livereload.js
Last active December 15, 2015 23:39
Gruntfile for livereload.
module.exports = function (grunt) {
// 라이브리로드를 위한 코드 파일
// https://github.com/gruntjs/grunt-contrib-livereload
var path = require('path');
var lrSnippet = require('grunt-contrib-livereload/lib/utils').livereloadSnippet;
var folderMount = function folderMount(connect, point) {
return connect.static(path.resolve(point));
};
@ohgyun
ohgyun / makeeventable.js
Last active December 15, 2015 20:09
Make an object eventable.
function makeEventable(obj) {
var getEventMap = function () {
this.__eventMap = this.__eventMap || {};
return this.__eventMap;
},
validateEventExistence = function (name) {
var eventMap = getEventMap.call(this);
if (typeof eventMap[name] === 'undefined') {
@ohgyun
ohgyun / makepropertiable.js
Last active December 15, 2015 20:09
Make an object propertiable.
function makePropertiable(obj) {
var getProps = function () {
this.__props = this.__props || {};
return this.__props;
},
emptyGet = function (currentValue) {
return currentValue;
},
emptySet = function (newValue) {
@ohgyun
ohgyun / bash_monitor.bash
Last active December 11, 2015 04:19
A bash file monitor. Watch a bashfile and run if it changed.
#!/bin/bash
# Watch a bashfile and run it if changed
function bash_monitor () {
if [[ -z "$1" ]]; then
echo "usage: bash_monitor <bashfilename>"
exit 1
fi