feat: Added more functions.

This commit is contained in:
Spencer Brower
2023-05-25 16:41:23 -04:00
parent fabd856ddf
commit a07d4188af
13 changed files with 401 additions and 80 deletions

View File

@@ -1,3 +1,4 @@
.envrc .envrc
src src
tsconfig.json
flake.* flake.*

View File

@@ -1 +1,7 @@
@sbrow/strings / [Exports](modules.md) @sbrow/strings / [Exports](modules.md)
# Strings
A small, simple, functional, and dependency-free library for JavaScript string manipulation.
You can view the docs [here](./docs/modules.md)

View File

@@ -6,12 +6,100 @@
### Functions ### Functions
- [afterFirst](modules.md#afterfirst)
- [afterLast](modules.md#afterlast)
- [endsWith](modules.md#endswith) - [endsWith](modules.md#endswith)
- [ltrim](modules.md#ltrim) - [ltrim](modules.md#ltrim)
- [rtrim](modules.md#rtrim)
- [startsWith](modules.md#startswith) - [startsWith](modules.md#startswith)
- [trim](modules.md#trim)
## Functions ## Functions
### afterFirst
**afterFirst**<`P`, `G`, `R`\>(`...p`): `RequiredKeys`<`ObjectOf`<`G`\>\> extends `never` ? `R` : `Curry`<(...`p`: `G`) => `R`\>
Curry a [[Function]]
**`Example`**
```ts
import {F} from 'ts-toolbelt'
/// If you are looking for creating types for `curry`
/// It handles placeholders and variable arguments
declare function curry<Fn extends F.Function>(fn: Fn): F.Curry<Fn>
```
#### Type parameters
| Name | Type |
| :------ | :------ |
| `P` | extends [separator: string \| typeof \_, str: string \| typeof \_] |
| `G` | extends readonly `any`[] = `GapsOf`<`P`, [separator: string, str: string]\> |
| `R` | extends `unknown` = `string` |
#### Parameters
| Name | Type |
| :------ | :------ |
| `...p` | `P` \| [separator: string \| typeof \_, str: string \| typeof \_] |
#### Returns
`RequiredKeys`<`ObjectOf`<`G`\>\> extends `never` ? `R` : `Curry`<(...`p`: `G`) => `R`\>
[[Function]]
#### Defined in
node_modules/ts-toolbelt/out/Function/Curry.d.ts:77
___
### afterLast
**afterLast**<`P`, `G`, `R`\>(`...p`): `RequiredKeys`<`ObjectOf`<`G`\>\> extends `never` ? `R` : `Curry`<(...`p`: `G`) => `R`\>
Curry a [[Function]]
**`Example`**
```ts
import {F} from 'ts-toolbelt'
/// If you are looking for creating types for `curry`
/// It handles placeholders and variable arguments
declare function curry<Fn extends F.Function>(fn: Fn): F.Curry<Fn>
```
#### Type parameters
| Name | Type |
| :------ | :------ |
| `P` | extends [separator: string \| typeof \_, str: string \| typeof \_] |
| `G` | extends readonly `any`[] = `GapsOf`<`P`, [separator: string, str: string]\> |
| `R` | extends `unknown` = `string` |
#### Parameters
| Name | Type |
| :------ | :------ |
| `...p` | `P` \| [separator: string \| typeof \_, str: string \| typeof \_] |
#### Returns
`RequiredKeys`<`ObjectOf`<`G`\>\> extends `never` ? `R` : `Curry`<(...`p`: `G`) => `R`\>
[[Function]]
#### Defined in
node_modules/ts-toolbelt/out/Function/Curry.d.ts:77
___
### endsWith ### endsWith
**endsWith**<`P`, `G`, `R`\>(`...p`): `RequiredKeys`<`ObjectOf`<`G`\>\> extends `never` ? `R` : `Curry`<(...`p`: `G`) => `R`\> **endsWith**<`P`, `G`, `R`\>(`...p`): `RequiredKeys`<`ObjectOf`<`G`\>\> extends `never` ? `R` : `Curry`<(...`p`: `G`) => `R`\>
@@ -84,6 +172,48 @@ node_modules/ts-toolbelt/out/Function/Curry.d.ts:77
___ ___
### rtrim
**rtrim**<`P`, `G`, `R`\>(`...p`): `RequiredKeys`<`ObjectOf`<`G`\>\> extends `never` ? `R` : `Curry`<(...`p`: `G`) => `R`\>
Curry a [[Function]]
**`Example`**
```ts
import {F} from 'ts-toolbelt'
/// If you are looking for creating types for `curry`
/// It handles placeholders and variable arguments
declare function curry<Fn extends F.Function>(fn: Fn): F.Curry<Fn>
```
#### Type parameters
| Name | Type |
| :------ | :------ |
| `P` | extends [cutset: string \| typeof \_, str: string \| typeof \_] |
| `G` | extends readonly `any`[] = `GapsOf`<`P`, [cutset: string, str: string]\> |
| `R` | extends `unknown` = `string` |
#### Parameters
| Name | Type |
| :------ | :------ |
| `...p` | `P` \| [cutset: string \| typeof \_, str: string \| typeof \_] |
#### Returns
`RequiredKeys`<`ObjectOf`<`G`\>\> extends `never` ? `R` : `Curry`<(...`p`: `G`) => `R`\>
[[Function]]
#### Defined in
node_modules/ts-toolbelt/out/Function/Curry.d.ts:77
___
### startsWith ### startsWith
**startsWith**<`P`, `G`, `R`\>(`...p`): `RequiredKeys`<`ObjectOf`<`G`\>\> extends `never` ? `R` : `Curry`<(...`p`: `G`) => `R`\> **startsWith**<`P`, `G`, `R`\>(`...p`): `RequiredKeys`<`ObjectOf`<`G`\>\> extends `never` ? `R` : `Curry`<(...`p`: `G`) => `R`\>
@@ -123,3 +253,45 @@ declare function curry<Fn extends F.Function>(fn: Fn): F.Curry<Fn>
#### Defined in #### Defined in
node_modules/ts-toolbelt/out/Function/Curry.d.ts:77 node_modules/ts-toolbelt/out/Function/Curry.d.ts:77
___
### trim
**trim**<`P`, `G`, `R`\>(`...p`): `RequiredKeys`<`ObjectOf`<`G`\>\> extends `never` ? `R` : `Curry`<(...`p`: `G`) => `R`\>
Curry a [[Function]]
**`Example`**
```ts
import {F} from 'ts-toolbelt'
/// If you are looking for creating types for `curry`
/// It handles placeholders and variable arguments
declare function curry<Fn extends F.Function>(fn: Fn): F.Curry<Fn>
```
#### Type parameters
| Name | Type |
| :------ | :------ |
| `P` | extends [cutset: string \| typeof \_, str: string \| typeof \_] |
| `G` | extends readonly `any`[] = `GapsOf`<`P`, [cutset: string, str: string]\> |
| `R` | extends `unknown` = `any` |
#### Parameters
| Name | Type |
| :------ | :------ |
| `...p` | `P` \| [cutset: string \| typeof \_, str: string \| typeof \_] |
#### Returns
`RequiredKeys`<`ObjectOf`<`G`\>\> extends `never` ? `R` : `Curry`<(...`p`: `G`) => `R`\>
[[Function]]
#### Defined in
node_modules/ts-toolbelt/out/Function/Curry.d.ts:77

7
lib/index.d.ts vendored
View File

@@ -1,6 +1,5 @@
export * from './trim';
export declare const startsWith: import("ts-toolbelt/out/Function/Curry").Curry<(needle: string, haystack: string) => boolean>; 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 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>;
* Trims characters from the left side of a string. export declare const afterLast: import("ts-toolbelt/out/Function/Curry").Curry<(separator: string, str: string) => string>;
*/
export declare const ltrim: import("ts-toolbelt/out/Function/Curry").Curry<(cutset: string, str: string) => string>;

View File

@@ -1,31 +1,29 @@
"use strict"; "use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.ltrim = exports.endsWith = exports.startsWith = void 0; exports.afterLast = exports.afterFirst = exports.endsWith = exports.startsWith = void 0;
const utils_1 = require("./utils"); const utils_1 = require("./utils");
__exportStar(require("./trim"), exports);
function escapeRegExp(str) { function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
} }
exports.startsWith = (0, utils_1.curry)((needle, haystack) => haystack.indexOf(needle) === 0); exports.startsWith = (0, utils_1.curry)((needle, haystack) => haystack.indexOf(needle) === 0);
exports.endsWith = (0, utils_1.curry)((needle, haystack) => (new RegExp(`${escapeRegExp(needle)}$`)).test(haystack)); exports.endsWith = (0, utils_1.curry)((needle, haystack) => (new RegExp(`${escapeRegExp(needle)}$`)).test(haystack));
/** exports.afterFirst = (0, utils_1.curry)((separator, str) => str.substring(str.indexOf(separator) + 1, str.length));
* Trims characters from the left side of a string. exports.afterLast = (0, utils_1.curry)((separator, str) => str.substring(str.lastIndexOf(separator) + 1, str.length));
*/
exports.ltrim = (0, utils_1.curry)((cutset, str) => {
const _trim = (x) => (0, exports.ltrim)(cutset)((0, utils_1.tail)(x));
// const _trim = compose(ltrim(cutset), tail);
const trimmed = () => (0, utils_1.when)((x = '') => cutset.includes(x.charAt(0)), _trim)(str);
return (0, utils_1.isEmpty)(cutset) || (0, utils_1.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 beforeFirst = curry( // export const beforeFirst = curry(
// /** // /**
// * // *

6
lib/ltrim.d.ts vendored Normal file
View File

@@ -0,0 +1,6 @@
/**
* Trims characters from the left side of a string.
*/
export declare const ltrim: import("ts-toolbelt/out/Function/Curry").Curry<(cutset: string, str: string) => string>;
export declare const rtrim: import("ts-toolbelt/out/Function/Curry").Curry<(cutset: string, str: string) => string>;
export declare const trim: import("ts-toolbelt/out/Function/Curry").Curry<(cutset: string, str: string) => any>;

25
lib/ltrim.js Normal file
View File

@@ -0,0 +1,25 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.trim = exports.rtrim = exports.ltrim = void 0;
const utils_1 = require("./utils");
/**
* Trims characters from the left side of a string.
*/
exports.ltrim = (0, utils_1.curry)((cutset, str) => {
const _trim = (x) => (0, exports.ltrim)(cutset)((0, utils_1.tail)(x));
// const _trim = compose(ltrim(cutset), tail);
const trimmed = () => (0, utils_1.when)((x = '') => cutset.includes(x.charAt(0)), _trim)(str);
return (0, utils_1.isEmpty)(cutset) || (0, utils_1.isEmpty)(str)
? str
: trimmed();
});
exports.rtrim = (0, utils_1.curry)((cutset, str) => {
const _trim = (x) => (0, exports.rtrim)(cutset)(x.substring(0, x.length - 1));
// const _trim = compose(rtrim(cutset), init);
const trimmed = () => (0, utils_1.when)((x = '') => cutset.includes(x.charAt(x.length - 1)), _trim)(str);
return (0, utils_1.isEmpty)(cutset) || (0, utils_1.isEmpty)(str)
? str
: trimmed();
});
// const trim = curry((cutset: string, str: string) => compose(ltrim(cutset), rtrim(cutset))(str));
exports.trim = (0, utils_1.curry)((cutset, str) => (0, exports.ltrim)(cutset)((0, exports.rtrim)(cutset, str)));

6
lib/trim.d.ts vendored Normal file
View File

@@ -0,0 +1,6 @@
/**
* Trims characters from the left side of a string.
*/
export declare const ltrim: import("ts-toolbelt/out/Function/Curry").Curry<(cutset: string, str: string) => string>;
export declare const rtrim: import("ts-toolbelt/out/Function/Curry").Curry<(cutset: string, str: string) => string>;
export declare const trim: import("ts-toolbelt/out/Function/Curry").Curry<(cutset: string, str: string) => any>;

25
lib/trim.js Normal file
View File

@@ -0,0 +1,25 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.trim = exports.rtrim = exports.ltrim = void 0;
const utils_1 = require("./utils");
/**
* Trims characters from the left side of a string.
*/
exports.ltrim = (0, utils_1.curry)((cutset, str) => {
const _trim = (x) => (0, exports.ltrim)(cutset)((0, utils_1.tail)(x));
// const _trim = compose(ltrim(cutset), tail);
const trimmed = () => (0, utils_1.when)((x = '') => cutset.includes(x.charAt(0)), _trim)(str);
return (0, utils_1.isEmpty)(cutset) || (0, utils_1.isEmpty)(str)
? str
: trimmed();
});
exports.rtrim = (0, utils_1.curry)((cutset, str) => {
const _trim = (x) => (0, exports.rtrim)(cutset)(x.substring(0, x.length - 1));
// const _trim = compose(rtrim(cutset), init);
const trimmed = () => (0, utils_1.when)((x = '') => cutset.includes(x.charAt(x.length - 1)), _trim)(str);
return (0, utils_1.isEmpty)(cutset) || (0, utils_1.isEmpty)(str)
? str
: trimmed();
});
// const trim = curry((cutset: string, str: string) => compose(ltrim(cutset), rtrim(cutset))(str));
exports.trim = (0, utils_1.curry)((cutset, str) => (0, exports.ltrim)(cutset)((0, exports.rtrim)(cutset, str)));

View File

@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest'; import { describe, expect, it } from 'vitest';
import fc from 'fast-check'; import fc from 'fast-check';
import { import {
/*afterFirstWord, beforeFirstWord, */ endsWith, ltrim, startsWith, /*afterFirstWord, beforeFirstWord, */ afterFirst, afterLast, endsWith, ltrim, rtrim, trim, startsWith,
} from './index'; } from './index';
describe('strings', () => { 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', () => { // describe('afterFirstWord', () => {
// it('removes the first word', () => { // it('removes the first word', () => {
// fc.assert(fc.property(fc.string(), (str) => { // 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);
}
}
}));
});
});
}); });

View File

@@ -1,4 +1,6 @@
import { curry, isEmpty, tail, when } from "./utils"; import { curry } from "./utils";
export * from './trim';
function escapeRegExp(str: string) { function escapeRegExp(str: string) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched 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)); export const endsWith = curry((needle: string, haystack: string) => (new RegExp(`${escapeRegExp(needle)}$`)).test(haystack));
/** export const afterFirst = curry(
* Trims characters from the left side of a string. (separator: string, str: string) => str.substring(str.indexOf(separator) + 1, str.length),
*/ );
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); export const afterLast = curry(
(separator: string, str: string) => str.substring(str.lastIndexOf(separator) + 1, str.length),
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 beforeFirst = curry( // export const beforeFirst = curry(
// /** // /**

83
src/trim.spec.ts Normal file
View 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
View 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)));