1.0.2 • Published 2 years ago

anytool v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Anytool, a staticly collection of useful methods and classes!

npm install anytool

Basic Usage

import * as Anytool from "anytool"; // es6 syntax
// const Anytool = require("anytool"); // common javascript syntax

Or

import { randomNumber, unique, } from "anytool"; // es6 syntax
// const { randomNumber, unique, } = require("anytool"); // common javascript syntax

Methods

randomItem(array, limit, unique)

argumenttypeoptionaldescriptiondefault
arrayArray | ObjectAny array
limitNumberyesLimit of randomly picked items, will return array of items, wheter specified
uniqueBooleanyesArray of randomly picked items must be unique

Examples

randomItem([1, 2, 3, 4]); // 3
randomItem([1, 2, 3, 4], 2); // [2, 3], might be [2, 2]
randomItem([1, 2, 3, 4], 2, true); // [2, 1]
randomItem({name1: "Dann", name2: "Not Dann", name3: "Always Dann"}); // ["name2", "Not Dann"], [key, value]
randomItem({name1: "Dann", name2: "Not Dann", name3: "Always Dann"}, 2); // [["name3", "Always Dann"], ["name2", "Not Dann"]]

unique(array)

argumenttypeoptionaldescriptiondefault
arrayArrayAny Array

Examples

unique([1, 2, 3, 3, 4, 5, 5, 6]); // [1, 2, 3, 4, 5, 6]

equal(primary, secondary1, secondary2, ..., secondaryN)

argumenttypeoptionaldescriptiondefault
primaryanyAny Value to check with
secondaryanyValues to check with primary value

Examples

equal(1, 1); // true
equal(1, "1"); // 
equal(1, 1, 2, 1); // 
equal([1, 2], [1, 2], [1, 2]); // true
equal([1, 2], [2, 1]); // 
equal({name: "Vardan"}, {"name": "Vardan"}); // true
equal({name: "Vardan", age: 18}, {age: 18, "name": "Vardan"}); // true
equal({name: "Vardan", age: 18}, {age: 18, "name": "Vardan"}, {name: "Diana", age: 18}); // 

randomNumber(min, max, dontRound)

argumenttypeoptionaldescriptiondefault
minNumberMinumum value of range
maxNumberMaximum value of range
dontRoundBooleanyesWheter the respone must be a float number

Examples

randomNumber(1, 20); // **13**
randomNumber(1, 10, true); // 3.2317609836...

shortenText(text)

argumenttypeoptionaldescriptiondefault
textStringAny Text

Examples

shortenText("Give me your love and I'll give you my sunshine", 10); // "Give me .."

currencyFormat(number)

argumenttypeoptionaldescriptiondefault
numberNumberAny number

Examples

currencyFormat(1437); // 1.4k
currencyFormat(3133917); // 3.1M

formatNumber(number, locale)

argumenttypeoptionaldescriptiondefault
numberNumberAny number
localeLocale StringyesLocale of countryen-us

Examples

formatNumber(12345679); // 123,456,789
formatNumber(123456789, "ru-ru"); // 123 456,789
formatNumber(123456789, "ar-EG"); // ١٢٣٤٥٦٫٧٨٩

removeFromArray(array, filter)

argumenttypeoptionaldescriptiondefault
arrayArrayAny Array
filterany | #IndexElement of array or index (formant #Index)

Examples

removeFromArray([1, 2, 3, 4], "#2"); // [1, 2, 4];
removeFromArray(["Dann", "Gago", "Meri", "Gago"], "Gago"); // ["Dann", "Meri"]

removeFromArrayExtended(array, filter)

argumenttypeoptionaldescriptiondefault
arrayArrayAny Array
filterRemoveFromArrayExtendedOptionsElement of array or index (formant #Index)

Examples

removeFromArrayExtended(["Dann", "Gago", "Meri", "Gago"], {indexes: [1, 3]}); // ["Dann", "Meri"]
removeFromArrayExtended(["Dann", "Gago", "Meri", "Gago"], {elements: ["Dann", "Meri"]}); // ["Gago", "Gago"]
removeFromArrayExtended(["Dann", "Gago", "Meri", "Gago"], {elements: ["Dann"], indexes: [3]}); // ["Gago", "Meri"]

uuid(length, options)

argumenttypeoptionaldescriptiondefault
lengthNumberLength of unique id
optionsUuidOptionsyesType and Style

Examples

uuid(10); // "d12Lc01dsL"
uuid(10, {only: "<>?"}); // "<<>?><<>?>"
uuid(10, {letters: "only"}); // "skFqlcPOcH"
uuid(10, {letters: "only", letterType: "lowercase"}); // "skdxchqkpo", default "both"
uuid(10, {numbers: "only"}); // "3789123752"
uuid(10, {letters: "only", aditional: "@#$"}); // "FDj$dx@A#x"

resultOf(numbers, operation)

argumenttypeoptionaldescriptiondefault
numbersNumber[]Any Numbers
operation"*" | "/" | "+" | "-"yesOperation"+"

Examples

resultOf([1, 2, 3]); // 6, default "+"
resultOf([1, 2, 3], "+"); // 6
resultOf([1, 2, 3], "*"); // 6
resultOf([1, 2, 3], "-"); // -4
resultOf([1, 2, 3], "/"); // 0.1666666666666667

reverseString(text)

argumenttypeoptionaldescriptiondefault
textStringAny text

Examples

reverseString("Hello everyone!"); // "!enoyreve olleH"

memoize(fn, doIf)

argumenttypeoptionaldescriptiondefault
fnFunctionAny Function
doIfFunction (returns boolean)yesDo filtering function when cached value was found

Examples

memoize((array1, array2) => array1.every(value => array2.includes(value)));
memoize((array1, array2) => array1.every(value => array2.includes(value)), (prevArgs, nextArgs) => {
   if (prevArgs[0][0] !== nextArgs[0][0])
      return false;

   return true;
});

numberArray(start, end)

argumenttypeoptionaldescriptiondefault
startNumberStart of range0
endNumberyesEnd of a rangestart

Examples

numberArray(5); // [1, 2, 3, 4, 5]
numberArray(5, 10); // [6, 7, 8, 9, 10]

Classes

See File

Extended Map for holding values new Chest()

Usage examples

JavaScript

const ages = new Chest();

ages.set("Dann", 18);

TypeScript

const ages = new Chest<string, number>();

ages.set("Dann", 18);

ages.set("Gago", "18"); // error

Some methods

ages.first(); // first value
ages.random(); // random value
ages.delete("Dann"); // delete item
ages.setMany(age => age < 18, 18); // Setting new values after filtering
ages.has("Dann"); // Wheter there is item with spec. key
ages.hasAll("Dann", "Gago"); // Wheter there are all spec. keys
ages.hasAny("Dann", "Gago"); // Wheter there is at least one key of spec keys

See File

Simple Cooldown system new Cooldown(time)

argumenttypeoptionaldescriptiondefault
timeNumberCooldown time in milliseconds

Examples

const commandLimiter = new Cooldown(5000);

console.log(commandLimiter.isLimited(anyUser.id))

Types

keyvalueTypedescription
indexesNumber[]Array of element indexes to be removed
elementsany[]Elements that, when matched, will be removed
keyvalueTypedescriptionexample
onlyStringUse only these characteresdj*24_cx@"
numbers"only" | falseUse only numbers 0-9 or false for disabling numbers"only
letters"only" | falseUse only letters a-z or A-Z or false for disabling letters"only
aditionalStringUse aditional symbols"_^&?>"
letterType"uppercase" | "lowercase" | "both"Letter type (default is "both")"lowercase