Skip to content

Commit

Permalink
Remove for loop in favor of single pass using Set
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Sep 4, 2024
1 parent c7ad145 commit a969ff2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { DomainFields, TopLevelDomain } from '@xyo-network/xns-record-paylo
import { DomainSchema } from '@xyo-network/xns-record-payload-plugins'

import { XnsNamePublicValidators } from '../validation/index.ts'
import { removeDisallowedCharacters } from './lib/index.ts'
import type { ValidSourceTypes } from './types/index.ts'

export class XnsNameHelper {
Expand Down Expand Up @@ -90,11 +91,6 @@ export class XnsNameHelper {
formattedXnsName = formattedXnsName.replaceAll(/^-+|-+$/g, '')

// Filter out disallowed characters.
// NOTE: not necessary because of the regex/replacement above, but leaving for when certain special characters become allowed
for (const reservedCharacter in DisallowedModuleIdentifierCharacters) {
formattedXnsName = formattedXnsName.replaceAll(reservedCharacter, '')
}

return formattedXnsName
return removeDisallowedCharacters(formattedXnsName)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './removeDisallowedCharacters.ts'
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DisallowedModuleIdentifierCharacters } from '@xyo-network/module-model'

const DISALLOWED_CHARACTERS = new Set(Object.keys(DisallowedModuleIdentifierCharacters))

/**
* Iterates over a string removing disallowed characters
* @param xnsName The XNS name to remove disallowed characters from
* @returns The XNS name with disallowed characters removed
*/
export const removeDisallowedCharacters = (xnsName: string): string => {
let result = ''
for (const char of xnsName) {
if (!DISALLOWED_CHARACTERS.has(char)) {
result += char
}
}
return result
}

0 comments on commit a969ff2

Please sign in to comment.