mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-11-22 11:02:11 +03:00
fix: move all stirng utils to shared ui utils
This commit is contained in:
parent
02d5a88612
commit
e1401d11af
@ -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');
|
||||
});
|
||||
});
|
@ -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, '-');
|
||||
}
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
@ -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, '-');
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user