From 5cb6d617a9b14dcb1fee941be9f72eef04072368 Mon Sep 17 00:00:00 2001 From: Elliot Date: Mon, 27 Feb 2023 18:59:56 +0200 Subject: [PATCH] Make reddit work, add tests --- .eslintrc.json | 2 +- src/format/records.ts | 13 +++++++++++-- src/format/records/reddit/reddit.ts | 3 ++- src/index.ts | 2 +- tests/records/reddit.spec.ts | 21 +++++++++++++++++++++ tests/records/telegram.spec.ts | 13 +++++++++++++ 6 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 tests/records/reddit.spec.ts create mode 100644 tests/records/telegram.spec.ts diff --git a/.eslintrc.json b/.eslintrc.json index 0039eb8..77952a4 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,7 +4,7 @@ "ecmaVersion": 2021 }, "extends": ["plugin:v3xlabs/recommended"], - "ignorePatterns": ["!**/*"], + "ignorePatterns": ["!**/*", "tests"], "plugins": ["v3xlabs"], "env": { "browser": true diff --git a/src/format/records.ts b/src/format/records.ts index 4f7eede..9bce777 100644 --- a/src/format/records.ts +++ b/src/format/records.ts @@ -7,7 +7,16 @@ import { formatTwitterRules } from './records/twitter/twitter'; import { formatWebsiteRules } from './records/website/website'; import { Rule } from './rule'; -const socialFormatters: Record = { +type Types = + | 'com.twitter' + | 'com.reddit' + | 'com.github' + | 'com.discord' + | 'com.linkedin' + | 'com.telegram' + | 'website'; + +const socialFormatters: Record = { 'com.twitter': formatTwitterRules, 'com.reddit': formatRedditRules, 'com.github': formatGithubRules, @@ -17,7 +26,7 @@ const socialFormatters: Record = { website: formatWebsiteRules, }; -export const formatRecord = (record: string, value: string | undefined) => { +export const formatRecord = (record: Types, value: string | undefined) => { if (!value) return; return socialFormatters[record]?.(value); diff --git a/src/format/records/reddit/reddit.ts b/src/format/records/reddit/reddit.ts index c0358ff..c7eeb99 100644 --- a/src/format/records/reddit/reddit.ts +++ b/src/format/records/reddit/reddit.ts @@ -2,7 +2,8 @@ import { formatter } from '../../formatter'; export const formatRedditRules = formatter([ (record) => record.replace(/^(https?:\/\/)?(www\.)?reddit\.com\//, ''), - (record) => record.replace(/^(user\/|user|u|subreddit\/|subreddit|r)/, ''), + (record) => record.replace(/^(user\/)/, 'u/'), + (record) => record.replace(/^(subreddit\/)/, 'r/'), (record) => record.replace(/\/+$/, ''), (record) => (/^.+$/.test(record) ? record : undefined), ]); diff --git a/src/index.ts b/src/index.ts index 91c406d..2acec1b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1 @@ -export * from './format/records'; \ No newline at end of file +export * from './format/records'; diff --git a/tests/records/reddit.spec.ts b/tests/records/reddit.spec.ts new file mode 100644 index 0000000..70f8174 --- /dev/null +++ b/tests/records/reddit.spec.ts @@ -0,0 +1,21 @@ +import { formatRecord } from '../../src/index'; + +it('Reddit Default', () => { + expect(formatRecord('com.reddit', 'robi0t')).toBe('robi0t'); +}); + +it('Reddit with R', () => { + expect(formatRecord('com.reddit', 'r/ethereum')).toBe('r/ethereum'); +}); + +it('Reddit with subreddit', () => { + expect(formatRecord('com.reddit', 'subreddit/ethereum')).toBe('r/ethereum'); +}); + +it('Reddit with U', () => { + expect(formatRecord('com.reddit', 'u/robi0t')).toBe('u/robi0t'); +}); + +it('Reddit with user', () => { + expect(formatRecord('com.reddit', 'user/robi0t')).toBe('u/robi0t'); +}); diff --git a/tests/records/telegram.spec.ts b/tests/records/telegram.spec.ts new file mode 100644 index 0000000..c5eb015 --- /dev/null +++ b/tests/records/telegram.spec.ts @@ -0,0 +1,13 @@ +import { formatRecord } from '../../src/index'; + +it('Telegram Default', () => { + expect(formatRecord('com.telegram', 'lucemansnl')).toBe('lucemansnl'); +}); + +it('Telegram No Protocol', () => { + expect(formatRecord('com.telegram', 't.me/lucemansnl')).toBe('lucemansnl'); +}); + +it('Telegram Url', () => { + expect(formatRecord('com.telegram', 'https://t.me/lucemansnl')).toBe('lucemansnl'); +});