Skip to content

Instantly share code, notes, and snippets.

View lili21's full-sized avatar
🎯
Focusing

li.li lili21

🎯
Focusing
View GitHub Profile
@lili21
lili21 / gist:3f33850c6a4f083c2df9
Last active August 29, 2015 14:07
angular watchers counts
(function () {
var root = angular.element(document.getElementsByTagName('body'));
var watchers = [];
var f = function (element) {
angular.forEach(['$scope', '$isolateScope'], function (scopeProperty) {
if (element.data() && element.data().hasOwnProperty(scopeProperty)) {
angular.forEach(element.data()[scopeProperty].$$watchers, function (watcher) {
watchers.push(watcher);
@lili21
lili21 / gist:1e2821e56d3ca5dc17d7
Last active August 29, 2015 14:07
extend deep ,
function type(source) {
return Object.prototype.toString.call(source).replace(/^\[object (.+)\]/g, '$1').toLowerCase();
}
function forEach(source, callback) {
var type = type(source)
if (type === 'array')
for (var i = 0, l = source.length;i < l;i++) {
callback(source[i], i);
}
}
@lili21
lili21 / dataBind.js
Last active August 29, 2015 14:07 — forked from euforic/dataBind.js
/**
* two way data binding
*/
// create root scope
window.$rootScope = {};
Array.apply(null, {length: N}).map(Number.call, Number)
Array.apply(null, {length: N}).map(Function.call, Math.random)
@lili21
lili21 / gist:4c4edd06fd3f23d8c71e
Last active August 29, 2015 14:21
determine if an element is in the visible viewport, 判断元素是否在可视区域内
// Determine if an element is in the visible viewport
function isInViewport(element) {
var rect = element.getBoundingClientRect();
var html = document.documentElement;
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || html.clientHeight) &&
rect.right <= (window.innerWidth || html.clientWidth)
);
@lili21
lili21 / b2b.js
Created June 1, 2015 09:13
base64 to blob
function b64ToUint6 (nChr) {
return nChr > 64 && nChr < 91 ?
nChr - 65
: nChr > 96 && nChr < 123 ?
nChr - 71
: nChr > 47 && nChr < 58 ?
nChr + 4
: nChr === 43 ?
@lili21
lili21 / saveAs.js
Last active August 29, 2015 14:22
save as (front end file saver)
;(function(self) {
'use strict';
/**
* 将base64数据转成Blob对象,
* @param {string} base64 base64字符串
* @param {string} type mime type
* @param {number} size slice size
* @return {object} Blob对象
*/
function b2b(base64) {
@lili21
lili21 / b2b-n.js
Last active August 29, 2015 14:22
base64 to blob (more readable)
function b2b(base64) {
var decode, i, length, byteArray;
i = 0;
decode = atob(base64);
length = decode.length;
byteArray = new Uint8Array(length);
for (;i < length;i++) {
byteArray[i] = decode.charCodeAt(i);
}
HTMLDocument.prototype.ready = function () {
return new Promise(function(resolve, reject) {
if (document.readyState === 'complete') {
resolve(document)
} else {
document.addEventListener('DOMContentLoaded', function() {
resolve(document)
});
}
});
@lili21
lili21 / di.js
Created September 1, 2015 05:27
dependency injection
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;
var FN_ARG_SPLIT = /, ?/g;
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var injector = {
store: {},
register: function(key, value) {
this.store[key] = value;
},
invoke: function(fn) {