Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Aug 1, 2015
0 parents commit 64bb15c
Show file tree
Hide file tree
Showing 15 changed files with 1,271 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# EditorConfig is awesome: http://EditorConfig.org

root = true

[*]
indent_size = 2
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
coverage
node_modules
npm-debug.log
lib
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
sudo: false
language: node_js

notifications:
email:
on_success: never
on_failure: change

node_js:
- "0.10"
- "0.12"
- "iojs"

after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Blake Embrey (hello@blakeembrey.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Decorator Debug

[![NPM version][npm-image]][npm-url]
[![NPM downloads][downloads-image]][downloads-url]
[![Build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]

> Simple ES7 decorator for debugging classes and methods.
## Installation

```sh
npm install decorator-debug --save
```

## Usage

Every time the class or method is called, a console log will be emitted with the property `name`, `result`, `args` and `time` taken.

```js
import debug = require('decorator-debug')

@debug
class Demo {
@debug
method (string) {
return true
}
}

new Demo(1, 2, 3).method('test')

//=> { name: 'Demo', result: {}, args: [ 1, 2, 3 ], time: 0.2647359999999992 }
//=> { name: 'method', result: true, args: [ 'test' ], time: 0.029204000000000008 }
```

**P.S.** You can debug conditionally.

```js
@debug(process.env.NODE_ENV !== 'production')
class Demo {}
```

## License

MIT

[npm-image]: https://img.shields.io/npm/v/decorator-debug.svg?style=flat
[npm-url]: https://npmjs.org/package/decorator-debug
[downloads-image]: https://img.shields.io/npm/dm/decorator-debug.svg?style=flat
[downloads-url]: https://npmjs.org/package/decorator-debug
[travis-image]: https://img.shields.io/travis/blakeembrey/decorator-debug.svg?style=flat
[travis-url]: https://travis-ci.org/blakeembrey/decorator-debug
[coveralls-image]: https://img.shields.io/coveralls/blakeembrey/decorator-debug.svg?style=flat
[coveralls-url]: https://coveralls.io/r/blakeembrey/decorator-debug?branch=master
51 changes: 51 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "decorator-debug",
"version": "0.0.0",
"description": "Simple ES7 decorator for debugging classes and methods",
"main": "lib/index.js",
"files": [
"lib/",
"LICENSE"
],
"scripts": {
"lint": "# TODO",
"build-ts": "ntsc",
"build": "rm -rf lib && npm run build-ts",
"test-spec": "mocha lib/**/*.spec.js -R spec --bail",
"test-cov": "istanbul cover -x *.spec.js node_modules/mocha/bin/_mocha -- lib/**/*.spec.js -R spec --bail",
"test": "npm run build && npm run lint && npm run test-cov",
"prepublish": "npm run build"
},
"repository": {
"type": "git",
"url": "git://github.com/blakeembrey/decorator-debug.git"
},
"keywords": [
"decorator",
"debug",
"console",
"log",
"es7"
],
"author": {
"name": "Blake Embrey",
"email": "hello@blakeembrey.com",
"url": "http://blakeembrey.me"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/blakeembrey/decorator-debug/issues"
},
"homepage": "https://github.com/blakeembrey/decorator-debug",
"devDependencies": {
"chai": "^3.2.0",
"istanbul": "^0.3.17",
"mocha": "^2.2.5",
"ntypescript": "^1.201507311101.1",
"pre-commit": "^1.1.1",
"sinon": "^1.15.4"
},
"dependencies": {
"performance-now": "^0.2.0"
}
}
82 changes: 82 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { spy } from 'sinon'
import { expect } from 'chai'
import debug = require('./index')

describe('decorator debug', () => {
it('should debug methods', () => {
class Demo {
@debug
method (x: string) {
return true
}
}

const s = spy(console, 'log')

new Demo().method('test')

const arg = s.getCall(0).args[0]

s.restore()

expect(arg.name).to.deep.equal('method')
expect(arg.args).to.deep.equal(['test'])
expect(arg.result).to.equal(true)
expect(arg.time).to.be.a('number')
})

it('should debug constructors', () => {
@debug
class Demo {
constructor (...numbers: number[]) {}
method () {}
}

const s = spy(console, 'log')

new Demo(1, 2, 3)

const arg = s.getCall(0).args[0]

s.restore()

expect(arg.name).to.deep.equal('Demo')
expect(arg.args).to.deep.equal([1, 2, 3])
expect(arg.result.method).to.be.a('function')
expect(arg.time).to.be.a('number')
})

describe('conditional', () => {
it('should enable debug', () => {
class Demo {
@debug(true)
method () {}
}

const s = spy(console, 'log')

new Demo().method()

const arg = s.getCall(0).args[0]

s.restore()

expect(s.callCount).to.equal(1)
})

it('should disable debug', () => {
class Demo {
@debug(false)
method () {}
}

const s = spy(console, 'log')

new Demo().method()

s.restore()

expect(s.callCount).to.equal(0)
})
})
})
46 changes: 46 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import now = require('performance-now')

interface Debug {
name: string
result: any
args: any[]
time: number
}

function debug (value: boolean): typeof debug
function debug <T extends Function> (value: T): T
function debug (value: Object, name: string, descriptor: PropertyDescriptor): void
function debug (value: any, name?: string, descriptor?: PropertyDescriptor) {
// Class properties.
if (arguments.length === 3) {
descriptor.value = wrap(descriptor.value, name)
return
}

// Wrap constructor functions.
if (typeof value === 'function') {
return wrap(value)
}

// Enable or disable debugging.
return value ? debug : undefined
}

function wrap <T extends Function> (fn: T, name?: string): T {
name = name || (<any> fn).name

return <T> <any> function debug (...args: any[]) {
const isNew = this instanceof debug
const start = now()
const result = isNew ? new (<any> fn)(...args) : fn.apply(this, args)
const end = now()
const time = end - start
const output: Debug = { name, result, args, time }

console.log(output)

return result
}
}

export = debug
5 changes: 5 additions & 0 deletions src/typings/performance-now.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module 'performance-now' {
function now (): number

export = now
}
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "es5",
"outDir": "lib",
"module": "commonjs",
"declaration": true,
"noImplicitAny": true,
"removeComments": true,
"sourceMap": true,
"experimentalDecorators": true
},
"files": [
"src/index.ts",
"src/index.spec.ts",
"src/typings/performance-now.d.ts",
"typings/tsd.d.ts"
]
}
18 changes: 18 additions & 0 deletions tsd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "v4",
"repo": "borisyankov/DefinitelyTyped",
"ref": "master",
"path": "typings",
"bundle": "typings/tsd.d.ts",
"installed": {
"chai/chai.d.ts": {
"commit": "4df20c9706ce6ca27137617770b57f3a0d3f9689"
},
"mocha/mocha.d.ts": {
"commit": "4df20c9706ce6ca27137617770b57f3a0d3f9689"
},
"sinon/sinon.d.ts": {
"commit": "4df20c9706ce6ca27137617770b57f3a0d3f9689"
}
}
}

0 comments on commit 64bb15c

Please sign in to comment.