Skip to content

Commit

Permalink
docs: add isEmpty, isNil & size fns
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanga-Ganapathy committed Mar 15, 2024
1 parent d24644a commit fd1f980
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 1 deletion.
5 changes: 4 additions & 1 deletion apps/docs/pages/Object/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"merge": "merge",
"mergeAll": "mergeAll",
"shallowMerge": "shallowMerge",
"shallowMergeAll": "shallowMergeAll"
"shallowMergeAll": "shallowMergeAll",
"isEmpty": "isEmpty",
"isNil": "isNil",
"size": "size"
}
84 changes: 84 additions & 0 deletions apps/docs/pages/Object/isEmpty.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Callout } from "nextra/components";
import REPL from "../../components/REPL";

> Checks if the given value is empty.
<Callout type="info">
The following type of values are supported:

- Array

- Plain Object

- String

- Map

- Set

- ArrayBuffer
</Callout>

## Syntax

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

isEmpty(val: unknown): Boolean;
```

## Examples

```ts
// Truthy cases
// *********
isEmpty('') //=> true

isEmpty([]) //=> true

isEmpty({}) //=> true

isEmpty(new Map()) //=> true

isEmpty(new Set()) //=> true

isEmpty(new ArrayBuffer(0)) //=> true


// Falsy cases
// *********

isEmpty() //=> false

isEmpty(undefined) //=> false

isEmpty(null) //=> false

isEmpty(1) //=> false

isEmpty(1.5) //=> false

isEmpty(1n) //=> false

isEmpty(false) //=> false

isEmpty(true) //=> false

isEmpty(' ') //=> false

isEmpty('a') //=> false

isEmpty([1]) //=> false

isEmpty({ length: 0, size: 0, byteLength: 0 }) //=> false

```

## Try

<REPL code={`const { isEmpty } = require('@opentf/utils');
log(isEmpty([]));
log(isEmpty({}));
isEmpty(null);
`} />
40 changes: 40 additions & 0 deletions apps/docs/pages/Object/isNil.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 value is Nil / Noting.
<Callout type="info">
The `undefined` & `null` values are considered `Nil` here.
</Callout>

## Syntax

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

isNil(val: unknown): Boolean;
```

## Examples

```ts
isNil('') //=> false

isNil([]) //=> false

isNil({}) //=> false

isNil() //=> true

isNil(undefined) //=> true

isNil(null) //=> true
```

## Try

<REPL code={`const { isNil } = require('@opentf/utils');
const arr = [1, null, 2, undefined, 3]
arr.filter((i) => !isNil(i));
`} />
93 changes: 93 additions & 0 deletions apps/docs/pages/Object/size.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Callout } from "nextra/components";
import REPL from "../../components/REPL";

> Returns the size of the given value or null.
<Callout type="info">
The following type of values are supported:

- Array

- Plain Object

- String

- Map

- Set

- ArrayBuffer
</Callout>

## Syntax

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

size(val: unknown): number | null;
```

## Examples

```ts
// Invalid objs
// ************

size() //=> null

size(undefined) //=> null

size(null) //=> null

size(1) //=> null

size(1.5) //=> null

size(1n) //=> null

size(false) //=> null

size(true) //=> null

// Eempty objs
// ***********

size('') //=> 0

size([]) //=> 0

size({}) //=> 0

size(new Map()) //=> 0

size(new Set()) //=> 0

size(new ArrayBuffer(0)) //=> 0

// Non empty objs
// **************

size(' ') //=> 1

size('abc') //=> 3

size([1]) //=> 1

size({ length: 0, size: 0, byteLength: 0 }) //=> 3

size(new Map([[1, 1]])) //=> 1

size(new Set(['a', 'b'])) //=> 2

size(new ArrayBuffer(8)) //=> 8
```

## Try

<REPL code={`const { size } = require('@opentf/utils');
log(size(true));
log(size([]));
log(size({a: 1, b: 2}));
size('Hello World');
`} />

0 comments on commit fd1f980

Please sign in to comment.