Files
Bubberstation/tgui/packages/common/string.test.ts
Jeremiah bca56852ca tgui: Strings.ts (#83096)
## About The Pull Request
Wrote better docs and tests for string helpers in tgui.



Something I noticed, which I'm ok with reverting, is that multiline does
precisely nothing. And despite being a template string fn, it doesn't
accept vars for interpolation. It looks as if it were used to circumvent
a babel limitation, which we are no longer using.

Removed:
- babel plugin (unused)
- multiline (no impact)
- createGlobPattern (unused)
- buildQueryString (unused)

<details>
<summary>Spot the difference</summary>

![Screenshot 2024-05-06
095420](https://github.com/tgstation/tgstation/assets/42397676/bbf2f58e-81b7-4023-8da4-8d7f19cd3bb1)

![Screenshot 2024-05-06
095443](https://github.com/tgstation/tgstation/assets/42397676/24f303ba-f352-4132-a2a6-873dff0d979b)

(There is none)

</details>

## Why It's Good For The Game
More core tools in TS.
Better docs so hopefully these functions are more widely used (and done
so properly)
Tests so things don't break
2024-05-13 02:37:25 -04:00

36 lines
1.3 KiB
TypeScript

import { createSearch, decodeHtmlEntities, toTitleCase } from './string';
describe('createSearch', () => {
it('matches search terms correctly', () => {
const search = createSearch('test', (obj: { value: string }) => obj.value);
const obj1 = { value: 'This is a test string.' };
const obj2 = { value: 'This is a different string.' };
const obj3 = { value: 'This is a test string.' };
const objects = [obj1, obj2, obj3];
expect(objects.filter(search)).toEqual([obj1, obj3]);
});
});
describe('toTitleCase', () => {
it('converts strings to title case correctly', () => {
expect(toTitleCase('hello world')).toBe('Hello World');
expect(toTitleCase('HELLO WORLD')).toBe('Hello World');
expect(toTitleCase('HeLLo wORLd')).toBe('Hello World');
expect(toTitleCase('a tale of two cities')).toBe('A Tale of Two Cities');
expect(toTitleCase('war and peace')).toBe('War and Peace');
});
});
describe('decodeHtmlEntities', () => {
it('decodes HTML entities and removes unnecessary HTML tags correctly', () => {
expect(decodeHtmlEntities('<br>')).toBe('\n');
expect(decodeHtmlEntities('<p>Hello World</p>')).toBe('Hello World');
expect(decodeHtmlEntities('&amp;')).toBe('&');
expect(decodeHtmlEntities('&#38;')).toBe('&');
expect(decodeHtmlEntities('&#x26;')).toBe('&');
});
});