Skip to content

Commit

Permalink
feat(object): add merge utils fns
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanga-Ganapathy committed Mar 15, 2024
1 parent e1bc61a commit 2ea7424
Show file tree
Hide file tree
Showing 21 changed files with 785 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-turkeys-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opentf/utils": minor
---

Added merge utils fns & renamed arrayDiff to arrDiff
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

> A collection of JavaScript utility functions.
# [Playground](https://js-utils.pages.dev/playground) | [Documentation](https://js-utils.pages.dev)
<div align="center">

## [Playground](https://js-utils.pages.dev/playground) | [Documentation](https://js-utils.pages.dev)

</div>

## Features

Expand Down Expand Up @@ -47,11 +51,11 @@ import { isNum, pascalCase, sort, clone, sleep } from "@opentf/utils";

isNum(NaN); //=> false

pascalCase('pascal case'); //=> PascalCase
pascalCase("pascal case"); //=> PascalCase

sort([1, 10, 21, 2], 'desc'); //=> [ 21, 10, 2, 1 ]
sort([1, 10, 21, 2], "desc"); //=> [ 21, 10, 2, 1 ]

const obj = {a: 1, b: 'abc', c: new Map([['key', 'val']])}
const obj = { a: 1, b: "abc", c: new Map([["key", "val"]]) };
clone(obj); // It returns deeply cloned value.

