Skip to content

Commit

Permalink
feat: assoc, deepclone and pick
Browse files Browse the repository at this point in the history
  • Loading branch information
ritesh404 committed Jan 14, 2021
1 parent 3ba8fd9 commit 147e80f
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ These are the functions under the `kudoJS.*` namespace
* [`kudoJS.compose`](docs/helper-functions.md#kudojscompose)
* [`kudoJS.constant`](docs/helper-functions.md#kudojsconstant)
* [`kudoJS.fmap`](docs/helper-functions.md#kudojsfmap)
* [`kudoJS.assoc`](docs/helper-functions.md#kudojsassoc)
* [`kudoJS.bimap`](docs/helper-functions.md#kudojsbimap)
* [`kudoJS.chain`](docs/helper-functions.md#kudojschain)
* [`kudoJS.caseOf`](docs/helper-functions.md#kudojscaseof)
Expand All @@ -44,6 +45,7 @@ These are the functions under the `kudoJS.*` namespace
* [`kudoJS.liftA4`](docs/helper-functions.md#kudojslifta4)
* [`kudoJS.liftA5`](docs/helper-functions.md#kudojslifta5)
* [`kudoJS.when`](docs/helper-functions.md#kudojswhen)
* [`kudoJS.pick`](docs/helper-functions.md#kudojspick)
* [`kudoJS.prop`](docs/helper-functions.md#kudojsprop)
* [`kudoJS.eitherToMaybe`](docs/helper-functions.md#kudojseithertomaybe)
* [`kudoJS.maybeToEither`](docs/helper-functions.md#kudojsmaybeToEither)
Expand Down
31 changes: 31 additions & 0 deletions docs/helper-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ These are the functions under the `kudoJS.*` namespace
- [`kudoJS.compose`](#kudojscompose)
- [`kudoJS.constant`](#kudojsconstant)
- [`kudoJS.fmap`](#kudojsfmap)
- [`kudoJS.assoc`](#kudojsassoc)
- [`kudoJS.bimap`](#kudojsbimap)
- [`kudoJS.chain`](#kudojschain)
- [`kudoJS.caseOf`](#kudojscaseof)
Expand All @@ -19,6 +20,7 @@ These are the functions under the `kudoJS.*` namespace
- [`kudoJS.liftA5`](#kudojslifta5)
- [`kudoJS.when`](#kudojswhen)
- [`kudoJS.prop`](#kudojsprop)
- [`kudoJS.pick`](#kudojspick)
- [`kudoJS.eitherToMaybe`](#kudojseithertomaybe)
- [`kudoJS.maybeToEither`](#kudojsmaybetoeither)

Expand Down Expand Up @@ -107,6 +109,22 @@ Takes a function and a functor, applies the function to each of the functor's va

----


### `kudoJS.assoc`

Associates a value to the specified key in the object. This returns a clone of the original object

`assoc(key: string, value: A, o: {[k:string]: A}): {[k:string]: A}`


| Param | Type | Description |
| --- | --- | --- |
| key | <code>string</code> | Key |
| value | <code>function</code> | Value to be assigned to key in object |
| o | <code>Object</code> | Object to which value needs to be assigned against the Key |

----

### `kudoJS.bimap`

Maps both sides of the disjunction
Expand Down Expand Up @@ -259,6 +277,19 @@ Returns a Maybe Just if value exists for the given key else returns a Nothing

----


### `kudoJS.pick`
Returns a Maybe Just containing the object with just the keys if values exists for the given keys else returns a Nothing

`pick( keys: Array<string>, o: { [k: string]: A}): Maybe<{[k:string]: A}>`

| Param | Type | Description |
| --- | --- | --- |
| keys | <code>Array<string></code> | Keys |
| o | <code>Object</code> | Key Value Object |

----

### `kudoJS.eitherToMaybe`
Converts an Either type to a Maybe Type

Expand Down
12 changes: 11 additions & 1 deletion src/adt/Maybe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,15 @@ const _prop = <A>(
if (!o) return Maybe.Nothing();
return key in o ? Maybe.fromNullable(o[key]) : Maybe.Nothing();
};

export const prop = curry(_prop);

function _pick(arr: Array<string>, o: any) {
if (!o || arr.length <= 0) return Maybe.Nothing();
const v = arr.reduce((acc: any, key) => {
if (o[key]) acc[key] = o[key];
return acc;
}, {});

return Object.keys(v).length > 0 ? Maybe.Just(v) : Maybe.Nothing();
}
export const pick = curry(_pick);
20 changes: 19 additions & 1 deletion src/adt/Maybe/maybe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import compose from "../../function/compose";
import fmap from "../../function/fmap";
import id from "../../function/id";
import Either from "../Either";
import Maybe, { eitherToMaybe, prop } from ".";
import Maybe, { eitherToMaybe, prop, pick } from ".";

// const laws: any = require("laws");
// console.log(laws);
Expand Down Expand Up @@ -216,5 +216,23 @@ test("Maybe", t => {
"prop returns a Nothing if value does not exists"
);

t.equals(
Maybe.isNothing(pick(["d"], undefined)),
true,
"should return 'Nothing' if object is undefined"
);

t.equals(
Maybe.isNothing(pick(["d"], {})),
true,
"should return 'Nothing' if object is does not have key"
);

t.equals(
pick(["d"], { d: 1 }).getValue().d,
1,
"should return 'Just(value)' if object has key"
);

t.end();
});
15 changes: 15 additions & 0 deletions src/function/assoc/assoc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as test from "tape";
import assoc from "./";

test("assoc", t => {
t.throws(
() => assoc("d", 1, undefined),
"should throw error if object is undefined"
);

t.equals(assoc("d", 1, {}).d, 1, "should set the value to a key in object");

const o = {};
t.notEquals(assoc("d", 1, o), o, "should not mutate original object");
t.end();
});
11 changes: 11 additions & 0 deletions src/function/assoc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import curry from "../curry";
import objectDeepClone from "../deepClone";

function assoc<A>(key: string, val: A, obj: any): any {
if (!obj || typeof obj !== "object")
throw new Error("assoc: expects an object");
const o = objectDeepClone(obj);
o[key] = val;
return o;
}
export default curry(assoc);
16 changes: 16 additions & 0 deletions src/function/deepClone/deepClone.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as test from "tape";
import deepClone from "./";

test("deep clone", t => {
t.equals(deepClone(1), 1, "should return value if type is not object");
const o1 = { d: 1 };
t.notEquals(deepClone(o1), o1, "should not mutate original object");
const s = { p: 1 };
const o2 = { d: s };
t.notEquals(
deepClone(o2).d,
o2.d,
"returned value should not be deep equal to original value"
);
t.end();
});
11 changes: 11 additions & 0 deletions src/function/deepClone/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function objectDeepClone(o: any) {
if (o == null || typeof o !== "object") return o;
const clone: any = Array.isArray(o) ? [] : {};
Object.keys(o).map(k => {
if (o.hasOwnProperty(k)) {
clone[k] = objectDeepClone(o[k]);
}
});

return clone;
}
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* istanbul ignore file */

import Either, { maybeToEither } from "./adt/Either";
import Maybe, { eitherToMaybe, prop } from "./adt/Maybe";
import Maybe, { eitherToMaybe, pick, prop } from "./adt/Maybe";
import Pair from "./adt/Pair";
import Reader from "./adt/Reader";
import State from "./adt/State";
import Task from "./adt/Task";
import assoc from "./function/assoc";
import bimap from "./function/bimap";
import caseOf from "./function/caseOf";
import chain from "./function/chain";
Expand All @@ -27,6 +28,7 @@ export default {
Reader,
State,
Task,
assoc,
bimap,
caseOf,
chain,
Expand All @@ -42,5 +44,6 @@ export default {
maybeToEither,
ocurry,
once,
pick,
prop
};

0 comments on commit 147e80f

Please sign in to comment.