+0
-0
@@ -0,0 +0,0 @@ { |
+0
-0
@@ -0,0 +0,0 @@ 'use strict'; |
+6
-4
@@ -9,3 +9,4 @@ 'use strict'; | ||
| value = value || ''; | ||
| var end = new Array(field.size + 1 - value.length).join(' '); | ||
| var endLength = Math.max(0, field.size + 1 - value.length); | ||
| var end = new Array(endLength).join(' '); | ||
| return value + end; | ||
@@ -29,6 +30,7 @@ } | ||
| var toStr = field.toFixedString || textToString; | ||
| if(obj[field.name] && obj[field.name].length > field.size) { | ||
| var strValue = toStr(field, obj[field.name]); | ||
| if(strValue && strValue.length > field.size) { | ||
| throw new Error('truncation error on field: ' + field.name + ', size: ' + field.size + ', value: ' + obj[field.name]); | ||
| } | ||
| return str + toStr(field, obj[field.name]); | ||
| return str + strValue; | ||
| }, ''); | ||
@@ -53,3 +55,3 @@ } | ||
| var strVal = value && value.toString ? value.toString() : '0', | ||
| pad = new Array(field.size + 1 - strVal.length).join('0'); | ||
| pad = new Array(Math.max(0, field.size + 1 - strVal.length)).join('0'); | ||
| return pad + strVal; | ||
@@ -56,0 +58,0 @@ } |
+27
-27
| { | ||
| "name": "fixedstr", | ||
| "description": "Transforms fixed string to object and vice versa", | ||
| "version": "0.0.2", | ||
| "keywords": [ | ||
| "string", | ||
| "fixed", | ||
| "parse" | ||
| ], | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/tryggingamidstodin/fixedstr.git" | ||
| }, | ||
| "homepage": "https://github.com/tryggingamidstodin/fixedstr", | ||
| "bugs": { | ||
| "url": "https://github.com/tryggingamidstodin/fixedstr/issues" | ||
| }, | ||
| "dependencies": {}, | ||
| "devDependencies": { | ||
| "chai": "^1.10.0", | ||
| "gulp": "^3.8.10", | ||
| "gulp-jshint": "^1.9.0", | ||
| "gulp-mocha": "^2.0.0", | ||
| "mocha": "~1.21.5" | ||
| }, | ||
| "scripts": { | ||
| "test": "mocha -w" | ||
| } | ||
| "name": "fixedstr", | ||
| "description": "Transforms fixed string to object and vice versa", | ||
| "version": "0.0.3", | ||
| "keywords": [ | ||
| "string", | ||
| "fixed", | ||
| "parse" | ||
| ], | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/tryggingamidstodin/fixedstr.git" | ||
| }, | ||
| "homepage": "https://github.com/tryggingamidstodin/fixedstr", | ||
| "bugs": { | ||
| "url": "https://github.com/tryggingamidstodin/fixedstr/issues" | ||
| }, | ||
| "dependencies": {}, | ||
| "devDependencies": { | ||
| "chai": "^1.10.0", | ||
| "gulp": "^3.8.10", | ||
| "gulp-jshint": "^1.9.0", | ||
| "gulp-mocha": "^2.0.0", | ||
| "mocha": "~1.21.5" | ||
| }, | ||
| "scripts": { | ||
| "test": "mocha -w" | ||
| } | ||
| } |
+0
-0
@@ -0,0 +0,0 @@ fixedstr |
+35
-3
@@ -8,3 +8,9 @@ 'use strict'; | ||
| fixedstr.str('foo', 2), | ||
| fixedstr.str('bar', 5), | ||
| { | ||
| name: 'bar', | ||
| size: 5, | ||
| parse: function(str) { | ||
| return str; | ||
| } | ||
| }, | ||
| fixedstr.number('baz', 3) | ||
@@ -17,3 +23,3 @@ ]); | ||
| foo: 'F', | ||
| bar: 'Bar', | ||
| bar: 'Bar ', | ||
| baz: 3 | ||
@@ -55,3 +61,3 @@ }); | ||
| it('should throw truncation error', function () { | ||
| it('should throw truncation error on string type', function () { | ||
| var ex; | ||
@@ -67,2 +73,28 @@ try{ | ||
| }); | ||
| it('should throw truncation error on number type', function () { | ||
| var ex; | ||
| try{ | ||
| transformer.stringify({ | ||
| baz: 12345 | ||
| }); | ||
| }catch(e) { | ||
| ex = e; | ||
| } | ||
| expect(ex.message).to.contain('truncation error on field: baz'); | ||
| }); | ||
| it('should not throw truncation error if toFixedString truncated the value', function() { | ||
| var t = fixedstr([{ | ||
| name: 'foo', | ||
| size: 4, | ||
| toFixedString: function(f, value) { | ||
| return value.substr(0, 4); | ||
| } | ||
| }]); | ||
| var str = t.stringify({ | ||
| foo: '123456' | ||
| }); | ||
| expect(str).to.equal('1234'); | ||
| }); | ||
| }); |
5972
12.64%150
27.12%