enhance(misskey-js): acct.parse()がacct:からはじまるものもパースするように
* acct.parse()がacct:から始まっていてもパースできるように * acct.tsのテストを追加
This commit is contained in:
parent
d127d82c5b
commit
1e88a32bdf
|
@ -6,6 +6,7 @@ export type Acct = {
|
|||
export function parse(_acct: string): Acct {
|
||||
let acct = _acct;
|
||||
if (acct.startsWith('@')) acct = acct.substring(1);
|
||||
else if (acct.startsWith('acct:')) acct = acct.substring(5);
|
||||
const split = acct.split('@', 2);
|
||||
return { username: split[0], host: split[1] || null };
|
||||
}
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
import { describe, it, expect } from 'vitest';
|
||||
import * as acct from '../src/acct.js';
|
||||
|
||||
describe('acct.parse', () => {
|
||||
it('parses plain username', () => {
|
||||
const res = acct.parse('alice');
|
||||
expect(res).toEqual({ username: 'alice', host: null });
|
||||
});
|
||||
|
||||
it('parses at-mark style without host', () => {
|
||||
const res = acct.parse('@alice');
|
||||
expect(res).toEqual({ username: 'alice', host: null });
|
||||
});
|
||||
|
||||
it('parses at-mark style with host', () => {
|
||||
const res = acct.parse('@alice@example.com');
|
||||
expect(res).toEqual({ username: 'alice', host: 'example.com' });
|
||||
});
|
||||
|
||||
it('parses acct: style', () => {
|
||||
const res = acct.parse('acct:alice@example.com');
|
||||
expect(res).toEqual({ username: 'alice', host: 'example.com' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('acct.toString', () => {
|
||||
it('returns username when host is null', () => {
|
||||
expect(acct.toString({ username: 'alice', host: null })).toBe('alice');
|
||||
});
|
||||
|
||||
it('returns username@host when host exists', () => {
|
||||
expect(acct.toString({ username: 'alice', host: 'example.com' })).toBe('alice@example.com');
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue