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

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 endsWith: import("ts-toolbelt/out/Function/Curry").Curry<(needle: string, haystack: string) => boolean>;
/**
* 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 afterFirst: import("ts-toolbelt/out/Function/Curry").Curry<(separator: string, str: string) => string>;
export declare const afterLast: import("ts-toolbelt/out/Function/Curry").Curry<(separator: string, str: string) => string>;

View File

@@ -1,31 +1,29 @@
"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 });
exports.ltrim = exports.endsWith = exports.startsWith = void 0;
exports.afterLast = exports.afterFirst = exports.endsWith = exports.startsWith = void 0;
const utils_1 = require("./utils");
__exportStar(require("./trim"), exports);
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
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));
/**
* 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();
});
// export const afterFirst = curry(
// /**
// * @param {String} separator
// * @param {String} str
// * @return {String}
// */
// (separator, str) => str.substring(str.indexOf(separator) + 1, str.length),
// );
exports.afterFirst = (0, utils_1.curry)((separator, str) => str.substring(str.indexOf(separator) + 1, str.length));
exports.afterLast = (0, utils_1.curry)((separator, str) => str.substring(str.lastIndexOf(separator) + 1, str.length));
// 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)));