Skip to content

Commit

Permalink
[H-0007] Update readme.md (#11)
Browse files Browse the repository at this point in the history
* [H-0007] Updated readme.md

* [H-0007] Added test for "removeDeepByKey"

* [H-0007] Added coveralls package

* [H-0007] Minor fixes to readme.md

* [H-0007] Update version v0.0.5

* [H-0007] Moved the test for "removeByKey" to the correct folder

---------

Co-authored-by: Kovalenkov Pavel <p.kovalenkov@cian.ru>
  • Loading branch information
kovalenkovpu and Kovalenkov Pavel committed Jan 29, 2023
1 parent 7f9b615 commit 99d41aa
Show file tree
Hide file tree
Showing 6 changed files with 1,077 additions and 39 deletions.
29 changes: 16 additions & 13 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@ name: Node.js CI

on:
push:
branches: [ "master" ]
branches: ["master"]
pull_request:
branches: [ "master" ]
branches: ["master"]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run lint
- run: npm test
- run: npx tsc
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: npm ci
- run: npm run lint
- run: npm test
- run: npx tsc
- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
235 changes: 216 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,227 @@
# Handlities
[![npm version](https://badge.fury.io/js/handilities.svg)](https://badge.fury.io/js/handilities)
[![Coverage Status](https://coveralls.io/repos/github/kovalenkovpu/handilities/badge.svg?branch=master)](https://coveralls.io/github/kovalenkovpu/handilities?branch=master)

# Handilities

> List of handy utilities for JS/TS projects in web and nodejs environments.
## Prerequisites

No specific version of nodejs and npm is required, but for the usage as an npm package please install [nodejs](https://nodejs.org/en/download/) of your choice.

## Table of contents

- [Project Name](#project-name)
- [Prerequisites](#prerequisites)
- [Table of contents](#table-of-contents)
- [Installation](#installation)
- [API](#api)
- [TS Utilities](#ts-utils)
- [KeyOf type](#key-of)
- [ValueOf type](#value-of)
- [objectKeys](#object-keys)
- [Common Utilities](#common-utils)
- [removeByKey](#remove-by-key)
- [removeDeepByKey](#remove-deep-by-key)
- [Contributing](#contributing)
- [Versioning](#versioning)
- [Authors](#authors)
- [License](#license)

## Installation

**BEFORE YOU INSTALL:** please read the [prerequisites](#prerequisites)

To install and set up the library, run:

```sh
$ npm install -S handilities
```

Or if you prefer using Yarn:

```sh
$ yarn add handilities
```

## API

---

## List of utilities
### <a id="ts-utils"></a>TS Utilities

#### <a id="key-of"></a> KeyOf

```ts
type KeyOf<T>;
```

Utility type that constructs a type consisting of own enumerable keys of a given object.

Example:

```ts
type TKey = KeyOf<{ a: 'a', b: 'b' }>;

const key1: TKey = 'a';
const key2: TKey = 'b';
const key3: TKey = 'c'; -> Type '"c"' is not assignable to type '"a" | "b"'.
```

#### <a id="value-of"></a> ValueOf

```ts
type ValueOf<T>;
```

Utility type that constructs a type consisting of values of own enumerable keys of a given object.

Example:

```ts
type TValue = ValueOf<{ a: 'a', b: 'b' }>

const value1: TValue = 'a';
const value2: TValue = 'b';
const value3: TValue = 'c'; -> Type '"c"' is not assignable to type 'TValue'.
```

#### <a id="object-keys"></a> objectKeys()

```ts
objectKeys(target: Record<string, any>);
```

Utility function that returns an array of own enumerable keys of a given object.

#### Options

| Name | Type | Default value |
| ------ | -------- | ------------- |
| target | `Object` | - |

Example:

```ts
const car = {
wheels: 4,
output: '200 HP',
name: 'Tesla',
};

// Compare:
const keys1 = Object.keys(car); -> const keys1: string[]
const keys2 = objectKeys(car); -> const keys2: ("wheels" | "output" | "name")[]
```

### <a id="common-utils"></a>Common Utilities

#### <a id="remove-by-key"></a> removeByKey()

```ts
removeByKey(path: string | string[], target: Record<string, any>);
```

Removes key-value pair in the object by a provided `path`. <br/>
Returns new object if the `path` was successfully resolved. <br/> Returns `target` if the `path` wasn't resolved.

#### Options

| Name | Type | Default value |
| ------ | ------------------ | ------------- |
| path | `string, string[]` | - |
| target | `Object` | - |

Examples:

```ts
// Removes 'b' key in the target object
const target = {
a: 'A',
b: 'B',
};

const result = removeByKey('a', target); -> { b: 'B' }

// Removes 'c' key in the target object
const target = {
a: {
c: 'C'
},
b: 'B',
};

const result = removeByKey(['a', 'c'], target); -> { a: {}, b: 'B' }

// Path isn't resolved
const target = {
a: 'a',
b: 'B',
};

const result = removeByKey(['c'], target); -> { a: 'a', b: 'B' }
const targetEqualsResult = result === target; -> true
```

#### <a id="remove-deep-by-key"></a> removeDeepByKey()

```ts
removeDeepByKey(path: string[], target: Record<string, any>);
```

Removes key-value pair in the object by a provided `path`. <br/> Also, if deleted key-value was the only one in an object, removes that empty object and recursively checks previous key-value pair for the same. <br/>
Returns new object if the `path` was successfully resolved. <br/> Returns `target` if the `path` wasn't resolved.

#### Options

| Name | Type | Default value |
| ------ | ---------- | ------------- |
| path | `string[]` | - |
| target | `Object` | - |

Examples:

```ts
// Removes 'b' key in the target object
const target = {
a: 'A',
b: 'B',
};

const result = removeDeepByKey(['a'], target); -> { b: 'B' }

// Removes 'c' key in the target object, and recursively removes empty "parents"
const target = {
a: {
c: 'C'
},
b: 'B',
};

const result = removeDeepByKey(['a', 'c'], target); -> { b: 'B' }

// Path isn't resolved
const target = {
a: 'a',
b: 'B',
};

const result = removeDeepByKey(['c'], target); -> { a: 'a', b: 'B' }
const targetEqualsResult = result === target; -> true
```

### Types
## Contributing

- `KeyOf`, `ValueOf` shortcuts
- `objectKeys`, `objectValues` micro utils
- more to come
_[TODO]:_ Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.

### React - *in progress*
## Versioning

- `usePrevious` hook
- `useDidMount` hook
- more to come
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/kovalenkovpu/handilities/tags).

### Common
## Authors

- `removeByKey`
- `removeDeepByKey`
- `pluralize` (RU locale first, maybe EN locale) - *in progress*
- more to come
- **Pavel Kovalenkov** - [kovalenkovpu](https://github.com/kovalenkovpu)

## Platforms
## License

- browser JS/TS
- server JS/TS
- ReactJS JS/TS
[ISC](https://en.wikipedia.org/wiki/ISC_license)
Loading

0 comments on commit 99d41aa

Please sign in to comment.