Skip to content

Commit 6d62609

Browse files
committedDec 21, 2015
Initial commit
0 parents  commit 6d62609

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed
 

‎LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Josh Graham
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Yule
2+
3+
Yule is a ES2016 decorator that helps you debug methods by logging information to
4+
the console both before and after invocation.
5+
6+
## Example
7+
When debugging a method it is common to put console log statements at the top and bottom to observe when it is getting called and what parameters were passed to it. Yule makes the process easier, just import Yule and decorate the method(s) you want to observe.
8+
9+
```js
10+
import yule from "yule";
11+
12+
class Cat {
13+
@yule
14+
meow(tone) {
15+
if (tone === "sassy") {
16+
console.log("me-oooow");
17+
} else {
18+
console.log("meow");
19+
}
20+
21+
return "purr";
22+
}
23+
}
24+
25+
let cat = new Cat();
26+
cat.meow("sassy");
27+
```
28+
29+
Open up the console and you will see the following.
30+
31+
```
32+
Yule: Starting method meow with args ["sassy"]
33+
me-oooow
34+
Yule: Finished method meow with result purr
35+
```

‎index.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const style = "color: #00B0FF"
2+
3+
module.exports = function yule(target, key, descriptor) {
4+
const original = descriptor.value;
5+
6+
descriptor.value = function(...args) {
7+
const self = this;
8+
9+
return function() {
10+
const methodCallback = function() {
11+
console.log(`%cYule: Starting method ${key} with args`, style, args);
12+
13+
const result = original.apply(self, args);
14+
15+
if (result) {
16+
console.log(`%cYule: Finished method ${key} with result`, style, result);
17+
} else {
18+
console.log(`%cYule: Finished method ${key}`, style)
19+
}
20+
21+
return result;
22+
};
23+
return methodCallback();
24+
}();
25+
};
26+
27+
return descriptor;
28+
}

‎package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "yule",
3+
"version": "0.1.0",
4+
"description": "A function decorator that logs enter and exit to the console.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"decorator",
11+
"console",
12+
"log"
13+
],
14+
"author": "Josh Graham",
15+
"license": "MIT",
16+
"registry": "npm"
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.