Skip to main content
added 10 characters in body
Source Link
nhgrif
  • 25.4k
  • 3
  • 64
  • 129

I need to convert a string using these rules:

  • Downcase the string
  • Replace spaces, a blacklist of "invalid" chars, and non-ascii letters (like é) with -
  • Replace repeated hyphens (ie --) with one hyphen (-)

For example, Is my résumé good enough? should convert to is-my-r-sum-good-enough-.

This Swift function converts the string. I'm looking for help making it shorter. Also, if there's a way to avoid import Foundation (by replacing containsString and stringByReplacingOccurrencesOfString), I'd like to do that. I know I could use an NSRegularExpression but I don't want to use Foundation classes.

Here's what I have so far:

extension Character {
    static let invalidSet = Set(" -&+$,/:;=?@\"#{}|^~[]`\\*()%.!'".characters)

    var invalid: Bool {
        return Character.invalidSet.contains(self)
    }
    
    var isAscii: Bool {
        guard let number = String(self).utf16.first else { return false }
        return (65..<127) ~= number
    }
}

func anchor(header: String) -> String {
    var output = String(header.lowercaseString.characters.map {
        $0.invalid || !$0.isAscii ? "-" : $0
    })
    
    while output.containsString("--") {
        output = output.stringByReplacingOccurrencesOfString("--", withString: "-")
    }
    
    return output
}

Usage:

let input = "Is my résumé good enough?"
let expectedResult = "is-my-r-sum-good-enough-"

print(anchor(input)) //prints is-my-r-sum-good-enough-
print(anchor(input) == expectedResult ? "😀" : "😞") //prints 😀
let input = "Is my résumé good enough?"
let expectedResult = "is-my-r-sum-good-enough-"

print(anchor(input)) //prints is-my-r-sum-good-enough-
print(anchor(input) == expectedResult ? "😀" : "😞") //prints 😀

I need to convert a string using these rules:

  • Downcase the string
  • Replace spaces, a blacklist of "invalid" chars, and non-ascii letters (like é) with -
  • Replace repeated hyphens (ie --) with one hyphen (-)

For example, Is my résumé good enough? should convert to is-my-r-sum-good-enough-.

This Swift function converts the string. I'm looking for help making it shorter. Also, if there's a way to avoid import Foundation (by replacing containsString and stringByReplacingOccurrencesOfString), I'd like to do that. I know I could use an NSRegularExpression but I don't want to use Foundation classes.

Here's what I have so far:

extension Character {
    static let invalidSet = Set(" -&+$,/:;=?@\"#{}|^~[]`\\*()%.!'".characters)

    var invalid: Bool {
        return Character.invalidSet.contains(self)
    }
    
    var isAscii: Bool {
        guard let number = String(self).utf16.first else { return false }
        return (65..<127) ~= number
    }
}

func anchor(header: String) -> String {
    var output = String(header.lowercaseString.characters.map {
        $0.invalid || !$0.isAscii ? "-" : $0
    })
    
    while output.containsString("--") {
        output = output.stringByReplacingOccurrencesOfString("--", withString: "-")
    }
    
    return output
}

Usage:

let input = "Is my résumé good enough?"
let expectedResult = "is-my-r-sum-good-enough-"

print(anchor(input)) //prints is-my-r-sum-good-enough-
print(anchor(input) == expectedResult ? "😀" : "😞") //prints 😀

I need to convert a string using these rules:

  • Downcase the string
  • Replace spaces, a blacklist of "invalid" chars, and non-ascii letters (like é) with -
  • Replace repeated hyphens (ie --) with one hyphen (-)

For example, Is my résumé good enough? should convert to is-my-r-sum-good-enough-.

This Swift function converts the string. I'm looking for help making it shorter. Also, if there's a way to avoid import Foundation (by replacing containsString and stringByReplacingOccurrencesOfString), I'd like to do that. I know I could use an NSRegularExpression but I don't want to use Foundation classes.

Here's what I have so far:

extension Character {
    static let invalidSet = Set(" -&+$,/:;=?@\"#{}|^~[]`\\*()%.!'".characters)

    var invalid: Bool {
        return Character.invalidSet.contains(self)
    }
    
    var isAscii: Bool {
        guard let number = String(self).utf16.first else { return false }
        return (65..<127) ~= number
    }
}

func anchor(header: String) -> String {
    var output = String(header.lowercaseString.characters.map {
        $0.invalid || !$0.isAscii ? "-" : $0
    })
    
    while output.containsString("--") {
        output = output.stringByReplacingOccurrencesOfString("--", withString: "-")
    }
    
    return output
}

Usage:

let input = "Is my résumé good enough?"
let expectedResult = "is-my-r-sum-good-enough-"

print(anchor(input)) //prints is-my-r-sum-good-enough-
print(anchor(input) == expectedResult ? "😀" : "😞") //prints 😀
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Source Link

Anchor Text String Transformation in Swift

I need to convert a string using these rules:

  • Downcase the string
  • Replace spaces, a blacklist of "invalid" chars, and non-ascii letters (like é) with -
  • Replace repeated hyphens (ie --) with one hyphen (-)

For example, Is my résumé good enough? should convert to is-my-r-sum-good-enough-.

This Swift function converts the string. I'm looking for help making it shorter. Also, if there's a way to avoid import Foundation (by replacing containsString and stringByReplacingOccurrencesOfString), I'd like to do that. I know I could use an NSRegularExpression but I don't want to use Foundation classes.

Here's what I have so far:

extension Character {
    static let invalidSet = Set(" -&+$,/:;=?@\"#{}|^~[]`\\*()%.!'".characters)

    var invalid: Bool {
        return Character.invalidSet.contains(self)
    }
    
    var isAscii: Bool {
        guard let number = String(self).utf16.first else { return false }
        return (65..<127) ~= number
    }
}

func anchor(header: String) -> String {
    var output = String(header.lowercaseString.characters.map {
        $0.invalid || !$0.isAscii ? "-" : $0
    })
    
    while output.containsString("--") {
        output = output.stringByReplacingOccurrencesOfString("--", withString: "-")
    }
    
    return output
}

Usage:

let input = "Is my résumé good enough?"
let expectedResult = "is-my-r-sum-good-enough-"

print(anchor(input)) //prints is-my-r-sum-good-enough-
print(anchor(input) == expectedResult ? "😀" : "😞") //prints 😀