@rjweb/utils
Advanced tools
Comparing version 1.5.2 to 1.5.3
@@ -0,1 +1,6 @@ | ||
## 1.5.3 | ||
- Add `time.fn` | ||
- Add typedocs | ||
## 1.5.2 | ||
@@ -2,0 +7,0 @@ |
{ | ||
"name": "@rjweb/utils", | ||
"version": "1.5.2", | ||
"version": "1.5.3", | ||
"description": "Easy and Lightweight Utilities", | ||
@@ -5,0 +5,0 @@ "module": "lib/esm/index.js", |
@@ -24,2 +24,3 @@ "use strict"; | ||
module.exports = __toCommonJS(time_exports); | ||
var import_types = require("util/types"); | ||
class Time { | ||
@@ -117,3 +118,28 @@ /** | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
}, | ||
/** | ||
* See how long a function takes to run | ||
* @example | ||
* ``` | ||
* import { time } from "@rjweb/utils" | ||
* | ||
* const [ time, result ] = time.fn(async() => { | ||
* await time.wait(time(20).s()) | ||
* }) | ||
* | ||
* console.log(time, result) // 20000 undefined | ||
* ``` | ||
* @since 1.5.3 | ||
*/ | ||
fn(fn) { | ||
const startTime = performance.now(); | ||
const res = fn(); | ||
if ((0, import_types.isPromise)(res)) | ||
return new Promise(async (resolve) => { | ||
const asyncRes = await res; | ||
return resolve([performance.now() - startTime, asyncRes]); | ||
}); | ||
else | ||
return [performance.now() - startTime, res]; | ||
} | ||
}); |
{ | ||
"name": "@rjweb/utils", | ||
"version": "1.5.2", | ||
"version": "1.5.3", | ||
"description": "Easy and Lightweight Utilities", | ||
@@ -5,0 +5,0 @@ "module": "lib/esm/index.js", |
@@ -0,1 +1,2 @@ | ||
import { isPromise } from "util/types"; | ||
class Time { | ||
@@ -93,2 +94,27 @@ /** | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
}, | ||
/** | ||
* See how long a function takes to run | ||
* @example | ||
* ``` | ||
* import { time } from "@rjweb/utils" | ||
* | ||
* const [ time, result ] = time.fn(async() => { | ||
* await time.wait(time(20).s()) | ||
* }) | ||
* | ||
* console.log(time, result) // 20000 undefined | ||
* ``` | ||
* @since 1.5.3 | ||
*/ | ||
fn(fn) { | ||
const startTime = performance.now(); | ||
const res = fn(); | ||
if (isPromise(res)) | ||
return new Promise(async (resolve) => { | ||
const asyncRes = await res; | ||
return resolve([performance.now() - startTime, asyncRes]); | ||
}); | ||
else | ||
return [performance.now() - startTime, res]; | ||
} | ||
@@ -95,0 +121,0 @@ }); |
@@ -79,3 +79,17 @@ import { Multiply } from "ts-arithmetic"; | ||
*/ wait(ms: number): Promise<void>; | ||
/** | ||
* See how long a function takes to run | ||
* @example | ||
* ``` | ||
* import { time } from "@rjweb/utils" | ||
* | ||
* const [ time, result ] = time.fn(async() => { | ||
* await time.wait(time(20).s()) | ||
* }) | ||
* | ||
* console.log(time, result) // 20000 undefined | ||
* ``` | ||
* @since 1.5.3 | ||
*/ fn<Fn extends () => Promise<any> | any>(fn: Fn): Fn extends () => Promise<infer R> ? Promise<[number, Awaited<R>]> : Fn extends () => infer R_1 ? [number, R_1] : never; | ||
}; | ||
export default _default; |
{ | ||
"name": "@rjweb/utils", | ||
"version": "1.5.2", | ||
"version": "1.5.3", | ||
"description": "Easy and Lightweight Utilities", | ||
@@ -10,2 +10,3 @@ "module": "lib/esm/index.js", | ||
"build": "rm -rf lib && tsc && esbuild `find src -type f -name \"*.ts\"` --platform='node' --ignore-annotations --format='cjs' --outdir='lib/cjs' && esbuild `find src -type f -name \"*.ts\"` --platform='node' --ignore-annotations --format='esm' --outdir='lib/esm' && cp package.json lib/cjs/pckg.json && cp package.json lib/esm/pckg.json", | ||
"docs": "rm -rf docs && typedoc --out docs src/index.ts && sed -i -e 's|<meta charSet=\"utf-8\"/>|<meta charSet=\"utf-8\"/><link rel=\"icon\" type=\"image/png\" href=\"https://img.rjansen.de/rjweb/icon.png\">|' docs/**/*.html && chmod -R a+rw docs", | ||
"test": "yarn build && node test" | ||
@@ -40,2 +41,3 @@ }, | ||
"esbuild": "^0.17.2", | ||
"typedoc": "^0.25.1", | ||
"typescript": "^5.1.0" | ||
@@ -42,0 +44,0 @@ }, |
112
README.MD
<h1 align="center">Welcome to @rjweb/utils 👋</h1> | ||
<center> | ||
<a href="https://www.npmjs.com/package/@rjweb/utils" target="_blank"> | ||
<img alt="Version" src="https://img.shields.io/npm/v/@rjweb/utils.svg"> | ||
</a> | ||
<a href="https://github.com/rotvproHD/rjweb-utils#readme" target="_blank"> | ||
<img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" /> | ||
</a> | ||
<a href="https://github.com/rotvproHD/rjweb-utils/graphs/commit-activity" target="_blank"> | ||
<img alt="Maintenance" src="https://img.shields.io/badge/Maintained%3F-yes-green.svg" /> | ||
</a> | ||
[<img src="https://img.rjansen.de/rjweb/types.svg" height="75px" style="margin-top:0px;margin-left:-15px;">](https://types.rjweb-utils.rjansen.de) | ||
Easy and Lightweight Utilities | ||
@@ -33,103 +26,4 @@ </center> | ||
Generating a Random Number in Range | ||
```js | ||
const { number } = require('@rjweb/utils') | ||
All Utilities and example are available in the [typedocs](https://types.rjweb-utils.rjansen.de). | ||
const min = 50 | ||
const max = 100 | ||
const result = number.generate(min, max) | ||
``` | ||
Generating a Random String | ||
```js | ||
const { string } = require('@rjweb/utils') | ||
const result = string.generate({ | ||
length: 25, | ||
numbers: true, | ||
symbols: true, | ||
uppercase: true, | ||
lowercase: true | ||
}) | ||
``` | ||
Encrypting a String | ||
```js | ||
const { string } = require('@rjweb/utils') | ||
const result = string.encrypt('Hello', 'secret', { | ||
output: 'hex' | ||
}) | ||
``` | ||
Decrypting a String | ||
```js | ||
const { string } = require('@rjweb/utils') | ||
const result = string.decrypt('df4d0fe46e0210d4ef46368a6c3d56bb', 'secret', { | ||
input: 'hex' | ||
}) | ||
``` | ||
Hashing a String | ||
```js | ||
const { string } = require('@rjweb/utils') | ||
const result = string.hash('Hello', { | ||
salt: 'secret', | ||
algorithm: 'sha256', | ||
output: 'hex' | ||
}) | ||
``` | ||
Check if a Host is reachable | ||
```js | ||
const { network } = require('@rjweb/utils') | ||
// host, port, timeout ms | ||
const result = await network.test('127.0.0.1', 80, 5000) | ||
``` | ||
Parsing Options | ||
```js | ||
const { object } = require('@rjweb/utils') | ||
const original = { | ||
ssl: true, | ||
maxRequests: 10000, | ||
other: { | ||
clock: true, | ||
date: { | ||
enabled: true, | ||
timezone: 'UTC' | ||
} | ||
} | ||
} | ||
const userProvided = { | ||
ssl: false, | ||
other: { | ||
date: { | ||
timezone: 'CET' | ||
} | ||
} | ||
} | ||
const result = object.deepParse(original, userProvided) | ||
/** | ||
* { | ||
* ssl: false, | ||
* maxRequests: 10000, | ||
* other: { | ||
* clock: true, | ||
* date: { | ||
* enabled: true, | ||
* timezone: 'CET' | ||
* } | ||
* } | ||
* } | ||
*/ | ||
``` | ||
## Author | ||
@@ -136,0 +30,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1050294
80
3888
6
43
6