Skip to content

Commit

Permalink
Add method toBoolean.
Browse files Browse the repository at this point in the history
Change version for release.
  • Loading branch information
Jojo-craft committed Aug 19, 2023
1 parent d549f89 commit c5e1b98
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

---

## 📦 [Unreleased]
## 📦 [2.2.0] - 2023-08-19

### 🐞 Fixed
- Methods:
Expand All @@ -16,9 +16,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### 💎 Added
- Methods:
- `toNumber`
- `toBoolean`

### ⛏️ Work in progress
- `toBoolean(string): boolean`: parse string to boolean (case insensitive).
---

## 📦 [Unreleased]

### 💡 Ideas for future release

Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ String.EMPTY;
| [`join`](#join) | Concatenates an array of strings using the specified separator between each member. | separator: `string`, values: (`string` | `null` | `undefined`)[] | `string` |
| [`countWords`](#countWords) | Counts the number of words in a sentence. | sentence: `string` | `null` | `undefined` | `number` |
| [`toNumber`](#toNumber) | Converts a string representation of a number to a JavaScript number. | value: `string` | `null` | `undefined` | `number` | `NaN` |
| [`toBoolean`](#toBoolean) | Converts a string representation to a boolean value. | value: `string` | `null` | `undefined` | `boolean` |


#### <a id="isEmpty"/> isEmpty
Expand Down Expand Up @@ -322,6 +323,28 @@ String.toNumber('10.1234');
// 10.1234
```

#### <a id="toBoolean"/> toBoolean

```typescript
String.toBoolean(undefined);
// false
```

```typescript
String.toBoolean(' ');
// false
```

```typescript
String.toBoolean('1');
// true
```

```typescript
String.toBoolean('true');
// true
```

---

## License
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "string-craft",
"version": "2.1.0",
"version": "2.2.0",
"description": "Simple string manipulation library for TypeScript",
"main": "dist/string.js",
"types": "dist/string.d.ts",
Expand Down
26 changes: 26 additions & 0 deletions src/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,4 +657,30 @@ describe('String', () => {
});
});
});

describe('toBoolean', () => {
describe('Should be false when input is:', () => {
it.each([null, undefined, '', ' '])('"%s"', (input: StringOrNullOrUndefined) => {
expect(String.toBoolean(input)).toBeFalsy();
});
});

describe('Should be false when string input is not correct:', () => {
it.each(['A', '!', '1234ADF', '@1', '-1', '3'])('"%s"', (input: StringOrNullOrUndefined) => {
expect(String.toBoolean(input)).toBeFalsy();
});
});

describe('Should be true when string input is a correct "true" boolean:', () => {
it.each(['true', 'True', '1', 'TRUE'])('"%s"', (input: StringOrNullOrUndefined) => {
expect(String.toBoolean(input)).toBeTruthy();
});
});

describe('Should be false when string input is a correct "false" boolean:', () => {
it.each(['false', 'False', '0', 'FALSE'])('"%s"', (input: StringOrNullOrUndefined) => {
expect(String.toBoolean(input)).toBeFalsy();
});
});
});
});
13 changes: 13 additions & 0 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,17 @@ export class String {

return +value;
}

/**
* Converts a string representation to a boolean value.
* @param {StringOrNullOrUndefined} value - The string value to convert to a boolean.
* @returns {boolean} The converted boolean value. Returns false for invalid inputs.
*/
static toBoolean(value: StringOrNullOrUndefined): boolean {
if (value == undefined) {
return false;
}

return value.toLowerCase() == 'true' || value == '1';
}
}

0 comments on commit c5e1b98

Please sign in to comment.