From f46b68c709b16fb67ea6df56271276f31b05a7fd Mon Sep 17 00:00:00 2001 From: Spencer Brower Date: Thu, 15 Jun 2023 13:43:40 -0400 Subject: [PATCH] fix: re-built code. --- docs/modules.md | 4 ++-- flake.nix | 1 + lib/index.d.ts | 3 +++ lib/index.js | 42 +++++++++++------------------------------- lib/shorten.d.ts | 3 +++ lib/shorten.js | 14 ++++++++++++++ lib/utils.d.ts | 3 ++- lib/utils.js | 6 +++++- 8 files changed, 41 insertions(+), 35 deletions(-) create mode 100644 lib/shorten.d.ts create mode 100644 lib/shorten.js diff --git a/docs/modules.md b/docs/modules.md index e694380..97f6e8e 100644 --- a/docs/modules.md +++ b/docs/modules.md @@ -81,7 +81,7 @@ ___ #### Defined in -[src/index.ts:34](https://github.com/sbrow/strings/blob/7b676b7/src/index.ts#L34) +[src/index.ts:38](https://github.com/sbrow/strings/blob/559217a/src/index.ts#L38) ___ @@ -299,7 +299,7 @@ ___ #### Defined in -[src/index.ts:35](https://github.com/sbrow/strings/blob/7b676b7/src/index.ts#L35) +[src/index.ts:39](https://github.com/sbrow/strings/blob/559217a/src/index.ts#L39) ___ diff --git a/flake.nix b/flake.nix index a39fdce..f8a85ce 100644 --- a/flake.nix +++ b/flake.nix @@ -14,6 +14,7 @@ in { devShells.x86_64-linux.default = pkgs.mkShell { buildInputs = with pkgs; [ + nodejs yarn ]; }; diff --git a/lib/index.d.ts b/lib/index.d.ts index 11517ea..24c0f00 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,3 +1,4 @@ +export * from "./shorten"; 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>; @@ -5,3 +6,5 @@ export declare const afterFirst: import("ts-toolbelt/out/Function/Curry").Curry< export declare const afterLast: import("ts-toolbelt/out/Function/Curry").Curry<(separator: string, str: string) => string>; export declare const beforeFirst: import("ts-toolbelt/out/Function/Curry").Curry<(separator: string, str: string) => string>; export declare const beforeFirstWord: import("ts-toolbelt/out/Function/Curry").Curry<(str: string) => string>; +export declare const afterFirstWord: (str: string) => string; +export declare const removeFirstWord: (str: string) => string; diff --git a/lib/index.js b/lib/index.js index 768f603..bc8d3a2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -14,8 +14,9 @@ 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.beforeFirstWord = exports.beforeFirst = exports.afterLast = exports.afterFirst = exports.endsWith = exports.startsWith = void 0; +exports.removeFirstWord = exports.afterFirstWord = exports.beforeFirstWord = exports.beforeFirst = exports.afterLast = exports.afterFirst = exports.endsWith = exports.startsWith = void 0; const utils_1 = require("./utils"); +__exportStar(require("./shorten"), exports); __exportStar(require("./trim"), exports); function escapeRegExp(str) { return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string @@ -24,35 +25,14 @@ exports.startsWith = (0, utils_1.curry)((needle, haystack) => haystack.indexOf(n exports.endsWith = (0, utils_1.curry)((needle, haystack) => new RegExp(`${escapeRegExp(needle)}$`).test(haystack)); 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)); -exports.beforeFirst = (0, utils_1.curry)((separator, str) => str.substring(0, str.indexOf(separator))); +exports.beforeFirst = (0, utils_1.curry)((separator, str) => { + const index = str.indexOf(separator); + return index === -1 + ? str + : str.substring(0, index); +}); // @todo Test exports.beforeFirstWord = (0, exports.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), -// ); +// @todo Test +exports.afterFirstWord = (0, exports.afterFirst)(" "); +exports.removeFirstWord = exports.afterFirstWord; diff --git a/lib/shorten.d.ts b/lib/shorten.d.ts new file mode 100644 index 0000000..cf7a784 --- /dev/null +++ b/lib/shorten.d.ts @@ -0,0 +1,3 @@ +export declare const shorterThan: import("ts-toolbelt/out/Function/Curry").Curry<(maxChars: number, str: string) => boolean>; +export declare const shorten: import("ts-toolbelt/out/Function/Curry").Curry<(maxChars: number, strategy: any) => (init: unknown) => unknown>; +export declare const removeWordsFromBeginning: import("ts-toolbelt/out/Function/Curry").Curry<(maxChars: number, str: string) => unknown>; diff --git a/lib/shorten.js b/lib/shorten.js new file mode 100644 index 0000000..7e4a26b --- /dev/null +++ b/lib/shorten.js @@ -0,0 +1,14 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.removeWordsFromBeginning = exports.shorten = exports.shorterThan = void 0; +const index_1 = require("./index"); +const utils_1 = require("./utils"); +exports.shorterThan = (0, utils_1.curry)((maxChars, str) => str.length <= maxChars); +exports.shorten = (0, utils_1.curry)((maxChars, strategy) => (0, utils_1.until)((0, exports.shorterThan)(maxChars), strategy)); +exports.removeWordsFromBeginning = (0, utils_1.curry)((maxChars, str) => (0, exports.shorten)(maxChars, index_1.removeFirstWord)(str)); +/* +export const removeWordsFromBeginning = uncurryN( + 2, + (maxChars: number) => shorten(maxChars, removeFirstWord), + ); +*/ diff --git a/lib/utils.d.ts b/lib/utils.d.ts index 28cf703..a78ef7e 100644 --- a/lib/utils.d.ts +++ b/lib/utils.d.ts @@ -1,5 +1,6 @@ -import type { curry as _curry, when as _when } from "ramda"; +import type { curry as _curry, until as _until, 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; +export declare const until: typeof _until; diff --git a/lib/utils.js b/lib/utils.js index 10f8191..343f7ac 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.when = exports.tail = exports.isEmpty = exports.curry = void 0; +exports.until = exports.when = exports.tail = exports.isEmpty = exports.curry = void 0; exports.curry = function (fn) { return (...args) => { if (args.length >= fn.length) { @@ -17,3 +17,7 @@ exports.tail = tail; exports.when = (0, exports.curry)((predicate, whenTrueFn, arg) => { return predicate(arg) ? whenTrueFn(arg) : arg; }); +exports.until = (0, exports.curry)((predicate, fn, arg) => { + const loop = (a) => predicate(a) ? a : loop(fn(a)); + return loop(arg); +});