Skip to content

Commit 35550ea

Browse files
author
ft
committed
first
0 parents  commit 35550ea

File tree

10 files changed

+394
-0
lines changed

10 files changed

+394
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
branches:
3+
only:
4+
- gh-pages
5+
env:
6+
global:
7+
- SAUCE_USERNAME: ftft1885
8+
- secure: "V/9ZbaLE5CNrXSN945IBzjQSK5pVcACtki32YDwZtY9KgK74exP5Joqh8gWz1P+px9VeRrbcWMQE8N7EVuqbdJIjiY7JFexlr5tuLWgzSDYzzsuMTWGQRO8LmWz+A9gjn8zew4FCzQ+k3jMKA2Hm8L1Lfaq0D2woEPEF6B2rBOs="

_readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
inspired by [tj@debug](https://github.com/visionmedia/debug)
2+
3+
[demo](http://chunpu.github.io/min-debug/)
4+
5+
Usage
6+
---
7+
8+
type `localStorage[mykey] = '*'`
9+
10+
if browser not support localStorage or you want to see debug on page, use url debug
11+
12+
type `mykey='*'` in `location.search` or `location.hash`, and will see debug info in a textarea on page
13+
14+
15+
differences with tj@debug
16+
---
17+
18+
- if you browserify your app, you should init the debug key self
19+
20+
```js
21+
module.exports = exports = require('min-debug')
22+
exports.init('mykey') // default is debug
23+
```
24+
25+
- min-debug does not support print format
26+
27+
```js
28+
debug('my data', {foo: 'bar'}) // min-debug style
29+
debug('my data: %o', {foo: 'bar'}) // tj@debug style, not support
30+
```

browser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module.exports = exports = require('./')
2+
exports.init()

debug.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
module.exports = exports = debug
2+
3+
exports.names = []
4+
exports.skips = []
5+
6+
var colors = 'lightseagreen forestgreen goldenrod dodgerblue darkorchid crimson'.split(' ')
7+
var colorIndex = 0
8+
9+
function noop() {}
10+
11+
function enable(namespaces) {
12+
if (!namespaces) return
13+
var split = namespaces.split(/[\s,]+/)
14+
for (var i = 0; i < split.length; i++) {
15+
if (!split[i]) continue
16+
namespaces = split[i].replace(/\*/g, '.*?')
17+
if ('-' == namespaces[0])
18+
exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'))
19+
else
20+
exports.names.push(new RegExp('^' + namespaces + '$'))
21+
}
22+
}
23+
24+
function enabled(name) {
25+
var i = 0, reg
26+
for (i = 0; reg = exports.skips[i++];) {
27+
if (reg.test(name)) return false
28+
}
29+
for (i = 0; reg = exports.names[i++];) {
30+
if (reg.test(name)) return true
31+
}
32+
}
33+
34+
function getColor() {
35+
return colors[colorIndex++ % colors.length]
36+
}
37+
38+
var prev
39+
var inherit = 'color:inherit'
40+
var console = global.console
41+
42+
exports.log = function(namespace, args, color) {
43+
var curr = +new Date()
44+
var ms = curr - (prev || curr)
45+
prev = curr
46+
47+
var label = 'ad:ares:' + namespace
48+
var main = '%c' + label + '%c'
49+
var arr = [null, color, inherit]
50+
for (var i = 0; i < args.length; i++) {
51+
arr.push(args[i])
52+
main += ' %o'
53+
}
54+
arr.push(color)
55+
main += '%c +' + ms + 'ms'
56+
arr[0] = main
57+
console.debug.apply(console, arr)
58+
}
59+
60+
function debug(namespace) {
61+
var color = 'color:' + getColor()
62+
return enabled(namespace) ? function() {
63+
exports.log(namespace, arguments, color)
64+
} : noop
65+
}
66+
67+
exports.init = function(key) {
68+
key = key || 'debug'
69+
var reg = new RegExp(key + '=(\\S+)')
70+
var res = reg.exec(location.href)
71+
if (res) {
72+
enable(res[1])
73+
var doc = document
74+
var elem = doc.createElement('textarea')
75+
var style = elem.style
76+
style.width = '100%'
77+
style.height = '300px'
78+
style.overflow = 'auto'
79+
var box = doc.body || doc.documentElement
80+
box.insertBefore(elem, box.firstChild)
81+
82+
exports.log = function(namespace, arr, color) {
83+
var ret = ['[' + namespace + ']']
84+
var len = arr.length
85+
for (var i = 0; i < len; i++) {
86+
var val = arr[i]
87+
try {
88+
val = JSON.stringify(val, 0, 4)
89+
} catch (e) {
90+
val = val + ''
91+
}
92+
ret.push(val)
93+
}
94+
elem.value += ret.join(' ') + '\n\n'
95+
}
96+
97+
} else if (global.localStorage && console) {
98+
try {
99+
enable(localStorage[key])
100+
} catch (ignore) {}
101+
}
102+
}

dist/debug.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.debug=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2+
module.exports = exports = require('./')
3+
exports.init()
4+
5+
},{"./":2}],2:[function(require,module,exports){
6+
(function (global){
7+
module.exports = exports = debug
8+
9+
exports.names = []
10+
exports.skips = []
11+
12+
var colors = 'lightseagreen forestgreen goldenrod dodgerblue darkorchid crimson'.split(' ')
13+
var colorIndex = 0
14+
15+
function noop() {}
16+
17+
function enable(namespaces) {
18+
if (!namespaces) return
19+
var split = namespaces.split(/[\s,]+/)
20+
for (var i = 0; i < split.length; i++) {
21+
if (!split[i]) continue
22+
namespaces = split[i].replace(/\*/g, '.*?')
23+
if ('-' == namespaces[0])
24+
exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'))
25+
else
26+
exports.names.push(new RegExp('^' + namespaces + '$'))
27+
}
28+
}
29+
30+
function enabled(name) {
31+
var i = 0, reg
32+
for (i = 0; reg = exports.skips[i++];) {
33+
if (reg.test(name)) return false
34+
}
35+
for (i = 0; reg = exports.names[i++];) {
36+
if (reg.test(name)) return true
37+
}
38+
}
39+
40+
function getColor() {
41+
return colors[colorIndex++ % colors.length]
42+
}
43+
44+
var prev
45+
var inherit = 'color:inherit'
46+
var console = global.console
47+
48+
exports.log = function(namespace, args, color) {
49+
var curr = +new Date()
50+
var ms = curr - (prev || curr)
51+
prev = curr
52+
53+
var label = 'ad:ares:' + namespace
54+
var main = '%c' + label + '%c'
55+
var arr = [null, color, inherit]
56+
for (var i = 0; i < args.length; i++) {
57+
arr.push(args[i])
58+
main += ' %o'
59+
}
60+
arr.push(color)
61+
main += '%c +' + ms + 'ms'
62+
arr[0] = main
63+
console.debug.apply(console, arr)
64+
}
65+
66+
function debug(namespace) {
67+
var color = 'color:' + getColor()
68+
return enabled(namespace) ? function() {
69+
exports.log(namespace, arguments, color)
70+
} : noop
71+
}
72+
73+
exports.init = function(key) {
74+
key = key || 'debug'
75+
var reg = new RegExp(key + '=(\\S+)')
76+
var res = reg.exec(location.href)
77+
if (res) {
78+
enable(res[1])
79+
var doc = document
80+
var elem = doc.createElement('textarea')
81+
var style = elem.style
82+
style.width = '100%'
83+
style.height = '300px'
84+
style.overflow = 'auto'
85+
var box = doc.body || doc.documentElement
86+
box.insertBefore(elem, box.firstChild)
87+
88+
exports.log = function(namespace, arr, color) {
89+
var ret = ['[' + namespace + ']']
90+
var len = arr.length
91+
for (var i = 0; i < len; i++) {
92+
var val = arr[i]
93+
try {
94+
val = JSON.stringify(val, 0, 4)
95+
} catch (e) {
96+
val = val + ''
97+
}
98+
ret.push(val)
99+
}
100+
elem.value += ret.join(' ') + '\n\n'
101+
}
102+
103+
} else if (global.localStorage && console) {
104+
try {
105+
enable(localStorage[key])
106+
} catch (ignore) {}
107+
}
108+
}
109+
110+
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
111+
},{}]},{},[1])(1)
112+
});

