npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

utils-for-js

v1.0.9

Published

Javascript utilities

Downloads

3

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