fix: move all stirng utils to shared ui utils

This commit is contained in:
ndom91 2024-10-22 11:00:09 +02:00 committed by Nico Domino
parent 02d5a88612
commit e1401d11af
4 changed files with 66 additions and 67 deletions

View File

@ -1,30 +0,0 @@
import { slugify } from '$lib/utils/string';
import { describe, expect, test } from 'vitest';
describe.concurrent('branch slugify with valid characters', () => {
test('forward slashes are fine', () => {
expect(slugify('my/branch')).toEqual('my/branch');
});
test('capitalization is fine', () => {
expect(slugify('MY/branch')).toEqual('MY/branch');
});
test('numbers are fine', () => {
expect(slugify('my/branch1')).toEqual('my/branch1');
});
});
describe.concurrent('branch slugify with replaced characters', () => {
test('whitespaces are truncated', () => {
expect(slugify(' my/branch ')).toEqual('my/branch');
});
test('whitespace in the middle becomes dash', () => {
expect(slugify('my branch')).toEqual('my-branch');
});
test('most special characters are nuked', () => {
expect(slugify('a!b@c$d;e%f^g&h*i(j)k+l=m~n`')).toEqual('abcdefghijklmn');
});
});

View File

@ -1,35 +0,0 @@
export function hashCode(s: string) {
let hash = 0;
let chr;
let i;
if (s.length === 0) return hash.toString();
for (i = 0; i < s.length; i++) {
chr = s.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to 32bit integer
}
return hash.toString();
}
export function isChar(char: string) {
return char.length === 1;
}
export function isStr(s: unknown): s is string {
return typeof s === 'string';
}
export function isWhiteSpaceString(s: string) {
return s.trim() === '';
}
export function slugify(input: string) {
return String(input)
.normalize('NFKD')
.replace(/[\u0300-\u036f]/g, '')
.trim()
.replace(/[^A-Za-z0-9/ -]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-');
}

View File

@ -1,7 +1,7 @@
import { camelCaseToTitleCase } from './string';
import { slugify, camelCaseToTitleCase } from '$lib/utils/string';
import { describe, expect, test } from 'vitest';
describe.concurrent('Commit types handled correctly', () => {
describe.concurrent('camelCaseToTitleCase with valid inputs', () => {
test('localAndRemote', () => {
expect(camelCaseToTitleCase('localAndRemote')).toEqual('Local And Remote');
});
@ -22,3 +22,31 @@ describe.concurrent('Commit types handled correctly', () => {
expect(camelCaseToTitleCase('LocalAndShadow')).toEqual('Local And Shadow');
});
});
describe.concurrent('branch slugify with valid characters', () => {
test('forward slashes are fine', () => {
expect(slugify('my/branch')).toEqual('my/branch');
});
test('capitalization is fine', () => {
expect(slugify('MY/branch')).toEqual('MY/branch');
});
test('numbers are fine', () => {
expect(slugify('my/branch1')).toEqual('my/branch1');
});
});
describe.concurrent('branch slugify with replaced characters', () => {
test('whitespaces are truncated', () => {
expect(slugify(' my/branch ')).toEqual('my/branch');
});
test('whitespace in the middle becomes dash', () => {
expect(slugify('my branch')).toEqual('my-branch');
});
test('most special characters are nuked', () => {
expect(slugify('a!b@c$d;e%f^g&h*i(j)k+l=m~n`')).toEqual('abcdefghijklmn');
});
});

View File

@ -12,3 +12,39 @@ export function camelCaseToTitleCase(input: string) {
}
return result;
}
export function hashCode(s: string) {
let hash = 0;
let chr;
let i;
if (s.length === 0) return hash.toString();
for (i = 0; i < s.length; i++) {
chr = s.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to 32bit integer
}
return hash.toString();
}
export function isChar(char: string) {
return char.length === 1;
}
export function isStr(s: unknown): s is string {
return typeof s === 'string';
}
export function isWhiteSpaceString(s: string) {
return s.trim() === '';
}
export function slugify(input: string) {
return String(input)
.normalize('NFKD')
.replace(/[\u0300-\u036f]/g, '')
.trim()
.replace(/[^A-Za-z0-9/ -]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-');
}