JavaScript utility functions for cleaner, more readable code
npm install jsfunx


The main reason why this library was created is to:
``js
import { isType, number, useStrict } from "jsfunx"; // ESM
// or
// "use strict"; // CJS
// const { isType, number, useStrict } = 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) {
useStrict(add, arguments);
// 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)));
`
`js
// "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)));
`
`js
import { isType, number, string, useStrict } from "jsfunx"; // ESM
// or
// "use strict"; // CJS
// const { isType, number, string, useStrict } = 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) {
useStrict(fun, arguments);
// 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));
`
`js
// "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));
`
`js
import { instancesof, useStrict } from "jsfunx"; // ESM
// or
// "use strict"; // CJS
// const { instancesof, useStrict } = 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) {
useStrict(add, arguments);
// 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)));
`
`js
// "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)));
`
`js
import { instancesof, useStrict } from "jsfunx"; // ESM
// or
// "use strict"; // CJS
// const { instancesof, useStrict } = 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) {
useStrict(fun, arguments);
// 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));
`
`js
// "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.
useStrict() was used to enforce strict argument passing in functions.
You can install jsfunx via npm:
`bash``
npm install jsfunx
This project is licensed under the MIT LICENSE - see the LICENSE for more details.