Skip to content

Commit

Permalink
docs: add number util fns
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanga-Ganapathy committed Mar 16, 2024
1 parent e2c01c0 commit d8e5e77
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 15 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ await sleep(1000); // It suspends the exection for 1 second.
- [percentage](https://js-utils.pages.dev/docs/Maths/percentage)
- [percentageOf](https://js-utils.pages.dev/docs/Maths/percentageOf)

## Number

- [clamp](https://js-utils.pages.dev/docs/Number/clamp)
- [isNegZero](https://js-utils.pages.dev/docs/Number/isNegZero)
- [isZero](https://js-utils.pages.dev/docs/Number/isZero)
- [toNum](https://js-utils.pages.dev/docs/Number/toNum)

### Function

- [sleep](https://js-utils.pages.dev/docs/Timers/sleep)
Expand Down
6 changes: 6 additions & 0 deletions apps/docs/pages/Number/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"clamp": "clamp",
"isNegZero": "isNegZero",
"isZero": "isZero",
"toNum": "toNum"
}
30 changes: 30 additions & 0 deletions apps/docs/pages/Number/clamp.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import REPL from "../../components/REPL";

> Returns a value clamped to the inclusive range of min and max.
## Syntax

```ts
import { clamp } from '@opentf/utils';

clamp(val: number, min: number, max: number): number;
```

## Examples

```ts
clamp(10, -5, 5) //=> 5

clamp(0, 1000, 1366) //=> 1000

clamp(1001, 1000, 1366) //=> 1001

clamp(1500, 1000, 1366) //=> 1366
```

## Try

<REPL code={`const { clamp } = require('@opentf/utils');
clamp(0, 1000, 1366);
`} />
36 changes: 36 additions & 0 deletions apps/docs/pages/Number/isNegZero.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Callout } from "nextra/components";
import REPL from "../../components/REPL";

> Checks if the given number is negative zero (-0).
<Callout type="info">
In JavaScript 0 === -0 is true.
</Callout>

## Syntax

```ts
import { isNegZero } from '@opentf/utils';

isNegZero(n: number): boolean;
```

## Examples

```ts
isNegZero(0) //=> false

isNegZero(-0) //=> true

isNegZero(-0.0) //=> true

isNegZero(-0x0) //=> true
```

## Try

<REPL code={`const { isNegZero } = require('@opentf/utils');
log(isNegZero(0));
isNegZero(-0);
`} />
40 changes: 40 additions & 0 deletions apps/docs/pages/Number/isZero.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Callout } from "nextra/components";
import REPL from "../../components/REPL";

> Checks if the given number is positive zero (0).
<Callout type="info">
In JavaScript 0 === -0 is true.
</Callout>

## Syntax

```ts
import { isZero } from '@opentf/utils';

isZero(n: number): boolean;
```

## Examples

```ts
isZero(0) //=> true

isZero(0.0) //=> true

isZero(0x0) //=> true

isZero(-0) //=> false

isZero(-0.0) //=> false

isZero(-0x0) //=> false
```

## Try

<REPL code={`const { isZero } = require('@opentf/utils');
log(isZero(0));
isZero(-0);
`} />
35 changes: 35 additions & 0 deletions apps/docs/pages/Number/toNum.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Callout } from "nextra/components";
import REPL from "../../components/REPL";

> Try to convert the given value into a finite number.
## Syntax

```ts
import { toNum } from '@opentf/utils';

toNum(val: unknown): number;
```

## Examples

```ts
toNum(1) //=> 1

toNum('1.3') //=> 1.3

toNum('1_000') //=> 1000

toNum(Infinity) //=> NaN

toNum('a') //=> NaN

toNum('0xF') //=> 15
```

## Try

<REPL code={`const { toNum } = require('@opentf/utils');
toNum('1_000');
`} />
7 changes: 7 additions & 0 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ await sleep(1000); // It suspends the exection for 1 second.
- [percentage](https://js-utils.pages.dev/docs/Maths/percentage)
- [percentageOf](https://js-utils.pages.dev/docs/Maths/percentageOf)

## Number

- [clamp](https://js-utils.pages.dev/docs/Number/clamp)
- [isNegZero](https://js-utils.pages.dev/docs/Number/isNegZero)
- [isZero](https://js-utils.pages.dev/docs/Number/isZero)
- [toNum](https://js-utils.pages.dev/docs/Number/toNum)

### Function

- [sleep](https://js-utils.pages.dev/docs/Timers/sleep)
Expand Down
6 changes: 2 additions & 4 deletions packages/utils/src/number/isNegZero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
*
* @example
*
* isNegZero() //=> false
* isNegZero(null) //=> false
* isNegZero('') //=> false
* isNegZero(0) //=> false
* isNegZero(-0) //=> true */
* isNegZero(-0) //=> true
*/

export default function isNegZero(n: number): boolean {
return Object.is(n, -0);
Expand Down
3 changes: 0 additions & 3 deletions packages/utils/src/number/isZero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
*
* @example
*
* isZero() //=> false
* isZero(null) //=> false
* isZero('') //=> false
* isZero(-0) //=> false
* isZero(0) //=> true
*/
Expand Down
8 changes: 4 additions & 4 deletions packages/utils/src/object/merge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import isArr from '../types/isArr';
import isObj from '../types/isObj';

type IterableObj = {
[key: number | string]: unknown;
};

/**
* It deeply merges objects or arrays.
*
Expand All @@ -9,10 +13,6 @@ import isObj from '../types/isObj';
* const b = { a: { c: 2 } };
* merge(a, b); //=> {a: { b: 1, c: 2 } }
*/
type IterableObj = {
[key: number | string]: unknown;
};

export default function merge(...objs: object[]) {
const filteredObjs = objs.filter((v) => isArr(v) || isObj(v));
const initialVal = isArr(filteredObjs[0]) ? [] : {};
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/src/object/shallowMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import isArr from '../types/isArr';
* Shallow merges objects or arrays.
*
* @example
*
* const a = { a: 1 };
* const b = { b: 2 };
* shallowMerge(a, b); //=> { a: 1, b: 2 }
*/
export default function shallowMerge(...objs: object[]) {
return objs.reduce((acc, cur) => {
Expand Down
11 changes: 8 additions & 3 deletions packages/utils/src/object/shallowMergeAll.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import isArr from '../types/isArr';

/**
* Shallow merges objects or arrays and also returns union of array items.
* Shallow merges objects and concatenates the array.
*
* @example
*
* const a = [1];
* const b = [2];
* const c = [3];
* shallowMergeAll(a, b, c); //=> [1, 2, 3]
*/
export default function shallowMergeAll(...objs: object[]) {
return objs.reduce((acc, cur) => {
if (Array.isArray(cur) && Array.isArray(acc)) {
if (isArr(cur) && isArr(acc)) {
return [...acc, ...cur];
}

Expand Down

0 comments on commit d8e5e77

Please sign in to comment.