diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..7617f84 --- /dev/null +++ b/lib/index.d.ts @@ -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>; diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..cfee34a --- /dev/null +++ b/lib/index.js @@ -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), +// ); diff --git a/lib/utils.d.ts b/lib/utils.d.ts new file mode 100644 index 0000000..1058174 --- /dev/null +++ b/lib/utils.d.ts @@ -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; diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..fe47f58 --- /dev/null +++ b/lib/utils.js @@ -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; +}); diff --git a/package.json b/package.json index 9fa8a4a..ec76719 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,13 @@ "name": "@sbrow/strings", "version": "0.1.0", "description": "Library for string manipulation", - "main": "src/index.ts", + "main": "./lib/index.js", + "types": "./lib/index.d.ts", "repository": "https://github.com/sbrow/strings", "author": "Spencer Brower ", "license": "MIT", "scripts": { + "build": "tsc", "test": "vitest --run" }, "devDependencies": { diff --git a/tsconfig.json b/tsconfig.json index a990343..11c26cb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -49,13 +49,13 @@ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ /* Emit */ - // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ // "declarationMap": true, /* Create sourcemaps for d.ts files. */ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ - // "outDir": "./", /* Specify an output folder for all emitted files. */ + "outDir": "./lib", /* Specify an output folder for all emitted files. */ // "removeComments": true, /* Disable emitting comments. */ // "noEmit": true, /* Disable emitting files from a compilation. */ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ @@ -104,6 +104,7 @@ /* Completeness */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ - "skipLibCheck": true /* Skip type checking all .d.ts files. */ - } + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + }, + "exclude": ["src/**/*.spec.ts", "lib"] }