Skip to content

Commit

Permalink
toNumber change return for release 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jojo-craft committed Aug 22, 2023
1 parent a0be1e2 commit ee5e29d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 47 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

---


## 📦 [Template] - YYYY-MM-dd

Expand Down
16 changes: 12 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

---


## 📦 [2.3.0] - 2023-08-23

### 🔧 Changed

- Methods:
- `toNumber`: will no longer return NaN, if the number is not correct, will return 0.


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

Expand All @@ -18,7 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `toNumber`
- `toBoolean`

---


## 📦 [2.1.0] - 2023-08-16

Expand All @@ -32,7 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `containsLowerCase`
- `isBasicStrongPassword`

---


## 📦 [2.0.0] - 2023-08-14

Expand All @@ -49,7 +57,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `containsNumber` | `notContainsNumber`
- `containsAlpha` | `notContainsAlpha`

---


## 📦 [1.0.1] - 2023-08-13

Expand Down
60 changes: 30 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Simple string manipulation library for TypeScript.

`string-craft` is a comprehensive TypeScript library designed to simplify and enhance string manipulation tasks.

---


## CI Status

Expand All @@ -22,7 +22,7 @@ The following table lists live workflows from various CI providers.
| [Codecov](https://about.codecov.io/) | [![codecov](https://codecov.io/gh/Jojo-craft/string-craft/branch/main/graph/badge.svg?token=QAHEKEG6FS)](https://codecov.io/gh/Jojo-craft/string-craft) |
| [Stryker-mutator dashboard](https://dashboard.stryker-mutator.io/) | [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2FJojo-craft%2Fstring-craft%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/Jojo-craft/string-craft/main) |

---


## Usage

Expand Down Expand Up @@ -53,26 +53,26 @@ String.EMPTY;

### Methods

| Name | Description | Input parameters | Return |
|:-------------------------------------------------------:|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:--------------------------------------------------------------------------:|:---------------------:|
| [`isEmpty`](#isEmpty) | Indicates whether the specified string is an empty string ("") (reverse with `isNotEmpty` method). | value: `string` | `boolean` |
| [`isNullOrEmpty`](#isNullOrEmpty) | Indicates whether the specified string is null, undefined or an empty string ("") (reverse with `isNotNullOrEmpty` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`isBlank`](#isBlank) | Indicates whether a specified string is empty, or consists only of white-space characters (reverse with `isNotBlank` method). | value: `string` | `boolean` |
| [`isNullOrBlank`](#isNullOrBlank) | Indicates whether a specified string is null, undefined, empty, or consists only of white-space characters (reverse with `isNotNullOrBlank` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`isNumber`](#isNumber) | Indicates whether the specified string is a valid numeric string (reverse with `isNotNumber` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`isAlpha`](#isAlpha) | Indicates whether a given value consists only of alphabetic characters (reverse with `isNotAlpha` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`isAlphaNumber`](#isAlphaNumber) | Indicates whether the specified string contains only alphabetic characters and numbers (reverse with `isNotAlphaNumber` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`isBasicStrongPassword`](#isBasicStrongPassword) | Indicates whether the specified string contains at least 1 uppercase letter, 1 lowercase letter, 1 number, 1 special character and a minimum length of 12 characters. | value: `string` | `null` | `undefined` | `boolean` |
| [`containsNumber`](#containsNumber) | Indicates whether the specified string contains at least one numeric digit (reverse with `notContainsNumber` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`containsAlpha`](#containsAlpha) | Indicates whether the specified string contains at least one alphabetic character (reverse with `notContainsAlpha` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`containsSpecialCharacter`](#containsSpecialCharacter) | Indicates whether a given string contains at least one special character (reverse with `notContainsSpecialCharacter` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`containsUpperCase`](#containsUpperCase) | Indicates whether the specified string contains at least one uppercase letter. | value: `string` | `null` | `undefined` | `boolean` |
| [`containsLowerCase`](#containsLowerCase) | Indicates whether the specified string contains at least one lowercase letter. | value: `string` | `null` | `undefined` | `boolean` |
| [`removeAccents`](#removeAccents) | Removes accents from a given string. | value: `string` | `string` |
| [`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` |
| Name | Description | Input parameters | Return |
|:-------------------------------------------------------:|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:--------------------------------------------------------------------------:|:---------:|
| [`isEmpty`](#isEmpty) | Indicates whether the specified string is an empty string ("") (reverse with `isNotEmpty` method). | value: `string` | `boolean` |
| [`isNullOrEmpty`](#isNullOrEmpty) | Indicates whether the specified string is null, undefined or an empty string ("") (reverse with `isNotNullOrEmpty` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`isBlank`](#isBlank) | Indicates whether a specified string is empty, or consists only of white-space characters (reverse with `isNotBlank` method). | value: `string` | `boolean` |
| [`isNullOrBlank`](#isNullOrBlank) | Indicates whether a specified string is null, undefined, empty, or consists only of white-space characters (reverse with `isNotNullOrBlank` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`isNumber`](#isNumber) | Indicates whether the specified string is a valid numeric string (reverse with `isNotNumber` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`isAlpha`](#isAlpha) | Indicates whether a given value consists only of alphabetic characters (reverse with `isNotAlpha` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`isAlphaNumber`](#isAlphaNumber) | Indicates whether the specified string contains only alphabetic characters and numbers (reverse with `isNotAlphaNumber` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`isBasicStrongPassword`](#isBasicStrongPassword) | Indicates whether the specified string contains at least 1 uppercase letter, 1 lowercase letter, 1 number, 1 special character and a minimum length of 12 characters. | value: `string` | `null` | `undefined` | `boolean` |
| [`containsNumber`](#containsNumber) | Indicates whether the specified string contains at least one numeric digit (reverse with `notContainsNumber` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`containsAlpha`](#containsAlpha) | Indicates whether the specified string contains at least one alphabetic character (reverse with `notContainsAlpha` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`containsSpecialCharacter`](#containsSpecialCharacter) | Indicates whether a given string contains at least one special character (reverse with `notContainsSpecialCharacter` method). | value: `string` | `null` | `undefined` | `boolean` |
| [`containsUpperCase`](#containsUpperCase) | Indicates whether the specified string contains at least one uppercase letter. | value: `string` | `null` | `undefined` | `boolean` |
| [`containsLowerCase`](#containsLowerCase) | Indicates whether the specified string contains at least one lowercase letter. | value: `string` | `null` | `undefined` | `boolean` |
| [`removeAccents`](#removeAccents) | Removes accents from a given string. | value: `string` | `string` |
| [`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` |
| [`toBoolean`](#toBoolean) | Converts a string representation to a boolean value. | value: `string` | `null` | `undefined` | `boolean` |


#### <a id="isEmpty"/> isEmpty
Expand Down Expand Up @@ -285,27 +285,27 @@ String.isBasicStrongPassword('1234abcefgH!');

```typescript
String.toNumber(null);
// NaN
// 0
```

```typescript
String.toNumber(undefined);
// NaN
// 0
```

```typescript
String.toNumber(' ');
// NaN
// 0
```

```typescript
String.toNumber('A123@');
// NaN
// 0
```

```typescript
String.toNumber('false');
// NaN
String.toNumber('true');
// 0
```

```typescript
Expand Down Expand Up @@ -345,13 +345,13 @@ String.toBoolean('true');
// true
```

---


## License

This software is released under the terms of the **MIT license**. See `LICENSE`.

---


## Contribute

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.2.0",
"version": "2.3.0",
"description": "Simple string manipulation library for TypeScript",
"main": "dist/string.js",
"types": "dist/string.d.ts",
Expand Down
12 changes: 6 additions & 6 deletions src/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,21 +616,21 @@ describe('String', () => {
});

describe('toNumber', () => {
describe('Should have NaN when input is:', () => {
describe('Should have 0 when input is:', () => {
it.each([null, undefined, '', ' '])('"%s"', (input: StringOrNullOrUndefined) => {
expect(String.toNumber(input)).toBe(Number.NaN);
expect(String.toNumber(input)).toBe(0);
});
});

describe('Should have NaN when string input is not a correct number:', () => {
describe('Should have 0 when string input is not a correct number:', () => {
it.each(['A', '!', '1234ADF', '@1'])('"%s"', (input: StringOrNullOrUndefined) => {
expect(String.toNumber(input)).toBe(Number.NaN);
expect(String.toNumber(input)).toBe(0);
});
});

describe('Should have NaN when string input is boolean:', () => {
describe('Should have 0 when string input is boolean:', () => {
it.each(['true', 'True', 'false', 'False', 'TRUE', 'FALSE'])('"%s"', (input: StringOrNullOrUndefined) => {
expect(String.toNumber(input)).toBe(Number.NaN);
expect(String.toNumber(input)).toBe(0);
});
});

Expand Down
10 changes: 5 additions & 5 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,18 +418,18 @@ export class String {
/**
* Converts a string representation of a number to a JavaScript number.
* @param {StringOrNullOrUndefined} value - The string value to convert to a number.
* @returns {number | typeof NaN} The converted number, or NaN (Not a number) if the input is not a valid number.
* @returns {number} The converted number, or 0 if the input is not a valid number.
*/
static toNumber(value: StringOrNullOrUndefined): number | typeof NaN {
static toNumber(value: StringOrNullOrUndefined): number {
if (value == undefined) {
return Number.NaN;
return 0;
}

if (String.isNotNumber(value)) {
return Number.NaN;
return 0;
}

return +value;
return Number(value);
}

/**
Expand Down

0 comments on commit ee5e29d

Please sign in to comment.