@oxog/string
Version:
Comprehensive string manipulation utilities with zero dependencies
58 lines (57 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.contains = contains;
exports.count = count;
exports.indexOfAll = indexOfAll;
exports.words = words;
exports.chars = chars;
exports.codePoints = codePoints;
exports.graphemes = graphemes;
const unicode_1 = require("../utils/unicode");
const algorithms_1 = require("../utils/algorithms");
function contains(str, search, caseSensitive = true) {
if (!caseSensitive) {
return str.toLowerCase().includes(search.toLowerCase());
}
return str.includes(search);
}
function count(str, search) {
if (!search)
return 0;
let count = 0;
let position = 0;
while ((position = str.indexOf(search, position)) !== -1) {
count++;
position += search.length;
}
return count;
}
function indexOfAll(str, search) {
if (!search)
return [];
return (0, algorithms_1.boyerMooreSearch)(str, search);
}
function words(str, locale) {
if (locale && typeof Intl !== 'undefined' && 'Segmenter' in Intl) {
try {
const segmenter = new Intl.Segmenter(locale, { granularity: 'word' });
return Array.from(segmenter.segment(str))
.filter((segment) => segment.isWordLike)
.map((segment) => segment.segment);
}
catch (_a) {
// Fall back to default implementation if locale is invalid
}
}
// Use a more inclusive regex that handles Unicode characters
return str.match(/[\p{L}\p{N}]+/gu) || [];
}
function chars(str) {
return (0, unicode_1.getGraphemes)(str);
}
function codePoints(str) {
return (0, unicode_1.getCodePoints)(str);
}
function graphemes(str) {
return (0, unicode_1.getGraphemes)(str);
}