Skip to content

Commit

Permalink
Merge pull request #1 from yidinghan/1-standardise-keys-record-process
Browse files Browse the repository at this point in the history
standardise req and res keys record process
  • Loading branch information
yidinghan authored Apr 11, 2017
2 parents 097fd82 + a84be3e commit 1ced492
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 5 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ cache:
- node_modules
before_script:
- 'npm prune'
- 'npm install winston'
after_success:
- npm run coveralls
78 changes: 77 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,88 @@
const winston = require('winston');
const get = require('lodash.get');
const set = require('lodash.set');
const unset = require('lodash.unset');
const pick = require('lodash.pick');

/**
* keysRecorder
* use ldoash pick, get and set to collect data from given target object
*
* @param {object} payload - input arguments
* @param {string[]} [payload.defaults] - default keys will be collected
* @param {string[]} [payload.selects] - keys will be collected as
* additional part
* @param {string[]} [payload.unselects] - keys that will be ignored at last
* @return {function} closure function, setting by given payload
* @example
* // without payload
* const recorder = keysRecorder();
* recorder() // {}
* recorder({ foo: 1, bar: 2, foobar: { a: 3, b: 4 } }) // {}
*
* // with defaults
* const recorder = keysRecorder({ defaults: ['foo'] });
* recorder() // {}
* recorder({ foo: 1, bar: 2, foobar: { a: 3, b: 4 } }) // { foo: 1 }
*
* // with defaults and selects
* const recorder = keysRecorder({ defaults: ['foo'], selects: ['foobar'] });
* recorder() // {}
* recorder({
* foo: 1,
* bar: 2,
* foobar: { a: 3, b: 4 }
* }) // { foo: 1, foobar: { a: 3, b: 4 } }
*
* // with defaults and unselects
* const recorder = keysRecorder({ defaults: ['foobar'], unselects: ['foobar.a'] });
* recorder() // {}
* recorder({
* foo: 1,
* bar: 2,
* foobar: { a: 3, b: 4 }
* }) // { foobar: { a: 3 } }
*
* // with defaults and selects and unselects
* const recorder = keysRecorder({
* defaults: ['foo'],
* selects: ['foobar'],
* unselects: ['foobar.b'],
* });
* recorder() // {}
* recorder({
* foo: 1,
* bar: 2,
* foobar: { a: 3, b: 4 }
* }) // { foo: 1, foobar: { a: 3 } }
*/
exports.keysRecorder = (payload = {}) => {
const {
defaults = [],
selects = [],
unselects = [],
} = payload;

return (target) => {
if (!target) { return {}; }

const logObject = {};
defaults.concat(selects).forEach((path) => {
set(logObject, path, get(target, path));
});
unselects.forEach((path) => {
unset(logObject, path, get(target, path));
});

return logObject;
};
};

exports.serializer = {
req: (payload) => {
const {
reqIgnores = ['headers.cookie'],
reqkeys = ['headers', 'url'],
reqkeys = ['headers', 'url', 'method', 'httpVersion', 'originalUrl', 'query'],
} = payload;

return (req) => {
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"scripts": {
"lint": "eslint ./test/*.js ./index.js --fix",
"release": "standard-version",
"nyc": "nyc npm test && nyc report --reporter=html",
"test": "ava"
},
"repository": {
Expand All @@ -32,13 +33,17 @@
"winston": "^2.3.1"
},
"dependencies": {
"lodash.get": "^4.4.2",
"lodash.pick": "^4.4.0",
"lodash.set": "^4.3.2",
"lodash.unset": "^4.5.2"
},
"devDependencies": {
"ava": "^0.19.0",
"eslint": "^3.19.0",
"eslint-config-airbnb-base": "^11.1.3",
"eslint-plugin-import": "^2.2.0"
"eslint-plugin-import": "^2.2.0",
"nyc": "^10.2.0",
"winston": "^2.3.1"
}
}
80 changes: 78 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,82 @@
const { test } = require('ava');
const koaWinston = require('../');
const { keysRecorder } = require('../');

const testTarget = {
a: 1,
b: 2,
c: {
d: 3,
e: 4,
},
};

test('ok', (t) => {
t.truthy(koaWinston);
t.truthy(keysRecorder);
});

test('keysRecorder:should get an function as return', (t) => {
const recorder = keysRecorder();

t.is(typeof recorder, 'function');
});

test('keysRecorder:should get defaults keys', (t) => {
const recorder = keysRecorder({
defaults: ['a'],
});
const result = recorder(testTarget);

t.deepEqual(result, { a: 1 });
});

test('keysRecorder:should get defaults and selects', (t) => {
const recorder = keysRecorder({
defaults: ['a'],
selects: ['b'],
});
const result = recorder(testTarget);

t.deepEqual(result, { a: 1, b: 2 });
});

test('keysRecorder:should only get c.d', (t) => {
const recorder = keysRecorder({
defaults: ['c.d'],
selects: ['b'],
unselects: ['b'],
});
const result = recorder(testTarget);

t.deepEqual(result, { c: { d: 3 } });
});

test('keysRecorder:should get defaults and selects and unselects', (t) => {
const recorder = keysRecorder({
defaults: ['a'],
selects: ['c'],
unselects: ['c.e'],
});
const result = recorder(testTarget);

t.deepEqual(result, { a: 1, c: { d: 3 } });
});

test('keysRecorder:should get defaults and unselects', (t) => {
const recorder = keysRecorder({
defaults: ['c'],
unselects: ['c.e'],
});
const result = recorder(testTarget);

t.deepEqual(result, { c: { d: 3 } });
});

test('keysRecorder:should get empty object without target', (t) => {
const recorder = keysRecorder({
defaults: ['c'],
unselects: ['c.e'],
});
const result = recorder();

t.deepEqual(result, {});
});

0 comments on commit 1ced492

Please sign in to comment.