Commit bcc383ec authored by Robert Hunt's avatar Robert Hunt

Moved the isNumeric function to a shared util

The isNumeric function was being used in two different places. This
moves that function to a shared util and updates the places to use
the new shared util instead. It also moves the test to the shard util
as well
parent 6c9c3b7e
......@@ -150,3 +150,24 @@ export const formattedChangeInPercent = (firstY, lastY, { nonFiniteResult = '-'
return `${change >= 0 ? '+' : ''}${change}%`;
};
/**
* Checks whether a value is numerical in nature by converting it using parseInt
*
* Example outcomes:
* - isNumeric(100) = true
* - isNumeric('100') = true
* - isNumeric(1.0) = true
* - isNumeric('1.0') = true
* - isNumeric('abc100') = false
* - isNumeric('abc') = false
* - isNumeric(true) = false
* - isNumeric(undefined) = false
* - isNumeric(null) = false
*
* @param value
* @returns {boolean}
*/
export const isNumeric = (value) => {
return !Number.isNaN(parseInt(value, 10));
};
......@@ -8,8 +8,8 @@ import {
import { debounce } from 'lodash';
import { deprecatedCreateFlash as createFlash } from '~/flash';
import httpStatusCodes from '~/lib/utils/http_status';
import { isNumeric } from '~/lib/utils/number_utils';
import { sprintf, s__, __ } from '~/locale';
import { isNumeric } from '../../../utils';
export default {
components: {
......
import { parsePikadayDate, pikadayToString } from '~/lib/utils/datetime_utility';
import { AVAILABLE_TOKEN_TYPES, AUDIT_FILTER_CONFIGS } from './constants';
export const isNumeric = (str) => {
return !Number.isNaN(parseInt(str, 10));
};
export const getTypeFromEntityType = (entityType) => {
return AUDIT_FILTER_CONFIGS.find(
({ entityType: configEntityType }) => configEntityType === entityType,
......
import Api from '~/api';
import httpStatus from '~/lib/utils/http_status';
import { isNumeric } from '~/lib/utils/number_utils';
import { EDIT_PATH_ID_FORMAT, PIPELINE_CONFIGURATION_PATH_FORMAT } from './constants';
const isNumeric = (value) => {
return !Number.isNaN(parseInt(value, 10));
};
export const injectIdIntoEditPath = (path, id) => {
if (!path.match(EDIT_PATH_ID_FORMAT) || !isNumeric(id)) {
return '';
......
import {
isNumeric,
getTypeFromEntityType,
getEntityTypeFromType,
parseAuditEventSearchQuery,
......@@ -7,34 +6,6 @@ import {
} from 'ee/audit_events/utils';
describe('Audit Event Utils', () => {
describe('isNumeric', () => {
describe.each`
value
${false}
${true}
${undefined}
${null}
${'abcd'}
${''}
`('for a list of non-numeric values', ({ value }) => {
it(`returns false for ${value}`, () => {
expect(isNumeric(value)).toBe(false);
});
});
describe.each`
value
${0}
${12345}
${'0'}
${'6789'}
`('for a list of numeric values', ({ value }) => {
it(`returns true for ${value}`, () => {
expect(isNumeric(value)).toBe(true);
});
});
});
describe('getTypeFromEntityType', () => {
it('returns the correct type when given a valid entity type', () => {
expect(getTypeFromEntityType('User')).toEqual('user');
......
......@@ -9,6 +9,7 @@ import {
median,
changeInPercent,
formattedChangeInPercent,
isNumeric,
} from '~/lib/utils/number_utils';
describe('Number Utils', () => {
......@@ -162,4 +163,25 @@ describe('Number Utils', () => {
expect(formattedChangeInPercent(0, 1, { nonFiniteResult: '*' })).toBe('*');
});
});
describe('isNumeric', () => {
it.each`
value | outcome
${0} | ${true}
${12345} | ${true}
${'0'} | ${true}
${'12345'} | ${true}
${1.0} | ${true}
${'1.0'} | ${true}
${'abcd'} | ${false}
${'abcd100'} | ${false}
${''} | ${false}
${false} | ${false}
${true} | ${false}
${undefined} | ${false}
${null} | ${false}
`('when called with $value it returns $outcome', ({ value, outcome }) => {
expect(isNumeric(value)).toBe(outcome);
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment