From e7e65f2d5b052851fed99ed27cb824605ea4082a Mon Sep 17 00:00:00 2001
From: Allen Jiji <47912595+allenjiji@users.noreply.github.com>
Date: Fri, 8 Dec 2023 17:11:20 +0530
Subject: [PATCH] Added the `hyphenate` function (#28)
* Added the hyphenate function
* Made minor changes
* Added tests for _truncate
---
README.md | 1 +
docs/pure/README.md | 1 +
docs/pure/strings.md | 49 ++++++++++++++++++++++++++++++----------
src/strings.js | 32 ++++++++++++++++++++------
tests/string.spec.js | 20 ++++++++++++++++
typeTemplates/index.d.ts | 5 +++-
6 files changed, 88 insertions(+), 20 deletions(-)
diff --git a/README.md b/README.md
index 0d4e162..fd87a35 100644
--- a/README.md
+++ b/README.md
@@ -69,6 +69,7 @@ Name
- [snakeToCamelCase](./docs/pure/strings.md#snaketocamelcase)
- [camelToSnakeCase](./docs/pure/strings.md#cameltosnakecase)
- [capitalize](./docs/pure/strings.md#capitalize)
+- [hyphenate](./docs/pure/strings.md#hyphenate)
- [truncate](./docs/pure/strings.md#truncate)
- [noop](./docs/pure/general.md#noop)
- [toLabelAndValue](./docs/pure/general.md#tolabelandvalue)
diff --git a/docs/pure/README.md b/docs/pure/README.md
index 66bd839..44e0aa5 100644
--- a/docs/pure/README.md
+++ b/docs/pure/README.md
@@ -96,6 +96,7 @@ import { slugify } from "@bigbinary/neeto-cist";
- [snakeToCamelCase](./strings.md#snaketocamelcase)
- [camelToSnakeCase](./strings.md#cameltosnakecase)
- [capitalize](./strings.md#capitalize)
+- [hyphenate](./strings.md#hyphenate)
- [truncate](./strings.md#truncate)
diff --git a/docs/pure/strings.md b/docs/pure/strings.md
index 6a0329c..ed9c76c 100644
--- a/docs/pure/strings.md
+++ b/docs/pure/strings.md
@@ -2,8 +2,7 @@
## slugify
-Curried: false
-Failsafe status: alternative available
+Curried: false Failsafe status: alternative available
Converts a given string to slug.
@@ -11,6 +10,7 @@ Converts a given string to slug.
(click for example)
### Arguments:
+
- `string`: The string to be converted.
### Usage:
@@ -27,8 +27,7 @@ slugify("my!@#$%^*(quiz"); // "myquiz"
## humanize
-Curried: false
-Failsafe status: alternative available
+Curried: false Failsafe status: alternative available
Converts common developer friendly string formats (camelCase, snake_case,
dash-case etc) to human readable string.
@@ -37,6 +36,7 @@ dash-case etc) to human readable string.
(click for example)
### Arguments:
+
- `string`: The string to be converted.
### Usage:
@@ -52,8 +52,7 @@ humanize("HelloUSA"); // "Hello usa"
## snakeToCamelCase
-Curried: false
-Failsafe status: alternative available
+Curried: false Failsafe status: alternative available
Converts snake_case string to camelCase.
@@ -61,6 +60,7 @@ Converts snake_case string to camelCase.
(click for example)
### Arguments:
+
- `string`: The string to be converted.
### Usage:
@@ -73,8 +73,7 @@ snakeToCamelCase("first_name"); // "firstName"
## camelToSnakeCase
-Curried: false
-Failsafe status: alternative available
+Curried: false Failsafe status: alternative available
Converts camelCase string to snake_case.
@@ -82,6 +81,7 @@ Converts camelCase string to snake_case.
(click for example)
### Arguments:
+
- `string`: The string to be converted.
### Usage:
@@ -94,8 +94,7 @@ camelToSnakeCase("firstName"); // "first_name"
## capitalize
-Curried: false
-Failsafe status: alternative available
+Curried: false Failsafe status: alternative available
Convert the first character of string to upper case.
@@ -103,6 +102,7 @@ Convert the first character of string to upper case.
(click for example)
### Arguments:
+
- `string`: The string to be converted.
### Usage:
@@ -115,10 +115,34 @@ capitalize("oLIVER"); // "OLIVER"
+## hyphenate
+
+Curried: false Failsafe status: alternative available
+
+The hyphenate function converts strings that contain underscores, spaces, and
+camelCase strings into hyphenated strings
+
+
+(click for example)
+
+### Arguments:
+
+- `string`: The string to be hyphenated.
+- `fallbackString`: value to be returned if string is empty
+
+### Usage:
+
+```js
+hyphenate("Hello World"); // "hello-world"
+hyphenate("hello_world"); // "hello-world"
+hyphenate("helloWorld"); // "hello-world"
+```
+
+
+
## truncate
-Curried: false
-Failsafe status: alternative available
+Curried: false Failsafe status: alternative available
Truncate the string with `...` if it is longer than specified string length.
@@ -126,6 +150,7 @@ Truncate the string with `...` if it is longer than specified string length.
(click for example)
### Arguments:
+
- `string`: The string to be truncated.
- `length`: The maximum allowed length of the string.
diff --git a/src/strings.js b/src/strings.js
index 3cccd41..e6e95d1 100644
--- a/src/strings.js
+++ b/src/strings.js
@@ -2,7 +2,7 @@ import { concat, isNil, slice } from "ramda";
import { nullSafe } from "./general";
-export const slugify = (string) =>
+export const slugify = string =>
string
.toString()
.toLowerCase()
@@ -13,7 +13,7 @@ export const slugify = (string) =>
.replace(/^-+/, "") // Trim - from start of text
.replace(/-+$/, ""); // Trim - from end of text
-export const humanize = (string) => {
+export const humanize = string => {
string = string
.replace(/[_-]+/g, " ")
.replace(/\s{2,}/g, " ")
@@ -26,15 +26,29 @@ export const humanize = (string) => {
return string;
};
-export const snakeToCamelCase = (string) =>
- string.replace(/(_\w)/g, (letter) => letter[1].toUpperCase());
+export const snakeToCamelCase = string =>
+ string.replace(/(_\w)/g, letter => letter[1].toUpperCase());
-export const camelToSnakeCase = (string) =>
- string.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
+export const camelToSnakeCase = string =>
+ string.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
-export const capitalize = (string) =>
+export const capitalize = string =>
string.charAt(0).toUpperCase() + string.slice(1);
+export const hyphenate = (string, fallbackString) => {
+ if (typeof string === "number") return String(string);
+
+ if (string && typeof string === "string" && string.replace) {
+ return string
+ .replace(/[\s_]/g, "-")
+ .replace(/([a-z])([A-Z])/g, "$1-$2")
+ .replace(/-+/g, "-")
+ .toLowerCase();
+ }
+
+ return fallbackString;
+};
+
export const truncate = (string, length) =>
string.length > length ? concat(slice(0, length, string), "...") : string;
@@ -43,5 +57,9 @@ export const _humanize = /*#__PURE__*/ nullSafe(humanize);
export const _snakeToCamelCase = /*#__PURE__*/ nullSafe(snakeToCamelCase);
export const _camelToSnakeCase = /*#__PURE__*/ nullSafe(camelToSnakeCase);
export const _capitalize = /*#__PURE__*/ nullSafe(capitalize);
+// eslint-disable-next-line @bigbinary/neeto/use-camel-case-or-pascal-case-for-function-names
+export const _hyphenate = (string, length) =>
+ isNil(string) ? string : hyphenate(string, length);
+// eslint-disable-next-line @bigbinary/neeto/use-camel-case-or-pascal-case-for-function-names
export const _truncate = (string, length) =>
isNil(string) ? string : truncate(string, length);
diff --git a/tests/string.spec.js b/tests/string.spec.js
index ca20a45..e11fd5b 100644
--- a/tests/string.spec.js
+++ b/tests/string.spec.js
@@ -4,7 +4,10 @@ import {
snakeToCamelCase,
slugify,
capitalize,
+ hyphenate,
+ _hyphenate,
truncate,
+ _truncate,
} from "src/strings";
describe("String operations", () => {
@@ -41,8 +44,25 @@ describe("String operations", () => {
expect(capitalize("OLIVER")).toBe("OLIVER");
});
+ test("hyphenate() method should hyphenate the string", () => {
+ expect(hyphenate("Hello World")).toBe("hello-world");
+ expect(hyphenate("hello_world")).toBe("hello-world");
+ expect(hyphenate("helloWorld")).toBe("hello-world");
+ });
+
+ test("_hyphenate() method should hyphenate the string", () => {
+ expect(_hyphenate("Hello World")).toBe("hello-world");
+ expect(_hyphenate("hello_world")).toBe("hello-world");
+ expect(_hyphenate("helloWorld")).toBe("hello-world");
+ });
+
test("truncate() method should truncate the string if it is longer than specified string length", () => {
expect(truncate("Hello World", 5)).toBe("Hello...");
expect(truncate("Hello World", 15)).toBe("Hello World");
});
+
+ test("_truncate() method should truncate the string if it is longer than specified string length", () => {
+ expect(_truncate("Hello World", 5)).toBe("Hello...");
+ expect(_truncate("Hello World", 15)).toBe("Hello World");
+ });
});
diff --git a/typeTemplates/index.d.ts b/typeTemplates/index.d.ts
index e22ae29..46ac496 100644
--- a/typeTemplates/index.d.ts
+++ b/typeTemplates/index.d.ts
@@ -176,6 +176,9 @@ export function getRandomInt(a?: number, b?: number): number;
export function humanize(string: string): string;
export function _humanize(string: NilOr): NilOr;
+export function hyphenate(string: string): string;
+export function _hyphenate(string: NilOr): NilOr;
+
export function isNot(a: any, b: any): boolean;
export function isNot(a: any): (b: any) => boolean;
@@ -333,7 +336,7 @@ export function transformObjectDeep(
): object;
export function truncate(string: string, length: number): string;
-export function truncate(string: NilOr, length: number): NilOr;
+export function _truncate(string: NilOr, length: number): NilOr;
export function nullSafe(
func: T