mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-12-25 17:10:50 +00:00
chore: create getPrimaryAlias utility function
Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
d1b7c2a2db
commit
8c214820e1
2 changed files with 44 additions and 10 deletions
|
@ -5,19 +5,36 @@
|
|||
*/
|
||||
import { randomBytes } from 'crypto';
|
||||
|
||||
import { generatePublicId } from './utils';
|
||||
import { User } from '../users/user.entity';
|
||||
import { Alias } from './alias.entity';
|
||||
import { Note } from './note.entity';
|
||||
import { generatePublicId, getPrimaryAlias } from './utils';
|
||||
|
||||
jest.mock('crypto');
|
||||
const random128bitBuffer = Buffer.from([
|
||||
0xe1, 0x75, 0x86, 0xb7, 0xc3, 0xfb, 0x03, 0xa9, 0x26, 0x9f, 0xc9, 0xd6, 0x8c,
|
||||
0x2d, 0x7b, 0x7b,
|
||||
]);
|
||||
const mockRandomBytes = randomBytes as jest.MockedFunction<typeof randomBytes>;
|
||||
mockRandomBytes.mockImplementation((_) => random128bitBuffer);
|
||||
|
||||
it('generatePublicId', () => {
|
||||
const random128bitBuffer = Buffer.from([
|
||||
0xe1, 0x75, 0x86, 0xb7, 0xc3, 0xfb, 0x03, 0xa9, 0x26, 0x9f, 0xc9, 0xd6,
|
||||
0x8c, 0x2d, 0x7b, 0x7b,
|
||||
]);
|
||||
const mockRandomBytes = randomBytes as jest.MockedFunction<
|
||||
typeof randomBytes
|
||||
>;
|
||||
mockRandomBytes.mockImplementationOnce((_) => random128bitBuffer);
|
||||
|
||||
expect(generatePublicId()).toEqual('w5trddy3zc1tj9mzs7b8rbbvfc');
|
||||
});
|
||||
|
||||
describe('getPrimaryAlias', () => {
|
||||
const alias = 'alias';
|
||||
let note: Note;
|
||||
beforeEach(() => {
|
||||
const user = User.create('hardcoded', 'Testy') as User;
|
||||
note = Note.create(user, alias);
|
||||
});
|
||||
it('finds correct primary alias', () => {
|
||||
note.aliases.push(Alias.create('annother', false));
|
||||
expect(getPrimaryAlias(note)).toEqual(alias);
|
||||
});
|
||||
it('returns undefined if there is no alias', () => {
|
||||
note.aliases[0].primary = false;
|
||||
expect(getPrimaryAlias(note)).toEqual(undefined);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -6,6 +6,9 @@
|
|||
import base32Encode from 'base32-encode';
|
||||
import { randomBytes } from 'crypto';
|
||||
|
||||
import { Alias } from './alias.entity';
|
||||
import { Note } from './note.entity';
|
||||
|
||||
/**
|
||||
* Generate publicId for a note.
|
||||
* This is a randomly generated 128-bit value encoded with base32-encode using the crockford variant and converted to lowercase.
|
||||
|
@ -14,3 +17,17 @@ export function generatePublicId(): string {
|
|||
const randomId = randomBytes(16);
|
||||
return base32Encode(randomId, 'Crockford').toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the primary alias from a aliases of a note
|
||||
* @param {Note} note - the note from which the primary alias should be extracted
|
||||
*/
|
||||
export function getPrimaryAlias(note: Note): string | undefined {
|
||||
const listWithPrimaryAlias = note.aliases.filter(
|
||||
(alias: Alias) => alias.primary,
|
||||
);
|
||||
if (listWithPrimaryAlias.length !== 1) {
|
||||
return undefined;
|
||||
}
|
||||
return listWithPrimaryAlias[0].name;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue