Predefine regexes for sort/filter

This commit is contained in:
Vincent Fiduccia 2021-09-11 01:42:21 -07:00
parent 35f8de8297
commit ba9d18051e
No known key found for this signature in database
GPG Key ID: 2B29AD6BB2BB2582
2 changed files with 54 additions and 47 deletions

View File

@ -134,59 +134,61 @@ function columnsToSearchField(columns) {
return out.filter(x => !!x);
}
const ipLike = /^[0-9a-f\.:]+$/i;
function matches(fields, token, item) {
const tokenMayBeIp = /^[0-9a-f\.:]+$/i.test(token);
for ( let field of fields ) {
if ( !field ) {
continue;
}
for ( let i = 0 ; i < fields.length ; i++ ) {
let field = fields[i];
let modifier;
const idx = field.indexOf(':');
if ( field ) {
const idx = field.indexOf(':');
let modifier = null;
if ( idx > 0 ) {
modifier = field.substr(idx + 1);
field = field.substr(0, idx);
}
if ( idx > 0 ) {
modifier = field.substr(idx + 1);
field = field.substr(0, idx);
let val;
if ( field.includes('.') ) {
val = get(item, field);
} else {
val = item[field];
}
if ( val === undefined ) {
continue;
}
val = (`${ val }`).toLowerCase();
if ( !val ) {
continue;
}
if ( !modifier ) {
if ( val.includes(token) ) {
return true;
}
let val = get(item, field);
if ( val === undefined ) {
continue;
}
val = (`${ val }`).toLowerCase();
if ( !val ) {
continue;
} else if ( modifier === 'exact' ) {
if ( val === token ) {
return true;
}
} else if ( modifier === 'ip' ) {
const tokenMayBeIp = ipLike.test(token);
switch ( modifier ) {
case 'exact':
if ( val === token ) {
return true;
}
break;
case 'ip':
if ( tokenMayBeIp ) {
const re = new RegExp(`(?:^|\.)${ token }(?:\.|$)`);
if ( re.test(val) ) {
return true;
}
}
break;
case 'prefix':
if ( val.indexOf(token) === 0) {
return true;
}
break;
default:
if ( val.includes(token) ) {
if ( tokenMayBeIp ) {
const re = new RegExp(`(?:^|\.)${ token }(?:\.|$)`);
if ( re.test(val) ) {
return true;
}
}
} else if ( modifier === 'prefix' ) {
if ( val.indexOf(token) === 0) {
return true;
}
}
}

View File

@ -209,12 +209,17 @@ export function sortBy(ary, keys, desc) {
}
// Turn foo1-bar2 into foo0000000001-bar0000000002 so that the numbers sort numerically
export function sortableNumericSuffix(str) {
str = str || '';
const splitRegex = /([^\d]+)/;
const notNumericRegex = /^[0-9]+$/;
return str.split(/([^\d]+)/).map(x => isNumeric(x) ? strPad(x, 10, '0') : x).join('').trim();
export function sortableNumericSuffix(str) {
if ( typeof str !== 'string' ) {
return str;
}
return str.split(splitRegex).map(x => x.match(notNumericRegex) ? strPad(x, 10, '0') : x).join('').trim();
}
export function isNumeric(num) {
return !!`${ num }`.match(/^[0-9]+$/);
return !!`${ num }`.match(notNumericRegex);
}