Skip to content

Instantly share code, notes, and snippets.

View john-yuan's full-sized avatar

JOHN YUAN john-yuan

View GitHub Profile
/**
* 将值转换为字符串并移除首尾空白字符
*
* @param {any} value 需要转换的值
* @returns {string}
*/
function toTrimedString(value) {
if (typeof value !== 'string') {
if (value === null || value === undefined) {
value = '';
@john-yuan
john-yuan / getCalendarDateList.js
Last active June 15, 2019 01:48
获取指定月份日历视图中的 42 个日期对象。
/**
* 获取指定月份日历视图中的 42 个日期对象。
*
* @param {number} year 完整的年份数字,如 `2019` 即代表 2019 年。
* @param {number} month 月份数字,如 `11` 即代表 11 月。
* @param {number} [startWeekDay=0] 可选的参数,默认为 `0`(星期日),表示日历中的第一列为星期几(一般为星期日或者星期一)。其中 `0` 表
* 示星期日,`1` 表示星期一,后面以此类推。
* @returns {Date[]} 返回一个数组,包含 42 个 `Date` 对象。
*/
function getCalendarDateList(year, month, startWeekDay) {
/**
* Convert rgb formatted color string to hash formatted color string
*
* @example
* // @returns #ff00ff
* rgb2hash('rgb(255, 0, 255)');
*
* @param {string} rgbColorStr The rgb formatted color string
/**
* 复制数据结构定义(definition)中指定的内容
*
* @param {Object.<string, *>} definition 数据结构定义
* @param {Object.<string, *>} dataSource 数据源
* @param {Object.<string, *>} [options] (可选)附加选项
* @param {any} [options.defaultObject=null] (可选)当数据源中没有定义中的对象时使用的默认值
* @param {any} [options.defaultArray=[]] (可选)当数据源中没有定义中的数组时使用的默认值
* @returns {Object.<string, *>} 返回过滤后的数据
*/
/**
* 主域名跨标签轻量级事件通知机制
*
* 主要用于具有相同主域名的多个浏览器标签之间的事件通知。比如现在浏览器中有两个标签,
* 第一个标签的域名为: site1.example.com;第二个标签的域名为:site2.example.com。
* 两个标签中都可以进行登录操作,但在登录之后需要通知另一个标签。此时便可以使用这个类。
*
* 注意:
*
* 1. 这个类使用 cookie 作为通信媒介
var onceTransitionEnd = function (node, time, callback) {
var finished = false;
var timer = setTimeout(listener, time + 50);
var listener = function () {
if (finished === false) {
removeListener();
node = null;
time = null;
listener = null;
removeListener = null;
/**
* @see https://gist.github.com/john-yuan/e53f55d3e8ec63c3b55ceb502bf5ce36
*/
var ICibaDictionary = (function () {
'use strict';
var BASE_URL = 'http://dict-co.iciba.com/api/dictionary.php';
var TYPE_JSON = 'json';
var TYPE_XML = 'xml';
@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) {
@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 / 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;