Skip to content

Instantly share code, notes, and snippets.

View laispace's full-sized avatar

赖小赖 laispace

  • AntFin
  • Shenzhen
View GitHub Profile
@laispace
laispace / fetchLocalJSON.m
Last active April 1, 2016 01:36
fetch local json file and parse it to a dictionary
- (NSDictionary *)fetchLocalJSON:(NSString *) pathForResource {
NSDictionary *dictionary;
// read JSON file
NSString *fileName = [[NSBundle mainBundle] pathForResource:pathForResource ofType:@"json"];
if (fileName) {
NSLog(@"file exist: %@", fileName);
// get file data
NSData *fileData = [[NSData alloc] initWithContentsOfFile:fileName];
@laispace
laispace / after.js
Created February 2, 2016 09:49
invoke a callback after all operations
function after (count, callback) {
return function () {
while(--count === 0) {
console.log(arguments);
callback.call(this, arguments);
}
}
}
// define some operations
@laispace
laispace / count-string-length.js
Created November 11, 2015 01:04
计算字符串长度, 中文算两个
function countStringLength (string) {
var len = string.length;
var count = 0;
for (var i = 0; i < len; i ++) {
var num = string.charCodeAt(i);
if (num == 94 || num > 127) {
count += 2;
} else {
count += 1;
}
@laispace
laispace / count-alloy-posts-per-month.py
Last active October 22, 2015 10:11
统计 alloyteam.com 每月博文阅读数量
__author__ = 'alloyteam.com'
# -*- coding: UTF-8 -*-
import urllib.request
import re
class Spider:
def __init__ (self, pageStartNum, pageEndNum, year, monthStart, monthEnd, sortOutput) :
@laispace
laispace / randomArray.js
Created August 31, 2015 12:28
打乱数组顺序
function randomArray (array) {
if (array.length === 1) {
return array;
} else {
var len = array.length;
var index = ~~(Math.random() * len);
var item = array.splice(index, 1);
return item.concat(randomArray(array));
}
@laispace
laispace / openWindowByUrl.js
Created July 28, 2015 08:45
打开一个新页面
/**
* 打开一个新的页面
* @param {String} url
*/
openWindowByUrl: function(url) {
var a = document.createElement('a');
a.href = '';
a.setAttribute("target", '_blank');
var evt = document.createEvent("HTMLEvents");
evt.initEvent("click");
@laispace
laispace / isEnglish.js
Created July 28, 2015 08:37
判断字符是否为英文
/**
* 判断置顶字符是否为英文
* @method isEnglish
* @param {String} s
* @return {Boolean}
*/
isEnglish: function(s) {
var code = ('' + s).charCodeAt(0);
if (code >= 0x0041 && code <= 0x005A || code >= 0x0061 && code <= 0x007A) {
return true;
@laispace
laispace / isChinese.js
Created July 28, 2015 08:36
判断字符是否为中文
/**
* 判断置顶字符是否为中文
* @method isChinese
* @param {String} s
* @return {Boolean}
*/
isChinese: function(s) {
//4E00 - 9FA5
var code = ('' + s).charCodeAt(0);
if (code >= 0x4E00 && code <= 0x9FA5) {
@laispace
laispace / pattern-decorator.js
Created February 8, 2015 08:50
设计模式-装饰者模式
// pattern-decorator.js
// ===== 1. let instance has new functions =====
function Person (name, age) {
this.name = name;
this.age = age;
}
Person.prototype.say = function (something) {
console.log(this.name, 'says: ', something);
@laispace
laispace / pattern-factory.js
Last active August 29, 2015 14:15
设计模式-工厂模式
// ===== 1.pattern-factory.js=====
// Car factory
function Car (options) {
this.type = options.type || 'car';
this.owner = options.owner || null;
this.price = options.price || null;
}
// Trunk factory