UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

68 lines (67 loc) 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.match = match; function globToRegExp(pattern) { let regex = '^'; let i = 0; while (i < pattern.length) { const char = pattern[i]; if (char === '\\') { const next = pattern[i + 1]; if (next) { regex += `\\${next}`; i += 2; } else { regex += '\\\\'; i += 1; } continue; } if (char === '*') { regex += '.*'; i += 1; continue; } if (char === '?') { regex += '.'; i += 1; continue; } if (char === '[') { const end = pattern.indexOf(']', i + 1); if (end > i + 1) { let content = pattern.slice(i + 1, end); if (content.startsWith('!')) { content = `^${content.slice(1)}`; } regex += `[${content}]`; i = end + 1; continue; } } if ('\\.^$+{}()|'.includes(char)) { regex += `\\${char}`; } else { regex += char; } i += 1; } regex += '$'; return new RegExp(regex); } function match(pattern, str) { // discuss at: https://locutus.io/tcl/match/ // parity verified: Tcl 8.6 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Uses Tcl-style glob pattern matching (`*`, `?`, and character classes like `[a-z]`). // example 1: match('h*o', 'hello') // returns 1: 1 // example 2: match('file?.txt', 'file1.txt') // returns 2: 1 // example 3: match('a[0-9]', 'ab') // returns 3: 0 const regex = globToRegExp(String(pattern)); return regex.test(String(str)) ? 1 : 0; }