Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/utilities": Removed
---

Unused utils with security vulnerabilities ([#11899](https://github.com/linode/manager/pull/11899))
49 changes: 1 addition & 48 deletions packages/utilities/src/helpers/stringUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { describe, expect, it } from 'vitest';

import {
getNextLabel,
getNumberAtEnd,
isNumeric,
removeNumberAtEnd,
truncateAndJoinList,
} from './stringUtils';
import { isNumeric, truncateAndJoinList } from './stringUtils';

describe('truncateAndJoinList', () => {
const strList = ['a', 'b', 'c'];
Expand Down Expand Up @@ -57,44 +51,3 @@ describe('isNumeric', () => {
expect(isNumeric('my-linode')).toBe(false);
});
});

describe('getNumberAtEnd', () => {
it('should return 1 when given test-1', () => {
expect(getNumberAtEnd('test-1')).toBe(1);
});
it('should return null if there is no number in the string', () => {
expect(getNumberAtEnd('test')).toBe(null);
});
it('should get the last number in the string', () => {
expect(getNumberAtEnd('test-1-2-3')).toBe(3);
});
it('should handle a string that only contains numbers', () => {
expect(getNumberAtEnd('123')).toBe(123);
});
});

describe('removeNumberAtEnd', () => {
it('should return 1 in "test-1"', () => {
expect(removeNumberAtEnd('test-1')).toBe('test-');
});
it('should return the same string if there is no number at the end', () => {
expect(removeNumberAtEnd('test')).toBe('test');
});
it('should return an empty string if the input is just a number', () => {
expect(removeNumberAtEnd('123')).toBe('');
});
it('should not remove the first number', () => {
expect(removeNumberAtEnd('1-2-3')).toBe('1-2-');
});
});

describe('getNextLabel', () => {
it('should append a number to get the next label', () => {
expect(getNextLabel({ label: 'test' }, [{ label: 'test' }])).toBe('test-1');
});
it('should not duplicate labels so that the returned label is unique', () => {
expect(getNextLabel({ label: 'test' }, [{ label: 'test-1' }])).toBe(
'test-2'
);
});
});
44 changes: 0 additions & 44 deletions packages/utilities/src/helpers/stringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,3 @@ export const truncateAndJoinList = (
export const wrapInQuotes = (s: string) => '"' + s + '"';

export const isNumeric = (s: string) => /^\d+$/.test(s);

export function getNumberAtEnd(str: string) {
// Use a regular expression to match one or more digits at the end of the string
const match = str.match(/\d+$/);

// If there is a match, return the matched number; otherwise, return null
return match ? parseInt(match[0], 10) : null;
}

export function removeNumberAtEnd(str: string) {
// Use a regular expression to match one or more digits at the end of the string
const regex = /\d+$/;

// Use the replace() method to remove the matched portion
return str.replace(regex, '');
}

/**
* Gets the next available unique entity label
*/
export function getNextLabel<T extends { label: string }>(
selectedEntity: T,
allEntities: T[]
): string {
const numberAtEnd = getNumberAtEnd(selectedEntity.label);

let labelToReturn = '';

if (numberAtEnd === null) {
labelToReturn = `${selectedEntity.label}-1`;
} else {
labelToReturn = `${removeNumberAtEnd(selectedEntity.label)}${
numberAtEnd + 1
}`;
}

if (allEntities.some((r) => r.label === labelToReturn)) {
return getNextLabel(
{ ...selectedEntity, label: labelToReturn },
allEntities
);
}
return labelToReturn;
}