mof-genestamp
Advanced tools
Comparing version 2.0.0 to 3.0.0
36
index.js
@@ -0,6 +1,14 @@ | ||
'use strict'; | ||
const {reject, filter, pick} = require('ramda'); | ||
module.exports = function(options) { | ||
options = options || {}; | ||
options = Object.assign({ printUri: false }, options); | ||
const values = Object.values(options); | ||
const inclusion = values.every(val => val); | ||
const exclusion = values.every(val => !val); | ||
if(inclusion + exclusion < 1) | ||
throw 'Projection should be either inclusion or exclusion, but not both.'; | ||
@@ -14,15 +22,18 @@ return (ctx, next) => { | ||
let msg = format(ctx, options); | ||
if (isRotate(transport)) { | ||
const tmp = transport.options.label; | ||
transport.options.label = ctx.func; | ||
logger.info(msg); | ||
transport.options.label = 'floodesh'; | ||
transport.options.label = tmp; | ||
} else { | ||
const tmp = transport.label; | ||
transport.label = ctx.func; | ||
logger.info(msg); | ||
transport.label = 'floodesh'; | ||
transport.label = tmp; | ||
} | ||
return next(); | ||
} | ||
} | ||
}; | ||
}; | ||
@@ -40,10 +51,16 @@ function isRotate(transport) { | ||
let msg = {}; | ||
if (options.printUri) { | ||
msg.uri = ctx.opt.uri; | ||
} | ||
const isInOptions = key => key in options; | ||
if (ctx.opt.gene) { | ||
let copy = JSON.parse(JSON.stringify(ctx.opt.gene)); | ||
if (typeof copy === 'object') { | ||
const projectValues = Object.values(options); | ||
const projectKeys = Object.keys(options); | ||
const finalKeys = projectValues.some(val => val) ? filter(isInOptions, projectKeys) : reject(isInOptions, Object.keys(copy)); | ||
if(projectKeys.length > 0){ | ||
copy = pick(finalKeys, copy); | ||
} | ||
sanitize(copy); | ||
} else if (copy === 'string') { | ||
} else if (typeof copy === 'string') { | ||
copy = isBalanced(copy) ? copy.replace(/[\r\n\t,]/g, '') : copy.replace(/[\[\]\r\n\t,]/g, ''); | ||
@@ -59,2 +76,3 @@ } | ||
if (!obj) return; | ||
Object.keys(obj).forEach(key => { | ||
@@ -61,0 +79,0 @@ if (typeof obj[key] === 'string') { |
{ | ||
"name": "mof-genestamp", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "middleware of floodesh, prints gene and url of a task, along with # of new tasks and # of records", | ||
@@ -13,2 +13,5 @@ "main": "index.js", | ||
}, | ||
"engines": { | ||
"node": ">=7.0.0" | ||
}, | ||
"keywords": [ | ||
@@ -28,3 +31,6 @@ "middleware", | ||
"chai": "4.1.2" | ||
}, | ||
"dependencies": { | ||
"ramda": "^0.26.1" | ||
} | ||
} |
@@ -29,4 +29,3 @@ # mof-genestamp | ||
# options | ||
* options `<Object>` Could be undefined. Default `{ printUri: false }`. | ||
* printUri `<boolean>` Allow genestamp to print `ctx.opt.uri` or not. Default `false`. | ||
* options `<Object>` Could be undefined. Use 0 to exclude fields. Alternatively, you may specify the inclusion of fields use 1. | ||
@@ -33,0 +32,0 @@ # log format |
@@ -30,3 +30,3 @@ /*jshint expr:true */ | ||
let format = sinon.spy(genestamp.__get__('format')); | ||
genestamp({ printUri: true })(ctx, next); | ||
genestamp()(ctx, next); | ||
expect(next.calledOnce).to.be.true; | ||
@@ -41,3 +41,3 @@ expect(format.notCalled).to.be.true; | ||
let format = sinon.spy(genestamp.__get__('format')); | ||
genestamp({ printUri: true })(ctx, next); | ||
genestamp()(ctx, next); | ||
expect(next.calledOnce).to.be.true; | ||
@@ -131,17 +131,17 @@ expect(format.notCalled).to.be.true; | ||
it('(a) should not print gene when gene is undefined or null', () => { | ||
let ctx = { tasks: [], dataSet: new Map(), opt: { uri: 'http://www.bda.com', gene: undefined } }; | ||
expect(format(ctx, { printUri: true })).to.deep.equal({ uri: 'http://www.bda.com', tasks: 0, dataSet: 0 }); | ||
let ctx = { tasks: [], dataSet: new Map(), opt: { gene: undefined } }; | ||
expect(format(ctx, {})).to.deep.equal({ tasks: 0, dataSet: 0 }); | ||
ctx.opt.gene = null; | ||
expect(format(ctx, { printUri: true })).to.deep.equal({ uri: 'http://www.bda.com', tasks: 0, dataSet: 0 }); | ||
expect(format(ctx, {})).to.deep.equal({ tasks: 0, dataSet: 0 }); | ||
}); | ||
it('(b) should work when gene is object', () => { | ||
let ctx = { tasks: [], dataSet: new Map(), opt: { uri: 'http://www.bda.com', gene: { a: 1, b: 2, c: 'i am \tkitty' } } }; | ||
expect(format(ctx, { printUri: true })).to.deep.equal({ uri: 'http://www.bda.com', gene: { a: 1, b: 2, c: 'i am kitty' }, tasks: 0, dataSet: 0 }); | ||
let ctx = { tasks: [], dataSet: new Map(), opt: { gene: { a: 1, b: 2, c: 'i am \tkitty' } } }; | ||
expect(format(ctx, {})).to.deep.equal({ gene: { a: 1, b: 2, c: 'i am kitty' }, tasks: 0, dataSet: 0 }); | ||
}); | ||
it('(c) gene should stay the same', () => { | ||
let ctx = { tasks: [], dataSet: new Map(), opt: { uri: 'http://www.bda.com', gene: { a: 1, b: 'i am\t kitty' } } }; | ||
let ctx = { tasks: [], dataSet: new Map(), opt: { gene: { a: 1, b: 'i am\t kitty' } } }; | ||
let beforeGene = JSON.stringify(ctx.opt.gene); | ||
format(ctx, { printUri: true }); | ||
format(ctx, {}); | ||
let afterGene = JSON.stringify(ctx.opt.gene); | ||
@@ -151,6 +151,7 @@ expect(beforeGene).to.equal(afterGene); | ||
it('(d) options.printUri should work', () => { | ||
let ctx = { tasks: [], dataSet: new Map(), opt: { uri: 'http://www.bda.com'} }; | ||
expect(format(ctx, { printUri: true })).to.deep.equal({ uri: 'http://www.bda.com', tasks: 0, dataSet: 0 }); | ||
expect(format(ctx, { printUri: false })).to.deep.equal({ tasks: 0, dataSet: 0 }); | ||
it('(d) projection should work', () => { | ||
let ctx = { tasks: [], dataSet: new Map(), opt: { gene: {name: 'Mike', age: 666, weight: 23, phone: '0102344222', mobile: 13000000000}}}; | ||
expect(format(ctx, {name:1, age:1})).to.deep.equal({ tasks: 0, dataSet: 0, gene:{name: 'Mike', age: 666 }}); | ||
expect(format(ctx, {weight:0})).to.deep.equal({ tasks: 0, dataSet: 0 , gene:{name: 'Mike', age: 666, phone: '0102344222', mobile: 13000000000}}); | ||
}); | ||
@@ -175,3 +176,3 @@ }); | ||
it('(b) genestamp should have label ctx.func and label should be reset to floodesh after genestamp, options should work as expected', (done) => { | ||
it('(b) genestamp should have label ctx.func and label should be reset to previous after genestamp, options should work as expected', (done) => { | ||
let logger = getRotate('floodesh'); | ||
@@ -186,2 +187,3 @@ const pid = process.pid; | ||
}); | ||
expect(logFile.length).to.equal(1); | ||
@@ -191,6 +193,6 @@ logFile = logFile[0]; | ||
let ctx = { func: 'func', tasks: [1,2,3,4], dataSet: new Map([['key1', {}],['key2', [1,2,3,4]],['key3', 'a\nb\nc\n']]), app: { logger: logger }, opt: { uri: 'http://www.bda.com', gene: { a:'[aa=1', bb:'2]' } } }; | ||
genestamp({ printUri: true })(ctx, function() {}); | ||
genestamp({ printUri: false })(ctx, function() {}); | ||
let ctx = { func: 'func', tasks: [1,2,3,4], dataSet: new Map([['key1', {}],['key2', [1,2,3,4]],['key3', 'a\nb\nc\n']]), app: { logger: logger }, opt: { gene: { a:'[aa=1', bb:'2]' } } }; | ||
genestamp()(ctx, function() {}); | ||
genestamp()(ctx, function() {}); | ||
genestamp()(ctx, function() {}); | ||
logger.info('hello world'); | ||
@@ -200,10 +202,8 @@ | ||
let lines = fs.readFileSync('/tmp/'+logFile).toString().trim().split('\n'); | ||
expect(lines.length).to.equal(4); | ||
expect(lines[0].indexOf('[func]') > -1).to.be.true; | ||
expect(lines[0].indexOf('uri') > -1).to.be.true; | ||
expect(lines[1].indexOf('[func]') > -1).to.be.true; | ||
expect(lines[1].indexOf('uri') === -1).to.be.true; | ||
expect(lines[2].indexOf('[func]') > -1).to.be.true; | ||
expect(lines[2].indexOf('uri') === -1).to.be.true; | ||
expect(lines[3].indexOf('[floodesh]') > -1).to.be.true; | ||
expect(lines[3].indexOf('[rotate]') > -1).to.be.true; | ||
done(); | ||
@@ -210,0 +210,0 @@ }, 50); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
13331
322
1
41
1
+ Addedramda@^0.26.1
+ Addedramda@0.26.1(transitive)