Skip to content

Commit

Permalink
Fixes for version 0,1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
macmoonshine committed Nov 12, 2023
1 parent bb065a3 commit af2532e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.9
// swift-tools-version: 5.7
/*
MIT License

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
```

Expand Down
6 changes: 1 addition & 5 deletions Sources/sqids/Sqids.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
58 changes: 58 additions & 0 deletions Tests/sqidsTests/EncodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit af2532e

Please sign in to comment.