Skip to content

Commit

Permalink
Merge pull request #14 from vodyani/beta
Browse files Browse the repository at this point in the history
Beta v1.1.1
  • Loading branch information
ChoGathK authored Jul 18, 2022
2 parents 8cf4b57 + d557fa3 commit 6db7fa8
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 61 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## [1.1.1-beta.1](https://github.com/vodyani/utils/compare/v1.1.0...v1.1.1-beta.1) (2022-07-18)


### Bug Fixes

* base type ([c2f3e3c](https://github.com/vodyani/utils/commit/c2f3e3ca2178f9bc32c522f556641e4b2d46eb30))
* options ([47de105](https://github.com/vodyani/utils/commit/47de10508f5bb1ff2402717ae2ba6fc35570dc86))

# [1.1.0](https://github.com/vodyani/utils/compare/v1.0.0...v1.1.0) (2022-07-13)


Expand Down
264 changes: 259 additions & 5 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Vodyani utils

building...
😁 Provides common utility functions used in server-side development.

[![Npm](https://img.shields.io/npm/v/@vodyani/utils/latest.svg)](https://www.npmjs.com/package/@vodyani/utils)
[![Npm](https://img.shields.io/npm/v/@vodyani/utils/beta.svg)](https://www.npmjs.com/package/@vodyani/utils)
Expand All @@ -10,16 +10,270 @@ building...
![Workflow](https://github.com/vodyani/utils/actions/workflows/release.yml/badge.svg)
[![codecov](https://codecov.io/gh/vodyani/utils/branch/master/graph/badge.svg?token=CI3WEJUN6L)](https://codecov.io/gh/vodyani/utils)

## Features

building...

## Installation

```sh
npm install @vodyani/utils
```

## Usage

|Features|Type|Description|
|:-:|:-:|:-:|
|[isDictionary](#isdictionary)|object|Checks whether the current object is a dictionary object.|
|[isKeyof](#iskeyof)|object|Determines whether the object contains the current attribute.|
|[toMatch](#tomatch)|object|Walks deeply through the object and returns the property value corresponding to the specified parameter.|
|[toRestore](#torestore)|object|Restores to the corresponding object based on the depth of the specified property and property value.|
|[toBuffer](#tobuffer)|stream|Convert Stream data to buffer.|
|[toStream](#tostream)|stream|Convert Buffer data to stream.|
|[toDelay](#todelay)|promise|Wait during an asynchronous function call.|
|[toCycle](#tocycle)|promise|Create a recurring scheduled task.|
|[toRetry](#toretry)|promise|Create a caller that can retry the callback function.|

### isDictionary

Checks whether the current object is a dictionary object.

⚠️ Dictionary is not in (`Map`/`Set`/`Symbol`/`Buffer`/`Array`/`Stream`/`RegExp`/`Function`/`String`/`Number`/`Boolean`).

**Params**

|param|type|description|
|:-:|:-:|:-:|
|`dict`|`any`|*The object to judge.*|

**return**

`boolean`

**example**
```ts
isDictionary({}) // true
isDictionary(Object()) // true
isDictionary({ name: 'dict' }) // true
isDictionary(new String()) // false
isDictionary(new Number()) // false
isDictionary(new Boolean()) // false
isDictionary(new Map()) // false
isDictionary(new Set()) // false
isDictionary(new Array()) // false
isDictionary(Buffer.from([])) // false
isDictionary(new Stream()) // false
isDictionary(null) // false
isDictionary(undefined) // false
```

### isKeyof

Determines whether the object contains the current attribute.

**param**

|param|type|description|
|:-:|:-:|:-:|
|`obj`|`object`|*The object to judge.*|
|`key`|`string`/`number`/`symbol`|*The property of object.*|

**return**

`boolean`

**example**

```ts
const sym = Symbol(1);
const obj = { test: 'test' };
const obj1 = { 0: 'test' };
const obj2 = { [sym]: 'test' };

isKeyof(obj, 'test') // true
isKeyof(obj, 'test1') // false
isKeyof(obj1, 0) // true
isKeyof(obj2, sym) // true
```

### toMatch

Walks deeply through the object and returns the property value corresponding to the specified parameter.

**Params**

|param|type|description|
|:-:|:-:|:-:|
|`obj`|`any`|*The object to judge.*|
|`token`|`string`|*The properties of the object and splice them with `rule`.*|
|`rule`|`string`|*The rules for splicing, by default, are `.`*|

**return**

`T` | `any`

**example**

```ts
toMatch({ job: { name: 'job' }}, 'job.name') // 'job'
toMatch({ cat: { info: { name: 'mimi' } }}, 'cat.info.name') // 'mimi'
```

### toRestore

Restores to the corresponding object based on the depth of the specified property and property value.

**Params**

|param|type|description|
|:-:|:-:|:-:|
|`value`|`any`|*The object properties value.*|
|`token`|`string`|*The properties of the object and splice them with `rule`.*|
|`rule`|`string`|*The rules for splicing, by default, are `.`*|

**return**

`T` | `any`

**example**

```ts
toRestore('job', 'job.name') // { job: { name: 'job' }}
toRestore('mimi', 'cat.info.name') // { cat: { info: { name: 'mimi' } }
```

### toBuffer

Convert Stream data to buffer.

**Params**

|param|type|description|
|:-:|:-:|:-:|
|`stream`|`Stream`|*The stream data.*|

**return**

`Promise<Buffer>`

**example**

```ts
await toBuffer(data) // => stream data
```

### toStream

Convert Buffer data to stream.

**Params**

|param|type|description|
|:-:|:-:|:-:|
|`buffer`|`Buffer`|*The buffer data.*|
|`encoding`|`ascii`/`utf8`/`utf-8`/`utf16le`/`ucs2`/`ucs-2`/`base64`/`base64url`/`latin1`/`binary`/`hex`|*The encoding type.*|

**return**

`Promise<Duplex>`

**example**

```ts
await toStream(data) // => buffer data
```

### toDelay

Wait during an asynchronous function call.

**Params**

|param|type|description|
|:-:|:-:|:-:|
|`delay`|`number`|*The waiting time (The time unit is milliseconds).*|

**return**

`Promise<void>`

**example**

```ts
await toDelay(2000) // To waiting 2000 milliseconds ...
```

### toCycle

Create a recurring scheduled task.

**Params**

|param|type|description|
|:-:|:-:|:-:|
|`callback`|`Function`|*A callback function that needs to be called periodically.*|
|`options`|`CycleOptions`|*Configuration options when triggered.*|

*CycleOptions*

```ts
export interface CycleOptions {
/** The cycle time, default 1000 (The time unit is milliseconds). */
interval: number;
/** The callback argument. */
args?: any[];
}
```

**return**

`CycleClearHandler` Returns a method that closes timed calls.

*CycleClearHandler*

```ts
export interface CycleClearHandler {
/** The closes timed calls. */
close: () => void
}
```

**example**

```ts
await toCycle(fn, { interval: 1000 }) // The fn function is executed every 2000 milliseconds ...
```

### toRetry

Create a recurring scheduled task.

**Params**

|param|type|description|
|:-:|:-:|:-:|
|`callback`|`Function`|*The callback function to call.*|
|`options`|`RetryOptions`|*Configuration options when triggered.*|

*RetryOptions*

```ts
export interface RetryOptions {
/** The retry count. */
count: number;
/** The waiting time (The time unit is milliseconds). */
delay: number;
/** The callback argument.*/
args?: any[];
}
```

**return**

`Promise<T>` | `Promise<any>`

**example**

```ts
await toRetry(fn, { delay: 100, count: 3 }) // The `fn` will wait 100 milliseconds after a failure and then call again three times
```

## Team

|[![ChoGathK](https://github.com/chogathK.png?size=100)](https://github.com/chogathK)|
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "@vodyani/utils",
"license": "MIT",
"version": "1.1.0",
"version": "1.1.1-beta.1",
"author": "ChoGathK",
"description": "utils",
"description": "😁 Provides common utility functions used in server-side development.",
"homepage": "https://github.com/vodyani/utils#readme",
"repository": {
"type": "git",
Expand Down Expand Up @@ -67,16 +67,16 @@
"@commitlint/cli": "16.2.3",
"@commitlint/config-conventional": "16.2.1",
"@types/jest": "27.4.1",
"@types/lodash": "^4.14.182",
"@types/lodash": "4.14.182",
"@types/node": "16.11.26",
"@vodyani/eslint-config": "1.0.10",
"@vodyani/eslint-config": "^1.0.10",
"@vodyani/semantic-release": "1.1.1",
"husky": "7.0.4",
"jest": "27.5.1",
"ts-jest": "27.1.3",
"typescript": "4.6.2"
},
"dependencies": {
"lodash": "^4.17.21"
"lodash": "4.17.21"
}
}
14 changes: 7 additions & 7 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
export interface RetryOptions {
/** retry count. */
/** The retry count. */
count: number;
/** waiting time (The time unit is milliseconds). */
/** The waiting time (The time unit is milliseconds). */
delay: number;
/** callback argument. */
/** The callback argument.*/
args?: any[];
}

export interface CycleOptions {
/** cycle time, default 1000 (The time unit is milliseconds). */
interval?: number;
/** callback argument. */
/** The cycle time, default 1000 (The time unit is milliseconds). */
interval: number;
/** The callback argument. */
args?: any[];
}

export interface CycleClearHandler {
/** closes timed calls */
/** The closes timed calls. */
close: () => void
}
Loading

0 comments on commit 6db7fa8

Please sign in to comment.