Skip to content

Commit

Permalink
Restructure & Introduce Address Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Apr 29, 2023
1 parent fdfe71e commit e5aa58b
Show file tree
Hide file tree
Showing 22 changed files with 648 additions and 654 deletions.
1,236 changes: 604 additions & 632 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/format/address/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const AddressRegex = /^0x[\dA-Fa-f]{40}$/;

/**
* Formats the address from 0x225f137127d9067788314bc7fcc1f36746a3c3B5 to 0x225f...c3B5
* Returns the address if it doesn't match the address pattern
* @param address The address to format
*/
export const formatAddress = (address: string) => {
if (address.match(AddressRegex) === null) return address;

return `${address.slice(0, 5)}...${address.slice(-4)}`;
};
1 change: 1 addition & 0 deletions src/format/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './address';
export { formatRecord } from './records';
export { formatDiscordRules } from './records/discord/discord';
export { formatLinkedinRules } from './records/linkedin/linkedin';
Expand Down
2 changes: 1 addition & 1 deletion src/format/records/discord/discord.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatter } from '../../formatter';
import { formatter } from '../formatter';

export const formatDiscordRules = formatter([
(record) =>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/format/records/github/github.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatter } from '../../formatter';
import { formatter } from '../formatter';

export const formatGithubRules = formatter([
(record) => record.replace(/^(https?:\/\/)?(www\.)?github\.com\//, ''),
Expand Down
14 changes: 7 additions & 7 deletions src/format/records.ts → src/format/records/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { formatDiscordRules } from './records/discord/discord';
import { formatGithubRules } from './records/github/github';
import { formatLinkedinRules } from './records/linkedin/linkedin';
import { formatRedditRules } from './records/reddit/reddit';
import { formatTelegramRules } from './records/telegram/telegram';
import { formatTwitterRules } from './records/twitter/twitter';
import { formatWebsiteRules } from './records/website/website';
import { formatDiscordRules } from './discord/discord';
import { formatGithubRules } from './github/github';
import { formatLinkedinRules } from './linkedin/linkedin';
import { formatRedditRules } from './reddit/reddit';
import { Rule } from './rule';
import { formatTelegramRules } from './telegram/telegram';
import { formatTwitterRules } from './twitter/twitter';
import { formatWebsiteRules } from './website/website';

type Types =
| 'com.twitter'
Expand Down
2 changes: 1 addition & 1 deletion src/format/records/linkedin/linkedin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatter } from '../../formatter';
import { formatter } from '../formatter';

export const formatLinkedinRules = formatter([
(record) =>
Expand Down
2 changes: 1 addition & 1 deletion src/format/records/reddit/reddit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatter } from '../../formatter';
import { formatter } from '../formatter';

export const formatRedditRules = formatter([
(record) => record.replace(/^(https?:\/\/)?(www\.)?reddit\.com\//, ''),
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/format/records/telegram/telegram.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatter } from '../../formatter';
import { formatter } from '../formatter';

export const formatTelegramRules = formatter([
(record) => record.replace(/^(https?:\/\/)?(www\.)?(mobile\.)?t\.me\//, ''),
Expand Down
2 changes: 1 addition & 1 deletion src/format/records/twitter/twitter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatter } from '../../formatter';
import { formatter } from '../formatter';

export const formatTwitterRules = formatter([
(record) =>
Expand Down
2 changes: 1 addition & 1 deletion src/format/records/website/website.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatter } from '../../formatter';
import { formatter } from '../formatter';

export const formatWebsiteRules = formatter([
(record) => record.match(/^(.+?:\/\/)?(([^.]+\.)+[^./]+)/)?.[2],
Expand Down
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';
9 changes: 9 additions & 0 deletions tests/format/address/address.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { formatAddress } from '../../../src/index';

it('Address Default', () => {
expect(formatAddress('0x225f137127d9067788314bc7fcc1f36746a3c3B5')).toBe('0x225f...c3B5');
});

it('ENS Name', () => {
expect(formatAddress('luc.eth')).toBe('luc.eth');
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatRecord } from '../../src/index';
import { formatRecord } from '../../../src/index';

it('Discord Default', () => {
expect(formatRecord('com.discord', 'Lucemans#2066')).toBe('Lucemans#2066');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatRecord } from '../../src/index';
import { formatRecord } from '../../../src/index';

it('Github Default', () => {
expect(formatRecord('com.github', 'lucemans')).toBe('lucemans');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatRecord } from '../../src/index';
import { formatRecord } from '../../../src/index';

it('Linkedin Default', () => {
expect(formatRecord('com.linkedin', 'in/lucemans')).toBe('lucemans');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatRecord } from '../../src/index';
import { formatRecord } from '../../../src/index';

it('Reddit Default', () => {
expect(formatRecord('com.reddit', 'robi0t')).toBe('robi0t');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatRecord } from '../../src/index';
import { formatRecord } from '../../../src/index';

it('Telegram Default', () => {
expect(formatRecord('org.telegram', 'lucemansnl')).toBe('lucemansnl');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatRecord } from '../../src/index';
import { formatRecord } from '../../../src/index';

it('Twitter Default', () => {
expect(formatRecord('com.twitter', 'lucemansnl')).toBe('@lucemansnl');
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"outDir": "lib",
"esModuleInterop": true
},
"include": ["src", "tests/records/discord.spec.js"],
"include": ["src", "tests/format/records/discord.spec.js"],
"exclude": ["node_modules", "tests", "lib"]
}

0 comments on commit e5aa58b

Please sign in to comment.