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);
|
||||
}
|
||||
}
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
31
src/index.ts
31
src/index.ts
@@ -1,4 +1,6 @@
|
||||
import { curry, isEmpty, tail, when } from "./utils";
|
||||
import { curry } from "./utils";
|
||||
|
||||
export * from './trim';
|
||||
|
||||
function escapeRegExp(str: string) {
|
||||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
||||
@@ -8,28 +10,13 @@ export const startsWith = curry((needle: string, haystack: string) => haystack.i
|
||||
|
||||
export const endsWith = curry((needle: string, haystack: string) => (new RegExp(`${escapeRegExp(needle)}$`)).test(haystack));
|
||||
|
||||
/**
|
||||
* Trims characters from the left side of a string.
|
||||
*/
|
||||
export const ltrim = curry((cutset: string, str: string) => {
|
||||
const _trim = (x: string) => ltrim(cutset)(tail(x));
|
||||
// const _trim = compose(ltrim(cutset), tail);
|
||||
export const afterFirst = curry(
|
||||
(separator: string, str: string) => str.substring(str.indexOf(separator) + 1, str.length),
|
||||
);
|
||||
|
||||
const trimmed: () => string = () => when((x: string = '') => cutset.includes(x.charAt(0)), _trim)(str);
|
||||
|
||||
return isEmpty(cutset) || isEmpty(str)
|
||||
? str
|
||||
: trimmed()
|
||||
});
|
||||
|
||||
// export const afterFirst = curry(
|
||||
// /**
|
||||
// * @param {String} separator
|
||||
// * @param {String} str
|
||||
// * @return {String}
|
||||
// */
|
||||
// (separator, str) => str.substring(str.indexOf(separator) + 1, str.length),
|
||||
// );
|
||||
export const afterLast = curry(
|
||||
(separator: string, str: string) => str.substring(str.lastIndexOf(separator) + 1, str.length),
|
||||
);
|
||||
|
||||
// export const beforeFirst = curry(
|
||||
// /**
|
||||
|
||||
83
src/trim.spec.ts
Normal file
83
src/trim.spec.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import fc from "fast-check";
|
||||
import { endsWith, startsWith } from "ramda";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { ltrim, rtrim, trim } from "./trim";
|
||||
|
||||
describe('trim', () => {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
describe('rtrim', () => {
|
||||
it('returns input when cutset is empty', () => {
|
||||
expect(rtrim('', 'foo')).toBe('foo');
|
||||
});
|
||||
it('returns empty when input is empty', () => {
|
||||
expect(rtrim('foo', '')).toBe('');
|
||||
});
|
||||
it('can be curried', () => {
|
||||
expect(rtrim('foo', '')).toBe(rtrim('foo')(''));
|
||||
expect(rtrim(' ', ' foo')).toBe(rtrim(' ')(' foo'));
|
||||
});
|
||||
it('trims cutset', () => {
|
||||
fc.assert(fc.property(fc.string(), fc.string(), (cutset, str) => {
|
||||
const got = rtrim(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(endsWith(char, got)).toBe(false);
|
||||
}
|
||||
}
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
describe('trim', () => {
|
||||
it("composes 'ltrim' and 'rtrim'", function() {
|
||||
fc.assert(fc.property(fc.string(), fc.string(), (cutset, str) => {
|
||||
expect(trim(cutset, str)).toBe(ltrim(cutset, rtrim(cutset, str)));
|
||||
}));
|
||||
});
|
||||
})
|
||||
});
|
||||
28
src/trim.ts
Normal file
28
src/trim.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { curry, isEmpty, tail, when } from "./utils";
|
||||
|
||||
/**
|
||||
* Trims characters from the left side of a string.
|
||||
*/
|
||||
|
||||
export const ltrim = curry((cutset: string, str: string) => {
|
||||
const _trim = (x: string) => ltrim(cutset)(tail(x));
|
||||
// const _trim = compose(ltrim(cutset), tail);
|
||||
const trimmed: () => string = () => when((x: string = '') => cutset.includes(x.charAt(0)), _trim)(str);
|
||||
|
||||
return isEmpty(cutset) || isEmpty(str)
|
||||
? str
|
||||
: trimmed();
|
||||
});
|
||||
|
||||
export const rtrim = curry((cutset: string, str: string) => {
|
||||
const _trim = (x: string) => rtrim(cutset)(x.substring(0, x.length - 1));
|
||||
// const _trim = compose(rtrim(cutset), init);
|
||||
const trimmed: () => string = () => when((x: string = '') => cutset.includes(x.charAt(x.length - 1)), _trim)(str);
|
||||
|
||||
return isEmpty(cutset) || isEmpty(str)
|
||||
? str
|
||||
: trimmed();
|
||||
});
|
||||
// const trim = curry((cutset: string, str: string) => compose(ltrim(cutset), rtrim(cutset))(str));
|
||||
|
||||
export const trim = curry((cutset: string, str: string) => ltrim(cutset)(rtrim(cutset, str)));
|
||||
Reference in New Issue
Block a user