Skip to content

Instantly share code, notes, and snippets.

View john-yuan's full-sized avatar

JOHN YUAN john-yuan

View GitHub Profile
@john-yuan
john-yuan / pagination.js
Last active January 6, 2019 08:52
A function to generate the page number list for page links.
/**
* This function is used to generate the page list like below:
*
* ```
* [1] 2 3 4 5 6 » 20
* 1 « 6 7 [8] 9 10 » 20
* 1 « 15 16 17 18 19 [20]
* ```
*
* Note:
@john-yuan
john-yuan / isAbsoluteURL.js
Created January 6, 2019 06:33
Check whether the URL is absolute.
/**
* Check whether the URL is absolute
*
* @param {string} url - The URL to check
* @returns {boolean} - If the url is absolute return true, otherwise return false
*/
var isAbsoluteURL = function (url) {
return /^(?:[a-z][a-z0-9\-\.\+]*:)?\/\//i.test(url);
};
@john-yuan
john-yuan / parseURL.js
Last active January 30, 2019 10:07
Parse the URL string to get the details of the URL.
/**
* Parse the URL string to get the details of the URL.
*
* The names of the properties of the returned object is same with the names of
* the properties in the `window.location` object in the browser, but the
* returned object only have one method, that is toString().
*
* @typedef {Object} URLInformation The URL Information
* @property {string} protocol example: https:
* @property {string} hostname example: developer.mozilla.org
@john-yuan
john-yuan / searchByIndexOf.js
Last active January 8, 2019 14:04
Using String.prorotype.indexOf to search in the data array.
/**
* Using String.prorotype.indexOf to search in the data array.
* This function just do simple match no rank in the result.
*
* @param {string} text text to search
* @param {any[]} array the data list
* @param {string[]} [keys] the keys that hold the string to be searched
* @returns {any[]} the filtered array
*/
var searchByIndexOf = function (text, array, keys) {
/**
* debounce
*
* @param {number} time time to wait next call
* @param {(...any) => any} callback the callback function
* @returns {(...any) => any}
*/
var debounce = function (time, callback) {
var timer = null;
var slice = Array.prototype.slice;
@john-yuan
john-yuan / indexOf.js
Created January 8, 2019 10:52
Get the index of the element in the given array
/**
* Get the index of the element in the given array
*
* @param {any} element the element to find
* @param {any[]} array the array to be searched
* @param {number} [fromIndex=0] the array index at which to begin the search
* @returns {number} returns the index of the element. if the element is not found, -1 is returned.
*/
var indexOf = function (element, array, fromIndex) {
if (typeof Array.prototype.indexOf === 'function') {
@john-yuan
john-yuan / SimpleEvent.js
Last active January 8, 2019 12:37
A simple event registry, designed to exchange message among components.
var SimpleEvent = (function () {
/**
* A simple event registry, designed to exchange message among components
*
* API:
*
* * `SimpleEvent.prototype.emit(eventName, message)`
* * `SimpleEvent.prototype.on(eventName, listener)`
* * `SimpleEvent.prototype.off(eventName, listener)`
* * `SimpleEvent.prototype.once(eventName, listener)`
@john-yuan
john-yuan / closestNode.js
Created January 9, 2019 01:25
Get the first Node that has the className in the path from the fromNode to the toNode.
/**
* Get the first `Node` that has the `className` in the path from the `fromNode` to the `toNode`
*
* @param {Node} fromNode The `Node` at which the search begins
* @param {string} className The class name to test (only single className)
* @param {Node} [toNode] The optional `Node` at which the search ends
* @returns {Node} Returns the `Node` we found, if not found, `null` is returned
*/
var closestNode = function (fromNode, className, toNode) {
var node = null;
@john-yuan
john-yuan / isNormalObject.js
Last active January 30, 2019 05:11
Check wether the variable passed in is a normal object.
/**
* Check whether the variable passed in is a normal object. Normal object means
* an object that is created by `{}` or `new Object()`.
*
* @param {any} it The variable to check
* @returns {boolean} Returns `true` if `it` is a normal object
*/
var isNormalObject = function (it) {
if (!it || '[object Object]' !== {}.toString.call(it)) {
return false;
@john-yuan
john-yuan / merge.js
Last active January 10, 2019 01:47
Merge the source object into the target object.
/**
* Merge the `source` into the `target`. This function will do deep copy, and
* modify the target object.
*
* @param {Object.<string, *>|any[]} target The target to merge into
* @param {Object.<string, *>|any[]} srouce The source to copy
* @returns {Object.<string, *>|any[]} returns The modified target
*/
var merge = function(target, source) {
var toString = Object.prototype.toString;