Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/path.ts>startsWith

A JavaScript extension package for building strong and modern applications.
Latest
function startsWith
Re-export
import { startsWith } from "https://deno.land/x/ayonli_jsext@v0.9.72/path.ts";

Checks if the path starts with the given sub path.

This function doesn't check the path string directly, instead, it checks the path segments.

This function is ignorant about the path separator, the query string and the hash string (if present). And is case-insensitive on Windows volume symbol by default.

Examples

Example 1

import { startsWith } from "@ayonli/jsext/path";

console.assert(startsWith("/usr/bin", "/usr"));
console.assert(startsWith("C:\\Windows\\System32", "c:/Windows"));
console.assert(startsWith("http://example.com/foo/bar", "http://example.com"));
console.assert(startsWith("file:///C:/Windows/System32", "file:///c:/Windows"));

// To be noted, the origin portion of a URL is considered as a whole segment.
console.assert(!startsWith("http://example.com/foo/bar", "example.com"));
console.assert(startsWith("http://example.com/foo/b", "http://example.com"));

// ignore file protocol
console.assert(startsWith("file:///C:/Windows/System32", "C:/Windows/System32", {
    ignoreFileProtocol: true,
}));

Parameters

path: string
sub: string
optional
options: PathCompareOptions = [UNSUPPORTED]

Returns

boolean