feat: add auto length
This commit is contained in:
parent
30e29a0e98
commit
e6eeb9bfb6
|
@ -40,8 +40,8 @@ https://count.dsrkafuu.net/dsrkafuu:demo?theme=gelbooru
|
|||
```
|
||||
|
||||
1. `<id>`: A string between 1-256 chars (`0-9a-zA-Z:!@#$%^&*_-`) starting with a letter (`a-zA-Z`)
|
||||
2. `<theme>`: `asoul`, `gelbooru`, `moebooru`, `rule34` (and two other themes, default is `gelbooru`)
|
||||
3. `<length>`: Number between 1-10 (default: 7)
|
||||
2. `<theme>`: `asoul`, `gelbooru`, `moebooru`, `rule34` (and two other themes, default: `gelbooru`)
|
||||
3. `<length>`: Number between 1-10 (default: 7) or `auto`
|
||||
|
||||
Recommend to use `user:usage` like string as ID for better management.
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "moe-counter",
|
||||
"version": "2.0.0",
|
||||
"version": "2.1.0",
|
||||
"description": "Fork of Moe Counter for fast global access powered by Cloudflare Workers.",
|
||||
"author": "DSRKafuU <dsrkafuu@outlook.com> (https://dsrkafuu.net)",
|
||||
"license": "MIT",
|
||||
|
|
|
@ -3,7 +3,13 @@ import themes from '../../themes';
|
|||
import { validateID, minify } from '../utils';
|
||||
|
||||
function genImage(count, theme, length) {
|
||||
const nums = count.toString().padStart(length, '0').split('');
|
||||
let nums;
|
||||
if (length === 'auto') {
|
||||
nums = count.toString().split('');
|
||||
} else {
|
||||
nums = count.toString().padStart(length, '0').split('');
|
||||
}
|
||||
|
||||
const { width, height, images } = themes[theme];
|
||||
let x = 0; // x axis
|
||||
const parts = nums.reduce((pre, cur) => {
|
||||
|
@ -29,14 +35,17 @@ export async function get(req, event) {
|
|||
if (!themes[theme]) {
|
||||
theme = 'gelbooru';
|
||||
}
|
||||
if (!length || length <= 0 || length > 10) {
|
||||
length = 7;
|
||||
let _length = length;
|
||||
if (length === 'auto') {
|
||||
_length = 'auto';
|
||||
} else if (!length || length <= 0 || length > 10) {
|
||||
_length = 7;
|
||||
}
|
||||
|
||||
// get times from KV
|
||||
const data = ((await KV.get(id)) || '|').split('|');
|
||||
const count = (Number.parseInt(data[0]) || 0) + 1;
|
||||
const image = genImage(count, theme, length);
|
||||
const image = genImage(count, theme, _length);
|
||||
// set time asynchronously (no await)
|
||||
event.waitUntil(KV.put(id, `${count}|${Date.now()}`));
|
||||
|
||||
|
|
Loading…
Reference in New Issue