mirror of
https://github.com/sbrow/strings.git
synced 2025-12-29 23:17:39 -05:00
feat: Added more functions.
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, */ afterFirst, afterLast, endsWith, ltrim, rtrim, trim, startsWith,
|
||||
} from './index';
|
||||
|
||||
describe('strings', () => {
|
||||
@@ -35,6 +35,24 @@ describe('strings', () => {
|
||||
}));
|
||||
})
|
||||
});
|
||||
describe('afterFirst', () => {
|
||||
it('removes everything before the first needle when possible', () => {
|
||||
expect(afterFirst(' ', 'foo bar bat')).toBe('bar bat');
|
||||
})
|
||||
|
||||
it('removes nothing when needle is not present', () => {
|
||||
expect(afterFirst('&', 'foo bar bat')).toBe('foo bar bat');
|
||||
})
|
||||
})
|
||||
describe('afterLast', () => {
|
||||
it('removes everything after the last needle when possible', () => {
|
||||
expect(afterLast(' ', 'foo bar bat')).toBe('bat');
|
||||
})
|
||||
|
||||
it('removes nothing when needle is not present', () => {
|
||||
expect(afterLast('&', 'foo bar bat')).toBe('foo bar bat');
|
||||
})
|
||||
})
|
||||
// describe('afterFirstWord', () => {
|
||||
// it('removes the first word', () => {
|
||||
// fc.assert(fc.property(fc.string(), (str) => {
|
||||
@@ -49,37 +67,4 @@ describe('strings', () => {
|
||||
// }));
|
||||
// });
|
||||
// });
|
||||
describe('ltrim', () => {
|
||||
it('returns input when cutset is empty', () => {
|
||||
expect(ltrim('', 'foo')).toBe('foo');
|
||||
});
|
||||
it('returns empty when input is empty', () => {
|
||||
expect(ltrim('foo', '')).toBe('');
|
||||
});
|
||||
it('can be curried', () => {
|
||||
expect(ltrim('foo', '')).toBe(ltrim('foo')(''));
|
||||
expect(ltrim(' ', 'foo')).toBe(ltrim(' ')('foo'));
|
||||
});
|
||||
it('trims cutset', () => {
|
||||
fc.assert(fc.property(fc.string(), fc.string(), (cutset, str) => {
|
||||
const got = ltrim(cutset, str);
|
||||
|
||||
if (cutset === '') {
|
||||
expect(got).toBe(str);
|
||||
} else if (str === '') {
|
||||
expect(got).toBe('');
|
||||
} else {
|
||||
if (got === '') {
|
||||
for (const char of str) {
|
||||
expect(cutset).contains(char);
|
||||
}
|
||||
}
|
||||
|
||||
for (const char of cutset) {
|
||||
expect(startsWith(char, got)).toBe(false);
|
||||
}
|
||||
}
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user