Commit 08bd572a authored by Vitaly Slobodin's avatar Vitaly Slobodin

Merge branch '323326-follow-up-from-create-edit-compliance-framework-page' into 'master'

Move the isNumeric util to the shared number_utils.js

See merge request gitlab-org/gitlab!55833
parents 0a2e826a bcc383ec
...@@ -150,3 +150,24 @@ export const formattedChangeInPercent = (firstY, lastY, { nonFiniteResult = '-' ...@@ -150,3 +150,24 @@ export const formattedChangeInPercent = (firstY, lastY, { nonFiniteResult = '-'
return `${change >= 0 ? '+' : ''}${change}%`; 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 { ...@@ -8,8 +8,8 @@ import {
import { debounce } from 'lodash'; import { debounce } from 'lodash';
import { deprecatedCreateFlash as createFlash } from '~/flash'; import { deprecatedCreateFlash as createFlash } from '~/flash';
import httpStatusCodes from '~/lib/utils/http_status'; import httpStatusCodes from '~/lib/utils/http_status';
import { isNumeric } from '~/lib/utils/number_utils';
import { sprintf, s__, __ } from '~/locale'; import { sprintf, s__, __ } from '~/locale';
import { isNumeric } from '../../../utils';
export default { export default {
components: { components: {
......
import { parsePikadayDate, pikadayToString } from '~/lib/utils/datetime_utility'; import { parsePikadayDate, pikadayToString } from '~/lib/utils/datetime_utility';
import { AVAILABLE_TOKEN_TYPES, AUDIT_FILTER_CONFIGS } from './constants'; import { AVAILABLE_TOKEN_TYPES, AUDIT_FILTER_CONFIGS } from './constants';
export const isNumeric = (str) => {
return !Number.isNaN(parseInt(str, 10));
};
export const getTypeFromEntityType = (entityType) => { export const getTypeFromEntityType = (entityType) => {
return AUDIT_FILTER_CONFIGS.find( return AUDIT_FILTER_CONFIGS.find(
({ entityType: configEntityType }) => configEntityType === entityType, ({ entityType: configEntityType }) => configEntityType === entityType,
......
import Api from '~/api'; import Api from '~/api';
import httpStatus from '~/lib/utils/http_status'; 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'; 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) => { export const injectIdIntoEditPath = (path, id) => {
if (!path.match(EDIT_PATH_ID_FORMAT) || !isNumeric(id)) { if (!path.match(EDIT_PATH_ID_FORMAT) || !isNumeric(id)) {
return ''; return '';
......
import { import {
isNumeric,
getTypeFromEntityType, getTypeFromEntityType,
getEntityTypeFromType, getEntityTypeFromType,
parseAuditEventSearchQuery, parseAuditEventSearchQuery,
...@@ -7,34 +6,6 @@ import { ...@@ -7,34 +6,6 @@ import {
} from 'ee/audit_events/utils'; } from 'ee/audit_events/utils';
describe('Audit Event 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', () => { describe('getTypeFromEntityType', () => {
it('returns the correct type when given a valid entity type', () => { it('returns the correct type when given a valid entity type', () => {
expect(getTypeFromEntityType('User')).toEqual('user'); expect(getTypeFromEntityType('User')).toEqual('user');
......
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
median, median,
changeInPercent, changeInPercent,
formattedChangeInPercent, formattedChangeInPercent,
isNumeric,
} from '~/lib/utils/number_utils'; } from '~/lib/utils/number_utils';
describe('Number Utils', () => { describe('Number Utils', () => {
...@@ -162,4 +163,25 @@ describe('Number Utils', () => { ...@@ -162,4 +163,25 @@ describe('Number Utils', () => {
expect(formattedChangeInPercent(0, 1, { nonFiniteResult: '*' })).toBe('*'); 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