jackknife
v4.0.0
Published
The useful compact tool.
Downloads
24
Maintainers
Readme
jackknife
Description
jackknife is a multi-tool that provides a set of utility functions to face the wild projects.
All the blades are typescript compatible.
Installation
npm i jackknife
In order to use any API you have to import them:
// Typescript
import { clone, getPassword, sort } from 'jackknife';
// or Node
const { clone, getPassword, sort } = require('jackknife');
Browser support
Latest versions of Chrome, Firefox, Opera, Safari and Edge.
Test, Bugs and Feature requests
If you want to execute unit test run:
npm test
For bugs and feature requests, please create an issue.
API
- Array
- Number
- String
- Object
- Cookie
chunks
chunks<T>(array: T[], chunkSize: number): T[][]
Split an array in a defined number of chunks.
shuffle
shuffle<T>(array: T[]): T[]
Shuffle an array.
sort
Sort an array of objects by an object property defined by the getField function.
unique
unique<T>(array: T[]): T[]
Remove the duplicated values from an array.
random
random(min: number, max: number, decimals = 0): number
Generate a random number (integer or float) in the defined range (inclusive).
round
round(value: number, decimals = 2): number
Round a number with the defined precision of decimals.
identifier
identifier(): number
Generate a numeric identifier useful as an ID value.
range
range(min: number, max: number): number[]
Generate an array of numbers in a defined range.
degToRad
degToRad(degrees: number): number
Convert degrees to radians.
radToDeg
radToDeg(radians: number): number
Convert radians to degrees.
datetime
datetime(date: Date | string | number, withTime = true): string
Get a human-readable format of a date value.
bytes
bytes(value: number, decimals = 2): string
Get a human-readable format of a bytes value.
code
code(length = 10, chars = 'all'): string
Generate a random string.
chars is the set of characters to use. It can be a predefined set between 'alphanumeric', 'letters', 'lower-letters', 'upper-letters', 'numbers', 'symbols', 'all' or a custom set.
color
color(): string
Generate a random color in hexadecimal notation.
pad
pad(value: number | string, length: number, symbol: string): string
Add a start padding to a value.
nested
nested<T>(root: Record<any, unknown>, path: string): T | undefined
Get the nested value of a object property by a string path.
querystring
querystring(): Record<string, string>
Get the query string of the current url.
LabelValue
interface LabelValue<T> { label: string; value: T; }
An useful interface to cast the objects with the common label-value pair structure.
getCookie
getCookie(name: string): string | undefined
Get the value of a cookie.
setCookie
setCookie(name: string, value: string, options: CookieOptions = {}): void
Set the value of a cookie.
CookieOptions are:
For the details of CookieOptions see MDN reference.
deleteCookie
deleteCookie(name: string): void
Delete a cookie.