Skip to content

Instantly share code, notes, and snippets.

@hcl1687
hcl1687 / type.js
Last active April 19, 2017 03:32
type
const types = 'Boolean Number String Function Array Date RegExp Object Error'.split(' ')
const class2type = {}
types.forEach(name => {
class2type['[object ' + name + ']'] = name.toLowerCase()
})
function type (obj) {
if (obj === null) {
return obj + ''
}
@hcl1687
hcl1687 / runStep.js
Last active April 10, 2017 06:07
runStep.js
export function runSteps (arr) {
if (arr.length === 0) {
return
}
return arr.reduce(function(promise, item) {
return promise.then(createStep(item))
}, Promise.resolve([]))
}
function createStep ({ step, timeout }) {
return new Promise(function (resolve, reject) {
@hcl1687
hcl1687 / sleep.js
Created February 13, 2017 12:27
js sleep function
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function demo() {
console.log('Taking a break...');
await sleep(2000);
console.log('Two second later');
}
@hcl1687
hcl1687 / classTool.js
Created January 13, 2017 02:01
operate class attribute
Element.prototype.hasClassName = function (a) {
return new RegExp("(?:^|\\s+)" + a + "(?:\\s+|$)").test(this.className);
};
Element.prototype.addClassName = function (a) {
if (!this.hasClassName(a)) {
this.className = [this.className, a].join(" ");
}
};
@hcl1687
hcl1687 / convertCurrency.js
Created August 2, 2016 11:41
convert number to zh_CN currency
function convertCurrency(currencyDigits) {
// Constants:
var MAXIMUM_NUMBER = 99999999999.99;
// Predefine the radix characters and currency symbols for output:
var CN_ZERO = "零";
var CN_ONE = "壹";
var CN_TWO = "贰";
var CN_THREE = "叁";
var CN_FOUR = "肆";
var CN_FIVE = "伍";
@hcl1687
hcl1687 / handleDispatcher.js
Created July 14, 2016 00:59
handle dispatcher in nd project
function handleDispatcher (options) {
if (!DISPATCHER_ENABLE) {
return
}
const inIgnore = DISPATCHER_IGNORE.some(res => {
return options.url && options.url.indexOf(res) > -1
})
if (inIgnore) {
@hcl1687
hcl1687 / parseUrl.js
Created July 14, 2016 00:57
parsr a url string to a obj
function parseUrl (url) {
const l = document.createElement('a')
l.href = url
const protocol = l.protocol + '//'
const host = l.hostname
const path = decodeURIComponent(l.pathname)
const reg = /\/(v\d\.\d+)(.*)/g
const match = reg.exec(path)
const ver = match && match[1] || ''
const api = ver ? path.replace('/' + ver, '') : path
@hcl1687
hcl1687 / index.html
Created May 27, 2016 03:59 — forked from battlehorse/index.html
Demo script to convert Google Chart Tools charts into PNG images.
<html>
<head>
<script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/rgbcolor.js"></script>
<script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/canvg.js"></script>
<script>
function getImgData(chartContainer) {
var chartArea = chartContainer.getElementsByTagName('iframe')[0].
contentDocument.getElementById('chartArea');
var svg = chartArea.innerHTML;
var doc = chartContainer.ownerDocument;
@hcl1687
hcl1687 / get-all-url-params.js
Created March 29, 2016 07:30
getAllUrlParams 获取所有的URL参数
function getAllParams () {
const params = {}
const reg = /([^&?;]*?)=([^&?;]*)/g
while (let match = reg.exec(location.search)) {
if (match[1]) {
const name = decodeURIComponent(match[1])
const value = match[2] ? decodeURIComponent(match[2]) : null
params[name] = value
}
@hcl1687
hcl1687 / geturlparam.js
Created March 29, 2016 06:30
getURLParam 获取URL参数
// 获取参数
function getURLParam (name) {
return decodeURIComponent(
(new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) ||
['', ''])[1].replace(/\+/g, '%20')) || null
}