Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Unix Match. Part 1 by Elena_Korljukova
import assert from "assert";
function unixMatch(filename: string, pattern: string): boolean {
if (filename.length < pattern.length || !pattern.includes('*') && filename.length > pattern.length)
return false;
for (let x of (pattern + '*').replace(/\?/g, '*').split('*')) {
if (!filename.includes(x))
return false;
}
return true;
}
console.log('Example:');
console.log(unixMatch('somefile.txt', '*'));
console.log(unixMatch("apache1.log","*.*"));
// These "asserts" are used for self-checking
assert.equal(unixMatch('somefile.txt', '*'), true);
assert.equal(unixMatch('other.exe', '*'), true);
assert.equal(unixMatch('my.exe', '*.txt'), false);
assert.equal(unixMatch('log1.txt', 'log?.txt'), true);
assert.equal(unixMatch('log12.txt', 'log?.txt'), false);
assert.equal(unixMatch('log12.txt', 'log??.txt'), true);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
Sept. 9, 2020