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
var stack1 = [];
var stack2 = [];
function isEmptyStack(stack) {
return stack.length === 0;
}
function enqueue(obj) {
return stack1.push(obj);
}
@Alex1990
Alex1990 / CustomEvent.js
Last active August 29, 2015 14:23
A cross-browser custom event module.
/**
* A cross-browser custom event module.
*/
var global = window;
var NativeCustomEvent = window.CustomEvent;
function isCustomEvent() {
try {
var p = new NativeCustomEvent('cat', { detail: { foo: 'bar' } });
@Alex1990
Alex1990 / input-event.js
Last active August 29, 2015 14:23
A simple cross browser input event.
/**
* A simple cross browser input event.
*/
function input(node, callback) {
var doc = document;
var isIE9 = doc.documentMode && (doc.documentMode === 9);
var isInputSupported = ('oninput' in node) && !isIE9;
var inputValue = node.value;
var data = ["ActionScript", "C", "C#", "C++", "Clojure", "CoffeeScript", "CSS", "Go", "Haskell", "HTML", "Java", "JavaScript", "Lua", "Matlab", "Objective-C", "Perl", "PHP", "Python", "R", "Ruby", "Scala", "Shell", "Swift", "TeX", "VimL", "ABAP", "Ada", "Agda", "AGS Script", "Alloy", "AMPL", "Ant Build System", "ANTLR", "ApacheConf", "Apex", "API Blueprint", "APL", "AppleScript", "Arc", "Arduino", "AsciiDoc", "ASP", "AspectJ", "Assembly", "ATS", "Augeas", "AutoHotkey", "AutoIt", "Awk", "Batchfile", "Befunge", "Bison", "BitBake", "BlitzBasic", "BlitzMax", "Bluespec", "Boo", "Brainfuck", "Brightscript", "Bro", "C-ObjDump", "C2hs Haskell", "Cap'n Proto", "CartoCSS", "Ceylon", "Chapel", "ChucK", "Cirru", "Clarion", "Clean", "CLIPS", "CMake", "COBOL", "ColdFusion", "ColdFusion CFC", "Common Lisp", "Component Pascal", "Cool", "Coq", "Cpp-ObjDump", "Creole", "Crystal", "Cucumber", "Cuda", "Cycript", "Cython", "D", "D-ObjDump", "Darcs Patch", "Dart", "desktop", "Diff", "DIGITAL Command Language", "DM", "Dockerfile",
@Alex1990
Alex1990 / timestamp.js
Created June 11, 2015 09:28
Unix timestamp
/**
* Unix timestamp - The number of seconds that have elapsed since 1970-01-01 00:00:00 UTC.
*/
function timestamp() {
return Math.round((Date.now ? Date.now() : new Date()) / 1000);
}
/**
* switch vs object
*/
var dateMethods = {
Y: 'FullYear',
M: 'Month',
D: 'Date',
h: 'Hours',
m: 'Minutes',
@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 / string-template.js
Last active August 29, 2015 14:21
A simple string template.
/**
* A simple string template function.
*
* For example:
*
* var str = 'Hi, {{ username }}! Your age is {{ age }}. You locate in {{ location.city }}, {{ location.province }}.';
* var user = {
* username: 'Alex Chao',
* age: 25,
* location: {
@Alex1990
Alex1990 / randPick.js
Last active August 29, 2015 14:19
Pick N elements from an array randomly.
/**
* Pick N elements from an array randomly.
*/
function randPick(arr, n) {
var len = arr.length;
var randIdx;
var tmp;
while (n--) {
@Alex1990
Alex1990 / escapeRegexp.js
Last active August 29, 2015 14:18
Escape regexp special characters.
/**
* Escape regexp special characters. The slash `/` will be escaped automatically.
* http://stackoverflow.com/questions/494035/how-do-you-pass-a-variable-to-a-regular-expression-javascript/494122#494122
*/
function escapeRegexp(str) {
return str.replace(/[.*+?^$|[\](){}\\-]/g, '\\$&');
}