Skip to content

Commit

Permalink
feat(fn): add noop fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanga-Ganapathy committed Mar 14, 2024
1 parent 5d18290 commit 9f6026e
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-melons-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opentf/utils": minor
---

Fixed sorting issues.
3 changes: 3 additions & 0 deletions apps/docs/pages/Colors/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hexToRGB": "hexToRGB"
}
File renamed without changes.
4 changes: 4 additions & 0 deletions apps/docs/pages/Function/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"noop": "noop",
"sleep": "sleep"
}
24 changes: 24 additions & 0 deletions apps/docs/pages/Function/noop.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import REPL from "../../components/REPL";

> The No Operation function.
## Syntax

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

noop(): undefined;
```

## Examples

```ts
noop(); //=> undefined
```

## Try

<REPL code={`const { noop } = require('@opentf/utils');
noop();
`} />
File renamed without changes.
4 changes: 0 additions & 4 deletions apps/docs/pages/Misc/_meta.json

This file was deleted.

62 changes: 56 additions & 6 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ bun add @opentf/utils
## Usage

```ts
import { isNum, range, camelCase, sleep } from '@opentf/utils';
import { isNum, range, pascalCase, sleep } from '@opentf/utils';

isNum(NaN); //=> false

range(1, 5); //=> [1, 2, 3, 4, 5]

camelCase('i phone'); //=> 'iPhone'
pascalCase('pascal case'); //=> PascalCase

await sleep(1000); // It suspends the exection for 1 second.
```
Expand All @@ -63,24 +63,33 @@ await sleep(1000); // It suspends the exection for 1 second.
- [groupBy](https://js-utils.pages.dev/docs/Array/groupBy)
- [range](https://js-utils.pages.dev/docs/Array/range)
- [move](https://js-utils.pages.dev/docs/Array/move)
- [sort](https://js-utils.pages.dev/docs/Array/sort)
- [sortBy](https://js-utils.pages.dev/docs/Array/sortBy)

### Maths

- [percentage](https://js-utils.pages.dev/docs/Maths/percentage)
- [percentageOf](https://js-utils.pages.dev/docs/Maths/percentageOf)

### Misc
### Function

- [hexToRGB](https://js-utils.pages.dev/docs/Misc/hexToRGB)
- [sleep](https://js-utils.pages.dev/docs/Timers/sleep)
- [noop](https://js-utils.pages.dev/docs/Timers/noop)

### Colors

- [hexToRGB](https://js-utils.pages.dev/docs/Misc/hexToRGB)

### Object

- [cloneObj](https://js-utils.pages.dev/docs/Object/cloneObj)
- [clone](https://js-utils.pages.dev/docs/Object/clone)
- [getInObj](https://js-utils.pages.dev/docs/Object/getInObj)
- [setInObj](https://js-utils.pages.dev/docs/Object/setInObj)
- [delInObj](https://js-utils.pages.dev/docs/Object/delInObj)
- [isEmpty](https://js-utils.pages.dev/docs/Object/isEmpty)
- [isNil](https://js-utils.pages.dev/docs/Object/isNil)
- [isShallowEql](https://js-utils.pages.dev/docs/Object/isShallowEql)
- [setInObj](https://js-utils.pages.dev/docs/Object/setInObj)
- [size](https://js-utils.pages.dev/docs/Object/size)

### String

Expand Down Expand Up @@ -116,6 +125,47 @@ await sleep(1000); // It suspends the exection for 1 second.
- [isWkRef](https://js-utils.pages.dev/Types/isWkRef)
- [isWkSet](https://js-utils.pages.dev/Types/isWkSet)

## Benchmarks

Some benchmark outputs are shown here for reference.

> [!IMPORTANT]
> Our priorities are reliability and accuracy rather than performance.
```sh
clone:
┌───┬──────────────────────────┬─────────┬────────────────────┬────────┬─────────┐
│ │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├───┼──────────────────────────┼─────────┼────────────────────┼────────┼─────────┤
│ 0 │ structuredClone (Native) │ 246,358 │ 4059.129038804869 │ ±1.65% │ 24636 │
│ 1 │ _.cloneDeep (Lodash) │ 169,432 │ 5902.043319168882 │ ±2.53% │ 16944 │
│ 2 │ R.clone (ramda) │ 178,957 │ 5587.9189808343945 │ ±2.26% │ 17897 │
│ 3 │ R2.clone (remeda) │ 314,247 │ 3182.204862216371 │ ±1.75% │ 31426 │
│ 4 │ clone │ 399,634 │ 2502.2859323385046 │ ±2.06% │ 39964 │
└───┴──────────────────────────┴─────────┴────────────────────┴────────┴─────────┘
*Note: Here the ramda & remeda does not support cloning Map & Set.

sortBy:
┌───┬────────────────────┬───────────┬───────────────────┬────────┬─────────┐
│ │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├───┼────────────────────┼───────────┼───────────────────┼────────┼─────────┤
│ 0 │ _.orderBy (Lodash) │ 1,231,295 │ 812.1529684071648 │ ±3.09% │ 123130 │
│ 1 │ R.sortWith (Ramda) │ 1,279,200 │ 781.7380570822326 │ ±2.27% │ 127921 │
│ 2 │ R2.sortBy (Remeda) │ 1,419,707 │ 704.3703291518029 │ ±2.81% │ 141971 │
│ 3 │ sort (Moderndash) │ 2,697,568 │ 370.7042634668106 │ ±1.82% │ 269757 │
│ 4 │ sortBy │ 2,728,366 │ 366.5196435965459 │ ±2.19% │ 272837 │
└───┴────────────────────┴───────────┴───────────────────┴────────┴─────────┘

*Note: Here the Moderndash does not support passing object prop as string.

```

### Running benchmarks

```sh
bun benchmark.js
```

## License

Copyright (c) [Thanga Ganapathy](https://github.com/Thanga-Ganapathy) ([MIT License](../../LICENSE)).
9 changes: 9 additions & 0 deletions packages/utils/__tests__/function/noop.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { noop } from '../../src';

describe('Misc', () => {
test('noop', () => {
expect(noop()).toBe(undefined);
expect(noop(null)).toBe(undefined);
expect(noop(1)).toBe(undefined);
});
});
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions packages/utils/src/function/noop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* The No Operation function.
*
* @example
*
* noop() //=> undefined
*/
export default function noop() {}
10 changes: 10 additions & 0 deletions packages/utils/src/function/sleep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* It suspends the execution for the given milliseconds.
*
* @example
*
* await sleep(1000) // It sleeps for 1 second
*/
export default async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
9 changes: 6 additions & 3 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ export { default as isEmpty } from './object/isEmpty';
export { default as isNil } from './object/isNil';
export { default as size } from './object/size';

// Misc
export { default as hexToRGB } from './misc/hexToRGB';
export { default as sleep } from './misc/sleep';
// function
export { default as sleep } from './function/sleep';
export { default as noop } from './function/noop';

// colors
export { default as hexToRGB } from './colors/hexToRGB';

// Number
export { default as clamp } from './number/clamp';
Expand Down
5 changes: 0 additions & 5 deletions packages/utils/src/misc/sleep.ts

This file was deleted.

0 comments on commit 9f6026e

Please sign in to comment.