Skip to content

Instantly share code, notes, and snippets.

View Alex1990's full-sized avatar
🐢
Get Things Done!

Alex Chao Alex1990

🐢
Get Things Done!
View GitHub Profile
@Alex1990
Alex1990 / common-regexp.js
Last active July 17, 2024 07:12
Common regular expressions.
/**
* Common regular expressions
*/
var rules = {
alphabet: /^[a-zA-Z]+$/,
numeric: /^[0-9]+$/,
alphanumeric: /^[0-9a-zA-Z]+$/,
alphadash: /^[0-9a-zA-Z_]+$/,
ascii: /[\x00-\xff]/,
nonascii: /[^\x00-\xff]/
@Alex1990
Alex1990 / mouseToTouch.js
Created October 22, 2014 14:06
Convert mouse events into touch events.
/**
* Convert mouse events into touch events.
* From: http://ross.posterous.com/2008/08/19/iphone-touch-events-in-javascript/
*/
function touchHandler(event) {
var touch = event.changedTouches[0];
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent({
touchstart: "mousedown",
@Alex1990
Alex1990 / self-signed.sh
Last active February 15, 2020 16:22
Generate a self-signed ssl certificate
#!/usr/bin/env bash
# The "Common Name (CN)" can be "example.com" or "*.example.com www.example.com"
# This command should be executed in non-root mode
domain="$1"
if [ -z "$domain" ]; then
echo "need domain parameter"
exit 1
@Alex1990
Alex1990 / retry.js
Last active December 28, 2019 19:29
Retry a function the specified times
async function retry(fn, times = 0, ...args) {
if (typeof times !== 'number') throw new Error('times must be a number')
if (times < 0) throw new Error('times must not be less than 0')
const totalTimes = times + 1
let result
let firstError
let i = 0
for (; i < totalTimes; i++) {
try {
result = await fn(...args)
@Alex1990
Alex1990 / safeActiveElement.js
Last active December 20, 2019 09:54
Get the current active element safely.
/**
* Get the current active element safely.
* Ref: https://github.com/jquery/jquery-ui/blob/2b84531ae9331f60e4d739fabca6d78abde89ae1/ui/safe-active-element.js
*/
function safeActiveElement(doc) {
doc = doc || document;
var activeElement;
// Support: IE 9 only
// IE9 throws an "Unspecified error" accessing document.activeElement from an <iframe>
@Alex1990
Alex1990 / strLen.js
Last active September 18, 2019 02:29
Count a string(mixing English and Chinese characters) length, and this is a rough function.
/**
* Description: Count a string (mixing English and Chinese characters) length.
* A basic and rough function.
*
* Performance:
* Multiple methods performance test on http://jsperf.com/count-string-length.
* You can see that using regexp to check range is very slow from the above test page.
*/
function strLen(str) {
var count = 0;
@Alex1990
Alex1990 / scrollable.js
Last active July 19, 2019 02:35
Stop propagation for scroll/mousewheel
/**
* Stop propagation behavior for scroll of the element but body.
* Compatibility: IE9+
* Ref:
* - http://stackoverflow.com/questions/5802467/prevent-scrolling-of-parent-element#answer-16324762
* - https://developer.mozilla.org/en-US/docs/Web/Events/wheel
*/
;(function ($) {
$.fn.scrollable = function () {
this.on('wheel', function (event) {
@Alex1990
Alex1990 / alexa.js
Created May 17, 2019 07:08 — forked from chilts/alexa.js
Getting the Alexa top 1 million sites directly from the server, unzipping it, parsing the csv and getting each line as an array.
var request = require('request');
var unzip = require('unzip');
var csv2 = require('csv2');
request.get('http://s3.amazonaws.com/alexa-static/top-1m.csv.zip')
.pipe(unzip.Parse())
.on('entry', function (entry) {
entry.pipe(csv2()).on('data', console.log);
})
;
@Alex1990
Alex1990 / Perf.js
Created March 30, 2019 07:53
Perf class
class Perf {
constructor () {
this.init()
this.phases = []
}
init () {
const now = Date.now()
this.start = now
this.lastTime = now
@Alex1990
Alex1990 / promisify.js
Created January 10, 2019 11:35
Like Node.js util.promisify
function promisify(func) {
return (...args) => {
const args1 = args.slice(0, args.length);
return new Promise((resolve, reject) => {
const callback = (err, ...rest) => {
if (err) {
reject(err);
} else {
resolve(...rest);
}