-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstringUtils.test.ts
More file actions
72 lines (61 loc) · 2.49 KB
/
stringUtils.test.ts
File metadata and controls
72 lines (61 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { describe, expect, it } from 'vitest';
import { stringUtils } from '../../src';
const { base64UrlSafetoUUID, fromBase64UrlSafe, generateRandomStringUrlSafe, toBase64UrlSafe } = stringUtils;
describe('stringUtils', () => {
describe('toBase64UrlSafe', () => {
it('converts standard Base64 to URL-safe Base64', () => {
const base64 = 'KHh+VVwlYjs/J3E=';
const urlSafe = toBase64UrlSafe(base64);
expect(urlSafe).toBe('KHh-VVwlYjs_J3E');
});
it('removes trailing "=" characters', () => {
const base64 = 'KHh+VVwlYjs/J3FiYw==';
const urlSafe = toBase64UrlSafe(base64);
expect(urlSafe).toBe('KHh-VVwlYjs_J3FiYw');
});
it('does not modify already URL-safe Base64 strings', () => {
const base64 = 'KHh-VVwlYjs_J3E';
const urlSafe = toBase64UrlSafe(base64);
expect(urlSafe).toBe(base64);
});
});
describe('fromBase64UrlSafe', () => {
it('converts URL-safe Base64 back to standard Base64', () => {
const urlSafe = 'KHh-VVwlYjs_J3E';
const base64 = fromBase64UrlSafe(urlSafe);
expect(base64).toBe('KHh+VVwlYjs/J3E=');
});
it('does not modify already standard Base64 strings', () => {
const urlSafe = 'KHh+VVwlYjs/J3FiYw==';
const base64 = fromBase64UrlSafe(urlSafe);
expect(base64).toBe(urlSafe);
});
});
describe('generateRandomStringUrlSafe', () => {
const getRandomNumber = (min: number, max: number) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
};
it('generates a string of the specified length', () => {
const length = getRandomNumber(1, 1000);
const randomString = generateRandomStringUrlSafe(length);
expect(randomString).toHaveLength(length);
});
it('generates a URL-safe string', () => {
const randomString = generateRandomStringUrlSafe(1000);
expect(randomString).not.toMatch(/[+/=]/);
});
it('throws an error if size is not positive', () => {
expect(() => generateRandomStringUrlSafe(0)).toThrow('Size must be a positive integer');
expect(() => generateRandomStringUrlSafe(-5)).toThrow('Size must be a positive integer');
});
});
describe('base64UrlSafetoUUID', () => {
it('converts a Base64 URL-safe string to a UUID v4 format', () => {
const base64UrlSafe = '8yqR2seZThOqF4xNngMjyQ';
const uuid = base64UrlSafetoUUID(base64UrlSafe);
expect(uuid).toBe('f32a91da-c799-4e13-aa17-8c4d9e0323c9');
});
});
});