Skip to content

Commit b437552

Browse files
Structures readme. Adds documentation for missing general functions. Adds documentation for missing object functions. (#30)
1 parent ea561d2 commit b437552

4 files changed

Lines changed: 174 additions & 32 deletions

File tree

README.md

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ A collection of common utility functions used across all our
66
[neeto](https://neeto.com) products. Try out the utility functions live at
77
[neetoCommons REPL](https://neeto-cist.neeto.com/).
88

9-
## Installation Instructions
9+
## Contents
10+
- [Installation](#installation)
11+
- [Usage](#usage)
12+
- [List of Pure Functions](#list-of-pure-functions)
13+
- [Development](#development)
14+
15+
## Installation
1016

1117
Install from npm:
1218

1319
```bash
14-
yarn add @bigbinary/neeto-cist
20+
yarn add @bigbinary/neeto-cist@latest
1521
```
1622

1723
Install the peer dependencies:
@@ -20,6 +26,26 @@ Install the peer dependencies:
2026
yarn add ramda
2127
```
2228

29+
## Usage
30+
31+
You can import all functions from `@bigbinary/neeto-cist`.
32+
33+
```js
34+
import { slugify } from "@bigbinary/neeto-cist";
35+
```
36+
37+
Exports several general utility functions that are used throughout neeto
38+
products. The functions are designed in a similar fashion as ramda so that they
39+
can easily interoperate with each other.
40+
41+
Pure functions were designed to be fail fast. If you call `findById(10, null)`,
42+
it will throw error saying that it can't iterate through `null`.
43+
44+
But for most such pure functions, there is a failsafe alternative available. The
45+
failsafe alternative function will be prefixed with `_`. Example:
46+
`_findById(10, null)` returns `null`, `_findById(10, undefined)` returns
47+
`undefined` and `_findById(10, [{ id: 10 }])` returns `{ id: 10 }`.
48+
2349
## List of Pure Functions
2450

2551
<table>
@@ -79,6 +105,9 @@ Name
79105
- [isNotEmpty](./docs/pure/general.md#isnotempty)
80106
- [isNot (alias notEquals)](./docs/pure/general.md#isnot_alias_notequals)
81107
- [isNotEqualDeep (alias notEqualsDeep)](./docs/pure/general.md#isnotequaldeep_alias_notequalsdeep)
108+
- [isNotPresent](./docs/pure/general.md#isnotpresent)
109+
- [isPresent](./docs/pure/general.md#ispresent)
110+
- [modifyWithImmer](./docs/pure/general.md#modifywithimmer)
82111

83112
</td>
84113
</tr>
@@ -87,35 +116,7 @@ Name
87116
</tbody>
88117
</table>
89118

90-
## Installation Instructions
91-
92-
Install from npm:
93-
94-
```bash
95-
yarn add "@bigbinary/neeto-cist@latest"
96-
```
97-
98-
## Usage
99-
100-
You can import all functions from `@bigbinary/neeto-cist`.
101-
102-
```js
103-
import { slugify } from "@bigbinary/neeto-cist";
104-
```
105-
106-
Exports several general utility functions that are used throughout neeto
107-
products. The functions are designed in a similar fashion as ramda so that they
108-
can easily interoperate with each other.
109-
110-
Pure functions were designed to be fail fast. If you call `findById(10, null)`,
111-
it will throw error saying that it can't iterate through `null`.
112-
113-
But for most such pure functions, there is a failsafe alternative available. The
114-
failsafe alternative function will be prefixed with `_`. Example:
115-
`_findById(10, null)` returns `null`, `_findById(10, undefined)` returns
116-
`undefined` and `_findById(10, [{ id: 10 }])` returns `{ id: 10 }`.
117-
118-
## Other references
119+
## Development
119120

120121
- [Development instructions](./docs/general/development-instructions.md)
121122
- [Building and releasing](./docs/general/building-and-releasing.md)

docs/pure/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ import { slugify } from "@bigbinary/neeto-cist";
8080
</td>
8181
<td style="vertical-align: top;">
8282

83+
- [matchesImpl](./objects.md#matchesimpl)
8384
- [transformObjectDeep](./objects.md#transformobjectdeep)
84-
- [preprocessForSerialization](./objects.md#preprocessforserialization)
8585
- [keysToCamelCase](./objects.md#keystocamelcase)
8686
- [keysToSnakeCase](./objects.md#keystosnakecase)
87+
- [serializeKeysToSnakeCase](./objects.md#serializekeystosnakecase)
88+
- [preprocessForSerialization](./objects.md#preprocessforserialization)
8789
- [deepFreezeObject](./objects.md#deepfreezeobject)
8890
- [matches](./objects.md#matches)
8991
- [filterNonNull](./objects.md#filternonnull)
@@ -102,14 +104,18 @@ import { slugify } from "@bigbinary/neeto-cist";
102104
</td>
103105
<td style="vertical-align: top;">
104106

107+
- [nullSafe](./general.md#nullsafe)
105108
- [noop](./general.md#noop)
106109
- [toLabelAndValue](./general.md#tolabelandvalue)
107110
- [getRandomInt](./general.md#getrandomint)
108111
- [randomPick](./general.md#randompick)
109112
- [dynamicArray](./general.md#dynamicarray)
110113
- [isNotEmpty](./general.md#isnotempty)
111114
- [isNot (alias notEquals)](./general.md#isnot_alias_notequals)
115+
- [isNotPresent](./general.md#isnotpresent)
116+
- [isPresent](./general.md#ispresent)
112117
- [isNotEqualDeep (alias notEqualsDeep)](./general.md#isnotequaldeep_alias_notequalsdeep)
118+
- [modifyWithImmer](./general.md#modifywithimmer)
113119
</td>
114120
<tr>
115121
</tbody>

docs/pure/general.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# General Functions
22

3+
## nullSafe
4+
5+
Curried: false
6+
Failsafe status: failsafe by default
7+
8+
This function takes a function as an argument and returns a curried version of the function.
9+
10+
<details>
11+
<summary>{click for more}</summary>
12+
13+
### Arguments:
14+
- `func`: A function that needs to be curried.
15+
16+
### Usage:
17+
```js
18+
const add = (a, b) => a + b;
19+
nullSafe(add)(1)(2);
20+
// Output: 3
21+
```
22+
23+
</details>
24+
325
## noop
426

527
Curried: false
@@ -152,6 +174,46 @@ isNot("Oliver", "Oliver"); // returns false
152174

153175
</details>
154176

177+
## isNotPresent
178+
179+
Curried: false
180+
Failsafe status: failsafe by default
181+
182+
Returns `true` if value is not present. Returns false otherwise.
183+
184+
<details>
185+
<summary>(click for more)</summary>
186+
187+
### Arguments:
188+
- One value to be checked
189+
190+
### Usage:
191+
```js
192+
isNotPresent(null);
193+
// Output: true
194+
```
195+
</details>
196+
197+
## isPresent
198+
199+
Curried: false
200+
Failsafe status: failsafe by default
201+
202+
Returns `true` if value is present. Returns false otherwise.
203+
204+
<details>
205+
<summary>(click for more)</summary>
206+
207+
### Arguments:
208+
- One value to be checked
209+
210+
### Usage:
211+
```js
212+
isPresent(null);
213+
// Output: false
214+
```
215+
</details>
216+
155217
## isNotEqualDeep (alias notEqualsDeep)
156218

157219
Curried: false
@@ -181,3 +243,32 @@ isNotEqualsDeep(object1, object2); //returns true
181243
```
182244

183245
</details>
246+
247+
## modifyWithImmer
248+
249+
Curried: true
250+
Failsafe status: Not failsafe
251+
252+
Receives a state and a modifier function. Returns modified state. Original state object is left untouched.
253+
254+
Modifier function receives draft state as the argument. We can use mutating methods like `push`, `pop`, `splice`, `delete` without any issues on that parameter.
255+
256+
<details>
257+
<summary>(click for more)</summary>
258+
259+
### Arguments:
260+
- `modifier`: The modifier function.
261+
- `data`: The state object.
262+
263+
### Usage:
264+
```js
265+
const modifier = (state) => state.names.push("Tom");
266+
const data = { names: ["Oliver", "Sam", "Jon"] };
267+
const updatedState = modifyWithImmer(modifier, data);
268+
269+
console.log(data);
270+
// { names: ["Oliver", "Sam", "Jon"] }
271+
console.log(updatedState);
272+
// { names: ["Oliver", "Sam", "Jon", "Tom"] }
273+
```
274+
</details>

docs/pure/objects.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
# Object operations
22

3+
## matchesImpl
4+
5+
Curried: false
6+
Failsafe status: failsafe by default
7+
8+
Non curried version of [matches](#matches). See matches for curried version.
9+
10+
Checks whether the given object matches the given pattern. Each primitive value (int, boolean, string, etc.) in the pattern should be same as the corresponding value in the object (deeply) and all conditions (functions) should be satisfied for a match.
11+
12+
<details>
13+
<summary>(click for more)</summary>
14+
15+
### Arguments:
16+
- `pattern`: The pattern object to be matched against the data.
17+
It's values can be either a value or a function.
18+
- value: Returns true if all the keys in pattern exist in data and the primitive values of those keys are identical to the data. Object values are compared recursively for inner primitives.
19+
- function: equality test is performed with corresponding object property. If equality fails, the function will be evaluated with the value of the corresponding property of the data. If function returns true, it will be considered as a match.
20+
- `data`: The data object.
21+
22+
### Usage:
23+
24+
```js
25+
const user = {
26+
firstName: "Oliver",
27+
address: { city: "Miami", phoneNumber: "389791382" },
28+
cars: [{ brand: "Ford" }, { brand: "Honda" }],
29+
};
30+
31+
matchesImpl({ firstName: "Oliver" }, user); // true
32+
matchesImpl({ address: { city: "Miami" } }, user); // true
33+
matchesImpl({ cars: [{ brand: "Ford" }] }, user); // true
34+
matchesImpl({ firstName: "Sam" }, user); // false
35+
matchesImpl({ address: { country: "US" } }, user); // false
36+
matchesImpl({ cars: [{ brand: "Honda" }] }, user); // false
37+
// array index as object key
38+
matchesImpl({ cars: { 1: { brand: "Honda" } } }, user); // true
39+
// conditional functions
40+
matchesImpl({ cars: arr => arr.length === 2 }, user); // true
41+
// point-free functions with ramda
42+
matchesImpl({ firstName: startsWith("O") }, user); // true
43+
```
44+
45+
</details>
46+
347
## transformObjectDeep
448

549
Curried: false

0 commit comments

Comments
 (0)