fixedstr
Version:
Transforms fixed string to object and vice versa
95 lines • 2.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const _1 = require("./");
describe('fixedstr', () => {
const transformer = new _1.FixedStr([
_1.FixedStr.str('foo', 2),
{
name: 'bar',
size: 5,
parse: str => str
},
_1.FixedStr.number('baz', 3)
]);
it('should objectify', () => {
chai_1.expect(transformer.objectify('F Bar 3')).to.eql({
foo: 'F',
bar: 'Bar ',
baz: 3
});
});
it('should objectify empty string', () => {
chai_1.expect(transformer.objectify('')).to.eql({
foo: '',
bar: '',
baz: 0
});
});
it('should objectify empty undefined', () => {
chai_1.expect(transformer.objectify()).to.eql({
foo: '',
bar: '',
baz: 0
});
});
it('should stringify', () => {
chai_1.expect(transformer.stringify({
foo: 'F',
bar: 'Bar',
baz: 3
})).to.equal('F Bar 003');
});
it('should stringify missing fields', () => {
chai_1.expect(transformer.stringify({
foo: 'F'
})).to.equal('F 000');
});
it('should throw truncation error on string type', () => {
let ex;
try {
transformer.stringify({
foo: 'Fooo'
});
}
catch (e) {
ex = e;
}
chai_1.expect(ex.message).to.contain('truncation error on field: foo');
});
it('should throw truncation error on number type', () => {
let ex;
try {
transformer.stringify({
baz: 12345
});
}
catch (e) {
ex = e;
}
chai_1.expect(ex.message).to.contain('truncation error on field: baz');
});
it('should not throw truncation error if toFixedString truncated the value', () => {
const t = new _1.FixedStr([
{
name: 'foo',
size: 4,
toFixedString: (_, value) => {
return value.substr(0, 4);
}
}
]);
const str = t.stringify({
foo: '123456'
});
chai_1.expect(str).to.equal('1234');
});
it('should not throw truncation error if using fixedstr.strTrunc', () => {
const t = new _1.FixedStr([_1.FixedStr.strTrunc('TEST', 5)]);
const str = t.stringify({
TEST: '123456'
});
chai_1.expect(str).to.equal('12345');
});
});
//# sourceMappingURL=test.js.map