Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,29 @@ on:
pull_request:
branches:
- main
schedule:
- cron: '0 0 1 * *' # Run on the first day of every month
workflow_dispatch:

jobs:
test:
strategy:
matrix:
version: ['4.0', '4.1', '4.2', '4.3', '4.4', '5.0', '5.1', 'latest']
fail-fast: false
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Create docker image and run container
run: |
docker build --build-arg VERSION=${{ matrix.version }} -t "sqids-bash/test:bash-${{ matrix.version }}" .
docker run -id --name "bash-${{ matrix.version }}" "sqids-bash/test:bash-${{ matrix.version }}"

- name: Run tests
run: docker container exec "bash-${{ matrix.version }}" bats ./tests

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -22,12 +42,3 @@ jobs:

- name: Run ShellCheck
uses: ludeeus/action-shellcheck@master

- name: Set up Bats
run: |
git clone https://github.com/bats-core/bats-core.git
cd bats-core
sudo ./install.sh /usr/local

- name: Run tests
run: bats -T ./tests
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# CHANGELOG

## [1.0.1] - 2024-01-28
## [1.0.1] - 2024-01-29

### Fixed

- Fix encoding numbers with leading zeros throws an error. (#6)
- Works with bash version 4.0 or higher (#7)

## [1.0.0] - 2024-01-28

Expand Down
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ARG VERSION

FROM bash:${VERSION}

COPY ./src /var/tmp/src
COPY ./tests /var/tmp/tests

WORKDIR /var/tmp

# install git and bats
RUN apk update \
&& apk add --no-cache git \
&& git clone https://github.com/bats-core/bats-core.git \
&& cd bats-core \
&& ./install.sh /usr/local
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# [Sqids Bash](https://sqids.org/bash)

![GitHub Release](https://img.shields.io/github/v/release/sqids/sqids-bash)
[![Tests](https://github.com/sqids/sqids-bash/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/sqids/sqids-bash/actions/workflows/tests.yml)
![GitHub Downloads (all assets, all releases)](https://img.shields.io/github/downloads/sqids/sqids-bash/total)

[Sqids](https://sqids.org/bash) (*pronounced "squids"*) is a small library that lets you **generate unique IDs from numbers**. It's good for link shortening, fast & URL-safe ID generation and decoding back into numbers for quicker database lookups.

Expand Down Expand Up @@ -29,12 +31,18 @@ Not good for:

## 🚀 Getting started

If your bash version is not 4.0 or higher, please upgrade your bash. (You can check with `$bash --version`.)

Once you have verified the version of bash, run

```bash
git clone https://github.com/sqids/sqids-bash.git
chmod +x sqids-bash/src/sqids
cp sqids-bash/src/sqids /usr/local/bin
```

You may need to add `sudo` before the command to run the commands as root.

## 👩‍💻 Examples

Simple encode & decode:
Expand Down
20 changes: 13 additions & 7 deletions src/sqids
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ to_id() {
local id_chars=()

while true; do
id_chars=("${alphabet:$((num % ${#alphabet})):1}" "${id_chars[@]}")
id_chars=("${alphabet:$((num % ${#alphabet})):1}" "${id_chars[@]+"${id_chars[@]}"}") # it can be "${id_chars[@]}" without `set -u`, workaround for bash 4.0
num=$((num / ${#alphabet}))
if [[ $num -eq 0 ]]; then
break
Expand Down Expand Up @@ -978,7 +978,7 @@ EOS
}

version() {
echo "sqids-bash version 1.0.0"
echo "sqids-bash version 1.0.1"
exit 0
}

Expand Down Expand Up @@ -1048,10 +1048,16 @@ main() {
exit 1
fi

if [[ -n $(echo "$alphabet" | fold -s1 | sort | uniq -d) ]]; then
echo "ERROR: Alphabet cannot contain duplicate characters" >&2
exit 1
fi
declare -A alphabet_dict
splitstr "$alphabet"
for char in $__RETURN; do
if ${alphabet_dict[$char]:-false}; then
echo "ERROR: Alphabet cannot contain duplicate characters" >&2
exit 1
else
alphabet_dict[$char]=true
fi
done

MIN_LENGTH_LIMIT=255
if [[ $min_length -lt 0 || $min_length -gt $MIN_LENGTH_LIMIT ]]; then
Expand Down Expand Up @@ -1090,7 +1096,7 @@ main() {
case "$mode" in
"encode")
if $flag_a || $flag_b; then
encode -a "$alphabet" -b "${filtered_blocklist[*]}" -l "$min_length" "$@"
encode -a "$alphabet" -b "${filtered_blocklist[*]+"${filtered_blocklist[*]}"}" -l "$min_length" "$@" # it can be "${filtered_blocklist[*]}" without `set -u`, workaround for bash 4.0
else
encode -a "$alphabet" -b "${block_list[*]}" -l "$min_length" "$@"
fi
Expand Down