Skip to content

Instantly share code, notes, and snippets.

View popomore's full-sized avatar
💭
I may be slow to respond.

Haoliang Gao popomore

💭
I may be slow to respond.
View GitHub Profile
@popomore
popomore / gist:5585736
Created May 15, 2013 17:31
nodejs 文件权限转换
var stat = fs.statSync(dir);
(stat.mode & 07777).toString(8); // => e.g. 755
var https = require('https');
var Base64 = require('js-base64').Base64; // can use other base64 implement
var twitterConsumerKey = 'your consumer key';
var twitterConsumerSecret = 'your consumer secret';
var encodedBearerToken = Base64.encode(twitterConsumerKey + ':' + twitterConsumerSecret);
var options = {
hostname: 'api.twitter.com',
path: '/oauth2/token',
@popomore
popomore / redirect.js
Created May 28, 2013 16:29
shell redirect in nodejs
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
function run(cmd) {
var output, out, index = cmd.indexOf('>');
if (index > -1) {
output = cmd.substring(index + 1).replace(/(^\s+|\s+$)/g, '');
cmd = cmd.substring(0, index).split(/\s+/);
} else {
@popomore
popomore / format.js
Created July 27, 2013 14:40
string format
function format(fmt, args) {
if (!fmt) return '';
var toString = Object.prototype.toString;
if (toString.call(args) === '[object Object]') {
return fmt.replace(/%{?([a-z]*)}?/g, function(all, m1){
return args[m1] ? args[m1] : all;
});
} else {
if (toString.call(args) !== '[object Array]') {
@popomore
popomore / Gruntfile.js
Last active December 20, 2015 07:39
Gruntfile 的一些配置
module.exports = function(grunt) {
grunt.initConfig({
// https://github.com/gruntjs/grunt-contrib-watch
// grunt.loadNpmTasks('grunt-contrib-watch');
// 页面上插入 <script src="http://localhost:35729/livereload.js"></script>
watch: {
scripts: {
files: ['**/*'],
task: [],
@popomore
popomore / download.js
Created July 29, 2013 13:59
simple download
var fs = require('fs');
var path = require('path');
var url = require('url');
var protocol = {
https: require('https'),
http: require('http')
};
function download(src, dest, cb) {
var options = url.parse(src);
function readDirs(dir) {
var result = [];
fs.readdirSync(dir)
.forEach(function(file) {
var sub = path.join(dir, file);
if (fs.statSync(sub).isDirectory()) {
result = result.concat(readDirs(sub).map(function(subFile) {
return path.join(file, subFile);
}));
} else {
@popomore
popomore / uniq.js
Created November 27, 2013 11:45
数组去重
Array.prototype.uniq = function() {
return this.filter(function(item, index, arr) {
return index === arr.indexOf(item);
});
};
// test
[1,2,3,2,1,2,4,6,8,3,2].uniq();
@popomore
popomore / README.md
Last active January 2, 2016 11:19
alfred cdn
# install gist by `brew install gist`
#
# create gist on first time
# update gist when .gistid exists
function gist() {
local gistid
args=$@
if [ -f .gistid ]; then
gistid=$(cat .gistid)