Get rid of lots of unnecessary lodash calls.

This commit is contained in:
baldo 2022-07-28 13:16:13 +02:00
parent b734a422a7
commit 5592892f0d
16 changed files with 99 additions and 115 deletions

View file

@ -2,7 +2,7 @@ import _ from "lodash";
export function inCondition<T>(field: string, list: T[]): {query: string, params: T[]} {
return {
query: '(' + field + ' IN (' + _.join(_.times(list.length, _.constant('?')), ', ') + '))',
query: '(' + field + ' IN (' + _.times(list.length, () =>'?').join(', ') + '))',
params: list,
}
}

View file

@ -184,7 +184,7 @@ export function filter<E>(entities: E[], allowedFilterFields: string[], restPara
if (!query) {
return true;
}
return _.some(allowedFilterFields, (field: string): boolean => {
return allowedFilterFields.some((field: string): boolean => {
if (!query) {
return true;
}
@ -209,15 +209,15 @@ export function filter<E>(entities: E[], allowedFilterFields: string[], restPara
const filters = restParams.filters;
function filtersMatch(entity: Entity): boolean {
if (_.isEmpty(filters)) {
if (isUndefined(filters) || _.isEmpty(filters)) {
return true;
}
return _.every(filters, (value: any, key: string): boolean => {
return Object.entries(filters).every(([key, value]) => {
if (isUndefined(value)) {
return true;
}
if (_.startsWith(key, 'has')) {
if (key.startsWith('has')) {
const entityKey = key.substring(3, 4).toLowerCase() + key.substring(4);
return _.isEmpty(entity[entityKey]).toString() !== value;
}
@ -225,9 +225,7 @@ export function filter<E>(entities: E[], allowedFilterFields: string[], restPara
});
}
return _.filter(entities, function (entity) {
return queryMatches(entity) && filtersMatch(entity);
});
return entities.filter(entity => queryMatches(entity) && filtersMatch(entity));
}
export function sort<T extends Record<S, any>, S extends string>(entities: T[], isSortField: TypeGuard<S>, restParams: RestParams): T[] {
@ -262,7 +260,7 @@ export function sort<T extends Record<S, any>, S extends string>(entities: T[],
return sorted;
}
export function getPageEntities(entities: Entity[], restParams: RestParams) {
export function getPageEntities<Entity>(entities: Entity[], restParams: RestParams): Entity[] {
const page = restParams._page;
const perPage = restParams._perPage;
@ -291,7 +289,7 @@ export function filterClause<S>(
return {
query: filter.query + ' ' + orderBy.query + ' ' + limitOffset.query,
params: _.concat(filter.params, orderBy.params, limitOffset.params)
params: [...filter.params, ...orderBy.params, ...limitOffset.params]
};
}

View file

@ -1,8 +1,8 @@
import _ from "lodash"
import {MAC} from "../types";
import {isString, MAC} from "../types";
export function normalizeString(str: string): string {
return _.isString(str) ? str.trim().replace(/\s+/g, ' ') : str;
return isString(str) ? str.trim().replace(/\s+/g, ' ') : str;
}
export function normalizeMac(mac: MAC): MAC {

View file

@ -1,5 +1,4 @@
import {DurationSeconds, UnixTimestampSeconds} from "../types";
import _ from "lodash";
import {DurationSeconds, isString, UnixTimestampSeconds} from "../types";
import moment, {Moment} from "moment";
export function now(): UnixTimestampSeconds {
@ -45,7 +44,7 @@ export function formatTimestamp(timestamp: UnixTimestampSeconds): string {
}
export function parseTimestamp(timestamp: any): UnixTimestampSeconds | null {
if (!_.isString(timestamp)) {
if (!isString(timestamp)) {
return null;
}
const parsed = moment.utc(timestamp);

View file

@ -1,4 +1,3 @@
import _ from "lodash"
import {config} from "../config"
import {MonitoringToken, Url} from "../types"
@ -12,15 +11,10 @@ function formUrl(route: string, queryParams?: { [key: string]: string }): Url {
}
if (queryParams) {
url += '?';
url += _.join(
_.map(
queryParams,
function (value, key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(value);
}
),
'&'
);
url +=
Object.entries(queryParams)
.map(([key, value]) => encodeURIComponent(key) + '=' + encodeURIComponent(value))
.join("&");
}
return url as Url;
}