chore: create getPrimaryAlias utility function

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-08-07 21:51:47 +02:00 committed by David Mehren
parent d1b7c2a2db
commit 8c214820e1
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 44 additions and 10 deletions

View file

@ -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;
}