/* * @Author: 陶秋峰 * @Date: 2015-12-25 22:49:53 * @Last Modified by: 陶秋峰 * @Last Modified time: 2015-12-25 22:51:33 * @CopyRight 蛮蛮工作室 */ import { Handle } from './interfaces'; const hasOwnProperty = Object.prototype.hasOwnProperty; /** * 生成一个唯一标识ID * @function * @access public * @param {number} [len=36] 长度 * @param {number} [salt=62] 随机数 * @return {string} 生成唯一标识ID(不带-) */ export function uuid(len?: number, salt?: number): string { var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); var chars = CHARS, uuid = [], rnd = Math.random, i; salt = salt || chars.length; if (len) { for (i = 0; i < len; i++) { uuid[i] = chars[0 | rnd() * salt]; } } else { var r; uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'; uuid[14] = '4'; for (i = 0; i < 36; i++) { if (!uuid[i]) { r = 0 | rnd() * 16; uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r & 0xf]; } } } return uuid.join('').replace(/-/g, ''); } /** * Returns an object with a destroy method that, when called, calls the passed-in destructor. * This is intended to provide a unified interface for creating "remove" / "destroy" handlers for * event listeners, timers, etc. * * @param destructor A function that will be called when the handle's `destroy` method is invoked * @return The handle object */ export function createHandle(destructor: () => void): Handle { return { destroy: function () { this.destroy = function () {}; destructor.call(this); } }; } /** * Returns a single handle that can be used to destroy multiple handles simultaneously. * * @param handles An array of handles with `destroy` methods * @return The handle object */ export function create_composite_handle(...handles: Handle[]): Handle { return createHandle(function () { for (let handle of handles) { handle.destroy(); } }); } function is_object(item: any): boolean { return Object.prototype.toString.call(item) === '[object Object]'; } function copy_array(array: any[], inherited: boolean): any[] { return array.map(function (item: any): any { if (Array.isArray(item)) { return copy_array(item, inherited); } return !is_object(item) ? item : _mixin({ deep: true, inherited: inherited, sources: [ item ], target: {} }); }); } interface MixinArgs { deep: boolean; inherited: boolean; sources: {}[]; target: {}; } function _mixin(kwArgs: MixinArgs): {} { const deep = kwArgs.deep; const inherited = kwArgs.inherited; const target = kwArgs.target; for (let source of kwArgs.sources) { for (let key in source) { if (inherited || hasOwnProperty.call(source, key)) { let value: any = ( source)[key]; if (deep) { if (Array.isArray(value)) { value = copy_array(value, inherited); } else if (is_object(value)) { value = _mixin({ deep: true, inherited: inherited, sources: [ value ], target: {} }); } } ( target)[key] = value; } } } return target; } export function deep_mixin(target: {}, ...sources: {}[]): any { return _mixin({ deep: true, inherited: true, sources: sources, target: target }); } export function mixin(target: {}, ...sources: {}[]): any { return _mixin({ deep: true, inherited: true, sources: sources, target: target }); } export function is_identical(a: any, b: any): boolean { return a === b || /* both values are NaN */ (a !== a && b !== b); } export function is_array(it: any): boolean{ return it && (it instanceof Array || typeof it == "array"); } export function is_string(it: any): boolean{ return (typeof it == "string" || it instanceof String); } export function duplicate(source: {}): {} { const target = Object.create(Object.getPrototypeOf(source)); return deep_mixin(target, source); } // export let trim = String.prototype.trim ? // function(str: string): string { return str.trim(); } : // function(str: string): string { return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); }