Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/malczak/hashids
Hashids, ported for Swift (http://www.hashids.org)
https://github.com/malczak/hashids
Last synced: 1 day ago
JSON representation
Hashids, ported for Swift (http://www.hashids.org)
- Host: GitHub
- URL: https://github.com/malczak/hashids
- Owner: malczak
- License: mit
- Created: 2015-02-20T17:57:59.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-07-17T13:53:24.000Z (4 months ago)
- Last Synced: 2024-11-05T00:05:12.166Z (9 days ago)
- Language: Swift
- Size: 41 KB
- Stars: 110
- Watchers: 8
- Forks: 35
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Hashids.swift
---
A Swift class to generate YouTube-like ids from numbers.
Ported from [Hashids.php](https://github.com/ivanakimov/hashids.php) by [ivanakimov](https://github.com/ivanakimov)
Read documentation at [http://hashids.org/php](http://hashids.org/php)
#### Example Usage
```swift
var hashids = Hashids(salt:"this is my salt");
var hash = hashids.encode(1, 2, 3); // hash:"laHquq"
var values = hashids.decode(hash!); // values:[1,2,3]
```Example with custom alphabet and minimum hash length
```swift
var hashids = Hashids(salt:"this is my salt", minHashLength:8, alphabet:"abcdefghij1234567890");
var hash = hashids.encode(1, 2, 3); // hash:"514cdi42"
var values = hashids.decode(s!); // values:[1,2,3]
```Example with UTF8 alphabet
```swift
var hashids = Hashids(salt:"this is my salt", minHashLength:0, alphabet:"▁▂▃▄▅▆▇█");
var hash = hashids.encode(1, 2, 3); // hash:"▅▅▂▄▃▆"
var values = hashids.decode(hash!); // values:[1,2,3]
```#### Notes
Internally `Hashids` is using integer array rather than strings.
By default `Hashids` class is just a type alias for `Hashids_` and is using unicode scalars for all string manipulations. This makes it possible to use unicode alphabets eq. ':hatched_chick::pig::cat::dog::mouse:'.Generic version `Hashids_` can be used for both ASCII and UTF alphabets. Based on how characters are interpreted in Swift there are possible 3 scenarios
- `Hashids_` ASCII, uses `String.UTF8View`
- `Hashids_` UTF16, uses `String.UTF16View`
- `Hashids_` Unicode, uses `String.UnicodeScalarView`##### Swift < 5
For Swift 4.2 version checkout branch [swift-4.2](https://github.com/malczak/hashids/tree/swift-4.2)
For Swift 4.1 version checkout branch [swift-4.1](https://github.com/malczak/hashids/tree/swift-4.1)
For Swift 3.0 version checkout branch [swift-3.0](https://github.com/malczak/hashids/tree/swift-3.0)
For Swift 2.3 (2) version checkout branch [swift-2.3](https://github.com/malczak/hashids/tree/swift-2.3)#### License
MIT License. See the `LICENSE` file.