await sleep(1000); // It suspends the exection for 1 second.
Expand Down Expand Up @@ -93,6 +97,10 @@ await sleep(1000); // It suspends the exection for 1 second.
- [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)
- [merge](https://js-utils.pages.dev/docs/Object/merge)
- [mergeAll](https://js-utils.pages.dev/docs/Object/mergeAll)
- [shallowMerge](https://js-utils.pages.dev/docs/Object/shallowMerge)
- [shallowMergeAll](https://js-utils.pages.dev/docs/Object/shallowMergeAll)

### String

Expand Down
3 changes: 2 additions & 1 deletion apps/docs/components/PlaygroundREPL.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ log(camelCase('i phone'));
`;

const setupCode = `const _ = require('lodash');
const R = require('ramda');
const log = console.log;`;

export default function PlaygroundREPL() {
Expand All @@ -30,7 +31,7 @@ export default function PlaygroundREPL() {
<NodeREPL
code={code}
setupCode={setupCode}
deps={["@opentf/utils", "lodash"]}
deps={["@opentf/utils", "lodash", "ramda"]}
layout="SPLIT_PANEL"
editor={{
darkMode: resolvedTheme === "dark",
Expand Down
3 changes: 2 additions & 1 deletion apps/docs/components/REPL.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const NodeREPL = dynamic(
);

const setupCode = `const _ = require('lodash');
const R = require('ramda');
const log = console.log;`;

export default function REPL({ code = "", layout = "DEFAULT" }) {
Expand All @@ -28,7 +29,7 @@ export default function REPL({ code = "", layout = "DEFAULT" }) {
<NodeREPL
code={code}
setupCode={setupCode}
deps={["@opentf/utils", "lodash"]}
deps={["@opentf/utils", "lodash", 'ramda']}
editor={{
darkMode: resolvedTheme === "dark",
header: false,
Expand Down
File renamed without changes.
6 changes: 5 additions & 1 deletion apps/docs/pages/Object/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
"delInObj": "delInObj",
"getInObj": "getInObj",
"setInObj": "setInObj",
"isShallowEql": "isShallowEql"
"isShallowEql": "isShallowEql",
"merge": "merge",
"mergeAll": "mergeAll",
"shallowMerge": "shallowMerge",
"shallowMergeAll": "shallowMergeAll"
}
38 changes: 38 additions & 0 deletions apps/docs/pages/Object/merge.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Callout } from "nextra/components";
import REPL from "../../components/REPL";

> It deeply merges objects or arrays.
## Syntax

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

merge(obj1, ...objN);
```

<Callout type="info" emoji="">
Immutable: This does not mutate the given arrays or objects.
</Callout>

## Examples

```ts
const a = { a: { b: 1 }, d: 3 };
const b = { a: { c: 2 }, e: 5 };
merge(a, b); //=> { a: { b: 1, c: 2 }, d: 3, e: 5 }

const a = { a: [1, 2] };
const b = { a: [3, 4, 5] };
const c = { a: [7, 8] };
merge(a, b, c); //=> { a: [7, 8, 5] }
```

## Try

<REPL code={`const { merge } = require('@opentf/utils');
const a = { a: { b: 1 }, d: 3 };
const b = { a: { c: 2 }, e: 5 };
merge(a, b);
`} />
33 changes: 33 additions & 0 deletions apps/docs/pages/Object/mergeAll.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Callout } from "nextra/components";
import REPL from "../../components/REPL";

> It deeply merges objects and concatenates the arrays if any present.
## Syntax

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

mergeAll(obj1, ...objN);
```

<Callout type="info" emoji="">
Immutable: This does not mutate the given arrays or objects.
</Callout>

## Examples

```ts
const a = { a: { b: [1] } };
const b = { a: { b: [2] } };
mergeAll(a, b); //=> { a: { b: [1, 2] }
```

## Try

<REPL code={`const { mergeAll } = require('@opentf/utils');
const a = { a: { b: [1] } };
const b = { a: { b: [2] } };
mergeAll(a, b);
`} />
38 changes: 38 additions & 0 deletions apps/docs/pages/Object/shallowMerge.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Callout } from "nextra/components";
import REPL from "../../components/REPL";

> Shallow Merges objects or arrays.
## Syntax

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

shallowMerge(obj1, ...objN);
```

<Callout type="info" emoji="">
Immutable: This does not mutate the given arrays or objects.
</Callout>

## Examples

```ts
const a = { a: 1 };
const b = { b: 2 };
shallowMerge(a, b); //=> { a: 1, b: 2 }

const a = [1];
const b = [2];
const c = [3];
shallowMerge(a, b, c); //=> [3]
```

## Try

<REPL code={`const { shallowMerge } = require('@opentf/utils');
const a = { a: 1 };
const b = { b: 2 };
shallowMerge(a, b);
`} />
35 changes: 35 additions & 0 deletions apps/docs/pages/Object/shallowMergeAll.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";

> Shallow merges objects and concatenates the array.
## Syntax

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

shallowMergeAll(obj1, ...objN);
```

<Callout type="info" emoji="">
Immutable: This does not mutate the given arrays or objects.
</Callout>

## Examples

```ts
const a = [1];
const b = [2];
const c = [3];
shallowMergeAll(a, b, c); //=> [1, 2, 3]
```

## Try

<REPL code={`const { shallowMergeAll } = require('@opentf/utils');
const a = [1];
const b = [2];
const c = [3];
shallowMergeAll(a, b, c);
`} />
3 changes: 2 additions & 1 deletion apps/docs/pages/playground.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import PlaygroundREPL from "../components/PlaygroundREPL";
</Callout>

<Callout type="info">
- The [Lodash](https://lodash.com/docs/) methods were preloaded. You can use it like, `_.isEmpty(value)`

The [Lodash](https://lodash.com/docs/) methods were preloaded. You can use it like, `_.isEmpty(value)`
- The [Ramda](https://ramdajs.com/) methods were preloaded. You can use it like, `R.pipe(Math.pow, R.negate, R.inc)`

</Callout>
19 changes: 15 additions & 4 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

> A collection of JavaScript utility functions.
# [Playground](https://js-utils.pages.dev/playground) | [Documentation](https://js-utils.pages.dev)
<div align="center">

## [Playground](https://js-utils.pages.dev/playground) | [Documentation](https://js-utils.pages.dev)

</div>

## Features

Expand Down Expand Up @@ -43,14 +47,17 @@ bun add @opentf/utils
## Usage

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

isNum(NaN); //=> false

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

pascalCase('pascal case'); //=> PascalCase

sort([1, 10, 21, 2], 'desc'); //=> [ 21, 10, 2, 1 ]

const obj = { a: 1, b: 'abc', c: new Map([['key', 'val']]) };
clone(obj); // It returns deeply cloned value.

await sleep(1000); // It suspends the exection for 1 second.
```

Expand Down Expand Up @@ -90,6 +97,10 @@ await sleep(1000); // It suspends the exection for 1 second.
- [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)
- [merge](https://js-utils.pages.dev/docs/Object/merge)
- [mergeAll](https://js-utils.pages.dev/docs/Object/mergeAll)
- [shallowMerge](https://js-utils.pages.dev/docs/Object/shallowMerge)
- [shallowMergeAll](https://js-utils.pages.dev/docs/Object/shallowMergeAll)

### String

Expand Down

0 comments on commit 2ea7424

Please sign in to comment.