index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script src='./dist/debug.js'></script>
2+
<a href='./?debug=*'>debug with textarea</a>
3+
<hr>
4+
<a href='./'>debug with localStorage, type `localStorage.debug = '*'` in Console and refresh</a>
5+
6+
<script>
7+
var a = debug('worker:a')
8+
var b = debug('worker:b')
9+
a('init')
10+
b('init')
11+
setInterval(function(){
12+
a('doing some work')
13+
}, 1000)
14+
15+
setInterval(function(){
16+
b('doing some work')
17+
}, 1200)
18+
</script>

package.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "min-debug",
3+
"version": "1.1.0",
4+
"description": "debug for browser in console or display on page",
5+
"main": "debug.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "browserify test/*.js | poor -b chrome",
11+
"readme": "pretty-readme > readme.md",
12+
"build": "browserify browser.js -s debug > dist/debug.js"
13+
},
14+
"author": "chunpu",
15+
"license": "ISC",
16+
"devDependencies": {
17+
"browserify": "^8.1.1",
18+
"mocha": "^2.1.0",
19+
"poor": "^1.1.3"
20+
},
21+
"dependencies": {
22+
"browserify": "^8.1.1",
23+
"poor": "^1.1.3"
24+
},
25+
"repository": {
26+
"type": "git",
27+
"url": "https://github.com/chunpu/min-debug.git"
28+
},
29+
"keywords": [
30+
"debug",
31+
"browser",
32+
"IE"
33+
],
34+
"bugs": {
35+
"url": "https://github.com/chunpu/min-debug/issues"
36+
},
37+
"homepage": "https://github.com/chunpu/min-debug"
38+
}

readme.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
min-debug
2+
===
3+
4+
[![Build status][travis-image]][travis-url]
5+
[![NPM version][npm-image]][npm-url]
6+
[![Downloads][downloads-image]][downloads-url]
7+
8+
debug for browser in console or display on page
9+
10+
Installation
11+
---
12+
13+
```sh
14+
npm install min-debug
15+
```
16+
17+
inspired by [tj@debug](https://github.com/visionmedia/debug)
18+
19+
[demo](http://chunpu.github.io/min-debug/)
20+
21+
Usage
22+
---
23+
24+
type `localStorage[mykey] = '*'`
25+
26+
if browser not support localStorage or you want to see debug on page, use url debug
27+
28+
type `mykey='*'` in `location.search` or `location.hash`, and will see debug info in a textarea on page
29+
30+
31+
differences with tj@debug
32+
---
33+
34+
- if you browserify your app, you should init the debug key self
35+
36+
```js
37+
module.exports = exports = require('min-debug')
38+
exports.init('mykey') // default is debug
39+
```
40+
41+
- min-debug does not support print format
42+
43+
```js
44+
debug('my data', {foo: 'bar'}) // min-debug style
45+
debug('my data: %o', {foo: 'bar'}) // tj@debug style, not support
46+
```
47+
48+
License
49+
---
50+
51+
ISC
52+
53+
[npm-image]: https://img.shields.io/npm/v/min-debug.svg?style=flat-square
54+
[npm-url]: https://npmjs.org/package/min-debug
55+
[travis-image]: https://img.shields.io/travis/chunpu/min-debug.svg?style=flat-square
56+
[travis-url]: https://travis-ci.org/chunpu/min-debug
57+
[downloads-image]: http://img.shields.io/npm/dm/min-debug.svg?style=flat-square
58+
[downloads-url]: https://npmjs.org/package/min-debug

0 commit comments

Comments
 (0)