Skip to content

Commit

Permalink
Moved some stuff to shared utils
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonStoltz committed Feb 9, 2021
1 parent 309f884 commit e98afe4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import { kea, MakeLogicType } from 'kea';
import { omit, cloneDeep, isEmpty } from 'lodash';

import { NUMBER } from '../../../shared/constants/field_types';
import { HttpLogic } from '../../../shared/http';
import { Schema, SchemaConflicts, SchemaTypes } from '../../../shared/types';
import { Schema, SchemaConflicts } from '../../../shared/types';
import { setSuccessMessage, flashAPIErrors } from '../../../shared/flash_messages';

import { Result } from '../result/types';
Expand All @@ -23,6 +22,7 @@ import {
DELETE_SUCCESS_MESSAGE,
DELETE_CONFIRMATION_MESSAGE,
} from './constants';
import { filterIfTerm, parseBoostCenter, removeBoostStateProps } from './utils';

interface RelevanceTuningProps {
searchSettings: SearchSettings;
Expand Down Expand Up @@ -105,29 +105,6 @@ interface RelevanceTuningValues {
resultsLoading: boolean;
}

// If the user hasn't entered a filter, then we can skip filtering the array entirely
const filterIfTerm = (array: string[], filterTerm: string): string[] => {
return filterTerm === '' ? array : array.filter((item) => item.includes(filterTerm));
};

const removeBoostStateProps = (searchSettings: SearchSettings) => {
const updatedSettings = cloneDeep(searchSettings);
const { boosts } = updatedSettings;
const keys = Object.keys(boosts);
keys.forEach((key) => boosts[key].forEach((boost) => delete boost.newBoost));

return updatedSettings;
};

const parseBoostCenter = (fieldType: SchemaTypes, value: string | number) => {
// Leave non-numeric fields alone
if (fieldType === NUMBER) {
const floatValue = parseFloat(value as string);
return isNaN(floatValue) ? value : floatValue;
}
return value;
};

export const RelevanceTuningLogic = kea<
MakeLogicType<RelevanceTuningValues, RelevanceTuningActions>
>({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { cloneDeep } from 'lodash';

import { NUMBER } from '../../../shared/constants/field_types';
import { SchemaTypes } from '../../../shared/types';

import { SearchSettings } from './types';

// If the user hasn't entered a filter, then we can skip filtering the array entirely
export const filterIfTerm = (array: string[], filterTerm: string): string[] => {
return filterTerm === '' ? array : array.filter((item) => item.includes(filterTerm));
};

export const removeBoostStateProps = (searchSettings: SearchSettings) => {
const updatedSettings = cloneDeep(searchSettings);
const { boosts } = updatedSettings;
const keys = Object.keys(boosts);
keys.forEach((key) => boosts[key].forEach((boost) => delete boost.newBoost));

return updatedSettings;
};

export const parseBoostCenter = (fieldType: SchemaTypes, value: string | number) => {
// Leave non-numeric fields alone
if (fieldType === NUMBER) {
const floatValue = parseFloat(value as string);
return isNaN(floatValue) ? value : floatValue;
}
return value;
};

0 comments on commit e98afe4

Please sign in to comment.