UNPKG

5.53 kBJavaScriptView Raw
1// BEGIN(BROWSER)
2/*
3 Utilities
4*/
5var Util = (function() {
6 var Util = {}
7
8 Util.extend = function extend() {
9 var target = arguments[0] || {},
10 i = 1,
11 length = arguments.length,
12 options, name, src, copy, clone
13
14 if (length === 1) {
15 target = this
16 i = 0
17 }
18
19 for (; i < length; i++) {
20 options = arguments[i]
21 if (!options) continue
22
23 for (name in options) {
24 src = target[name]
25 copy = options[name]
26
27 if (target === copy) continue
28 if (copy === undefined) continue
29
30 if (Util.isArray(copy) || Util.isObject(copy)) {
31 if (Util.isArray(copy)) clone = src && Util.isArray(src) ? src : []
32 if (Util.isObject(copy)) clone = src && Util.isObject(src) ? src : {}
33
34 target[name] = Util.extend(clone, copy)
35 } else {
36 target[name] = copy
37 }
38 }
39 }
40
41 return target
42 }
43
44 Util.each = function each(obj, iterator, context) {
45 var i, key
46 if (this.type(obj) === 'number') {
47 for (i = 0; i < obj; i++) {
48 iterator(i, i)
49 }
50 } else if (obj.length === +obj.length) {
51 for (i = 0; i < obj.length; i++) {
52 if (iterator.call(context, obj[i], i, obj) === false) break
53 }
54 } else {
55 for (key in obj) {
56 if (iterator.call(context, obj[key], key, obj) === false) break
57 }
58 }
59 }
60
61 Util.type = function type(obj) {
62 return (obj === null || obj === undefined) ? String(obj) : Object.prototype.toString.call(obj).match(/\[object (\w+)\]/)[1].toLowerCase()
63 }
64
65 Util.each('String Object Array RegExp Function'.split(' '), function(value) {
66 Util['is' + value] = function(obj) {
67 return Util.type(obj) === value.toLowerCase()
68 }
69 })
70
71 Util.isObjectOrArray = function(value) {
72 return Util.isObject(value) || Util.isArray(value)
73 }
74
75 Util.isNumeric = function(value) {
76 return !isNaN(parseFloat(value)) && isFinite(value)
77 }
78
79 Util.keys = function(obj) {
80 var keys = [];
81 for (var key in obj) {
82 if (obj.hasOwnProperty(key)) keys.push(key)
83 }
84 return keys;
85 }
86 Util.values = function(obj) {
87 var values = [];
88 for (var key in obj) {
89 if (obj.hasOwnProperty(key)) values.push(obj[key])
90 }
91 return values;
92 }
93
94 /*
95 ### Mock.heredoc(fn)
96
97 * Mock.mockjax(fn)
98
99 以直观、安全的方式书写(多行)HTML 模板。
100
101 **使用示例**如下所示:
102
103 var tpl = Mock.heredoc(function() {
104 /*!
105 {{email}}{{age}}
106 <!-- Mock {
107 email: '@EMAIL',
108 age: '@INT(1,100)'
109 } -->
110 *\/
111 })
112
113 **相关阅读**
114 * [Creating multiline strings in JavaScript](http://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript)、
115 */
116 Util.heredoc = function heredoc(fn) {
117 // 1. 移除起始的 function(){ /*!
118 // 2. 移除末尾的 */ }
119 // 3. 移除起始和末尾的空格
120 return fn.toString()
121 .replace(/^[^\/]+\/\*!?/, '')
122 .replace(/\*\/[^\/]+$/, '')
123 .replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '') // .trim()
124 }
125
126 Util.noop = function() {}
127
128 return Util
129})()
130
131// END(BROWSER)
132
133module.exports = Util
134
135if (!console.group) {
136 console._log = console.log
137 console._indent = ''
138 console._styles = {
139 //styles
140 'bold': ['\033[1m', '\033[22m'],
141 'italic': ['\033[3m', '\033[23m'],
142 'underline': ['\033[4m', '\033[24m'],
143 'inverse': ['\033[7m', '\033[27m'],
144 //grayscale
145 'white': ['\033[37m', '\033[39m'],
146 'grey': ['\033[90m', '\033[39m'],
147 'black': ['\033[30m', '\033[39m'],
148 //colors
149 'blue': ['\033[34m', '\033[39m'],
150 'cyan': ['\033[36m', '\033[39m'],
151 'green': ['\033[32m', '\033[39m'],
152 'magenta': ['\033[35m', '\033[39m'],
153 'red': ['\033[31m', '\033[39m'],
154 'yellow': ['\033[33m', '\033[39m']
155 }
156
157 console.log = function() {
158 var args = [].slice.call(arguments, 0)
159 if (args[0] === '[context]') args[0] = console._styles.green[0] + args[0] + console._styles.green[1]
160 if (args[0] === '[options]') args[0] = console._styles.yellow[0] + args[0] + console._styles.yellow[1]
161 if (console._indent) {
162 args = args.join(' ').split('')
163 for (var i = 0, len = args.length; i < len; i++) {
164 if (i > 0 && i % 150 === 0) args.splice(i, 0, '\n' + console._indent)
165 }
166 args = [args.join('')]
167 args = [console._indent.slice(0, console._indent.length - 4) + '├── '].concat(args)
168 }
169 console._log.apply(console, args)
170 }
171
172 console.group = function() {
173 var args = [].slice.call(arguments, 0),
174 style = console._styles.bold;
175 args[0] = style[0] + args[0] + style[1]
176 console.log.apply(console, args)
177
178 console._indent += '│ ' // │ ├ ─ ─
179 }
180 console.groupEnd = function() {
181 console._indent = console._indent.slice(0, console._indent.length - 4)
182 }
183
184}
\No newline at end of file