Skip to content

Commit

Permalink
Made minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
allenjiji committed Dec 7, 2023
1 parent b005e43 commit 823bc0a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
35 changes: 18 additions & 17 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 @@ -117,39 +117,40 @@ capitalize("oLIVER"); // "OLIVER"

## hyphenate

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

Hyphenates a string.
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("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
8 changes: 4 additions & 4 deletions src/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ export const camelToSnakeCase = string =>
export const capitalize = string =>
string.charAt(0).toUpperCase() + string.slice(1);

export const hyphenate = string => {
export const hyphenate = (string, fallbackString) => {
if (typeof string === "number") return String(string);

if (typeof 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 string;
return fallbackString;
};

export const truncate = (string, length) =>
Expand All @@ -57,6 +57,6 @@ export const _humanize = /*#__PURE__*/ nullSafe(humanize);
export const _snakeToCamelCase = /*#__PURE__*/ nullSafe(snakeToCamelCase);
export const _camelToSnakeCase = /*#__PURE__*/ nullSafe(camelToSnakeCase);
export const _capitalize = /*#__PURE__*/ nullSafe(capitalize);
export const _hyphenate = /*#__PURE__*/ nullSafe(hyphenate);
export const _truncate = (string, length) =>
isNil(string) ? string : truncate(string, length);
export const _hyphenate = /*#__PURE__*/ nullSafe(hyphenate);

0 comments on commit 823bc0a

Please sign in to comment.