Skip to content

Commit

Permalink
Merge pull request #1 from v3xlabs/feat/fixes
Browse files Browse the repository at this point in the history
Make reddit work, add tests
  • Loading branch information
robiot authored Feb 27, 2023
2 parents 5abe879 + 5cb6d61 commit a5c91ce
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"ecmaVersion": 2021
},
"extends": ["plugin:v3xlabs/recommended"],
"ignorePatterns": ["!**/*"],
"ignorePatterns": ["!**/*", "tests"],
"plugins": ["v3xlabs"],
"env": {
"browser": true
Expand Down
13 changes: 11 additions & 2 deletions src/format/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ import { formatTwitterRules } from './records/twitter/twitter';
import { formatWebsiteRules } from './records/website/website';
import { Rule } from './rule';

const socialFormatters: Record<string, Rule> = {
type Types =
| 'com.twitter'
| 'com.reddit'
| 'com.github'
| 'com.discord'
| 'com.linkedin'
| 'com.telegram'
| 'website';

const socialFormatters: Record<Types, Rule> = {
'com.twitter': formatTwitterRules,
'com.reddit': formatRedditRules,
'com.github': formatGithubRules,
Expand All @@ -17,7 +26,7 @@ const socialFormatters: Record<string, Rule> = {
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);
Expand Down
3 changes: 2 additions & 1 deletion src/format/records/reddit/reddit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './format/records';
export * from './format/records';
21 changes: 21 additions & 0 deletions tests/records/reddit.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
13 changes: 13 additions & 0 deletions tests/records/telegram.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});

0 comments on commit a5c91ce

Please sign in to comment.