+2
-1
@@ -11,3 +11,3 @@ mt940-js changelog | ||
| 2019-01-19 v1.2.0-beta | ||
| 2019-01-19 v1.2.0 | ||
| ------------------ | ||
@@ -17,2 +17,3 @@ + support for reversal flag, field 61 also contains isReversal attribute | ||
| + support for /XXX/ field 86 subtags | ||
| + support post parse middlewares | ||
@@ -19,0 +20,0 @@ 2016-09-08 v1.1.0 |
+28
-0
@@ -57,2 +57,5 @@ /** | ||
| class Parser { | ||
| constructor () { | ||
| this.postParseMiddlewareStack = []; | ||
| } | ||
@@ -76,2 +79,6 @@ /** | ||
| for (let s of statements) { | ||
| this._applyPostParseMiddlewares(s); | ||
| } | ||
| return statements; | ||
@@ -81,2 +88,23 @@ } | ||
| /** | ||
| * usePostParse - use middleware(s) after parsing, before result return | ||
| * @param {function} fn - middleware fn(statement, next) | ||
| */ | ||
| usePostParse(fn) { | ||
| if (typeof fn !== 'function') throw Error('middleware must be a function'); | ||
| this.postParseMiddlewareStack.push(fn); | ||
| } | ||
| /** | ||
| * _aaplyPostParse - internal apply post parse middlewares | ||
| * @param {object} statement - statement to process | ||
| */ | ||
| _applyPostParseMiddlewares(statement) { | ||
| if (this.postParseMiddlewareStack.length === 0) return; | ||
| const chainFn = this.postParseMiddlewareStack | ||
| .reverse() | ||
| .reduce((next, fn) => fn.bind(null, statement, next), () => {}); | ||
| chainFn(statement); | ||
| } | ||
| /** | ||
| * Split text into lines, replace clutter, remove empty lines ... | ||
@@ -83,0 +111,0 @@ * @private |
+1
-1
| { | ||
| "name": "mt940js", | ||
| "version": "1.1.4", | ||
| "version": "1.2.0", | ||
| "description": "javascript mt940 bank statement parser", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+19
-5
@@ -39,3 +39,3 @@ # SWIFT MT940 bank statement format JS parser | ||
| **Statement** | ||
| ### Statement | ||
@@ -81,7 +81,7 @@ - `transactionReference` {string} - tag 20 reference | ||
| **Invocation** | ||
| ### Invocation | ||
| The `Parser` has just one method - `parse(data, withTags = false)` - which will convert raw mt940 string to an array of statements described above. The optional `withTags` parameter would preserve parsed tags to an additional `tags` attribute of a statement (for any additional further analysis). | ||
| **Support for field 86 structure** | ||
| ### Support for field 86 structure | ||
@@ -117,7 +117,21 @@ Currently the library supports the following tag formats: | ||
| ### Middlewares | ||
| **Currently experimental, may change** | ||
| The library support post processing middlewares which is called before returning parsed result. To append a middleware call `usePostParse` passing `fn(statement, next)`. Middlewares are called in the order of appending. Middlewares modify statement object directly. Beware that input data may contain several statements, middlewares are called over each of them one by one. | ||
| ```javascript | ||
| const parser = new Parser(); | ||
| parser.usePostParse((s, next) => { | ||
| s.hasOverdraft = (s.closingBalance < 0); | ||
| next(); | ||
| }); | ||
| ``` | ||
| ## Contribution | ||
| Contribution is welcomed :) | ||
| ## Plans | ||
| - pre/post parsing callbacks | ||
| ## TODO | ||
| - pre parsing middlewares | ||
@@ -124,0 +138,0 @@ ## Author |
+24
-0
@@ -194,2 +194,26 @@ const assert = require('chai').assert; | ||
| /* MIDDLEWARES */ | ||
| describe('Middlewares', () => { | ||
| it('post parse wrong fn throws', () => { | ||
| const parser = new Parser(); | ||
| assert.throws(() => parser.usePostParse(1), /middleware must be a function/); | ||
| }); | ||
| it('post parse middleware', () => { | ||
| const parser = new Parser(); | ||
| parser.usePostParse((s, next) => { | ||
| s.dummyMarker = true; | ||
| next(); | ||
| }); | ||
| parser.usePostParse((s, next) => { | ||
| if (s.dummyMarker) s.dummyMarker2 = true; | ||
| next(); | ||
| }); | ||
| const result = parser.parse(DUMMY_STATEMENT_LINES.join('\n')); | ||
| assert.isDefined(result); | ||
| assert.isTrue(result[0].dummyMarker); | ||
| assert.isTrue(result[0].dummyMarker2); | ||
| }); | ||
| }); | ||
| /* INTEGRATION TEST */ | ||
@@ -196,0 +220,0 @@ describe('Integration test', () => { |
61190
3.78%1104
4.45%146
10.61%