Skip to content

Instantly share code, notes, and snippets.

View bpceee's full-sized avatar
💭
In meditation

Ken Bi bpceee

💭
In meditation
  • Auckland, New Zealand
View GitHub Profile
@bpceee
bpceee / uri.js
Last active August 29, 2015 14:27 — forked from jlong/uri.js
URI Parsing with Javascript
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
@bpceee
bpceee / gist:bbaef12e0853b6165cd1
Last active August 29, 2015 14:28
Node.js 中使用 request 请求 GBK 页面
var request = require('request');
var Iconv = require('iconv').Iconv;
var iconv = new Iconv('GBK', 'UTF-8//TRANSLIT//IGNORE');
request.get({
url: 'http://foo.bar',
encoding: null
}, function(err, res, body) {
console.log(iconv.convert(body).toString());
});
@bpceee
bpceee / dropzone-directive.js
Created October 24, 2015 03:49 — forked from compact/dropzone-directive.js
AngularJS directive for Dropzone.js
/**
* An AngularJS directive for Dropzone.js, http://www.dropzonejs.com/
*
* Usage:
*
* <div ng-app="app" ng-controller="SomeCtrl">
* <button dropzone="dropzoneConfig">
* Drag and drop files here or click to upload
* </button>
* </div>
@bpceee
bpceee / printPrototypeChain.js
Created December 13, 2015 09:09 — forked from LukeChannings/printPrototypeChain.js
prints the prototype chain for a constructed object.
/**
* prints the prototype chain for a constructed object.
* @param {Object} a constructed object.
* @param asArray {Boolean} if true, the chain will be returned as an array.
*/
function printPrototypeChain(instance, asArray) {
var chain = []
while (instance = Object.getPrototypeOf(instance)) {
@bpceee
bpceee / gist:e447405a777a3c67b5a5
Created December 28, 2015 06:28 — forked from Mithrandir0x/gist:3639232
Difference between Service, Factory and Provider in AngularJS
// Source: https://groups.google.com/forum/#!topic/angular/hVrkvaHGOfc
// jsFiddle: http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/
// author: Pawel Kozlowski
var myApp = angular.module('myApp', []);
//service style, probably the simplest one
myApp.service('helloWorldFromService', function() {
this.sayHello = function() {
return "Hello, World!"
@bpceee
bpceee / gist:9bc0c8536eae94143f4486e4e47a5fc3
Last active October 19, 2016 09:12
Get/set Cursor In Html TextArea
<html>
<!-- http://snipplr.com/view/5144/getset-cursor-in-html-textarea/ -->
<head>
<title>Get/Set Caret in Textarea Example</title>
<script>
function doGetCaretPosition (ctrl) {
var CaretPos = 0;
// IE Support
if (document.selection) {
@bpceee
bpceee / index.html
Last active October 20, 2016 06:10
set caret(cursor) position in contenteditable element (div)
<html>
<!-- http://jsfiddle.net/timdown/vXnCM/-->
<head>
<title>Get/Set Caret in Textarea Example</title>
<script>
function test() {
var el = document.getElementById('editable');
var range = document.createRange();
@bpceee
bpceee / gist:5eebb82b40fb4abd450fab106352687d
Last active March 17, 2017 07:35
angular 1.x measure digest time
angular.element(document)
.injector()
.invoke(['$rootScope', function($rootScope) {
var a = performance.now();
$rootScope.$apply();
return performance.now()-a;
}])
angular.element(document)
@bpceee
bpceee / scopeWalker.js
Last active March 31, 2017 06:37
walk angular 1.x scope tree
function scopeWalker(scope, op) {
var _walker = (scope) => {
op(scope);
var currentChildScope = scope.$$childHead;
while(currentChildScope) {
_walker(currentChildScope);
currentChildScope = currentChildScope.$$nextSibling;
}
}
_walker(scope);
@bpceee
bpceee / jsType.js
Created August 22, 2017 03:43
get real js type
function type(obj) {
var toString = Object.prototype.toString;
var map = {
'[object Boolean]' : 'boolean',
'[object Number]' : 'number',
'[object String]' : 'string',
'[object Function]' : 'function',
'[object Array]' : 'array',
'[object Date]' : 'date',
'[object RegExp]' : 'regExp',