mirror of
https://github.com/sbrow/strings.git
synced 2025-12-29 23:17:39 -05:00
feat: Added words and sentences.
This commit is contained in:
2
lib/index.d.ts
vendored
2
lib/index.d.ts
vendored
@@ -1,5 +1,7 @@
|
||||
export * from "./sentences";
|
||||
export * from "./shorten";
|
||||
export * from "./trim";
|
||||
export * from "./words";
|
||||
export declare const startsWith: import("ts-toolbelt/out/Function/Curry").Curry<(needle: string, haystack: string) => boolean>;
|
||||
export declare const endsWith: import("ts-toolbelt/out/Function/Curry").Curry<(needle: string, haystack: string) => boolean>;
|
||||
export declare const afterFirst: import("ts-toolbelt/out/Function/Curry").Curry<(separator: string, str: string) => string>;
|
||||
|
||||
@@ -16,8 +16,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.removeFirstWord = exports.afterFirstWord = exports.beforeFirstWord = exports.beforeFirst = exports.afterLast = exports.afterFirst = exports.endsWith = exports.startsWith = void 0;
|
||||
const utils_1 = require("./utils");
|
||||
__exportStar(require("./sentences"), exports);
|
||||
__exportStar(require("./shorten"), exports);
|
||||
__exportStar(require("./trim"), exports);
|
||||
__exportStar(require("./words"), exports);
|
||||
function escapeRegExp(str) {
|
||||
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
||||
}
|
||||
|
||||
3
lib/sentences.d.ts
vendored
Normal file
3
lib/sentences.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export declare function sentencesLazy(str: string, pattern?: RegExp): Generator<string>;
|
||||
export declare function sentences(str: string, pattern?: RegExp): string[];
|
||||
export declare function countSentences(str: string, pattern?: RegExp): number;
|
||||
26
lib/sentences.js
Normal file
26
lib/sentences.js
Normal file
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.countSentences = exports.sentences = exports.sentencesLazy = void 0;
|
||||
const matchSentenceEnd = /[!?]|(?<!\be|\be\.g|\betc)\./;
|
||||
function* sentencesLazy(str, pattern = matchSentenceEnd) {
|
||||
if (str.trim() !== '') {
|
||||
const match = str.match(pattern);
|
||||
const index = (match === null || match === void 0 ? void 0 : match.index) ? match.index + 1 : undefined;
|
||||
if (index) {
|
||||
yield str.substring(0, index);
|
||||
yield* sentencesLazy(str.substring(index));
|
||||
}
|
||||
else {
|
||||
yield str;
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.sentencesLazy = sentencesLazy;
|
||||
function sentences(str, pattern = matchSentenceEnd) {
|
||||
return Array.from(sentencesLazy(str, pattern));
|
||||
}
|
||||
exports.sentences = sentences;
|
||||
function countSentences(str, pattern = matchSentenceEnd) {
|
||||
return sentences(str, pattern).length;
|
||||
}
|
||||
exports.countSentences = countSentences;
|
||||
3
lib/words.d.ts
vendored
Normal file
3
lib/words.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export declare function wordsLazy(str: string, pattern?: RegExp): Generator<string, void, unknown>;
|
||||
export declare function words(str: string, pattern?: RegExp): string[];
|
||||
export declare function wordCount(str: string, pattern?: RegExp): number;
|
||||
21
lib/words.js
Normal file
21
lib/words.js
Normal file
@@ -0,0 +1,21 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.wordCount = exports.words = exports.wordsLazy = void 0;
|
||||
const matchWord = /\S+/g;
|
||||
function* wordsLazy(str, pattern = matchWord) {
|
||||
for (const match of str.matchAll(pattern)) {
|
||||
yield match[0];
|
||||
}
|
||||
}
|
||||
exports.wordsLazy = wordsLazy;
|
||||
function words(str, pattern = matchWord) {
|
||||
return Array.from(wordsLazy(str, pattern));
|
||||
}
|
||||
exports.words = words;
|
||||
function wordCount(str, pattern = matchWord) {
|
||||
let count = 0;
|
||||
for (const {} of str.matchAll(pattern))
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
exports.wordCount = wordCount;
|
||||
Reference in New Issue
Block a user