44 * Discover exports: Trans. Date, Post Date, Description, Amount, Category
55 * Amount sign: positive = charge (EXPENSE), negative = payment/refund (INCOME)
66 * Date format: MM/DD/YYYY
7+ *
8+ * Mapping model:
9+ * - LEFT side (Discover's category strings) is hardcoded here — it's a
10+ * fact about Discover's export format.
11+ * - RIGHT side (target app category) is resolved at runtime against the
12+ * user's real DB categories via keyword hints. Unmatched entries fall
13+ * back to "Other".
714 */
815
916const DISCOVER_HEADERS = [
@@ -14,24 +21,28 @@ const DISCOVER_HEADERS = [
1421 'Category' ,
1522] ;
1623
17- const CATEGORY_MAP = {
18- 'Supermarkets' : 'Groceries' ,
19- 'Gasoline' : 'Transport' ,
20- 'Merchandise' : 'Shopping' ,
21- 'Restaurants' : 'Food & Dining' ,
22- 'Travel/ Entertainment' : 'Entertainment' ,
23- 'Travel/Entertainment' : 'Entertainment' ,
24- 'Medical Services' : 'Healthcare' ,
25- 'Services' : 'Services' ,
26- 'Government Services' : 'Services' ,
27- 'Education' : 'Education' ,
28- 'Automotive' : 'Transport' ,
29- 'Warehouse Clubs' : 'Groceries' ,
30- 'Payments and Credits' : 'Other' ,
31- 'Awards and Rebate Credits' : 'Other' ,
32- 'Department Stores' : 'Shopping' ,
24+ // Hints are keyword substrings to fuzzy-match against user category names
25+ // (case-insensitive). First hint that matches an existing category wins.
26+ // Empty array = go straight to the "Other" fallback.
27+ const CATEGORY_HINTS = {
28+ 'Supermarkets' : [ 'grocer' ] ,
29+ 'Gasoline' : [ 'transport' , 'gas' , 'fuel' ] ,
30+ 'Merchandise' : [ 'shopping' ] ,
31+ 'Restaurants' : [ 'dining' , 'restaurant' , 'food' ] ,
32+ 'Medical Services' : [ 'health' , 'medical' ] ,
33+ 'Services' : [ 'utilit' ] ,
34+ 'Government Services' : [ 'utilit' ] ,
35+ 'Education' : [ 'education' ] ,
36+ 'Automotive' : [ 'transport' , 'auto' ] ,
37+ 'Warehouse Clubs' : [ 'grocer' ] ,
38+ 'Department Stores' : [ 'shopping' ] ,
39+ 'Payments and Credits' : [ ] ,
40+ 'Awards and Rebate Credits' : [ ] ,
3341} ;
3442
43+ const TRAVEL_RIDES_HINTS = [ 'transport' ] ;
44+ const TRAVEL_OTHER_HINTS = [ 'entertain' ] ;
45+
3546const TRANSPORT_KEYWORDS = [ 'uber' , 'lyft' , 'taxi' , 'cab' , 'rideshare' ] ;
3647
3748const RIDES_SUFFIX = '::rides' ;
@@ -46,6 +57,26 @@ function isTravelEntertainment(bankCategory) {
4657 return bankCategory === 'Travel/ Entertainment' || bankCategory === 'Travel/Entertainment' ;
4758}
4859
60+ /**
61+ * Fuzzy-match hints against the user's DB categories.
62+ * Returns the matched category NAME, or the name of "Other", or null.
63+ */
64+ function matchCategory ( hints , userCategories ) {
65+ const expenseCats = userCategories . filter (
66+ ( c ) => ( c . type || '' ) . toUpperCase ( ) === 'EXPENSE'
67+ ) ;
68+
69+ for ( const hint of hints ) {
70+ const found = expenseCats . find ( ( c ) =>
71+ c . name . toLowerCase ( ) . includes ( hint . toLowerCase ( ) )
72+ ) ;
73+ if ( found ) return found . name ;
74+ }
75+
76+ const other = expenseCats . find ( ( c ) => c . name . toLowerCase ( ) === 'other' ) ;
77+ return other ? other . name : expenseCats [ 0 ] ?. name || null ;
78+ }
79+
4980export function getMappingDisplayName ( key ) {
5081 if ( key . endsWith ( RIDES_SUFFIX ) ) {
5182 return key . replace ( RIDES_SUFFIX , '' ) + ' (rides)' ;
@@ -61,7 +92,11 @@ export function validate(headers) {
6192 return DISCOVER_HEADERS . every ( ( h ) => normalized . includes ( h ) ) ;
6293}
6394
64- export function getDefaultMapping ( rows ) {
95+ /**
96+ * Build the default bankCategory → appCategoryName map by cross-referencing
97+ * parser hints against the user's real DB categories.
98+ */
99+ export function getDefaultMapping ( rows , userCategories = [ ] ) {
65100 const mapping = { } ;
66101 let hasRides = false ;
67102 let hasOtherTravel = false ;
@@ -82,18 +117,19 @@ export function getDefaultMapping(rows) {
82117 }
83118
84119 if ( ! mapping [ bankCat ] ) {
85- mapping [ bankCat ] = CATEGORY_MAP [ bankCat ] || 'Other' ;
120+ const hints = CATEGORY_HINTS [ bankCat ] || [ ] ;
121+ mapping [ bankCat ] = matchCategory ( hints , userCategories ) || '' ;
86122 }
87123 }
88124
89125 if ( travelKey ) {
90- if ( hasRides && hasOtherTravel ) {
91- mapping [ travelKey + RIDES_SUFFIX ] = 'Transport' ;
92- mapping [ travelKey + OTHER_SUFFIX ] = 'Entertainment ';
93- } else if ( hasRides ) {
94- mapping [ travelKey + RIDES_SUFFIX ] = 'Transport' ;
95- } else {
96- mapping [ travelKey + OTHER_SUFFIX ] = 'Entertainment ';
126+ if ( hasRides ) {
127+ mapping [ travelKey + RIDES_SUFFIX ] =
128+ matchCategory ( TRAVEL_RIDES_HINTS , userCategories ) || ' ';
129+ }
130+ if ( hasOtherTravel ) {
131+ mapping [ travelKey + OTHER_SUFFIX ] =
132+ matchCategory ( TRAVEL_OTHER_HINTS , userCategories ) || ' ';
97133 }
98134 }
99135
@@ -121,14 +157,22 @@ export function parse(rows, categoryMapping) {
121157
122158 if ( isTravelEntertainment ( rawCategory ) && categoryMapping ) {
123159 const suffix = isTransportDescription ( rawDescription ) ? RIDES_SUFFIX : OTHER_SUFFIX ;
124- category = categoryMapping [ rawCategory + suffix ]
125- || categoryMapping [ rawCategory ]
126- || ( isTransportDescription ( rawDescription ) ? 'Transport' : 'Entertainment' ) ;
160+ category =
161+ categoryMapping [ rawCategory + suffix ] ||
162+ categoryMapping [ rawCategory ] ||
163+ '' ;
127164 } else {
128- category = categoryMapping ?. [ rawCategory ] || CATEGORY_MAP [ rawCategory ] || 'Other ';
165+ category = categoryMapping ?. [ rawCategory ] || ' ';
129166 }
130167
131- return { date, amount : numeric , description : rawDescription , type : 'EXPENSE' , category, bankCategory : rawCategory } ;
168+ return {
169+ date,
170+ amount : numeric ,
171+ description : rawDescription ,
172+ type : 'EXPENSE' ,
173+ category,
174+ bankCategory : rawCategory ,
175+ } ;
132176 } )
133177 . filter ( Boolean ) ;
134178}
0 commit comments