-
Notifications
You must be signed in to change notification settings - Fork 2k
/
table-rows.js
72 lines (57 loc) · 1.75 KB
/
table-rows.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/** @format */
/**
* External dependencies
*/
import { moment } from 'i18n-calypso';
import { flatten, isDate, omit, some, values, without } from 'lodash';
function formatDate( date ) {
return moment( date ).format( 'll' );
}
function getSearchableStrings( transaction ) {
const rootStrings = values( omit( transaction, 'items' ) ),
transactionItems = transaction.items || [],
itemStrings = flatten( transactionItems.map( values ) );
return without( rootStrings.concat( itemStrings ), null, undefined );
}
function search( transactions, searchQuery ) {
return transactions.filter( function( transaction ) {
return some( getSearchableStrings( transaction ), function( val ) {
let haystack, needle;
if ( isDate( val ) ) {
val = formatDate( val );
}
haystack = val.toString().toLowerCase();
needle = searchQuery.toLowerCase();
return haystack.indexOf( needle ) !== -1;
} );
} );
}
function filter( transactions, params ) {
if ( params.search ) {
transactions = search( transactions, params.search );
}
if ( params.date ) {
if ( params.date.newest ) {
transactions = transactions.slice( 0, params.date.newest );
} else if ( params.date.month || params.date.before ) {
transactions = transactions.filter( function( transaction ) {
const date = moment( transaction.date );
if ( params.date.month ) {
return date.isSame( params.date.month, 'month' );
} else if ( params.date.before ) {
return date.isBefore( params.date.before, 'month' );
}
} );
}
}
if ( params.app && params.app !== 'all' ) {
transactions = transactions.filter( function( transaction ) {
return transaction.service === params.app;
} );
}
return transactions;
}
export default {
formatDate: formatDate,
filter: filter,
};