https://github.com/russbaz/cuid2
cuid2 implemented in swift
https://github.com/russbaz/cuid2
Last synced: 12 months ago
JSON representation
cuid2 implemented in swift
- Host: GitHub
- URL: https://github.com/russbaz/cuid2
- Owner: RussBaz
- License: mit
- Created: 2024-05-30T08:46:38.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-05-30T10:56:48.000Z (about 2 years ago)
- Last Synced: 2025-04-09T05:03:26.508Z (about 1 year ago)
- Language: Swift
- Size: 4.88 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# _cuid2_ in Swift
The original implementation in js: [https://github.com/paralleldrive/cuid2](https://github.com/paralleldrive/cuid2)
`cuid2` is a possible replacement to globally unique identifiers such as UUID and such. The length of such an id is configurable and can be easier to copy-paste manually.
Here is an example of the `cuid2` with its default length: `lnapog2xtrbfatrw5qtrpvs6`.
To learn more, please follow the link to the reference implementation in javascript.
## Installation
Please add this dependency to your `Package.swift` to use this library:
```swift
.package(url: "https://github.com/RussBaz/cuid2.git", from: "0.0.1"),
```
and then add the dependency to the target:
```swift
.product(name: "cuid2", package: "cuid2"),
```
## How to use
### As a library
```swift
import cuid2
let id = createId() // quickest way to create a one off id
isCuid2(id: id) // to verify if the string can possibly be cuid2
// Alternatively, you can customise how the ids a generated
// by providing a custom session counter, cuid length and a custom fingerprint
func createId(counter: Cuid2SessionCounter? = nil, length: Int? = nil, fingerprint: String? = nil) -> String { }
// If you need to create many ids
// 1. please create an instance of the generator
struct Cuid2Generator {
// other code omitted
init(counter: Cuid2SessionCounter? = nil, length: Int? = nil, fingerprint: String? = nil) { }
// 2. and then use this method to create an array of ids
func generate(times: Int) -> [String] { }
// skipping more code
}
```
### As a CLI tool
Clone the repository, build the release build with `swift build -c release` from the project root. The `cuid2cli` is now ready for use in the `.build//release/` folder. Move it or copy it into the target destination.
```
USAGE: tool [--count ] [--include-line-number] [--length ] [--fingerprint ]
OPTIONS:
-c, --count The number of times to repeat 'cuid2'. (default: 1)
-i, --include-line-number
Include a line number with each repetition.
-l, --length The 'cuid2' length in characters.
-f, --fingerprint
The custom fingerprint.
-h, --help Show help information.
```