mirror of
https://github.com/sbrow/strings.git
synced 2026-02-28 04:21:44 -05:00
feat: Added docs.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import fc from 'fast-check';
|
||||
import {
|
||||
/*afterFirstWord, beforeFirstWord, endsWith,*/ ltrim, startsWith,
|
||||
/*afterFirstWord, beforeFirstWord, */ endsWith, ltrim, startsWith,
|
||||
} from './index';
|
||||
|
||||
describe('strings', () => {
|
||||
@@ -20,6 +20,21 @@ describe('strings', () => {
|
||||
}));
|
||||
})
|
||||
});
|
||||
describe('endsWith', () => {
|
||||
it('returns true if haystack end with needle', () => {
|
||||
expect(endsWith('bar', 'foobar')).toBe(true);
|
||||
})
|
||||
it('returns true when haystack is needle', () => {
|
||||
expect(endsWith('foo', 'foo')).toBe(true);
|
||||
})
|
||||
it('works', () => {
|
||||
fc.assert(fc.property(fc.string(), fc.string(), (needle, haystack) => {
|
||||
if (haystack.substring(haystack.indexOf(needle)) == needle) {
|
||||
expect(endsWith(needle, haystack)).toBe(true);
|
||||
}
|
||||
}));
|
||||
})
|
||||
});
|
||||
// describe('afterFirstWord', () => {
|
||||
// it('removes the first word', () => {
|
||||
// fc.assert(fc.property(fc.string(), (str) => {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { curry, isEmpty, tail, when } from "./utils";
|
||||
|
||||
// function escapeRegExp(string) {
|
||||
// return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
||||
// }
|
||||
function escapeRegExp(str: string) {
|
||||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
||||
}
|
||||
|
||||
export const startsWith = curry((needle: string, haystack: string) => haystack.indexOf(needle) === 0);
|
||||
|
||||
// export const endsWith = curry((needle, haystack) => (new RegExp(`${escapeRegExp(needle)}$`)).test(haystack));
|
||||
export const endsWith = curry((needle: string, haystack: string) => (new RegExp(`${escapeRegExp(needle)}$`)).test(haystack));
|
||||
|
||||
/**
|
||||
* Trims characters from the left side of a string.
|
||||
|
||||
Reference in New Issue
Block a user