🐢 A simple JavaScript utility for conditionally query string together.
About • Getting started • Authors • Acknowledgements • License
Turls is a simple, JavaScript utility for conditionally query string together. The idea behind Turls is to prevent developers from rewriting the same code in multiple places, while also making it easy to dynamically construct advanced queries that can be easily shared across multiple files.
Turls is perfect for applications that need to dynamically query an API or database, but it can also be used as a general purpose URL builder.
- Install with npm, yarn or pnpm:
npm install turls
yarn add turls
pnpm add turls
- Use the library:
// ESM
import turls from 'turls';
turls("https://api.example.com/", "query", {
"id=1": true,
"name=John Doe": true,
"age=30": true
})
// => "https://api.example.com/query?id=1&name=John%20Doe&age=30"
// CommonJS
const turls = require('turls');
turls("https://api.example.com/", "query", {
"id=1": true,
"name=John Doe": true,
"age=30": true
})
// => "https://api.example.com/query?id=1&name=John%20Doe&age=30"
turls("https://api.example.com/", "query");
// => "https://api.example.com/query"
turls("https://api.example.com/", "query", "search");
// => "https://api.example.com/querysearch"
turls("https://api.example.com/", "query", {
"id=1": true,
"name=John Doe": true,
"age=30": true
})
// => "https://api.example.com/query?id=1&name=John%20Doe&age=30"
turls("https://api.example.com/", "query", {
"id=1": true,
"name=John Doe": true,
"age=30": false
})
// => "https://api.example.com/query?id=1&name=John%20Doe"
turls("https://api.example.com/", "query", {
"id=1": false,
"name=John Doe": true,
"age=30": false,
})
// => "https://api.example.com/query?name=John%20Doe"
- Inspired by @JedWatson, creator of classnames
- MIT License