Skip to content

Commit

Permalink
Merge pull request #3 from nicelgueta/make-all-unique
Browse files Browse the repository at this point in the history
Make all unique
  • Loading branch information
nicelgueta authored Aug 14, 2024
2 parents 510df60 + a813672 commit 4cebb35
Show file tree
Hide file tree
Showing 8 changed files with 592 additions and 155 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/pyci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ jobs:
target: x86_64
- runner: ubuntu-latest
target: x86
- runner: ubuntu-latest
target: aarch64
- runner: ubuntu-latest
target: armv7
- runner: ubuntu-latest
target: s390x
- runner: ubuntu-latest
target: ppc64le
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
Expand Down Expand Up @@ -66,10 +58,6 @@ jobs:
target: x86_64
- runner: ubuntu-latest
target: x86
- runner: ubuntu-latest
target: aarch64
- runner: ubuntu-latest
target: armv7
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
Expand Down
36 changes: 33 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustyrs"
version = "0.4.2"
version = "0.5.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ Usable as a standalone binary, web applications as a WebAssembly module (WASM),
## Why?
I needed a way to generate random slugs for a web project so thought it was a good opporunity to try out Rust's WebAssembly capabilities while also being able to use the same code as a zero-dependency python module for other projects.

### Key features
- No dependencies
- Fast
## Key features
- Generates unique random slugs for a input length in words
- Blazingly fast
- Zero dependencies
- Customisable slug length in words
- Over half a million unique combinations for 2-word slugs ranging up to over **280 trillion** unique combinations for 5-word slugs
- Over half a million unique combinations for 2-word slugs ranging up to over **280 trillion** unique combinations for 5-word slugs.

## Usage

Expand Down Expand Up @@ -100,8 +101,8 @@ Other features:
```

## Performance
- 1m x 2 word slugs: ~4.995s
- 1m x 5 word slugs: ~10.447s
- 0.5 million x 2 word slugs: ~0.47s
- 1 million x 5 word slugs: ~1.717s

## License
MIT
30 changes: 23 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
</head>
<body>
<script type="module" >
import init, { random_slugs } from "./pkg/rustyrs.js";
import init, { random_slugs, combinations } from "./pkg/rustyrs.js";
init();
export const test = () => {
const word_length = document.getElementById("word_len_inp").value;
const slugs = random_slugs(word_length, 10);
const word_length = document.getElementById("word_len_inp?").value;
const n_outputs = document.getElementById("n_outputs").value;
if (n_outputs > 10000) {
alert("Max number of slugs is 10000");
return;
}
const slugs = random_slugs(word_length, n_outputs);

// const word_combos = combinations(word_length);

Expand All @@ -22,18 +27,29 @@

const slugsElement = document.getElementById("slugs");
slugsElement.innerHTML = "";
slugs.forEach(slug => {
try {
slugs.forEach(slug => {
const li = document.createElement("li");
li.textContent = slug;
slugsElement.appendChild(li);
});
} catch {
const li = document.createElement("li");
li.textContent = slug;
li.textContent = `Error: Likely requested more slugs than unique possible combinations. Max combinations for ${word_length} word(s) is ${combinations(word_length)}`;
slugsElement.appendChild(li);
});
}
};
document.getElementById("test").addEventListener("click", test);
</script>
<p>Enter length of slug in words (min 1, max 5)</p>
<input type="number" id="word_len_inp" max="5" min="1" />
<p>Enter number of slugs to output (min 1, max 10000)</p>
<input type="number" id="n_outputs" max="10000" min="1" />
<!-- <p>Possible combinations with <span id="wl"></span> word(s): <span id="wlr"></span></p> -->
<button id="test">Get 10 slugs</button>
<br>
<p>
<button style="padding-top: 10;" id="test">Go</button>
</p>
<ul id="slugs"></ul>
</body>
</html>
9 changes: 6 additions & 3 deletions rustyrs.pyi
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
class SlugGenerator(object):
"""
A generator that yields slugs of a given word length forever.
Each slug is guaranteed to be unique for the lifetime of the generator.
"""
def __new__(cls, word_length: int) -> SlugGenerator: ...
def __iter__(self) -> SlugGenerator: ...
def __next__(self) -> str: ...

def get_slug(word_length: int) -> str:
"""
Creates a slug of a given word length.
Creates a slug of a given word length. This is stateless and does not
account for any slugs that have already been generated from a previous call.
Args:
word_length: The length of the slug in words
"""
...
def random_slugs(word_length: int, num_outputs: int = 1) -> list[str]:
"""
Creates a list of random slugs of a given word length.
Creates a list of random slugs of a given word length. Each slug is
guaranteed to be unique.
Args:
word_length: The length of the slug in words
num_outputs: The number of slugs to generate
"""
...
def combinations(word_length: int) -> int:
"""
Calculates the number of possible combinations for a given word length.
Calculates the number of unique possible combinations for a given word length.
Args:
word_length: The length of the slug in words
"""
Expand Down
Loading

0 comments on commit 4cebb35

Please sign in to comment.