jsfunx
v1.7.0
Published
JavaScript utility functions for cleaner, more readable code
Maintainers
Readme
jsfunx
The main reason why this library was created is to:
use
import { type } from "jsfunx"; // ESM
// or
// "use strict"; // CJS
// const { type } = require("jsfunx"); // CJS
console.log(type({})); // object Object (more details)
console.log(type(Object)); // function Object (more details)
console.log(type(null)); // null (not a bug)in place of
// "use strict"; // CJS
console.log(typeof {}); // object (less details)
console.log(typeof Object); // function (less details)
console.log(typeof null); // object (bug)and
import { isType } from "jsfunx"; // ESM
// or
// "use strict"; // CJS
// const { isType } = require("jsfunx"); // CJS
// 17 char + more readable.
console.log(isType(1, Number));in place of
// "use strict"; // CJS
// 21 char + less readable.
console.log(typeof 1 === "number");and
import { isType, useStrict } from "jsfunx"; // ESM
// or
// "use strict"; // CJS
// const { isType, 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);
// 28 char + more readable.
if (isType([num1, num2], Number)) {
return num1 + num2;
}
throw new TypeError("invalid arguments types");
}
console.log(add(1, 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) {
// 52 char + less readable.
if (typeof num1 === "number" && typeof num2 === "number") {
return num1 + num2;
}
throw new TypeError("invalid arguments types");
}
console.log(add(1, 3));and
import { isType, useStrict } from "jsfunx"; // ESM
// or
// "use strict"; // CJS
// const { isType, 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);
// 37 char + more readable.
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);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) {
// 51 char + less readable.
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);and
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);
// 33 char + more readable.
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) {
// 48 char + less readable.
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, 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);
// 42 char + more readable.
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) {
// 47 char + less readable.
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.
Note
useStrict() was used to enforce strict argument passing in functions.
License
This project is licensed under the MIT LICENSE - see the LICENSE for more details.
