utils-for-js

Javascript utilities

Usage no npm install needed!

<script type="module">
  import utilsForJs from 'https://cdn.skypack.dev/utils-for-js';
</script>

README

Utils for JavaScript

The package contains several utils functions that can be helpful for every day use.

  • Compare objects
  • Compare arrays
  • Check if an array or an object is empty
  • Make array unique
  • Check if there is arrays intersection
  • Get min value of an array of numbers
  • Get max value of an array of numbers
  • Check if variable is an integer
  • Create random number
  • Cache function result
  • Calculate how much time a function takes to run

Compare objects

import { compare } from "utils-for-js";

compare({ fname: "pera" }, { fname: "pera" }); // true
compare({ fname: "pera" }, { fname: "trta" }); // false
compare(
  { fname: "pera", address: { street: "street1", number: 10 } },
  { fname: "pera", address: { street: "street1", number: 10 } }
); // true
const a = { test: 123 };
const b = { test: 123 };
compare(a, b); // true

Compare arrays

import { compare } from "utils-for-js";

compare([1, 2, 3], [1, 2, 3]); // true
compare([1, 2, 3], [1]); // false
compare([1, 2, [3, 4, 5]], [1, 2, [3, 4, 5]]); // true
const a = [{ fname: "pera" }];
const b = [{ fname: "pera" }];
compare(a, b); // true

Check if an array or an object is empty

import { isEmpty } from "utils-for-js";

isEmpty([]); // true
isEmpty([1, 2, 3]); // false
isEmpty({}); // true
isEmpty({ fname: "pera" }); // false

Make array unique

import { uniqueArray } from "utils-for-js";

uniqueArray([1, 2, 3, 3, 4, 4, 4]); // [1, 2, 3, 4]
uniqueArray(["a", "b", "a", "c"]); // [a, b, c]
let person = { fname: "pera" };
uniqueArray([person, person]); // [ { fname: 'pera' } ]

Check if there is arrays intersection Suggestion by Tim Anthony Manuel

import { hasIntersection } from "utils-for-js";

hasIntersection([1, 2, 3], [1, 5, 7]); // true
hasIntersection([1, 2, 3], [4, 5, 7]); // false

Get min value of an array of numbers

import { minValArray } from "utils-for-js";

minValArray([1, 2, 3, 3, 4, 4, 4]); // 1

Get max value of an array of numbers

import { maxValArray } from "utils-for-js";

maxValArray([1, 2, 3, 3, 4, 4, 4]); // 4

Check if variable is an integer

import { isInt } from "utils-for-js";

isInt(1); // true
isInt(1.1); // false
isInt(false); // true
isInt("test"); // false
isInt([1, 2, 3]); // false
isInt({ fname: "pera" }); // false
isInt(null); // false

Create random number

import { randomInt } from "utils-for-js";

randomInt(1, 10); // 7
randomInt(1, 10, true); // 10

Cache function result

import { cacheFunc } from "utils-for-js";

const add = (a, b) => {
  return a + b;
};

// second parameter will inculde logs, false is default value
const cachedAdd = cacheFunc(add, true);
cachedAdd(2, 2); // returns 4 and results are not from cache at this point - prints out 'no cache'
cachedAdd(2, 2); // returns 4 and results are from cache - prints out 'cache'

Calculate how much time a function takes to run

import { timeSpent } from "utils-for-js";

function test() {
  for (let i = 0; i < 1000000; i++) {}
}

const testWithTimeSpent = timeSpent(test);
// const testWithTimeSpent = timeSpent(test, false); // will disable calculation results

testWithTimeSpent(); // test: 7.239ms