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

@hdoc/js-utils

v1.2.0

Published

Different utils for javascript

Downloads

1

Readme

js-utils

Different utils for javascript

Index

Installation

npm install --save-dev @hdoc/js-utils

Arithmetic functions

Provides sum and average functions for array of numbers

const { sum, avg } = require('@hdoc/js-utils');

const numbers = [1, 2, 3];

// logs 6
console.log(sum(numbers));

// logs 3
console.log(avg(numbers));

Array functions

Provides functions and validations for arrays

const {
  arrayOfNumbers,
  arrayOfStrings,
  randomElement,
  sort,
} = require('@hdoc/js-utils');

const numbers = [1, 2, 3];
const strings = ['string', 'word', 'random'];

// logs true
console.log(arrayOfNumbers(numbers));
console.log(arrayOfStrings(strings));

// logs false
console.log(arrayOfNumbers('string', { name: 'object' }, [null]));
console.log(arrayOfStrings(1, [], null));

// logs a random element of array numbers
console.log(randomElement(numbers));

// logs numbers and strings in ascendent order
console.log(sort(numbers));
console.log(sort(strings));

console.log(sort(numbers, 1));
console.log(sort(strings, 1));

// logs numbers and strings in descendent order
console.log(sort(numbers, -1));
console.log(sort(strings, -1));

// logs numbers and strings in random order
console.log(sort(numbers, 'r'));
console.log(sort(strings, 'r'));

Note: randomElement() and sort() returns a new array.

Equation functions

Provides a solver for cuadratic equations of the form: ax2 + bx + c = 0. Where a, b and c are the coefficients of the equation.

const { equation } = require('@hdoc/js-utils');

const coefficients = {
  coeA: 2,
  coeB: 1,
  coeC: 0,
};

// logs { root1: 0, root2: -0.5 }
console.log(equation.cuadratic(coefficients));

Browser utils

Browser utils are only available as ESModules. You need to download them from utils/browser folder.

Note: I recommend to place the utils files in utils folders

DOM accesing

Provides simplified function names for dom accesing.

Having the next html file ...

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Example</title>
    <script src="js/main.js" type="module" defer></script>
  </head>
  <body>
    <main>
      <p id="text1" class="text">Texto 1</p>
      <p id="text2" class="text">Texto 2</p>
      <p id="text3" class="text">Texto 3</p>
      <p id="text4" class="text">Texto 4</p>
      <p id="text5" class="text">Texto 5</p>
    </main>
  </body>
</html>

You need to import in main.js ...

import { qs, qsa, gid } from './utils/dom.js';

// logs main tag
console.log(qs('main'));

// logs NodeList of elements with class 'text' as an array
console.log(qsa('.text'));

// logs element with id text1
console.log(gid('text1'));

Form functions

Provides functions to handle forms.

Having the next html file ...

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Example</title>
    <script src="js/main.js" type="module" defer></script>
  </head>
  <body>
    <form id="my-form">
      <input type="text" name="usr_name" required />
      <input type="email" name="usr_email" required />
      <input type="submit" value="Send" />
    </form>
  </body>
</html>

You need to import in main.js ...

import { gid } from './utils/dom.js';
import { formInputs } from './utils/form.js';

gid('my-form').addEventListener('submit', (e) => {
  e.preventDefault();

  const data = formInputs(e.target);

  // logs { usr_name: usr_name_value, usr_email: usr_email_value}
  console.log(data);
});

Note: formInputs() returns an object with data of each input that have name attribute.