Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anhgelus/human-readable-slug
A little algorithm generating human readable slug in Go
https://github.com/anhgelus/human-readable-slug
algorithm go golang slug
Last synced: 5 days ago
JSON representation
A little algorithm generating human readable slug in Go
- Host: GitHub
- URL: https://github.com/anhgelus/human-readable-slug
- Owner: anhgelus
- License: mpl-2.0
- Created: 2023-10-04T08:41:22.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-06T12:38:00.000Z (9 months ago)
- Last Synced: 2024-11-25T13:45:20.313Z (2 months ago)
- Topics: algorithm, go, golang, slug
- Language: Go
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Human Readable Slug
Lightweight Go library generating a human-readable slug by avoiding some bad combinations like:
- I and l
- 0 and O
- m and n
- or if letter1 = letter2## Usage
To install this library, run this command:
```bash
go get -u github.com/anhgelus/human-readable-slug
```And now you generate some slugs with the function `GenerateSlug` in `github.com/anhgelus/human-readable-slug`. E.g.:
```go
slug := GenerateSlug(time.Now().Unix(), 7) // generates a 7 chars-length slug with the current timestamp as seed
```You can also check if a slug is human-readable with the function `IsHumanReadable`. E.g.:
```go
IsHumanReadable("aab") // returns false with the default options
IsHumanReadable("Aab") // returns true with the default options
```If you want to add a custom bad combination, you can use the function `AddBadCombination`.E.g.:
```go
AddBadCombination('b', 'd') // add the combination "bd" and "db"
AddBadCombination('q', 'p') // add the combination "qp" and "pq"
```
### Customize
If you want to customize the options of the slug generation, you must use the `CustomSlug` type.
- `HasCapitalLetters` is true if you want to have capital letters (default: true)
- `HasNumbers` is true if you want to have numbers (default: true)
- `CanBe2ConsecutiveLetters` is true if you want to have 2 consecutive letters or more (default: false)With this type you can now call every other functions except the `AddBadCombination` which is global.
```go
options := CustomSlug{
CanBe2ConsecutiveLetters: false,
HasNumbers: false,
HasCapitalLetters: true,
}
options.GenerateSlug(time.Now().Unix(), 7) // Will generate a 7 chars length slug without numbers
options.IsHumanReadable("helloWorld") // returns true
options.IsHumanReadable("hell0World") // returns false due to the `0`
```