jackknife
TypeScript icon, indicating that this package has built-in type declarations

4.0.0 • Public • Published

jackknife logo

jackknife

npm npm bundle size license Issues


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


chunks

chunks<T>(array: T[], chunkSize: number): T[][]

Split an array in a defined number of chunks.

const array = ['a', 'b', 'c', 'd', 'e'];
const arrayChunks = chunks<string>(array, 2);
console.log(arrayChunks); // [['a', 'b'], ['c', 'd'], ['e']];

shuffle

shuffle<T>(array: T[]): T[]

Shuffle an array.

const array = ['a', 'b', 'c', 'd', 'e'];
const shuffled = shuffle<string>(array);
console.log(shuffled); // e.g. ['b', 'd', 'c', 'a', 'e']

sort

sort<T>( array: T[], getField: (item: T) => string | number | Date, sortType: 'asc' | 'desc' = 'asc' ): T[]

Sort an array of objects by an object property defined by the getField function.

interface TestItem { name: string; }
const array: TestItem[] = [{ name: 'b' }, { name: 'c' }, { name: 'a' }];
const sorted = sort<TestItem>(array, item => item.name);
console.log(sorted); // [{ name: 'a' }, { name: 'b' }, { name: 'c' }]

unique

unique<T>(array: T[]): T[]

Remove the duplicated values from an array.

const array = ['a', 'b', 'a', 'b'];
const result = unique<string>(array);
console.log(result); // ['a', 'b']

random

random(min: number, max: number, decimals = 0): number

Generate a random number (integer or float) in the defined range (inclusive).

const randomInteger = random(5, 10);
console.log(randomInteger); // e.g. 7

const randomFloat = random(5, 10, 3);
console.log(randomFloat); // e.g. 8.842

round

round(value: number, decimals = 2): number

Round a number with the defined precision of decimals.

const rounded = round(0.15666, 3);
console.log(rounded); // 0.157

identifier

identifier(): number

Generate a numeric identifier useful as an ID value.

const id = identifier();
console.log(id); // 15764928947236012

range

range(min: number, max: number): number[]

Generate an array of numbers in a defined range.

const array = range(8, 12);
console.log(array); // [8, 9, 10, 11, 12]

degToRad

degToRad(degrees: number): number

Convert degrees to radians.

const radians = degToRad(180);
console.log(radians); // PI value

radToDeg

radToDeg(radians: number): number

Convert radians to degrees.

const degrees = radToDeg(Math.PI);
console.log(degrees); // 180

datetime

datetime(date: Date | string | number, withTime = true): string

Get a human-readable format of a date value.

const now = datetime(Date.now());
console.log(now); // e.g. '10/04/2024 23:46:24 CET'

bytes

bytes(value: number, decimals = 2): string

Get a human-readable format of a bytes value.

const label = bytes(1024 * 1024);
console.log(label); // 1.00 MB

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.

// Default
console.log(code()); // e.g. 'nk3IGBPiOh'

// Predefined set
console.log(code(10, 'letters')); // e.g. 'LTaezrWFom'
console.log(code(10, 'numbers')); // e.g. '9060379844'

// Custom set
console.log(code(10, 'abc123$%&')); // e.g. '12&%c222ac'

color

color(): string

Generate a random color in hexadecimal notation.

const color = color();
console.log(color); // e.g. '#BC37D3'

pad

pad(value: number | string, length: number, symbol: string): string

Add a start padding to a value.

const padded = pad(2, 4, '0');
console.log(padded); // 0002

nested

nested<T>(root: Record<any, unknown>, path: string): T | undefined

Get the nested value of a object property by a string path.

const test = { a: { b: [{ c: 'banana' }] } };
const nested = nested<string>(test, 'a.b.0.c');
console.log(nested); // 'banana'

querystring

querystring(): Record<string, string>

Get the query string of the current url.

const query = querystring();
console.log(query); // e.g. { param: 'value' }

LabelValue

interface LabelValue<T> { label: string; value: T; }

An useful interface to cast the objects with the common label-value pair structure.

const stringCasted: LabelValue<string> = {
  label: 'A label',
  value: 'A string';
};

const numberCasted: LabelValue<number> = {
  label: 'A label',
  value: 8;
};

const genericCasted: LabelValue<any> = {
  label: 'A label',
  value: ...a mysterious thing
};

getCookie

getCookie(name: string): string | undefined

Get the value of a cookie.

const token = getCookie('token');
console.log(token); // cookie value

setCookie

setCookie(name: string, value: string, options: CookieOptions = {}): void

Set the value of a cookie.

setCookie('token', 'abc');

CookieOptions are:

class CookieOptions {
  expires?: Date;
  maxAge?: number;
  domain?: string;
  path?: string;
  secure?: boolean;
  httpOnly?: boolean;
  sameSite?: 'Strict' | 'Lax' | 'None';
}

For the details of CookieOptions see MDN reference.


deleteCookie

deleteCookie(name: string): void

Delete a cookie.

deleteCookie('token');

Package Sidebar

Install

npm i jackknife

Weekly Downloads

6

Version

4.0.0

License

MIT

Unpacked Size

26.1 kB

Total Files

21

Last publish

Collaborators

  • zosma