0.3.0-build.14 • Published 1 year ago

@kyri123/k-javascript-utils v0.3.0-build.14

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Javascript Utility Functions

How to use

Import useAddons to your project index.ts / index.js file

import "@kyri123/k-javascript-utils/lib/useAddons"
// or if you want to use as esm
import "@kyri123/k-javascript-utils/esm/useAddons"

Array Functions

rm()

interface Array<T> {
	/**
	 * remove a element from the array
	 * @returns return the numbers of elements removed elements
	 * @param ShouldRemoveAllMatches should all removed that match the criteria by default false and remove only the first match
	 * @param Element Element to check
	 */
	rm( Element : T | ( ( value : T, index : number, obj : T[] ) => boolean ), ShouldRemoveAllMatches? : boolean ) : number;
}

const Arr = [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 11, 11, 11 ];
console.log( Arr.rm( ( V => V === 11 ), false ), Arr ); // 1, [ 5,  6,  7,  8,  9, 10, 12, 13, 14, 15, 11, 11,  11 ]

const Arr2 = [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 11, 11, 11 ];
console.log( Arr2.rm( 11, true ), Arr ); // 4, [ 5,  6,  7,  8,  9, 10, 12, 13, 14, 15, 11, 11, 11 ]

empty()

interface Array<T> {
	/**
	 * clear all elements from the array
	 */
	empty() : Array<T>;
}

const Arr = [ 8, 645, 1258, 457, 623 ]

Arr.empty()
console.log( Arr ) // [ ]

isEmpty()

interface Array<T> {
	/**
	 * Check if the array is empty
	 * @returns true if the array is empty
	 */
	isEmpty() : boolean;
}

const Arr = [ 8, 645, 1258, 457, 623 ]
console.log( Arr ) // false

Arr.length = 0; // Arr = [ ]
console.log( Arr ) // true

append()

interface Array<T> {
	/**
	 * modified version of contact
	 * @param OtherArray Array to compare with
	 * @param ClearOtherArray Should the other array be cleared? (default false)
	 * @returns the current Array
	 */
	append( OtherArray : T, ClearOtherArray? : boolean ) : Array<T>;
}

const Arr = [ 8, 645, 1258 ]
const Arr2 = [ 457, 623 ]

Arr.append( Arr2 )
console.log( Arr ) // [ 8, 645, 1258, 457, 623 ]
console.log( Arr2 ) // [ 457, 623 ]

Arr.append( Arr2, true )
console.log( Arr ) // [ 8, 645, 1258, 457, 623, 457, 623 ]
console.log( Arr2 ) // [ ]

addAtIndex()

interface Array<T> {
	/**
	 * Add a Object to any given Index (if no Index provided, add to the start of the array).
	 * @param Object Array element to add
	 * @param Index Index where to add the element
	 */
	addAtIndex( Object : T, Index? : number ) : Array<T>;
}

const Arr = [ 8, 645, 1258, 457, 623 ]

Arr.addAtIndex( 600, 2 )
console.log( Arr ) // [ 8, 645, 600, 1258, 457, 623 ]

rmFromIndex()

interface Array<T> {
	/**
	 * Remove an element from any given Index
	 * @param Index Index where the element should be removed (can also be an array to remove multiple elements)
	 */
	rmFromIndex( Index : number | number[] ) : Array<T>;
}

const Arr = [ 8, 645, 1258, 457, 623 ]

Arr.rmFromIndex( 1 )
console.log( Arr ) // [ 8, 1258, 457, 623 ]

Arr.rmFromIndex( [ 2, 3 ] )
console.log( Arr ) // [ 8, 1258 ]

swapElements()

interface Array<T> {
	/**
	 * Swap 2 Array Elements
	 * @returns if one of both Index invalid returns false
	 */
	swapElements( A : number, B : number ) : boolean;
}

const Arr = [ 8, 645, 1258, 457, 623 ]

Arr.swapElements( 1, 400 )
console.log( Arr ) // [ 8, 645, 1258, 457, 623 ]

Arr.swapElements( 1, 4 )
console.log( Arr ) // [ 8, 623, 1258, 457, 600 ]

idxValid()

interface Array<T> {
	/**
	 * Check if an element exists in any given Index
	 * @returns true if the index at valid (not undefined)
	 */
	idxValid( Index : number ) : boolean;
}

const Arr = [ 8, 645, 1258, 457, 623 ]

console.log( Arr.idxValid( -5 ) ) // false
console.log( Arr.idxValid( 2 ) ) // true
console.log( Arr.idxValid( 500 ) ) // false

replaceIdx()

interface Array<T> {
	/**
	 * Replace a element with a new given element at a given index
	 * @returns return the removed element or undefined if no element was replaced
	 */
	replaceIdx( NewObject : T, Index : number ) : T | undefined;
}

const Arr = [ 8, 645, 1258, 457, 623 ]

Arr.replaceIdx( 1000, 1 )
console.log( Arr.idxValid( -5 ) ) // [ 8, 1000, 1258, 457, 623 ]

Math Functions

clamp()

interface Math {
	/**
	 * clamp a number between `min` and `max`
	 * @returns the clamped Number
	 */
	clamp( min : number, value : number, max : number ) : number;
}

console.log( Math.clamp( 5, -1, 855 ) ) // 5
console.log( Math.clamp( 5, 8445, 855 ) ) // 855
console.log( Math.clamp( 5, 100, 855 ) ) // 100

rnd()

interface Math {
	/**
	 * Generate a random integer between `min` (inclusive) and `max` (inclusive)
	 * By default, this will return a random integer between `0` and `100`
	 * @returns a random number between `min` (inclusive) and `max` (inclusive)
	 */
	rnd( min : number, max : number ) : number;
}

console.log( Math.rnd() ) // 0-100
console.log( Math.rnd( 50, 75 ) ) // 50 - 75

String Functions

clearWs()

interface String {
	/**
	 * a modified version of the native `String.prototype.includes` that ignores any space characters and uppercase
	 * @param searchingFor Array element to add
	 * @param Ws ignore any space
	 * @returns true if the string includes the search term
	 */
	contains( searchingFor : string, Ws? : boolean ) : boolean;
}

const String : string = "Hello World!\nI'm a string...";
console.log( String.clearWs() ); // "HelloWorld!I'mastring..."

contains()

interface String {
	/**
	 * a modified version of the native `String.prototype.includes` that ignores any space characters and uppercase
	 * @param searchingFor Array element to add
	 * @param Ws ignore any space
	 * @returns true if the string includes the search term
	 */
	contains( searchingFor : string, Ws? : boolean ) : boolean;
}

const String : string = "Hello World!\nI'm a string...";
console.log( String.contains( "heLLoWorld!" ) ); // true
console.log( String.contains( "heLLoWorld!", false ) ); // false
0.3.0-build.14

1 year ago

0.3.0-build.13

1 year ago

0.2.1-build.12

1 year ago

0.2.0-build.11

1 year ago

0.2.0-build.10

1 year ago

0.2.0-build.9

1 year ago

0.2.0-build.8

1 year ago

0.1.1-build.7

1 year ago

0.1.0-build.6

1 year ago

0.1.0-build.4

1 year ago

0.1.0

1 year ago