Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the hyphenate function #28

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/pure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

</td>
Expand Down
49 changes: 37 additions & 12 deletions docs/pure/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

## slugify

Curried: false
Failsafe status: alternative available
Curried: false Failsafe status: alternative available

Converts a given string to slug.

<details>
<summary>(click for example)</summary>

### Arguments:

- `string`: The string to be converted.

### Usage:
Expand All @@ -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.
Expand All @@ -37,6 +36,7 @@ dash-case etc) to human readable string.
<summary>(click for example)</summary>

### Arguments:

- `string`: The string to be converted.

### Usage:
Expand All @@ -52,15 +52,15 @@ humanize("HelloUSA"); // "Hello usa"

## snakeToCamelCase

Curried: false
Failsafe status: alternative available
Curried: false Failsafe status: alternative available

Converts snake_case string to camelCase.

<details>
<summary>(click for example)</summary>

### Arguments:

- `string`: The string to be converted.

### Usage:
Expand All @@ -73,15 +73,15 @@ snakeToCamelCase("first_name"); // "firstName"

## camelToSnakeCase

Curried: false
Failsafe status: alternative available
Curried: false Failsafe status: alternative available

Converts camelCase string to snake_case.

<details>
<summary>(click for example)</summary>

### Arguments:

- `string`: The string to be converted.

### Usage:
Expand All @@ -94,15 +94,15 @@ 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.

<details>
<summary>(click for example)</summary>

### Arguments:

- `string`: The string to be converted.

### Usage:
Expand All @@ -115,17 +115,42 @@ capitalize("oLIVER"); // "OLIVER"

</details>

## hyphenate

Curried: false Failsafe status: alternative available

The hyphenate function converts strings that contain underscores, spaces, and
camelCase strings into hyphenated strings

<details>
<summary>(click for example)</summary>

### 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"
```

</details>

## 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.

<details>
<summary>(click for example)</summary>

### Arguments:

- `string`: The string to be truncated.
- `length`: The maximum allowed length of the string.

Expand Down
32 changes: 25 additions & 7 deletions src/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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, " ")
Expand All @@ -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;

Expand All @@ -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);
20 changes: 20 additions & 0 deletions tests/string.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
snakeToCamelCase,
slugify,
capitalize,
hyphenate,
_hyphenate,
truncate,
_truncate,
} from "src/strings";

describe("String operations", () => {
Expand Down Expand Up @@ -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");
});
});
5 changes: 4 additions & 1 deletion typeTemplates/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ export function getRandomInt(a?: number, b?: number): number;
export function humanize(string: string): string;
export function _humanize(string: NilOr<string>): NilOr<string>;

export function hyphenate(string: string): string;
export function _hyphenate(string: NilOr<string>): NilOr<string>;

export function isNot(a: any, b: any): boolean;
export function isNot(a: any): (b: any) => boolean;

Expand Down Expand Up @@ -333,7 +336,7 @@ export function transformObjectDeep(
): object;

export function truncate(string: string, length: number): string;
export function truncate(string: NilOr<string>, length: number): NilOr<string>;
export function _truncate(string: NilOr<string>, length: number): NilOr<string>;

export function nullSafe<T extends Function>(
func: T
Expand Down
Loading