Created
October 24, 2016 08:35
-
-
Save wuliupo/d4c70d89930e7aba4bca1ee4ba7e26cd to your computer and use it in GitHub Desktop.
Utilities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var utils = window.utils = { | |
| random: function (min, max) { | |
| min = min || 0; | |
| max = max || 10000; | |
| return Math.floor(Math.random() * (max - min + 1)) + min; | |
| }, | |
| uuid: function (identifyNumber, options) { | |
| var time = new Date().getTime(), | |
| uuid = ('xxxxxxxx-xxxx-' + identifyNumber + 'xxx-yxxx-xxxxxxxxxxxx').replace(/[xy]/g, function (char) { | |
| var random = (time + Math.random() * 16) % 16 | 0; | |
| time = Math.floor(time / 16); | |
| return (char === 'x' ? random : (random & 0x7 | 0x8)).toString(16); | |
| }); | |
| if(options && !options.classic){ | |
| uuid = uuid.replace(/-/g, ''); | |
| if(options.length && length < 35){ | |
| uuid = uuid.substr(0, options.length); | |
| } | |
| } | |
| if(options && options.prefix){ | |
| uuid = options.prefix + uuid; | |
| } | |
| return uuid; | |
| }, | |
| get: function (object, paths, defaultValue, fromIndex) { | |
| if(!fromIndex) { | |
| fromIndex = 0; | |
| } | |
| if(!object || !paths || paths.length <= fromIndex) { | |
| return defaultValue; | |
| } | |
| var operate = paths[fromIndex]; | |
| object = (typeof operate === 'function') ? operate(object) : object[operate]; | |
| fromIndex++; | |
| return paths.length === fromIndex ? object : utils.get(object, paths, defaultValue, fromIndex); | |
| }, | |
| html2str: function(html){ | |
| var dom = document.createElement('DIV'); | |
| dom.innerHTML = html; | |
| return dom.textContent || dom.innerText || ''; | |
| }, | |
| ellipsis = function(html, max) { | |
| var str = utils.html2str(html); | |
| if (str.length > max) { | |
| str = str.replace(/\r|\n|\r\n/g,' ').substr(0, max).concat('...'); | |
| } | |
| return str; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment