diff --git a/package-lock.json b/package-lock.json index 4a7ba3c..d5cffb9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -761,9 +761,9 @@ "dev": true }, "handlebars": { - "version": "4.7.3", - "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.7.3.tgz", - "integrity": "sha512-SRGwSYuNfx8DwHD/6InAPzD6RgeruWLT+B8e8a7gGs8FWgHzlExpTFMEq2IA6QpAfOClpKHy6+8IqTjeBCu6Kg==", + "version": "4.5.1", + "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.5.1.tgz", + "integrity": "sha512-C29UoFzHe9yM61lOsIlCE5/mQVGrnIOrOq7maQl76L7tYPCgC1og0Ajt6uWnX4ZTxBPnjw+CUvawphwCfJgUnA==", "dev": true, "requires": { "neo-async": "^2.6.0", @@ -1922,9 +1922,9 @@ "dev": true }, "uglify-js": { - "version": "3.8.0", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.8.0.tgz", - "integrity": "sha512-ugNSTT8ierCsDHso2jkBHXYrU8Y5/fY2ZUprfrJUiD7YpuFvV4jODLFmb3h4btQjqr5Nh4TX4XtgDfCU1WdioQ==", + "version": "3.6.7", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.7.tgz", + "integrity": "sha512-4sXQDzmdnoXiO+xvmTzQsfIiwrjUCSA95rSP4SEd8tDb51W2TiDOlL76Hl+Kw0Ie42PSItCW8/t6pBNCF2R48A==", "dev": true, "optional": true, "requires": { diff --git a/src/lib/categoriser.ts b/src/lib/categoriser.ts index 22cbe0e..f2fb1e9 100644 --- a/src/lib/categoriser.ts +++ b/src/lib/categoriser.ts @@ -1,11 +1,10 @@ import { Transaction } from './transaction'; + import { parse_string_rules, parse_number_rules, parse_identifier_rules } from './rule/matcher'; import { Category } from '../types/category'; import { CategoryRule } from '../types/category-rule'; import { NumericMatchConfig, StringMatchConfig } from '../types/match-config'; -import { RuleMatchMode } from '../lib/enums'; - import moment from 'moment'; export class Categoriser { @@ -26,15 +25,7 @@ export class Categoriser { } static transaction_matches_rule(txn: Transaction, rule: CategoryRule): boolean { - let mode = rule['mode'] || RuleMatchMode.Strict; - // Start with true for strict mode (if anything doesn't match it's false, - // if all match it's true) or false for flex mode (if any of the things - // match it's true, if none match it's false) - let match: boolean = mode == RuleMatchMode.Strict; - - const _cmp = (first: boolean, second: boolean) => ( - mode === RuleMatchMode.Strict ? first && second : first || second - ); + let match: boolean = true; // identifier match is most important if ( @@ -47,7 +38,7 @@ export class Categoriser { ['type', 'description', 'source'].forEach((prop: string) => { let match_config: StringMatchConfig = rule[prop]; if (match_config) { - match = _cmp(match, parse_string_rules(txn[prop], match_config)); + match = match && parse_string_rules(txn[prop], match_config); } }); @@ -55,7 +46,7 @@ export class Categoriser { let match_config: NumericMatchConfig = rule[prop]; if (match_config) { - match = _cmp(match, parse_number_rules(txn[prop], match_config)); + match = match && parse_number_rules(txn[prop], match_config); } }); @@ -64,7 +55,7 @@ export class Categoriser { let match_config: NumericMatchConfig = rule['txn_day']; let day: number = moment(txn.date).utc().date(); - match = _cmp(match, parse_number_rules(day, match_config)); + match = match && parse_number_rules(day, match_config); } return match; diff --git a/src/types/category-rule.ts b/src/types/category-rule.ts index b748126..5c17c3c 100644 --- a/src/types/category-rule.ts +++ b/src/types/category-rule.ts @@ -1,5 +1,4 @@ import { NumericMatchConfig, StringMatchConfig, IdentifierMatchConfig } from "./match-config"; -import { RuleMatchMode } from "../lib/enums"; export type CategoryRule = { identifier?: IdentifierMatchConfig, @@ -9,6 +8,5 @@ export type CategoryRule = { source?: StringMatchConfig; debitAmount?: NumericMatchConfig; creditAmount?: NumericMatchConfig; - mode?: RuleMatchMode; [key: string]: any, }; diff --git a/test/categoriser.spec.ts b/test/categoriser.spec.ts index b9e15a8..c06d38b 100644 --- a/test/categoriser.spec.ts +++ b/test/categoriser.spec.ts @@ -1,12 +1,11 @@ -import { expect, assert } from 'chai'; -import 'mocha'; - +//mocha -r ts-node/register t/report.spec.ts import { Transaction } from '../src/lib/transaction'; import { Categoriser } from '../src/lib/categoriser'; -import { Category } from '../src/types/category'; +import { expect, assert } from 'chai'; +import 'mocha'; import { RuleMatchMode } from '../src/lib/enums'; - +import { Category } from '../src/types/category'; describe('Categoriser', () => { it('Can parse rules', () => { let transaction = new Transaction({ @@ -184,12 +183,6 @@ describe('Categoriser', () => { ["=", "CDE"], ] }, - "description": { - "rules": [ - ["=~", "this is a bill"] - ] - }, - "mode": RuleMatchMode.Flex }, "className": "cat-bills", "id": "bills" @@ -357,18 +350,10 @@ describe('Categoriser', () => { debitAmount: 5000, creditAmount: 0, identifier: 'CDE', - }), - new Transaction({ - date: '20/01/2017', - type: 'DEB', - description: 'this is a bill', - debitAmount: 99.99, - creditAmount: 0, }) ]; return categoriser.categorise_transactions(transactions).then(() => { - console.log(transactions) expect(transactions[0].calculatedMonth).to.equal('201701'); expect(transactions[0].categories.map((cat) => cat.id)).to.deep.equal(['tfr-pers']); expect(Categoriser.is_internal_transfer(transactions[0])).to.be.true; @@ -390,7 +375,6 @@ describe('Categoriser', () => { expect(transactions[8].categories.map((cat) => cat.id)).to.deep.equal(['income']); expect(transactions[9].categories.map((cat) => cat.id)).to.deep.equal(['exp', 'bills']); expect(transactions[10].categories.map((cat) => cat.id)).to.deep.equal(['exp', 'bills']); - expect(transactions[11].categories.map((cat) => cat.id)).to.deep.equal(['exp', 'bills']); }); }); });