npm.io
1.0.2 • Published 4 years ago

anytool

Licence
MIT
Version
1.0.2
Deps
0
Size
96 kB
Vulns
0
Weekly
0

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

randomItem(array, limit, unique)

argument type optional description default
array Array | Object Any array
limit Number yes Limit of randomly picked items, will return array of items, wheter specified
unique Boolean yes Array 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

unique(array)

argument type optional description default
array Array Any Array
Examples
unique([1, 2, 3, 3, 4, 5, 5, 6]); // [1, 2, 3, 4, 5, 6]
equal

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

argument type optional description default
primary any Any Value to check with
secondary any Values 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

randomNumber(min, max, dontRound)

argument type optional description default
min Number Minumum value of range
max Number Maximum value of range
dontRound Boolean yes Wheter the respone must be a float number
Examples
randomNumber(1, 20); // **13**
randomNumber(1, 10, true); // 3.2317609836...
shortenText

shortenText(text)

argument type optional description default
text String Any Text
Examples
shortenText("Give me your love and I'll give you my sunshine", 10); // "Give me .."
currencyFormat

currencyFormat(number)

argument type optional description default
number Number Any number
Examples
currencyFormat(1437); // 1.4k
currencyFormat(3133917); // 3.1M
formatNumber

formatNumber(number, locale)

argument type optional description default
number Number Any number
locale Locale String yes Locale of country en-us
Examples
formatNumber(12345679); // 123,456,789
formatNumber(123456789, "ru-ru"); // 123 456,789
formatNumber(123456789, "ar-EG"); // ١٢٣٤٥٦٫٧٨٩
removeFromArray

removeFromArray(array, filter)

argument type optional description default
array Array Any Array
filter any | #Index Element 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

removeFromArrayExtended(array, filter)

argument type optional description default
array Array Any Array
filter RemoveFromArrayExtendedOptions Element 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

uuid(length, options)

argument type optional description default
length Number Length of unique id
options UuidOptions yes Type 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

resultOf(numbers, operation)

argument type optional description default
numbers Number[] Any Numbers
operation "*" | "/" | "+" | "-" yes Operation "+"
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

reverseString(text)

argument type optional description default
text String Any text
Examples
reverseString("Hello everyone!"); // "!enoyreve olleH"
memoize

memoize(fn, doIf)

argument type optional description default
fn Function Any Function
doIf Function (returns boolean) yes Do 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

numberArray(start, end)

argument type optional description default
start Number Start of range 0
end Number yes End of a range start
Examples
numberArray(5); // [1, 2, 3, 4, 5]
numberArray(5, 10); // [6, 7, 8, 9, 10]

Classes

Chest

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
Cooldown

See File

Simple Cooldown system new Cooldown(time)

argument type optional description default
time Number Cooldown time in milliseconds
Examples
const commandLimiter = new Cooldown(5000);

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

Types

RemoveFromArrayExtendedOptions
key valueType description
indexes Number[] Array of element indexes to be removed
elements any[] Elements that, when matched, will be removed
UuidOptions
key valueType description example
only String Use only these characteres dj*24_cx@"
numbers "only" | false Use only numbers 0-9 or false for disabling numbers "only
letters "only" | false Use only letters a-z or A-Z or false for disabling letters "only
aditional String Use aditional symbols "_^&?>"
letterType "uppercase" | "lowercase" | "both" Letter type (default is "both") "lowercase