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 / map-polyfill.js
Last active August 29, 2015 14:04
This is my `Array.prototype.map` polyfill.
/**
* Author: Alex Chao(alexchao1990@gmail.com)
* Date: 2014-08-01
*/
/**
* Note: You can choose the other polyfill which is compatible with ECMA-262, 5th edition
* Url: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
*/
@Alex1990
Alex1990 / forEach-polyfill.js
Created August 2, 2014 03:03
My `Array.prototype.forEach` polyfill.
/**
* Author: Alex Chao(alexchao1990@gmail.com)
* Date: 2014-08-02
*/
/**
* Note: You can choose the other polyfill which is compatible with ECMA-262, 5th edition
* Url: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
*/
@Alex1990
Alex1990 / isDigit.js
Last active August 29, 2015 14:05
Check if a string consists of the digit characters (0-9)
// Check if a string consists of the digit characters (0-9)
function isDigit(str) {
return str && !/[^\d]/.test(str);
}
@Alex1990
Alex1990 / isValidDate.js
Created August 18, 2014 00:57
Check if an object is a valid Date object
/*
* Ref: http://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript
*/
function isValidDate(d) {
return Object.prototype.toString.call(d) === '[object Date]' && !isNaN(d.valueOf());
}
@Alex1990
Alex1990 / isEmpty.js
Created August 18, 2014 01:41
Check if a string is empty (not contains any characters)
/*
* Ref: http://stackoverflow.com/questions/154059/how-do-you-check-for-an-empty-string-in-javascript
*/
// `str` must be a string
function isEmpty(str) {
return !!str.length;
}
// check if the `str` is empty, null or undefined (`str` may be 0 or false)
@Alex1990
Alex1990 / isBlank.js
Created August 18, 2014 01:51
check if a string is blank (may contain white-space)
/*
* Ref: http://stackoverflow.com/questions/154059/how-do-you-check-for-an-empty-string-in-javascript
*/
function isBlank(str) {
return !str || !/\S/.test(str);
}
@Alex1990
Alex1990 / isFullYear.js
Created August 18, 2014 02:02
check if the digit of the number is expected
// when you need a range, you'd better use comparison operators
function isFullYear(year) {
return !/^\d{4}$/.test(year);
}
@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 / isFalsy.js
Last active August 29, 2015 14:05
Check if a value is falsy. The word "falsy" means the value is one of undefined, null, false, 0, NaN or an empty string "".
// Check if a value is falsy. This is, its value is one of undefined, null, false, 0, NaN or an empty string "".
// Also, you can use the Boolean function to convert the value.
function isFalsy(o) {
return !o;
}
@Alex1990
Alex1990 / isTruthy.js
Last active October 24, 2018 18:04
Check if a value is truthy. The word "truthy" means the value is not one of undefined, null, false, 0, NaN or an empty string "".
// Check if a value is truthy. The word truthy means the value is not one of undefined, null, false, 0, NaN or an empty string "".
// Also, you can use the Boolean function to convert the value.
function isTruthy(o) {
return !!o;
}