fwsp-umf-message
Advanced tools
Comparing version 0.1.0 to 0.1.1
54
index.js
@@ -10,2 +10,14 @@ 'use strict'; | ||
const shortToLong = { | ||
frm: 'from', | ||
ts: 'timestamp', | ||
ver: 'version', | ||
bdy: 'body' | ||
}, longToShort = { | ||
from: 'frm', | ||
timestamp: 'ts', | ||
version: 'ver', | ||
body: 'bdy' | ||
}; | ||
class UMFMessage { | ||
@@ -69,6 +81,46 @@ constructor() { | ||
} | ||
return msg; | ||
return new Proxy(msg, { | ||
get: (obj, prop) => { | ||
if (shortFormat && prop in longToShort) { | ||
return obj[longToShort[prop]]; | ||
} else if (!shortFormat && prop in shortToLong) { | ||
return obj[shortToLong[prop]]; | ||
} else { | ||
return obj[prop]; | ||
} | ||
}, | ||
set: (obj, prop, value) => { | ||
if (shortFormat && prop in longToShort) { | ||
obj[longToShort[prop]] = value; | ||
} else if (!shortFormat && prop in shortToLong) { | ||
obj[shortToLong[prop]] = value; | ||
} else { | ||
obj[prop] = value; | ||
} | ||
return true; | ||
} | ||
}); | ||
} | ||
/** | ||
* @name messageToObject | ||
* @param {object} message - message to be converted | ||
* @return {object} unproxied message object | ||
*/ | ||
messageToObject(message) { | ||
let ret = {}; | ||
Object.keys(message).forEach(k => ret[k] = message[k]); | ||
return ret; | ||
} | ||
/** | ||
* @name messageToJSON | ||
* @param {object} message - message to be converted | ||
* @return {string} JSON version of message | ||
*/ | ||
messageToJSON(message) { | ||
return Utils.safeJSONStringify(this.messageToObject(message)); | ||
} | ||
/** | ||
* @name messageToShort | ||
@@ -75,0 +127,0 @@ * @summary convert a long message to a short one |
{ | ||
"name": "fwsp-umf-message", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "umf-message: a tool for creating and working with UMF style messages", | ||
@@ -5,0 +5,0 @@ "author": "Carlos Justiniano", |
@@ -1,2 +0,2 @@ | ||
# UMF Message | ||
# UMF Message [![Build Status](https://travis-ci.org/flywheelsports/fwsp-umf-message.svg?branch=master)](https://travis-ci.org/flywheelsports/fwsp-umf-message) | ||
Used to create and validate [UMF](https://github.com/cjus/umf) style messages. | ||
@@ -3,0 +3,0 @@ |
@@ -5,2 +5,3 @@ 'use strict'; | ||
const Utils = require('fwsp-jsutils'); | ||
const UMFMessage = require('../index.js'); | ||
@@ -116,3 +117,60 @@ | ||
}); | ||
}); | ||
describe('proxied object', () => { | ||
it('should return values for long-form keys on short-form message ', () => { | ||
let msg = UMFMessage.createMessage({ | ||
to: 'someservice:/', | ||
frm: 'tester', | ||
bdy: { | ||
val: 'some value' | ||
} | ||
}, true); | ||
expect(msg.frm).to.equal(msg.from); | ||
expect(msg.bdy).to.equal(msg.body); | ||
expect(msg.ts).to.equal(msg.timestamp); | ||
expect(msg.ver).to.equal(msg.version); | ||
}); | ||
it('should return values for short-form keys on long-form message', () => { | ||
let msg = UMFMessage.createMessage({ | ||
to: 'someservice:/', | ||
from: 'tester', | ||
body: { | ||
val: 'some value' | ||
} | ||
}, false); | ||
expect(msg.from).to.equal(msg.frm); | ||
expect(msg.body).to.equal(msg.bdy); | ||
expect(msg.timestamp).to.equal(msg.ts); | ||
expect(msg.version).to.equal(msg.ver); | ||
}); | ||
}); | ||
describe('messageToJSON', () => { | ||
it('should return long-form keys in JSON for a long-form message', () => { | ||
let msg = UMFMessage.createMessage({ | ||
to: 'someservice:/', | ||
from: 'tester', | ||
body: { | ||
val: 'some value' | ||
} | ||
}, false); | ||
let json = UMFMessage.messageToJSON(msg); | ||
let parsed = Utils.safeJSONParse(json); | ||
expect(parsed).to.have.property('from'); | ||
}); | ||
it('should return short-form keys in JSON for a short-form message', () => { | ||
let msg = UMFMessage.createMessage({ | ||
to: 'someservice:/', | ||
frm: 'tester', | ||
bdy: { | ||
val: 'some value' | ||
} | ||
}, true); | ||
let json = UMFMessage.messageToJSON(msg); | ||
let parsed = Utils.safeJSONParse(json); | ||
expect(parsed).to.have.property('frm'); | ||
}); | ||
}); |
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
20241
10
400
1