Skip to content

Commit

Permalink
refactore: Refactor NanoID.Alphabet to use OptionSet
Browse files Browse the repository at this point in the history
  • Loading branch information
rakuyoMo committed Aug 12, 2024
1 parent 835dd73 commit 95ebe4f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
62 changes: 41 additions & 21 deletions Sources/NanoID/NanoID+Alphabet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,48 @@

import Foundation

// MARK: - NanoID.Alphabet

extension NanoID {
public enum Alphabet {
case urlSafe
case uppercasedLatinLetters
case lowercasedLatinLetters
case numbers

public var supportedChars: String {
switch self {
case .uppercasedLatinLetters:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
case .lowercasedLatinLetters:
"abcdefghijklmnopqrstuvwxyz"
case .numbers:
"1234567890"
case .urlSafe:
[
Self.uppercasedLatinLetters,
Self.lowercasedLatinLetters,
Self.numbers,
].map(\.supportedChars).joined() + "-_"
}
public struct Alphabet: OptionSet {
public let rawValue: Int

public init(rawValue: Int) {
self.rawValue = rawValue
}
}
}

extension NanoID.Alphabet {
public static let uppercasedLatinLetters = Self(rawValue: 1 << 0)
public static let lowercasedLatinLetters = Self(rawValue: 1 << 1)
public static let numbers = Self(rawValue: 1 << 2)
public static let symbol = Self(rawValue: 1 << 3)

public static let urlSafe: Self = [uppercasedLatinLetters, lowercasedLatinLetters, numbers, symbol]
}

extension NanoID.Alphabet {
public var supportedChars: String {
var chars = ""
lazy var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

if contains(.uppercasedLatinLetters) {
chars.append(letters.uppercased())
}

if contains(.lowercasedLatinLetters) {
chars.append(letters.lowercased())
}

if contains(.numbers) {
chars.append("1234567890")
}

if contains(.symbol) {
chars.append("-_")
}

return chars
}
}
22 changes: 9 additions & 13 deletions Sources/NanoID/NanoID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ import Foundation
public final class NanoID {
public enum Default {
public static let size = 21
public static let aphabet = [NanoID.Alphabet.urlSafe]
public static let aphabet = NanoID.Alphabet.urlSafe
}

private var size: Int
private var alphabet: String
private var alphabet: Alphabet

public init(alphabet: [Alphabet] = Default.aphabet, size: Int = Default.size) {
public init(alphabet: Alphabet = Default.aphabet, size: Int = Default.size) {
self.size = size
self.alphabet = Self.parse(alphabet)
self.alphabet = alphabet
}
}

// MARK: - Public

extension NanoID {
public static func new(alphabet: [Alphabet] = Default.aphabet, size: Int = Default.size) -> String {
generate(from: parse(alphabet), of: size)
public static func new(alphabet: Alphabet = Default.aphabet, size: Int = Default.size) -> String {
generate(from: alphabet, of: size)
}

public func new() -> String {
Expand All @@ -57,17 +57,13 @@ extension NanoID {
// MARK: - Private

extension NanoID {
/// Parses input alphabets into a string
private static func parse(_ alphabets: [Alphabet]) -> String {
alphabets.map(\.supportedChars).joined()
}

/// Generates a Nano ID using given parameters
private static func generate(from alphabet: String, of length: Int) -> String {
private static func generate(from alphabet: Alphabet, of length: Int) -> String {
let alphabetSupportedChars = alphabet.supportedChars
var nanoID = ""

for _ in 0 ..< length {
let randomCharacter = randomCharacter(from: alphabet)
let randomCharacter = randomCharacter(from: alphabetSupportedChars)
nanoID.append(randomCharacter)
}

Expand Down

0 comments on commit 95ebe4f

Please sign in to comment.