add camel case util funktion

This commit is contained in:
Mads Rasmussen
2024-02-23 21:45:42 +01:00
parent fafd626ec4
commit 7db12b141c
3 changed files with 80 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ 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 './type/diff.type.js';
export * from './to-camel-case/to-camel-case.js';
declare global {
interface Window {

View File

@@ -0,0 +1,72 @@
import { expect } from '@open-wc/testing';
import { toCamelCase } from './to-camel-case.js';
describe('to-camel-case', () => {
/* All tests have been run against the the lodash camelCase function,
to ensure that the toCamelCase function behaves the same way as the lodash camelCase function. */
it('converts string to camelCase', () => {
expect(toCamelCase('to-camel-case')).to.equal('toCamelCase');
expect(toCamelCase('b2b_registration_request')).to.equal('b2BRegistrationRequest');
expect(toCamelCase('b2b_registration_b2b_request')).to.equal('b2BRegistrationB2BRequest');
expect(toCamelCase('foo')).to.equal('foo');
expect(toCamelCase('IDs')).to.equal('iDs');
expect(toCamelCase('FooIDs')).to.equal('fooIDs');
expect(toCamelCase('foo-bar')).to.equal('fooBar');
expect(toCamelCase('foo-bar-baz')).to.equal('fooBarBaz');
expect(toCamelCase('foo--bar')).to.equal('fooBar');
expect(toCamelCase('--foo-bar')).to.equal('fooBar');
expect(toCamelCase('FOO-BAR')).to.equal('fooBar');
expect(toCamelCase('-foo-bar-')).to.equal('fooBar');
expect(toCamelCase('--foo--bar--')).to.equal('fooBar');
expect(toCamelCase('foo.bar')).to.equal('fooBar');
expect(toCamelCase('foo..bar')).to.equal('fooBar');
expect(toCamelCase('..foo..bar..')).to.equal('fooBar');
expect(toCamelCase('foo_bar')).to.equal('fooBar');
expect(toCamelCase('__foo__bar__')).to.equal('fooBar');
expect(toCamelCase('foo bar')).to.equal('fooBar');
expect(toCamelCase(' foo bar ')).to.equal('fooBar');
expect(toCamelCase('fooBar')).to.equal('fooBar');
expect(toCamelCase('fooBar-baz')).to.equal('fooBarBaz');
expect(toCamelCase('fooBarBaz-bazzy')).to.equal('fooBarBazBazzy');
expect(toCamelCase('FBBazzy')).to.equal('fbBazzy');
expect(toCamelCase('F')).to.equal('f');
expect(toCamelCase('Foo')).to.equal('foo');
expect(toCamelCase('FOO')).to.equal('foo');
expect(toCamelCase('FooBar')).to.equal('fooBar');
expect(toCamelCase('Foo')).to.equal('foo');
expect(toCamelCase('FOO')).to.equal('foo');
expect(toCamelCase('XMLHttpRequest')).to.equal('xmlHttpRequest');
expect(toCamelCase('AjaxXMLHttpRequest')).to.equal('ajaxXmlHttpRequest');
expect(toCamelCase('Ajax-XMLHttpRequest')).to.equal('ajaxXmlHttpRequest');
expect(toCamelCase('mGridCol6')).to.equal('mGridCol6');
expect(toCamelCase('Hello1World')).to.equal('hello1World');
expect(toCamelCase('Hello11World')).to.equal('hello11World');
expect(toCamelCase('hello1world')).to.equal('hello1World');
expect(toCamelCase('Hello1World11foo')).to.equal('hello1World11Foo');
expect(toCamelCase('Hello1')).to.equal('hello1');
expect(toCamelCase('hello1')).to.equal('hello1');
expect(toCamelCase('1Hello')).to.equal('1Hello');
expect(toCamelCase('1hello')).to.equal('1Hello');
expect(toCamelCase('1hello')).to.equal('1Hello');
expect(toCamelCase('h2w')).to.equal('h2W');
});
it('ignores special characters', () => {
expect(toCamelCase('-')).to.equal('');
expect(toCamelCase(' - ')).to.equal('');
expect(toCamelCase('--')).to.equal('');
expect(toCamelCase('')).to.equal('');
expect(toCamelCase(' ')).to.equal('');
expect(toCamelCase('_')).to.equal('');
expect(toCamelCase('.')).to.equal('');
expect(toCamelCase('..')).to.equal('');
expect(toCamelCase(' ')).to.equal('');
expect(toCamelCase('__')).to.equal('');
expect(toCamelCase('--__--_--_')).to.equal('');
expect(toCamelCase('A::a')).to.equal('aA');
expect(toCamelCase('foo bar?')).to.equal('fooBar');
expect(toCamelCase('foo bar!')).to.equal('fooBar');
expect(toCamelCase('foo bar$')).to.equal('fooBar');
expect(toCamelCase('foo bar#')).to.equal('fooBar');
});
});

View File

@@ -0,0 +1,7 @@
export const toCamelCase = (str: string) => {
const s = str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
?.map((x: string) => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase())
.join('');
return (s && s.slice(0, 1).toLowerCase() + s.slice(1)) || '';
};