-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from RakuyoKit/feature/nanoid
Feature/nanoid
- Loading branch information
Showing
12 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Collection+RAK.swift | ||
// ConfigMediator.swift | ||
// RakuyoKit | ||
// | ||
// Created by Rakuyo on 2024/4/9. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Collection+RAK.swift | ||
// ConfigMediatorProtocol.swift | ||
// RakuyoKit | ||
// | ||
// Created by Rakuyo on 2024/4/9. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Collection+RAK.swift | ||
// ConfigSourceProtocol.swift | ||
// RakuyoKit | ||
// | ||
// Created by Rakuyo on 2024/4/9. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Collection+RAK.swift | ||
// ColorConfig.swift | ||
// RakuyoKit | ||
// | ||
// Created by Rakuyo on 2024/4/9. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Collection+RAK.swift | ||
// EmptyConfig.swift | ||
// RakuyoKit | ||
// | ||
// Created by Rakuyo on 2024/4/9. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// NanoID+Alphabet.swift | ||
// RakuyoKit | ||
// | ||
// Created by Rakuyo on 2024/8/12. | ||
// Copyright © 2024 RakuyoKit. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - NanoID.Alphabet | ||
|
||
extension NanoID { | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// NanoID.swift | ||
// RakuyoKit | ||
// | ||
// Created by Rakuyo on 2024/8/12. | ||
// Copyright © 2024 RakuyoKit. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - NanoID | ||
|
||
/// Used to create NanoID | ||
/// | ||
/// ```swift | ||
/// // Nano ID with default alphabet (0-9a-zA-Z_-) and length (21 chars) | ||
/// let id = NanoID.new() | ||
/// | ||
/// // Nano ID with default alphabet and given length | ||
/// let id = NanoID.new(size: 12) | ||
/// | ||
/// // Nano ID with given alphabet and length | ||
/// let id = NanoID.new(alphabet: .uppercasedLatinLetters, size: 15) | ||
/// | ||
/// // Nano ID with preset custom parameters | ||
/// let nanoID = NanoID(alphabet: [.lowercasedLatinLetters, .numbers], size: 10) | ||
/// let idFirst = nanoID.new() | ||
/// let idSecond = nanoID.new() | ||
/// ``` | ||
public final class NanoID { | ||
public enum Default { | ||
public static let size = 21 | ||
public static let aphabet = NanoID.Alphabet.urlSafe | ||
} | ||
|
||
private var size: Int | ||
private var alphabet: Alphabet | ||
|
||
public init(alphabet: Alphabet = Default.aphabet, size: Int = Default.size) { | ||
self.size = size | ||
self.alphabet = alphabet | ||
} | ||
} | ||
|
||
// MARK: - Public | ||
|
||
extension NanoID { | ||
public static func new(alphabet: Alphabet = Default.aphabet, size: Int = Default.size) -> String { | ||
generate(from: alphabet, of: size) | ||
} | ||
|
||
public func new() -> String { | ||
Self.generate(from: alphabet, of: size) | ||
} | ||
} | ||
|
||
// MARK: - Private | ||
|
||
extension NanoID { | ||
/// Generates a Nano ID using given parameters | ||
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: alphabetSupportedChars) | ||
nanoID.append(randomCharacter) | ||
} | ||
|
||
return nanoID | ||
} | ||
|
||
/// Returns a random character from a given string | ||
private static func randomCharacter(from string: String) -> Character { | ||
let randomNum = Int.random(in: 0 ..< string.count) | ||
let randomIndex = string.index(string.startIndex, offsetBy: randomNum) | ||
return string[randomIndex] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters