Specialized string operations for prefix and suffix checking.
Asserts that the reference string starts with the provided value. Case-sensitive comparison.
/**
* Asserts that the reference string starts with provided value
* @param {string} value - String value to check for at start
* @returns {Assertion} Assertion chain object for further chaining
*/
startWith(value)Alias:
startsWith()Usage Examples:
const Code = require('@hapi/code');
const expect = Code.expect;
// Basic usage
expect('hello world').to.startWith('hello');
expect('JavaScript').to.startWith('Java');
expect('https://example.com').to.startWith('https://');
// Case sensitivity
expect('Hello').to.startWith('Hello'); // Passes
expect('Hello').to.not.startWith('hello'); // Passes - case sensitive
// Empty string
expect('anything').to.startWith(''); // Passes - all strings start with empty string
// Whole string match
expect('hello').to.startWith('hello'); // Passes - can match entire string
// Common patterns
const url = getApiUrl();
expect(url).to.be.a.string().and.startWith('https://');
const filename = getFilename();
expect(filename).to.startWith('temp_').and.endWith('.txt');Asserts that the reference string ends with the provided value. Case-sensitive comparison.
/**
* Asserts that the reference string ends with provided value
* @param {string} value - String value to check for at end
* @returns {Assertion} Assertion chain object for further chaining
*/
endWith(value)Alias:
endsWith()Usage Examples:
// Basic usage
expect('hello world').to.endWith('world');
expect('filename.txt').to.endWith('.txt');
expect('path/to/file.js').to.endWith('.js');
// Case sensitivity
expect('Hello').to.endWith('Hello'); // Passes
expect('Hello').to.not.endWith('hello'); // Passes - case sensitive
// Empty string
expect('anything').to.endWith(''); // Passes - all strings end with empty string
// Whole string match
expect('hello').to.endWith('hello'); // Passes - can match entire string
// File extensions
const files = ['image.jpg', 'doc.pdf', 'script.js'];
files.forEach(file => {
if (file.endsWith('.js')) {
expect(file).to.endWith('.js');
}
});const Code = require('@hapi/code');
const expect = Code.expect;
// Chain multiple string checks
expect('hello world')
.to.be.a.string()
.and.startWith('hello')
.and.endWith('world')
.and.include('o w')
.and.have.length(11);
// URL validation pattern
const apiUrl = 'https://api.example.com/v1/users';
expect(apiUrl)
.to.be.a.string()
.and.startWith('https://')
.and.include('api.')
.and.endWith('/users');
// File path validation
const tempFile = '/tmp/upload_12345.tmp';
expect(tempFile)
.to.startWith('/tmp/')
.and.endWith('.tmp')
.and.include('upload_');While
startWith()endWith()// Exact prefix/suffix with pattern matching in middle
const logEntry = '[2023-12-01] INFO: User logged in';
expect(logEntry)
.to.startWith('[')
.and.endWith('logged in')
.and.match(/\[\d{4}-\d{2}-\d{2}\]/);
// Validate email-like strings
const email = 'user@example.com';
expect(email)
.to.include('@')
.and.endWith('.com')
.and.match(/^[^@]+@[^@]+\.[^@]+$/);// These will throw errors - only works with strings
try {
expect(123).to.startWith('1'); // Error: Can only assert startsWith on string
} catch (err) {
expect(err).to.be.an.error();
}
try {
expect(['hello']).to.endWith('lo'); // Error: Can only assert endsWith on string
} catch (err) {
expect(err).to.be.an.error();
}
// Type checking before string assertions
const value = getValue();
if (typeof value === 'string') {
expect(value).to.startWith('prefix');
} else {
expect(value).to.not.be.a.string();
}// API response validation
const response = await fetch('/api/data');
const location = response.headers.get('location');
if (location) {
expect(location).to.startWith('/api/');
}
// Configuration validation
const config = {
database_url: 'postgresql://localhost:5432/mydb',
redis_url: 'redis://localhost:6379',
log_file: '/var/log/app.log'
};
expect(config.database_url).to.startWith('postgresql://');
expect(config.redis_url).to.startWith('redis://');
expect(config.log_file).to.startWith('/var/log/').and.endWith('.log');
// Template string validation
const template = '{{user.name}} has {{user.points}} points';
expect(template).to.startWith('{{').and.endWith('}}');
expect(template).to.include('{{user.name}}').and.include('{{user.points}}');