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:
7
lib/index.d.ts
vendored
7
lib/index.d.ts
vendored
@@ -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>;
|
||||
|
||||
38
lib/index.js
38
lib/index.js
@@ -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
6
lib/ltrim.d.ts
vendored
Normal 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
25
lib/ltrim.js
Normal 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
6
lib/trim.d.ts
vendored
Normal 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
25
lib/trim.js
Normal 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)));
|
||||
Reference in New Issue
Block a user