Skip to content

Commit

Permalink
Merge pull request #8387 from cfpb/ans_remove_string_utils
Browse files Browse the repository at this point in the history
Remove `stringEscape` and `stringMatch`
  • Loading branch information
anselmbradford committed May 7, 2024
2 parents 2e721b5 + d401478 commit e67f57c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 70 deletions.
21 changes: 1 addition & 20 deletions cfgov/unprocessed/js/modules/util/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,4 @@ function formatTimestamp(totalSeconds) {
return timestamp;
}

/**
* Escapes a string.
* @param {string} s - The string to escape.
* @returns {string} The escaped string.
*/
function stringEscape(s) {
return s.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&');
}

/**
* Tests whether a string matches another.
* @param {string} x - The control string.
* @param {string} y - The comparison string.
* @returns {boolean} True if `x` and `y` match, false otherwise.
*/
function stringMatch(x, y) {
return RegExp(stringEscape(y.trim()), 'i').test(x);
}

export { formatTimestamp, stringEscape, stringMatch };
export { formatTimestamp };
51 changes: 1 addition & 50 deletions test/unit_tests/js/modules/util/strings-spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
formatTimestamp,
stringEscape,
stringMatch,
} from '../../../../../cfgov/unprocessed/js/modules/util/strings.js';
let string;
let control;
import { formatTimestamp } from '../../../../../cfgov/unprocessed/js/modules/util/strings.js';

describe('Strings formatTimestamp()', () => {
it('should convert 23 seconds into 00:23 timestamp', () => {
Expand All @@ -22,46 +16,3 @@ describe('Strings formatTimestamp()', () => {
expect(formatTimestamp(seconds)).toBe('04:26:41');
});
});

describe('Strings stringEscape()', () => {
it('should escape a url', () => {
string = 'https://google.com';

expect(stringEscape(string)).toBe('https://google\\.com');
});

it('should escape a hyphenated name', () => {
string = 'Miller-Webster';

expect(stringEscape(string)).toBe('Miller\\-Webster');
});

it('should escape a comma', () => {
string = 'Students, Parents, and Teachers';

expect(stringEscape(string)).toBe('Students, Parents, and Teachers');
});
});

describe('Strings stringMatch()', () => {
it('should return true when testing matching strings', () => {
string = 'Test String';
control = 'Test String';

expect(stringMatch(control, string)).toBe(true);
});

it('should return true when testing matching strings with differing casing', () => {
string = 'test string';
control = 'Test String';

expect(stringMatch(control, string)).toBe(true);
});

it('should return false when testing differing strings', () => {
string = 'Test String';
control = 'Result String';

expect(stringMatch(control, string)).toBe(false);
});
});

0 comments on commit e67f57c

Please sign in to comment.