forked from adamschwartz/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.coffee
84 lines (66 loc) · 2.04 KB
/
log.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
return unless window.console and window.console.log
log = ->
args = []
makeArray(arguments).forEach (arg) ->
if typeof arg is 'string'
args = args.concat stringToArgs arg
else
args.push arg
_log.apply window, args
_log = ->
console.log.apply console, makeArray(arguments)
makeArray = (arrayLikeThing) ->
Array::slice.call arrayLikeThing
formats = [{
# Italic
regex: /\*([^\*)]+)\*/
replacer: (m, p1) -> "%c#{p1}%c"
styles: -> ['font-style: italic', '']
}, {
# Bold
regex: /\_([^\_)]+)\_/
replacer: (m, p1) -> "%c#{p1}%c"
styles: -> ['font-weight: bold', '']
}, {
# Code
regex: /\`([^\`)]+)\`/
replacer: (m, p1) -> "%c#{p1}%c"
styles: -> ['background: rgb(255, 255, 219); padding: 1px 5px; border: 1px solid rgba(0, 0, 0, 0.1)', '']
}, {
# Custom syntax: [c="color: red"]red[c]
regex: /\[c\=\"([^\")]+)\"\]([^\[)]+)\[c\]/
replacer: (m, p1, p2) -> "%c#{p2}%c"
styles: (match) -> [match[1], '']
}]
hasMatches = (str) ->
_hasMatches = false
formats.forEach (format) ->
if format.regex.test str
_hasMatches = true
return _hasMatches
getOrderedMatches = (str) ->
matches = []
formats.forEach (format) ->
match = str.match format.regex
if match
matches.push
format: format
match: match
return matches.sort((a, b) -> a.match.index - b.match.index)
stringToArgs = (str, args) ->
styles = []
while hasMatches str
matches = getOrderedMatches str
firstMatch = matches[0]
str = str.replace firstMatch.format.regex, firstMatch.format.replacer
styles = styles.concat firstMatch.format.styles(firstMatch.match)
[str].concat styles
# TODO - replace these with a feature test
isSafari = -> /Safari/.test(navigator.userAgent) and /Apple Computer/.test(navigator.vendor)
isIE = -> /MSIE/.test(navigator.userAgent)
# Export
if isSafari() or isIE()
window.log = _log
else
window.log = log
window.log.l = _log