Skip to content

Commit

Permalink
Merge pull request #63 from RakuyoKit/feature/nanoid
Browse files Browse the repository at this point in the history
Feature/nanoid
  • Loading branch information
rakuyoMo authored Aug 12, 2024
2 parents 5e94b52 + 95ebe4f commit d44ec66
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let package = Package(
.library(name: "RAKCombine", targets: ["RAKCombine"]),
.library(name: "RAKEpoxy", targets: ["RAKEpoxy"]),
.library(name: "RAKGRDB", targets: ["RAKGRDB"]),
.library(name: "RAKNanoID", targets: ["RAKNanoID"]),
],
dependencies: [
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "1.8.2"),
Expand All @@ -44,6 +45,7 @@ let package = Package(
"RAKGradient",
"RAKLocalCache",
"RAKNotification",
"RAKNanoID",
]
),

Expand Down Expand Up @@ -135,6 +137,12 @@ let package = Package(
path: "Sources/GRDB"
),

.target(
name: "RAKNanoID",
dependencies: ["RAKCore"],
path: "Sources/NanoID"
),

.testTarget(
name: "RakuyoKitTests",
dependencies: ["RakuyoKit"]
Expand Down
2 changes: 1 addition & 1 deletion Sources/Config/Mediator/ConfigMediator.swift
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.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Config/Mediator/ConfigMediatorProtocol.swift
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.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Config/Mediator/ConfigSourceProtocol.swift
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.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Config/Model/ColorConfig.swift
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.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Config/Model/EmptyConfig.swift
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.
Expand Down
8 changes: 8 additions & 0 deletions Sources/LocalCache/LocalCache+Migrated.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//
// LocalCache+Migrated.swift
// RakuyoKit
//
// Created by Rakuyo on 2024/4/10.
// Copyright © 2024 RakuyoKit. All rights reserved.
//

import Foundation

import RAKCodable
Expand Down
8 changes: 8 additions & 0 deletions Sources/LocalCache/LocalCache+Storage.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//
// LocalCache+Storage.swift
// RakuyoKit
//
// Created by Rakuyo on 2024/4/10.
// Copyright © 2024 RakuyoKit. All rights reserved.
//

import Foundation

import RAKCore
Expand Down
8 changes: 8 additions & 0 deletions Sources/LocalCache/LocalCache.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//
// LocalCache.swift
// RakuyoKit
//
// Created by Rakuyo on 2024/4/10.
// Copyright © 2024 RakuyoKit. All rights reserved.
//

import Foundation

/// Namespaces
Expand Down
55 changes: 55 additions & 0 deletions Sources/NanoID/NanoID+Alphabet.swift
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
}
}
79 changes: 79 additions & 0 deletions Sources/NanoID/NanoID.swift
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]
}
}
1 change: 1 addition & 0 deletions Sources/RakuyoKit/Exports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
@_exported import RAKEncrypte
@_exported import RAKGradient
@_exported import RAKLocalCache
@_exported import RAKNanoID
@_exported import RAKNotification

0 comments on commit d44ec66

Please sign in to comment.