From af2532e1bb424cd2da7af4ac31c809d560f02350 Mon Sep 17 00:00:00 2001 From: Clemens Wagner Date: Sun, 12 Nov 2023 11:55:18 +0100 Subject: [PATCH] Fixes for version 0,1.0 --- Package.swift | 2 +- README.md | 2 +- Sources/sqids/Sqids.swift | 6 +--- Tests/sqidsTests/EncodeTests.swift | 58 ++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/Package.swift b/Package.swift index 7f9f250..16535e8 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.9 +// swift-tools-version: 5.7 /* MIT License diff --git a/README.md b/README.md index 9f2be04..ae4103f 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Add the following dependency to your Swift `Package.swift`: ```swift dependencies.append( - .package(url: "https://github.com/squids/squitss-swift.git", from: "1.0.0") + .package(url: "https://github.com/squids/squids-swift.git", from: "0.1.0") ) ``` diff --git a/Sources/sqids/Sqids.swift b/Sources/sqids/Sqids.swift index 4e6f9ee..0064970 100644 --- a/Sources/sqids/Sqids.swift +++ b/Sources/sqids/Sqids.swift @@ -32,7 +32,6 @@ public struct Sqids { case invalidMinLength(Int) case valueError(Id) case maximumAttemptsReached - case invalidId } public static let defaultAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" public static let minAlphabetLength = 3 @@ -147,10 +146,7 @@ public struct Sqids { if !id.isEmpty { let characterSet = CharacterSet(alphabet.flatMap({ $0.unicodeScalars })) - if !id.unicodeScalars.reduce(true, { $0 && characterSet.contains($1) }) { - throw Error.invalidId - } - else { + if id.unicodeScalars.reduce(true, { $0 && characterSet.contains($1) }) { let offset = alphabet.firstIndex(of: id.first!)! var alphabet = splitReverse(offset: offset) var value = String(Array(id).suffix(from: 1)) diff --git a/Tests/sqidsTests/EncodeTests.swift b/Tests/sqidsTests/EncodeTests.swift index cc0c6e1..8e5050e 100644 --- a/Tests/sqidsTests/EncodeTests.swift +++ b/Tests/sqidsTests/EncodeTests.swift @@ -66,4 +66,62 @@ final class EncodeTests: XCTestCase { XCTAssertEqual(try sqids.decode(id), numbers) } } + + func testIncrementalNumbersSameIndex1() throws { + let sqids = Sqids() + let ids: [String: Sqids.Ids] = [ + "SvIz": [0, 0], + "nWqP": [1, 0], + "tSyw": [2, 0], + "eX68": [3, 0], + "rxCY": [4, 0], + "sV8a": [5, 0], + "uf2K": [6, 0], + "7Cdk": [7, 0], + "3aWP": [8, 0], + "m2xn": [9, 0], + ] + for (id, numbers) in ids { + XCTAssertEqual(try sqids.encode(numbers), id) + XCTAssertEqual(try sqids.decode(id), numbers) + } + } + + func testMultiInput() throws { + let sqids = Sqids() + let numbers: Sqids.Ids = Array(1..<100) + let output = try sqids.decode(try sqids.encode(numbers)) + + XCTAssertEqual(numbers, output) + } + + func testEncodingNoNumbers() throws { + let sqids = Sqids() + + XCTAssertEqual(try sqids.encode([]), "") + } + + func testDecodingEmptyString() throws { + let sqids = Sqids() + + XCTAssertEqual(try sqids.decode(""), []) + } + + func testDecodingInvalidCharacter() throws { + let sqids = Sqids() + + XCTAssertEqual(try sqids.decode("*"), []) + } + + func testEncodeOutOfRangeNumbers() throws { + let sqids = Sqids() + + do { + _ = try sqids.encode([-1]) + XCTFail() + } + catch Sqids.Error.valueError(let id) { + XCTAssertEqual(-1, id) + } + } }