A simple, easy to use, and relatively performant (see benchmarks) way to create and join uri parts together.
Please note that this is not meant to handle many edge cases, and is meant and made for simple use cases.
import { assertEquals } from "https://deno.land/std@0.122.0/testing/asserts.ts";
import uri from "https://deno.land/x/mouri/mod.ts";
const API_URL = "https://api.example.com/";
const userPostsUrl = (id: string, limit: number, offset: number) => {
return uri`${API_URL}/users/${id}/posts?${{ limit, offset }}`;
};
assertEquals(
userPostsUrl("112233445566778899", 10, 5),
"https://api.example.com/users/112233445566778899/posts?limit=10&offset=5",
);
> npm i mouri
import { strict as assert } from 'assert';
import uri from 'mouri';
const API_URL = "https://api.example.com/";
const userPostsUrl = (id, limit, offset) => {
return uri`${API_URL}/users/${id}/posts?${{ limit, offset }}`;
};
assert.strictEqual(
userPostsUrl("112233445566778899", 10, 5),
"https://api.example.com/users/112233445566778899/posts?limit=10&offset=5"
);
To run: deno run .\bench.ts
pattern:
{API_URL}/users/{id}/posts/limit={limit}&offset={offset}
expected result:
https://api.example.com/users/112233445566778899/posts/limit=10&offset=5
Name | Runs | Total (ms) | Average (ms) |
---|---|---|---|
mouri | 2000 | 12.058 | 0.006 |
urlcat | 2000 | 37.778 | 0.019 |
handwritten | 2000 | 46.136 | 0.023 |