@suouzuki/nodeutils

1.3.2 • Public • Published

NodeUtils

This library was created specifically for personal use, but you can also use it! It only contains things that I, suouzuki, need in my projects.

Installation

To install it in your project is quite simple! Just use the code below

npm install @suouzuki/nodeutils


This is...

It checks if the value is indeed that, in the example above we used 'isString', which checks if it's a string, but there are various others such as:

isJSON(value) -> Check if the value is JSON or valid JSON.

isJSON({ name: "John", age: 30, city: "New York" }); // true
isJSON('{"name":"John","age":30,"city":"New York"}'); // true

isJSON('{"name":"John","age":30,"city":"New York"'); // false (invalid JSON)
isJSON([]); // false

isEmpty(value) -> Check if the value is empty. (String, Array, Object, Map)

isEmpty('  '); // true (empty string)
isEmpty([]); // true (empty array)
isEmpty(new Map()); // true (empty map)
isEmpty({}); // true (empty object)

isEmpty('Hello World!'); // false (non-empty string)
isEmpty([1, 2, 3]); // false (non-empty array)
isEmpty({ key: 'value' }); // false (non-empty object)

isPrimitive(value) -> Check if the value is a primitive (null, number, string, boolean, undefined, or symbol).

isPrimitive(42); // true
isPrimitive("Hello World!"); // true
isPrimitive({}); // false

isBoolean(value)* -> Check if the value is a Boolean.

isBoolean(false); // true
isBoolean("false"); // false

isString(value) -> Check if the value is a String.

isString("Hello World!"); // true
isString(42); // false

... -> And there are numerous functions like these that I can't mention all of.


String, Number, and Array

Useful functions for manipulating: String, Number, and Array. There are many more functions, but I'll only include two of each (my favorites for each).

- String

limit(string, limit, final) -> Limits the length of a string and adds a final character.

const limitedString = limit("Olá, eu me chamo Suouzuki e programo Node.js!", 20, "...");
// Returns: "Olá, eu me chamo Suo..."

verify(string, ...search) -> Verifies the presence of specific substrings in a string.

const searchResults = verify("Olá, quem é você?", "Olá", "você", "eu");
// Returns: [true, true, false]

...

- Number

toRoman(number) -> Convert a number to its Roman numeral representation.

toRoman(42) // "XLII"
toRoman(1999) // "MCMXCIX"

abbrev(number) -> Convert a large number to an abbreviated format (e.g., 1000 to 1K, 1000000 to 1M).

abbrev(1000) // "1K"
abbrev(1000000) // "1M"

...

- Array

get(array, search) -> Get an item from an array based on a search text using Fuse.js.

const arr = ["Nino.", "José.", "Suouzuki."];
const value = get(arr, "josé"); // [{ name: "José.", ID: 1, score: 0.02 }]

random(array, path) -> Returns a random element from an array, based on a probability value.

const arr = [
  { value: 'Suouzuki.', probability: 0.2 },
  { value: 'José.', probability: 0.3 },
  { value: 'Nino.', probability: 0.5 },
];

const randomElement = random(arr, 'probability'); // { value: 'Nino.', probability: 0.5 } or other.

...


Custom Error

It creates a custom error, allowing you to choose the name and description of the error.

Error.custom("myError", "Oops! An unexpected error occurred.");
// myError: Oops! An unexpected error occurred.

Custom Console

To create a custom console, it's very easy. Using the 'format' method transforms the text into the desired format, making it colorful and personalized (please note that it only accepts strings, so make sure to convert your text to a string before formatting). Additionally, we also use the 'addText' function, which allows you to add a new template for formatting.

logger.addText("success", "{type:color}{type}</>({time}): {text} {path}");
console.success = function success(text) {
  console.log(logger.format(text, { time: { snow: true, format: "HH-mm-ss" }}));
};
console.fail = function fail(text) {
  console.log(logger.format(text, { time: { snow: true, format: "HH-mm-ss" }, path: true }));
};

Collection

Creates a collector similar to discord.js but changing certain things.

const myCollection = new collection();
myCollection.set("value1", { test: true });
myCollection.set("value2", "hello");

Color

Adds color to texts using the 'colors' library. To add a new color, you can use '<color=>' followed by the desired color name. For example, '<color=red>' will set the text color to red. Don't forget to include '</>' to indicate where you want the color to end. For instance, if you want the word 'hello' to be yellow in 'Hello World!', you can do '<color=yellow>Hello</> World!'.

We also have styles available. Instead of '<color=>', you can use '<style=>' followed by the desired style type.

For backgrounds, we use '<background=>' followed by the desired color after the '=' sign.

Oh, and if you're wondering how to make the colors bright, just add a '+' sign after the color name. For example, '<color=red+>VERY RED</>'.

Do you want to know which styles and colors are available? Check out the 'colors' library. If you want more styles, here are some that we have created ourselves:

  • Undefined (Working....)
console.log(color("<color=red>Hello</>, are you <style=random>okay</>?"));

Package Sidebar

Install

npm i @suouzuki/nodeutils

Weekly Downloads

3

Version

1.3.2

License

MIT

Unpacked Size

123 kB

Total Files

14

Last publish

Collaborators

  • suouzuki