Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Add unit tests for string functions #2405

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 1 addition & 6 deletions src/packages/core/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,5 @@ export * from './path/umbraco-path.function.js';
export * from './path/url-pattern-to-string.function.js';
export * from './selection-manager/selection.manager.js';
export * from './state-manager/index.js';
export * from './string/from-camel-case.function.js';
export * from './string/generate-umbraco-alias.function.js';
export * from './string/increment-string.function.js';
export * from './string/split-string-to-array.js';
export * from './string/string-or-string-array-contains.function.js';
export * from './string/to-camel-case/to-camel-case.function.js';
export * from './string/index.js';
export type * from './type/index.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect } from '@open-wc/testing';
import { fromCamelCase } from './from-camel-case.function.js';

describe('fromCamelCase', () => {
it('should return the string with spaces between words', () => {
expect(fromCamelCase('thisIsATest')).to.equal('This Is A Test');
});

it('should uppercase the first character', () => {
const result = fromCamelCase('thisIsATest');
expect(result.charAt(0)).to.equal('T');
});
});
1 change: 1 addition & 0 deletions src/packages/core/utils/string/from-camel-case/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './from-camel-case.function.js';
18 changes: 0 additions & 18 deletions src/packages/core/utils/string/generate-umbraco-alias.function.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { toCamelCase } from '../to-camel-case/index.js';

/**
* @description Generate an alias from a string
* @param {string} text The text to generate the alias from
* @returns {string} The alias
*/
export function generateAlias(text: string): string {
return toCamelCase(text);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './generate-umbraco-alias.function.js';
7 changes: 0 additions & 7 deletions src/packages/core/utils/string/increment-string.function.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { incrementString } from './increment-string.function.js';
import { expect } from '@open-wc/testing';

describe('incrementString', () => {
it('increments a string with a number at the end', () => {
const inputString = 'test';
const result = incrementString(inputString);

expect(result).to.equal('test1');
});

it('increments a string with a number at the end by 1', () => {
const inputString = 'test1';
const result = incrementString(inputString);

expect(result).to.equal('test2');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @description Increment string
* @param {string} text The text to increment
* @returns {string} The incremented string
*/
export function incrementString(text: string): string {
return text.replace(/(\d*)$/, (_, t) => (+t + 1).toString().padStart(t.length, '0'));
}
1 change: 1 addition & 0 deletions src/packages/core/utils/string/increment-string/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './increment-string.function.js';
6 changes: 6 additions & 0 deletions src/packages/core/utils/string/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './from-camel-case/index.js';
export * from './generate-umbraco-alias/index.js';
export * from './increment-string/index.js';
export * from './split-string-to-array/index.js';
export * from './string-or-string-array-contains/index.js';
export * from './to-camel-case/index.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './split-string-to-array.function.js';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { splitStringToArray } from './string/split-string-to-array.js';
import { splitStringToArray } from './split-string-to-array.function.js';
import { expect } from '@open-wc/testing';

describe('splitStringToArray', () => {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './string-or-string-array-contains.function.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { stringOrStringArrayContains } from './string-or-string-array-contains.function.js';
import { expect } from '@open-wc/testing';

describe('stringOrStringArrayContains', () => {
it('returns true if a string matches a search string', () => {
const result = stringOrStringArrayContains('hello', 'hello');
expect(result).to.be.true;
});

it('returns false if a string does not contain a search string', () => {
const result = stringOrStringArrayContains('hello', 'world');
expect(result).to.be.false;
});

it('returns false if a search string partly matches the string', () => {
const result = stringOrStringArrayContains('hello', 'ello');
expect(result).to.be.false;
});

it('returns true if an array of strings contains a search string', () => {
const result = stringOrStringArrayContains(['hello', 'world'], 'world');
expect(result).to.be.true;
});

it('returns false if an array of strings does not contain a search string', () => {
const result = stringOrStringArrayContains(['hello', 'world'], 'foo');
expect(result).to.be.false;
});

it('returns false if an empty array is passed', () => {
const result = stringOrStringArrayContains([], 'foo');
expect(result).to.be.false;
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @description Check if a string or array of strings contains a specific string
* @param value The string or array of strings to search in
* @param search The string to search for
* @returns {boolean} Whether the string or array of strings contains the search string
*/
export function stringOrStringArrayContains(value: string | Array<string>, search: string): boolean {
return Array.isArray(value) ? value.indexOf(search) !== -1 : value === search;
}
1 change: 1 addition & 0 deletions src/packages/core/utils/string/to-camel-case/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './to-camel-case.function.js';
Loading