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 / 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 / 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:
/**
* 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 / 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) {
@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 / 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;
@john-yuan
john-yuan / querystring.js
Last active January 12, 2019 09:10
An util to encode object to query string or decode query string to object.
/**
* An util to encode object to query string or decode query string to object.
*
* API:
*
* * `QS.encode(object: Object.<string, *>, keepArrayIndex?: boolean) => string`
* * `QS.decode(string: string) => Object.<string, *>`
*
* @author John Yuan <https://github.com/john-yuan>
* @see {@link https://gist.github.com/john-yuan/fac5c91a6f7b6b6dea4e73f00f5636e2}
@john-yuan
john-yuan / template.js
Last active January 12, 2019 09:26
一个简单的字符串模板函数。
/**
* 模板函数
*
* @author John Yuan <https://github.com/john-yuan>
* @see {@link https://gist.github.com/john-yuan/40994d24f366bda7c98a7ede994afa71}
* @param {string} template 模板字符串
* @param {Object.<string, *>} data 数据对象
* @returns {string} 编译之后的文本
*/
var template = function (template, data) {