Skip to content

Add Increment and Decrement Functions #79

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Before submitting a pull request, please make sure the following is done:
- Fork the repository and create your feature branch from dev.
- Run `npm install` to have all dependencies.
- To start development run `npm run test:watch`.
- Write tests in `src/<scope>/<name>.test.ts` and implementation in `src/<scope>/<name>.ts`.
- Write tests in `src/<scope>/<name>.spec.ts` and implementation in `src/<scope>/<name>.ts`.
- Add the documentation page to the `docs/<scope>/<function>.mdx` and update `docs/<scope>/_meta.json` file.
- Ensure everything is ok `npm run verify`.

Expand Down
2 changes: 2 additions & 0 deletions docs/pages/number/_meta.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"clamp": "clamp",
"divide": "divide",
"inc": "inc",
"dec": "dec",
"fallback-number": "fallbackNumber",
"is-in-range": "isInRange",
"percent": "percent",
Expand Down
22 changes: 22 additions & 0 deletions docs/pages/number/dec.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# dec

Decrements the given number by 1, or by a specified step. Useful for counters and mathematical operations.

### Import

```typescript copy
import { dec } from '@fullstacksjs/toolbox';
```

### Signature

```typescript copy
function dec(n: number, step?: number): number {}
```

### Examples

```typescript copy
dec(3) // 2
dec(3, 2) // 1
```
22 changes: 22 additions & 0 deletions docs/pages/number/inc.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# inc

Increments the given number by 1, or by a specified step. Useful for counters and mathematical operations.

### Import

```typescript copy
import { inc } from '@fullstacksjs/toolbox';
```

### Signature

```typescript copy
function inc(n: number, step?: number): number {}
```

### Examples

```typescript copy
inc(3) // 4
inc(3, 2) // 5
```
14 changes: 14 additions & 0 deletions src/number/dec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Decrements a number by 1 or a specified step.
*
* @param {number} n - The number to decrement.
* @param {number} [step=1] - The step size to decrement by. Defaults to 1.
* @returns {number} The decremented number.
*
* @example
* dec(3) // 2
* dec(3, 2) // 1
*/
export function dec(n: number, step: number = 1): number {
return n - step;
}
17 changes: 17 additions & 0 deletions src/number/des.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { dec } from './dec';

describe('dec', () => {
it('should decrement by 1 when no step is provided', () => {
expect(dec(2)).toBe(1);
expect(dec(5)).toBe(4);
});

it('should decrement by the given step', () => {
expect(dec(4, 2)).toBe(2);
expect(dec(8, 3)).toBe(5);
});

it('should handle negative steps correctly', () => {
expect(dec(5, -2)).toBe(7);
});
});
17 changes: 17 additions & 0 deletions src/number/inc.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { inc } from './inc';

describe('inc', () => {
it('should increment by 1 when no step is provided', () => {
expect(inc(0)).toBe(1);
expect(inc(5)).toBe(6);
});

it('should increment by the given step', () => {
expect(inc(0, 2)).toBe(2);
expect(inc(5, 3)).toBe(8);
});

it('should handle negative steps correctly', () => {
expect(inc(5, -2)).toBe(3);
});
});
14 changes: 14 additions & 0 deletions src/number/inc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Increments a number by 1 or a specified step.
*
* @param {number} n - The number to increment.
* @param {number} [step=1] - The step size to increment by. Defaults to 1.
* @returns {number} The incremented number.
*
* @example
* inc(3) // 4
* inc(3, 2) // 5
*/
export function inc(n: number, step: number = 1): number {
return n + step;
}
2 changes: 2 additions & 0 deletions src/number/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export { clamp } from './clamp.ts';
export { divide } from './divide.ts';
export { inc } from './inc.ts';
export { dec } from './dec.ts';
export { fallbackNumber } from './fallbackNumber.ts';
export { isInRange } from './isInRange.ts';
export { percent } from './percent.ts';
Expand Down