mirror of
https://github.com/sbrow/strings.git
synced 2025-12-29 15:17:38 -05:00
build: Prepared package for npm publishing.
This commit is contained in:
6
lib/index.d.ts
vendored
Normal file
6
lib/index.d.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
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>;
|
||||
67
lib/index.js
Normal file
67
lib/index.js
Normal file
@@ -0,0 +1,67 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ltrim = exports.endsWith = exports.startsWith = void 0;
|
||||
const utils_1 = require("./utils");
|
||||
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),
|
||||
// );
|
||||
// export const beforeFirst = curry(
|
||||
// /**
|
||||
// *
|
||||
// * @param {String} separator
|
||||
// * @param {String} str
|
||||
// * @returns {String}
|
||||
// */
|
||||
// (separator, str) => str.substring(0, str.indexOf(separator)),
|
||||
// );
|
||||
// export const beforeFirstWord = beforeFirst(' ');
|
||||
// /**
|
||||
// * @param {String} str
|
||||
// * @return {String}
|
||||
// */
|
||||
// export function afterFirstWord(str) {
|
||||
// return afterFirst(' ', str);
|
||||
// }
|
||||
// export const removeFirstWord = afterFirstWord;
|
||||
// /**
|
||||
// * @param {Number} maxChars
|
||||
// * @param {String} str
|
||||
// * @return {Boolean} False if str is longer than maxChars characters.
|
||||
// */
|
||||
// const shorterThan = curry((maxChars, str) => str.length <= maxChars);
|
||||
// /**
|
||||
// * @param {Number} maxChars The maximum length of the desired output string.
|
||||
// * @param strategy a function that accepts a string and returns a shorter string.
|
||||
// * @return {String} The input string, shortened to maxChars by strategy.
|
||||
// */
|
||||
// const shortenString = (maxChars, strategy) => until(shorterThan(maxChars), strategy);
|
||||
// /**
|
||||
// * @param {Number} maxChars
|
||||
// * @param {String} str The string to remove words from.
|
||||
// * @return {String} the shortened string.
|
||||
// */
|
||||
// export const removeWordsFromStartOfString = uncurryN(
|
||||
// 2,
|
||||
// (maxChars) => shortenString(maxChars, removeFirstWord),
|
||||
// );
|
||||
5
lib/utils.d.ts
vendored
Normal file
5
lib/utils.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { curry as _curry, when as _when } from 'ramda';
|
||||
export declare const curry: typeof _curry;
|
||||
export declare const isEmpty: (str: string) => boolean;
|
||||
export declare const tail: (str: string) => string;
|
||||
export declare const when: typeof _when;
|
||||
19
lib/utils.js
Normal file
19
lib/utils.js
Normal file
@@ -0,0 +1,19 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.when = exports.tail = exports.isEmpty = exports.curry = void 0;
|
||||
exports.curry = function (fn) {
|
||||
return (...args) => {
|
||||
if (args.length >= fn.length) {
|
||||
return fn(...args);
|
||||
}
|
||||
// @ts-ignore
|
||||
return (...more) => (0, exports.curry)(fn)(...args, ...more);
|
||||
};
|
||||
};
|
||||
const isEmpty = (str) => str == null || str === '';
|
||||
exports.isEmpty = isEmpty;
|
||||
const tail = (str) => str.substring(1);
|
||||
exports.tail = tail;
|
||||
exports.when = (0, exports.curry)((predicate, whenTrueFn, arg) => {
|
||||
return predicate(arg) ? whenTrueFn(arg) : arg;
|
||||
});
|
||||
Reference in New Issue
Block a user