first commit
This commit is contained in:
+108
@@ -0,0 +1,108 @@
|
||||
// native console
|
||||
const _console = console;
|
||||
|
||||
// ダミー用
|
||||
const dummy = function(){}
|
||||
|
||||
// 参考: https://developer.mozilla.org/en-US/docs/Web/API/Console
|
||||
const methodNameArr = [
|
||||
'_exception',
|
||||
'assert',
|
||||
'count',
|
||||
'debug',
|
||||
'dir',
|
||||
'dirxml',
|
||||
'error',
|
||||
'group',
|
||||
'groupCollapsed',
|
||||
'groupEnd',
|
||||
'info',
|
||||
'log',
|
||||
'profile',
|
||||
'profileEnd',
|
||||
'table',
|
||||
'time',
|
||||
'timeEnd',
|
||||
'timeStamp',
|
||||
'trace',
|
||||
'warn'
|
||||
];
|
||||
|
||||
// コンストラクタ
|
||||
function Console(){
|
||||
this.disable();
|
||||
this.status = undefined;
|
||||
}
|
||||
|
||||
// 状態
|
||||
Console.prototype.status = undefined;
|
||||
|
||||
// 参照用
|
||||
Console.prototype._console = _console;
|
||||
|
||||
// extで使う引数キャッシュ
|
||||
Console.prototype.cache = [];
|
||||
|
||||
// 通常モード
|
||||
Console.prototype.enable = function(){
|
||||
this.status = 'enable';
|
||||
methodNameArr.forEach( (name)=>{
|
||||
if( _console[name] ){
|
||||
this[name] = _console[name].bind(_console);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ダミーモード、標準設定
|
||||
Console.prototype.disable = function(){
|
||||
this.status = 'disable';
|
||||
methodNameArr.forEach( (name)=>{
|
||||
this[name] = dummy;
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
拡張モード
|
||||
行数はわからなくなるが任意の処理を挟めるやつ
|
||||
@param {
|
||||
cache: boolean,
|
||||
calback: function,
|
||||
disable: boolean,
|
||||
result: boolean
|
||||
}
|
||||
.callback が関数ならmethod実行時にcallback({...})
|
||||
.cache:true なら引数を保存する
|
||||
.disable:true ならconsole.method()を実行しない。
|
||||
.result:true なら引数を配列にして返す
|
||||
@return undefined or [...arg]
|
||||
*/
|
||||
Console.prototype.ext = function({cache, callback, disable, result}){
|
||||
this.status = 'ext';
|
||||
methodNameArr.forEach( (name)=>{
|
||||
this[name] = (...arg)=>{
|
||||
disable===true || _console[name](...arg);
|
||||
typeof callback==='function' && callback({
|
||||
target: this,
|
||||
timestamp: Date.now(),
|
||||
type: name,
|
||||
arguments: [...arg]
|
||||
});
|
||||
cache && this.cache.push([...arg]);
|
||||
return this.result && [...arg];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
同調モード
|
||||
console.sync.method();
|
||||
既に設定されていればそのまま何もしない。
|
||||
設定されていなければモード切替え。
|
||||
*/
|
||||
Console.prototype.__defineGetter__('sync', function(){
|
||||
return this.status ?
|
||||
{ext:dummy, enable:dummy, disable:dummy}:
|
||||
this;
|
||||
});
|
||||
|
||||
module.exports = new Console();
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "console-wrapper",
|
||||
"version": "1.1.0",
|
||||
"description": "よくあるconsoleのラッパー",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/honeo/console-wrapper.git"
|
||||
},
|
||||
"keywords": [
|
||||
"console",
|
||||
"wrapper"
|
||||
],
|
||||
"author": "honeo",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/honeo/console-wrapper/issues"
|
||||
},
|
||||
"homepage": "https://github.com/honeo/console-wrapper#readme"
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
## なにこれ
|
||||
よくあるconsoleラッパー。
|
||||
require()のキャッシュを利用して同ディレクトリ内モジュールのコンソール動作を一括設定する。
|
||||
[honeo/console-wrapper](https://github.com/honeo/console-wrapper)
|
||||
[console-wrapper](https://www.npmjs.com/package/console-wrapper)
|
||||
|
||||
## 使い方
|
||||
```sh
|
||||
$ npm i console-wrapper
|
||||
```
|
||||
```js
|
||||
const console = require('console-wrapper');
|
||||
|
||||
console.log('hoge'); // no output
|
||||
|
||||
console.enable();
|
||||
console.log('hoge'); //"hoge"
|
||||
```
|
||||
Method
|
||||
-----
|
||||
### .enable()
|
||||
有効化。
|
||||
通常のconsoleメソッドを使う。
|
||||
### .disable()
|
||||
標準。
|
||||
無効化、何もしないダミー関数を使う。
|
||||
### .extension({...})
|
||||
有効化(拡張モード)、行数が表示されなくなる代わりに……。
|
||||
.cache: trueならコンソールメソッド実行時に引数を保存する。
|
||||
.callback: 関数ならコンソールメソッド実行時に引数を渡して実行する。
|
||||
.disable: trueならコンソールメソッドは実行しない。
|
||||
.result: trueならコンソールメソッド実行後に引数を配列に入れて返す。
|
||||
```js
|
||||
// example
|
||||
console.ext({
|
||||
cache: false,
|
||||
callback({type, arguments}){},
|
||||
disable: false,
|
||||
result: false
|
||||
});
|
||||
```
|
||||
### .sync.method();
|
||||
既に動作モードが設定されていればそのまま。
|
||||
されていなければ.method()を実行する。
|
||||
|
||||
## Properties
|
||||
### .cache
|
||||
.ext({cache: true}) 時にコンソールメソッド実行時の引数が配列で保存される。
|
||||
メモリに注意。
|
||||
### ._console
|
||||
ネイティブのconsoleオブジェクトへの参照。
|
||||
Reference in New Issue
Block a user