Skip to content

Commit

Permalink
refactor: added ens recognition and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechsimetka committed Apr 26, 2022
1 parent 8909e58 commit d869749
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/pages/files/Download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { History } from '../../components/History'
import { Context, defaultUploadOrigin } from '../../providers/File'
import { Context as SettingsContext } from '../../providers/Settings'
import { ROUTES } from '../../routes'
import { recognizeSwarmHash } from '../../utils'
import { recognizeEnsOrSwarmHash } from '../../utils'
import { determineHistoryName, HISTORY_KEYS, putHistory } from '../../utils/local-storage'
import { FileNavigation } from './FileNavigation'

Expand Down Expand Up @@ -83,7 +83,7 @@ export function Download(): ReactElement {
confirmLabelDisabled={Boolean(referenceError) || loading}
placeholder="e.g. 31fb0362b1a42536134c86bc58b97ac0244e5c6630beec3e27c2d1cecb38c605"
expandedOnly
mapperFn={value => recognizeSwarmHash(value)}
mapperFn={value => recognizeEnsOrSwarmHash(value)}
loading={loading}
/>
<History title="Download History" localStorageKey={HISTORY_KEYS.DOWNLOAD_HISTORY} />
Expand Down
52 changes: 32 additions & 20 deletions src/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extractSwarmHash, extractSwarmCid, recognizeSwarmHash, recognizeEns } from './index'
import { extractSwarmHash, extractSwarmCid, extractEns, recognizeEnsOrSwarmHash } from './index'

interface TestObject {
input: string
Expand Down Expand Up @@ -122,21 +122,6 @@ describe('extractSwarmCid', () => {
})
})

describe('recognizeSwarmHash', () => {
test('should correctly extract hash', () => {
;[...correctHashes, ...correctCids].forEach(({ input, expectedOutput }) => {
const hash = recognizeSwarmHash(input)
expect(hash).toBe(expectedOutput)
})
})
test('should not extract hash from incorrect inputs but instead return them', () => {
;[...wrongHashes, ...wrongCids].forEach(url => {
const hash = recognizeSwarmHash(url)
expect(hash).toBe(url)
})
})
})

const correctEns: TestObject[] = [
{
input: 'test.eth',
Expand All @@ -154,20 +139,47 @@ const correctEns: TestObject[] = [
input: 'https://alice.test.eth?whatever',
expectedOutput: 'alice.test.eth',
},
{
input: 'swarm.example.eth/?id=1&page=2',
expectedOutput: 'swarm.example.eth',
},
{
input: 'http://swarm.example.eth#up',
expectedOutput: 'swarm.example.eth',
},
{
input: 'http://site.eth:8008',
expectedOutput: 'site.eth',
},
]

const wrongEns: string[] = ['http://test.ethereum/whatever']

describe('recognizeEns', () => {
describe('extractEns', () => {
test('should correctly extract ens domain', () => {
correctEns.forEach(({ input, expectedOutput }) => {
const hash = recognizeEns(input)
const hash = extractEns(input)
expect(hash).toBe(expectedOutput)
})
})
test('should not extract hash from incorrect inputs but instead return them', () => {
test('should not extract ens from incorrect inputs', () => {
wrongEns.forEach(url => {
const hash = recognizeSwarmHash(url)
const hash = extractEns(url)
expect(hash).toBe(undefined)
})
})
})

describe('recognizeEnsOrSwarmHash', () => {
test('should correctly extract hash or ens', () => {
;[...correctHashes, ...correctCids, ...correctEns].forEach(({ input, expectedOutput }) => {
const hash = recognizeEnsOrSwarmHash(input)
expect(hash).toBe(expectedOutput)
})
})
test('should not extract hash or ens from incorrect inputs but instead return them', () => {
;[...wrongHashes, ...wrongCids, ...wrongEns].forEach(url => {
const hash = recognizeEnsOrSwarmHash(url)
expect(hash).toBe(url)
})
})
Expand Down
14 changes: 8 additions & 6 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,18 @@ export function extractSwarmCid(s: string): string | undefined {
}
}

export function recognizeSwarmHash(value: string): string {
return extractSwarmHash(value) || extractSwarmCid(value) || value
}

// Matches any number of subdomains ending with .eth
// e.g. this.is.just-a-test.eth
const regexpEns = /((?:(?!-)[a-z0-9-]{1,63}(?<!-)\.)+eth)(?:$|[/?:#].*)/i

export function recognizeEns(value: string): string {
export function extractEns(value: string): string | undefined {
const matches = value.match(regexpEns)

return (matches && matches[1]) || value
return (matches && matches[1]) || undefined
}

export function recognizeEnsOrSwarmHash(value: string): string {
return extractEns(value) || extractSwarmHash(value) || extractSwarmCid(value) || value
}

export function uuidV4(): string {
Expand Down

0 comments on commit d869749

Please sign in to comment.