add tests for stirngs helpers, fix leftover mistakes from merge

This commit is contained in:
Arman
2023-03-23 12:16:02 +01:00
parent f55c5865e8
commit e9510db0ba
5 changed files with 122 additions and 4 deletions
+20
View File
@@ -20,3 +20,23 @@ export function capitalize(str: string): string {
export function singular(str: string): string {
return str.replace(/s$/, '');
}
/**
* Convert a dash/underscore/space separated string to camelCase.
*
* @export
* @param {string} str - The string to convert.
* @returns {string} The camelized string.
*/
export function camelize(str: string): string {
if (!str) {
return '';
}
return str
.replace(/[-_\s]+(.)?/g, (_, char: string) => {
return char ? char.toUpperCase() : '';
})
.replace(/^(.)/, (firstChar) => {
return firstChar.toLowerCase();
});
}
@@ -61,7 +61,7 @@
} else updateButtonDisabled = true;
</script>
<Modal size="big" bind:show={showEdit} on:submit={submit} icon={option?.icon}>
<Modal size="big" bind:show={showEdit} onSubmit={submit} icon={option?.icon}>
<svelte:fragment slot="header">
{attr?.type}
{#if attr?.type === 'Relationship'}
@@ -157,7 +157,7 @@
bind:value={data.related}
required
placeholder="Select a collection"
options={collectionList?.collections?.map((n) => ({ value: n.$id, label: n.name })) ?? []} />
options={collections?.map((n) => ({ value: n.$id, label: n.name })) ?? []} />
{#if data?.related}
{@const selectedCol = collections?.find((n) => n.$id === data.related)}
@@ -7,7 +7,7 @@
import { Button, Form, InputSelectSearch, InputText } from '$lib/elements/forms';
import { difference } from '$lib/helpers/array';
import { addNotification } from '$lib/stores/notifications';
import { sdkForProject } from '$lib/stores/sdk';
import { sdk } from '$lib/stores/sdk';
import { onMount } from 'svelte';
import { attributes, collection } from '../store';
@@ -57,7 +57,7 @@
(displayNames?.length && !displayNames[displayNames?.length - 1]);
</script>
<Form on:submit={updateDisplayName}>
<Form onSubmit={updateDisplayName}>
<CardGrid>
<Heading tag="h6" size="7">Display Name</Heading>
<p class="text">
+98
View File
@@ -0,0 +1,98 @@
import { singular, camelize, capitalize } from '$lib/helpers/string';
/*
CAMELIZE
*/
test('camelize should convert hyphenated strings to camel case', () => {
const hyphenated = 'this-is-a-test';
const expected = 'thisIsATest';
expect(camelize(hyphenated)).toBe(expected);
});
test('camelize should convert underscored strings to camel case', () => {
const underscored = 'this_is_a_test';
const expected = 'thisIsATest';
expect(camelize(underscored)).toBe(expected);
});
test('camelize should convert spaced strings to camel case', () => {
const spaced = 'this is a test';
const expected = 'thisIsATest';
expect(camelize(spaced)).toBe(expected);
});
test('camelize should return empty string for falsy input', () => {
expect(camelize(null)).toBe('');
expect(camelize(undefined)).toBe('');
expect(camelize('')).toBe('');
});
test('camelize should handle edge cases', () => {
const edgeCases = [
{ input: 'foo', expected: 'foo' },
{ input: 'foo-bar-', expected: 'fooBar' },
{ input: '-foo-bar', expected: 'fooBar' },
{ input: '--foo-bar--', expected: 'fooBar' },
{ input: '__foo__bar__', expected: 'fooBar' },
{ input: 'foo bar', expected: 'fooBar' },
{ input: 'foo\nbar', expected: 'fooBar' }
];
edgeCases.forEach(({ input, expected }) => {
expect(camelize(input)).toBe(expected);
});
});
/*
SINGULAR
*/
test('singular should remove the "s" from strings', () => {
const pluralNouns = ['apples', 'bananas', 'cherries', 'elephants', 'horses', 'zebras'];
const singularNouns = ['apple', 'banana', 'cherrie', 'elephant', 'horse', 'zebra'];
pluralNouns.forEach((noun, index) => {
expect(singular(noun)).toBe(singularNouns[index]);
});
});
test('singular should not remove characters from strings that do not contain "s"', () => {
const singularNouns = ['apple', 'banana', 'cherry'];
singularNouns.forEach((noun) => {
expect(singular(noun)).toBe(noun);
});
});
test('singular should handle edge cases', () => {
const edgeCases = [
{ input: '', expected: '' },
{ input: 's', expected: '' },
{ input: 'ss', expected: 's' }
];
edgeCases.forEach(({ input, expected }) => {
expect(singular(input)).toBe(expected);
});
});
/*
CAPITALIZE
*/
test('capitalize should capitalize the first letter of a string', () => {
const strings = ['hello world', 'this is a test', 'another example', '1234 testing'];
const expected = ['Hello world', 'This is a test', 'Another example', '1234 testing'];
strings.forEach((str, index) => {
expect(capitalize(str)).toBe(expected[index]);
});
});
test('capitalize should handle empty strings', () => {
expect(capitalize('')).toBe('');
});
test('capitalize should handle strings with no lowercase letters', () => {
expect(capitalize('HELLO')).toBe('HELLO');
});
test('capitalize should handle strings with only one character', () => {
expect(capitalize('a')).toBe('A');
});