Skip to content

Instantly share code, notes, and snippets.

View wangpin34's full-sized avatar
🎯
Focusing

Penn wangpin34

🎯
Focusing
View GitHub Profile
@wangpin34
wangpin34 / trim.js
Last active May 18, 2016 03:24
trim begin and end spaces of string
function leftTrim(str){
return str.replace(/^[\s\t]+/g, '');
}
function rightTrim(str){
return str.replace(/[\s\t]+$/g, '');
}
function trim(str){
return str.replace(/(^[\s\t]+)|([\s\t]+$)/g, '');
@wangpin34
wangpin34 / cookie.js
Last active May 18, 2016 03:31
cookie handler
@wangpin34
wangpin34 / meta.js
Created August 17, 2016 10:04
fetch meta info contains http-equiv, name
function fetchMetas() {
var metas = [].slice.call(document.getElementsByTagName('meta')).map(function(e) {
var data = {}
var key = ''
var value = ''
if(e.getAttribute('name')){
data.name = e.getAttribute('name')
data.content = e.getAttribute('content')
return data
@wangpin34
wangpin34 / fileUtil.js
Created November 10, 2016 08:39
Nodejs file util
'use strict'
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const path = require('path');
/*
* Bom headers are 3 Bytes binary
*/
const removeBom = filePath => {
/*
* Add pre zero to number which is less than 10, and then return the final string
* @ num Number 0
*/
function addPreZero(num) {
if(arguments.length == 0) num = 0;
if(num < 10){
return '0' + num;
}
return '' + num;
@wangpin34
wangpin34 / browser-features.js
Last active March 2, 2017 05:44
Turn off these browser features which is not proper for your application like text selection, drag then open the file etc.
/*
* Disable text selection when double click or drag cursor
* @dom html element
*/
function disableTextSelection(dom){
if(dom.addEventListener){
dom.addEventListener('mousedown', function(event){
event.stopPropagation();
event.preventDefault();
})
@wangpin34
wangpin34 / winLocale.js
Created March 17, 2017 02:18
retrieve locale and language info of windows os
const spawn = require('child_process').spawn;
const locales = spawn('systeminfo.exe', []);
const localeReg = /([a-z]+-[a-z]+);/ig;
let result = "";
locales.stdout.on('data', (data) => {
if(data && data.length > 0){
result += data;
@wangpin34
wangpin34 / Q.js
Last active March 31, 2017 02:46
manage event listener
/**
* Lib Q
* For easliy manage event listener
* v0.0.2
*/
(function(w){
function Q(){
var elements = [];
@wangpin34
wangpin34 / urlUtil.js
Last active April 7, 2017 09:48
Url parameter serialized and parsed
/**
* @param jsonObj {Object} Sample: { name: 'wangpin', job: 'engineer' }
* @return params {String} Sample: name=wangpin&job=engineer
*/
function stringifyParams(jsonObj) {
if(typeof jsonObj !== 'object' || !(jsonObj instanceof Object)){
throw new Error('Not a valid json object');
}
var result = [];
@wangpin34
wangpin34 / Query.java
Created May 5, 2017 10:21
Java baidu image
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;