jsfunx
v1.1.2
Published
JavaScript utility functions for cleaner, more readable code
Maintainers
Readme
jsfunx
The main reason why this library was created is to:
use
import { isType, number } from "jsfunx"; // ESM
// or
// "use strict"; // CJS
// const { isType, number } = require("jsfunx"); // CJS
/**
* Adds two numbers together and returns the result.
*
* @param {number} num1 - The first number.
* @param {number} num2 - The second number.
* @returns {number} The sum of `num1` and `num2`.
* @throws {TypeError} If one or both arguments are not numbers.
*/
function add(num1, num2) {
// 35 char
if (isType([num1, num2], number)) {
return num1 + num2;
}
throw new TypeError("invalid arguments types");
}
console.log(add(1, 3));
// or
console.log(add(Number(1), Number(3)));in place of
// "use strict"; // CJS
/**
* Adds two numbers together and returns the result.
*
* @param {number} num1 - The first number.
* @param {number} num2 - The second number.
* @returns {number} The sum of `num1` and `num2`.
* @throws {TypeError} If one or both arguments are not numbers.
*/
function add(num1, num2) {
// 59 char
if (typeof num1 === "number" && typeof num2 === "number") {
return num1 + num2;
}
throw new TypeError("invalid arguments types");
}
console.log(add(1, 3));
// or
console.log(add(Number(1), Number(3)));and
import { isType, number, string } from "jsfunx"; // ESM
// or
// "use strict"; // CJS
// const { isType, number, string } = require("jsfunx"); // CJS
/**
* Prints a message with the given name and age.
*
* @param {string} name - The person's name.
* @param {number} age - The person's age.
* @returns {void} This function does not return a value.
* @throws {TypeError} If `name` is not a string or `age` is not a number.
*/
function fun(name, age) {
// 44 char
if (isType([name, age], [string, number])) {
console.log(`your name is ${name} and your age is ${age}`);
return;
}
throw new TypeError("invalid arguments types");
}
fun("mohamed", 23);
// or
fun(String("mohamed"), Number(23));in place of
// "use strict"; // CJS
/**
* Prints a message with the given name and age.
*
* @param {string} name - The person's name.
* @param {number} age - The person's age.
* @returns {void} This function does not return a value.
* @throws {TypeError} If `name` is not a string or `age` is not a number.
*/
function fun(name, age) {
// 58 char
if (typeof name === "string" && typeof age === "number") {
console.log(`your name is ${name} and your age is ${age}`);
return;
}
throw new TypeError("invalid arguments types");
}
fun("mohamed", 23);
// or
fun(String("mohamed"), Number(23));and
import { instancesof } from "jsfunx"; // ESM
// or
// "use strict"; // CJS
// const { instancesof } = require("jsfunx"); // CJS
/**
* Adds two numbers together and returns the result.
*
* @param {Number} num1 - The first number.
* @param {Number} num2 - The second number.
* @returns {Number} The sum of `num1` and `num2`.
* @throws {TypeError} If one or both arguments are not numbers objects.
*/
function add(num1, num2) {
// 40 char
if (instancesof([num1, num2], Number)) {
return new Number(num1 + num2);
}
throw new TypeError("invalid arguments types");
}
console.log(add(new Number(1), new Number(3)));in place of
// "use strict"; // CJS
/**
* Adds two numbers together and returns the result.
*
* @param {Number} num1 - The first number.
* @param {Number} num2 - The second number.
* @returns {Number} The sum of `num1` and `num2`.
* @throws {TypeError} If one or both arguments are not numbers objects.
*/
function add(num1, num2) {
// 55 char
if (num1 instanceof Number && num2 instanceof Number) {
return new Number(num1 + num2);
}
throw new TypeError("invalid arguments types");
}
console.log(add(new Number(1), new Number(3)));and
import { instancesof } from "jsfunx"; // ESM
// or
// "use strict"; // CJS
// const { instancesof } = require("jsfunx"); // CJS
/**
* Prints a message with the given name and age.
*
* @param {String} name - The person's name.
* @param {Number} age - The person's age.
* @returns {void} This function does not return a value.
* @throws {TypeError} If `name` is not a string object or `age` is not a number object.
*/
function fun(name, age) {
// 49 char
if (instancesof([name, age], [String, Number])) {
console.log(`your name is ${name} and your age is ${age}`);
return;
}
throw new TypeError("invalid arguments types");
}
fun(new String("mohamed"), new Number(23));in place of
// "use strict"; // CJS
/**
* Prints a message with the given name and age.
*
* @param {String} name - The person's name.
* @param {Number} age - The person's age.
* @returns {void} This function does not return a value.
* @throws {TypeError} If `name` is not a string object or `age` is not a number object.
*/
function fun(name, age) {
// 54 char
if (name instanceof String && age instanceof Number) {
console.log(`your name is ${name} and your age is ${age}`);
return;
}
throw new TypeError("invalid arguments types");
}
fun(new String("mohamed"), new Number(23));but not just this there is other funcs you can see the source code and check all the tests in the main fun.
Installation
You can install jsfunx via npm:
npm install jsfunxLicense
This project is licensed under the MIT LICENSE - see the LICENSE for more details.
