1.2.9 • Published 2 months ago
@lv00/toolkit v1.2.9
🛠️ ToolKit
Versatile JavaScript Toolkit for Everyday Use
Table of Contents
Installation
npm install @lv00/toolkit # or
pnpm install @lv00/toolkit # or
yarn add @lv00/toolkit # or
bun install @lv00/toolkit
Usage
import { ... } from "@lv00/toolkit";
Check
Easily check data type and format.
import { Is } from "@lv00/toolkit";
is.ip('127.0.0.1'); // true
is.ip('1.1.1'); // false
is.email('test@test.local'); // true
is.email('test@test'); // false
... // use intellisense to see more
CSV
Create CSV on the fly.
import { CSV } from '@lv00/toolkit';
const csv = new CSV({ header: ['name', 'age'] });
// Add a line to the CSV
csv.addLine(['John', 20]);
// Add value one by one
csv.addSequentially('Jane & Doe');
csv.addSequentially(21);
csv.addSequentially('Jack');
csv.addSequentially(22);
// Get the CSV as string
csv.toString('|');
// Clear the CSV and keep the header
csv.clear();
// Convert the CSV to a JS object
csv.toObject();
// [
// { name: 'John', age: 20 },
// { name: 'Jane & Doe', age: 21 },
// { name: 'Jack', age: 22 }
// ]
// Read CSV from string
const csv2 = CSV.readString('name,age\r\nJohn,20\r\nJane,21\r\nJack,22', ',');
csv2.toString('|');
Gen
Generate pseudo-random data
random Name
import { Gen } from '@lv00/toolkit';
const newName = Gen.randomName(); // antonelli-xenodochial
random Number
import { Gen } from '@lv00/toolkit';
const newNumber = Gen.randomNumber(0, 100); // 42
Log
Easily log to various outputs.
import { Log } from 'lv00/toolkit';
const { Logger, Console, Level, File, Csv, Json } = Log;
// Define the endpoint of the log (transporter)
const logger = new Logger({
t: [
new Console(), // Log all levels to the console
new File({ l: [Level.INFO], path: 'log.txt' }), // Log only INFO to a text based file
new Csv({ l: [Level.ERROR], path: 'log.csv' }), // Log only ERROR to a CSV file
new Json({ l: [Level.DEBUG], path: 'log.json' }), // Log only DEBUG to a JSON file
],
});
logger.log('Hello, World!'); // log to all transports registered for the level INFO
logger.log('Hello, World!', Level.ERROR); // log to all transports registered for the level ERROR
logger.info('Hello, World!');
logger.ok('Hello, World!');
logger.warn('Hello, World!');
logger.error('Hello, World!');
logger.debug('Hello, World!');
// Log an error
new Promise((_, reject) => {
reject(new Error('Promise Error'));
}).catch((e) => logger.catch(e));
Parser
Parse data to object.
Dot
import { Parser } from "@lv00/toolkit";
const data = `battery.charge: 100
battery.charge.low: 20
battery.runtime: 995
battery.type: PbAc
device.mfr: EATON
device.model: Ellipse PRO 650
device.serial: P354M05BE0
device.type: ups
driver.name: usbhid-ups`;
const result = dot(data);
console.log(result);
// Will return the following object
{
battery: {
charge: {
_value: "100",
low: "20",
},
runtime: "995",
type: "PbAc",
},
device: {
mfr: "EATON",
model: "Ellipse PRO 650",
serial: "P354M05BE0",
type: "ups",
},
driver: {
name: "usbhid-ups",
},
}
Plugin
Lazy loading of plugins.
import { Plugin } from "@lv00/toolkit";
// defined a plugin to exptend from (src/Plugin/index.ts)
export default class ExamplePlugin extends Plugin {
name: string;
constructor(name: string) {
super();
this.name = name;
}
execute() {
return `Plugin ${this.name} executed`;
}
}
// Create all the plugins you need
// Plugin 1 (src/Plugin/ExamplePlugin1.ts)
export default class ExamplePlugin1 extends ExamplePlugin {
constructor() {
super("1");
}
}
// Plugin 2 (src/Plugin/ExamplePlugin2.ts)
export default class ExamplePlugin2 extends ExamplePlugin {
constructor() {
super("2");
}
}
// Import the plugins
Plugin.import("./src/Plugin/")
.forEach((plugin) => {
const p = new plugin();
p.execute();
});
// or
// Load all the plugins
Plugin.load("./src/Plugin/")
.forEach((plugin) => {
plugin.execute();
});
Scanner
Read folder synchronously without headache
import { Scanner } from '@lv00/toolkit';
const scanner = Scanner.readFolder('./path/to/folder');
// Available properties
scanner.parent;
scanner.files;
scanner.folders;
scanner.path;
// return a array of files
scanner.flat();
// return json and remove circular references
scanner.toJson();
Unit
Convert data to human readable format
import { Unit } from './src';
// By default it will use 1000 as base
// and K, M, G, T, P as units
const size = Unit.sizeToUnitAuto(1000);
size.n; // 1
size.unit; // K
size.toString(); // 1K
size.round(2); // 1.00
size.roundToString(2); // 1.00K
Url
Conveniently build urls with query parameters.
import { Url, Uri } from '@lv00/toolkit';
const url = 'https://lv0.eu/';
const query = {
p: '2',
brand: ['sony', 'microsoft', 'nintendo'],
};
const q = Url.buildUrlWithQuery(url, query);
console.log(q.toString()); // https://lv0.eu/?p=2&brand=sony&brand=microsoft&brand=nintendo'
const uri = new Uri(
url,
"GET"
query,
)
const json = await uri.fetchJson()
1.2.9
2 months ago
1.2.8
2 months ago
1.2.7
2 months ago
1.2.6
2 months ago
1.2.5
3 months ago
1.2.4
3 months ago
1.2.3
3 months ago
1.2.2
4 months ago
1.2.1
4 months ago
1.2.0
8 months ago
1.1.11
9 months ago
1.1.9
9 months ago
1.1.10
9 months ago
1.1.8
10 months ago
1.1.7
10 months ago
1.1.6
1 year ago
1.1.5
1 year ago
1.1.4
1 year ago
1.1.3
1 year ago
1.1.2
1 year ago
1.1.1
1 year ago
1.1.0
1 year ago
1.0.9
1 year ago
1.0.8
1 year ago
1.0.7
1 year ago
1.0.6
1 year ago
1.0.5
1 year ago
1.0.4
1 year ago
1.0.3
1 year ago
1.0.2
1 year ago
1.0.1
1 year ago
1.0.0
1 year ago