A simple url-style-string builder with pure javascript
npm install @js-tiny/urlimport { PureUrl, PureQuery } from "@js-tiny/url";
// Create a new URL
const url = new PureUrl("https://example.com/path?query=value#hash");
// Get URL components
console.log(url.protocol()); // 'https:'
console.log(url.hostname()); // 'example.com'
console.log(url.pathname()); // '/path'
console.log(url.search()); // '?query=value'
console.log(url.hash()); // '#hash'
// Modify URL components
url
.protocol("http:")
.hostname("newdomain.com")
.pathname("/new/path")
.hash("#new-hash");
console.log(url.toString()); // 'http://newdomain.com/new/path?query=value#new-hash'const url = new PureUrl();
url.protocol("https"); // Auto adds ':' suffix
console.log(url.protocol()); // 'https:'
url.protocol(null); // Clear protocolconst url = new PureUrl();
// Set username and password together
url.auth("username:password");
console.log(url.auth()); // 'username:password'
// Or set separately
url.username("user");
url.password("pass");
console.log(url.auth()); // 'user:pass'const url = new PureUrl();
// Set host with port
url.host("example.com:8080");
console.log(url.host()); // 'example.com:8080'
console.log(url.hostname()); // 'example.com'
console.log(url.port()); // '8080'
// Or set separately
url.hostname("subdomain.example.com");
url.port("3000");const url = new PureUrl();
// Set full path
url.pathname("/api/v1/users");
// Work with path segments
url.pathnames("api", "v1", "users");
console.log(url.pathname()); // '/api/v1/users'
// Get path segments
const segments = url.pathnames(); // ['api', 'v1', 'users']const url = new PureUrl();
// Set query string
url.search("?key1=value1&key2=value2");
// Use the query object
url.query.set("param", "value");
url.query.add("tags", "javascript");
url.query.add("tags", "nodejs");
console.log(url.query.get("param")); // 'value'
console.log(url.query.getAll("tags")); // ['javascript', 'nodejs']
// Check if parameter exists
console.log(url.query.has("param")); // true
// Remove parameter
url.query.remove("param");
// Clear all query parameters
url.query.clear();const url = new PureUrl();
url.hash("section1"); // Auto adds '#' prefix
console.log(url.hash()); // '#section1'
url.hash(null); // Clear hashThe PureQuery class provides powerful query parameter management:
import { PureQuery } from "@xesam/pure-url";
// Create from query string
const query = new PureQuery("a=1&b=2&b=3", true); // auto decode
// Get values
console.log(query.get("a")); // '1'
console.log(query.getAll("b")); // ['2', '3']
// Set values
query.set("c", "new-value");
query.set("d", "value1", "value2", "value3");
// Add values (avoid duplicates)
query.add("tags", "javascript");
query.add("tags", "javascript"); // Won't add duplicate
// Add multiple values at once
query.addAll({
category: "programming",
tags: ["javascript", "nodejs", "react"],
status: "active"
});
// Or using array format
query.addAll([
["author", "john"],
["tags", ["frontend", "backend"]]
]);
// Add if not exists
query.addIfNotExist("config", "only-once");
// Merge with another query
query.merge("x=10&y=20");
// Serialize back to string
console.log(query.toUrlString()); // 'a=1&b=2&b=3&c=new-value&d=value1&d=value2&d=value3&x=10&y=20'
#### Batch Operations with addAll
The `addAll` method allows you to add multiple key-value pairs efficiently:
```javascript
const query = new PureQuery();
// Using object format
query.addAll({
search: "javascript",
page: "1",
tags: ["react", "vue", "angular"]
});
// Using array format (tuples)
query.addAll([
["category", "frontend"],
["difficulty", ["beginner", "intermediate"]]
]);
// Works with existing data
query.set("existing", "value");
query.addAll({ existing: "new-value", newKey: "new-data" });
// Result: existing has both values, newKey is added
// Method chaining support
query.addAll({ param1: "value1" })
.addAll([["param2", "value2"]])
.add("param3", "value3");
### Chaining Methods
All setter methods return the instance, allowing method chaining:
```javascript
const url = new PureUrl()
.protocol("https")
.hostname("api.example.com")
.port("443")
.pathname("/v1/users")
.query.set("limit", "10")
.query.set("offset", "0")
.hash("results");
console.log(url.toString());
// 'https://api.example.com:443/v1/users?limit=10&offset=0#results'
Any component can be reset by passing null, undefined, or empty string:
const url = new PureUrl(
"https://user:pass@example.com:8080/path?query=value#hash"
);
// Clear components
url.protocol(null);
url.auth(null);
url.host(null);
url.pathname(null);
url.search(null);
url.hash(null);
console.log(url.toString()); // '' (empty string)new PureUrl(href?: string)- Create a new URL instance, optionally from a string
query: PureQuery- Query parameters manager
Protocol:
protocol(value?: string)- Get/set protocol (e.g., 'http:', 'https:')
Authentication:
auth(value?: string)- Get/set auth as 'username:password'username(value?: string)- Get/set usernamepassword(value?: string)- Get/set password
Host:
host(value?: string)- Get/set host as 'hostname:port'hostname(value?: string)- Get/set hostnameport(value?: string)- Get/set port
Path:
path(value?: string)- Get/set path including query stringpathname(value?: string)- Get/set pathnamepathnames(...fragments: string[])- Get/set path segments
Query:
search(value?: string)- Get/set query string
Hash:
hash(value?: string)- Get/set hash fragment
Serialization:
toUrlString()- Get full URL stringtoString()- Alias for toUrlString()
new PureQuery(qString?: string, decodeValue?: boolean)
isEmpty()- Check if query is emptyhas(key: string)- Check if key existskeys()- Get all keysget(key: string, index?: number)- Get value by keygetAll(key: string)- Get all values for keyadd(key: string, value: string)- Add value to keyaddAll(entries: object | array)- Add multiple key-value pairs at onceaddIfNotExist(key: string, value: string)- Add only if key doesn't existset(key: string, ...values: string[])- Set values for keyremove(key: string)- Remove keyclear()- Clear all keysload(qString: string, decodeValue?: boolean)- Load from query stringmerge(otherQuery: string | PureQuery, decodeValue?: boolean)- Merge with another querytoUrlString(encodeValue?: boolean)- Serialize to query stringtoString()- Alias for toUrlString()