fix: Updated some things.

This commit is contained in:
Spencer Brower
2023-05-25 15:23:03 -04:00
parent 3a791a2fd5
commit 840a73a4e3
6 changed files with 145 additions and 30 deletions

View File

@@ -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', () => {

View File

@@ -1,39 +1,25 @@
// import { compose, curry, isEmpty, uncurryN, until, when, } from 'ramda'
import { curry, isEmpty, tail, when } from "./utils";
// function escapeRegExp(string) {
// return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
// }
export const startsWith = curry((needle, haystack) => haystack.indexOf(needle) === 0);
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 ltrim = (cutset: string, str = undefined) => {
// if (str === undefined) {
// return str => ltrim(cutset, str);
// }
// if (isEmpty(cutset) || isEmpty(str)) {
// return str
// }
// // const _trim = x => ltrim(cutset)(tail(x));
// const _trim = compose(ltrim(cutset), tail)
// return when((x = '') => cutset.includes(x.charAt(0)), _trim)(str);
// }
/** String -> String -> String */
/**
* Trims characters from the left side of a string.
*/
export const ltrim = curry((cutset: string, str: string) => {
const _trim = x => ltrim(cutset)(tail(x));
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
: when((x = '') => cutset.includes(x.charAt(0)), _trim, str);
: trimmed()
});
// export const afterFirst = curry(

View File

@@ -1,20 +1,20 @@
type Predicate<T> = (arg: T) => boolean;
type Fn<T, U = any> = (arg: T) => U;
import type { curry as _curry, when as _when } from 'ramda';
export function curry (fn) {
export const curry = function(fn) {
return (...args) => {
if (args.length >= fn.length) {
return fn(...args);
}
// @ts-ignore
return (...more) => curry(fn)(...args, ...more);
}
}
} as typeof _curry;
export const isEmpty = (str: string) => str == null || str === '';
export const tail = (str: string) => str.substring(1);
export function when<T>(predicate: Predicate<T>, whenTrueFn: Fn<T>, arg: T): any {
export const when = curry((predicate, whenTrueFn, arg) => {
return predicate(arg) ? whenTrueFn(arg) : arg;
}
}) as typeof _when;