Packages@accelint/predicates
doesStartWith
Curried predicate for testing if strings start with a specific prefix
Creates a predicate function that determines if a string starts with a specific prefix.
Usage
import { doesStartWith } from '@accelint/predicates';
const isHttpUrl = doesStartWith('http');
isHttpUrl('https://example.com'); // true
isHttpUrl('ftp://example.com'); // falseReference
function doesStartWith(prefix: string): (input: string) => booleanParameters
| Parameter | Type | Description |
|---|---|---|
prefix | string | The substring to use as the prefix |
Returns
Curried function that accepts a string and returns true if it starts with the prefix.
Examples
Example: URL validation
import { doesStartWith } from '@accelint/predicates';
const isHttps = doesStartWith('https://');
const isHttp = doesStartWith('http://');
function isSecureUrl(url: string): boolean {
return isHttps(url);
}
isSecureUrl('https://example.com'); // true
isSecureUrl('http://example.com'); // falseExample: Filtering file paths
import { doesStartWith } from '@accelint/predicates';
const paths = [
'/home/user/documents',
'/var/log/system',
'/home/user/downloads',
'/etc/config'
];
const userPaths = paths.filter(doesStartWith('/home/user'));
// ['/home/user/documents', '/home/user/downloads']Good to know: Comparisons are case-sensitive. Use native String methods like
.toLowerCase()before applying this predicate if case-insensitive matching is needed.
Related
- doesEndWith - Check if string ends with suffix
- doesNotStartWith - Negated version of this function
- doesNotEndWith - Check if string does not end with suffix
- isLike - Pattern matching with RegExp
- isNotLike - Negated pattern matching
- isString - Type guard for strings