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 🙏

© 2026 – Pkg Stats / Ryan Hefner

methodules

v1.0.0

Published

Relevant methodules for JavaScript value manipulation and security.

Downloads

3

Readme

Methodules

Relevant/useful reusable methods for JavaScript. Documents of methods are listed and linked below:

Methods

useUnion - This function will enforce the same type or object given for the new value.

useStrictUnion) - This function will enforce an initialized variable to use the given types everytime a, initialized variable is set to a new value. (Note: limit is default to Infinity).

keySearch - This function will search and return the first matching value.

keySearchAll - This function will search all and return all matching values.

requestData - This function will make any request from a desired API server.

keyValueSwitch - This function will switch all keys and values.

arrayShuffle - This function will shuffle an array.

useUnion(value: any, types: any[])

First argument is the initial/new value of any type. However, the value cannot be a function, because there are no use-cases or no reasonable use-case to enforce the value. It is best to use an arrow function instead useUnion method if the value is intended to be a function.

Second argument is the array of primitive types (String, Number, BigInt, null, etc.) and/or built-in objects (Date, Math, Map, etc.) and/or custom constructor functions.

const a = useUnion('hello world', [String]); // returns: 'hello world'
const b = useUnion(2, [String, Number]); // returns: 2
const c = useUnion([1,2,3], [BigInt]); // returns: Undefined

Objects

const myDate = useUnion(new Date(), [Date]);
const myMap = useUnion(new Map(), [Map]);

console.log(myDate.getFullYear()); // returns current year
console.log(myMap.size); //  returns 0

Constructor function

function Person (name, age) {
  this.name = name;
  this.age = age;
}

const P1 = useUnion(new Person('Alice', 99), [Person]);
const P2 = useUnion(new Person('John', 34), [Object]); // Will return as undefined

console.log(P1.name); //  Alice
console.log(P2.name); //  TypeError: Cannot read properties of undefined (reading 'name')

Use case example:

//  index.js

import { fun } from "../fun.js";

fun.createFun(document.getElementByClassName('fun'));


// fun.js

import { useUnion } from "../methodules.js";

function _createFun(htmlcollection)) {

  if (useUnion(htmlcollection, [HTMLCollection]) {
    htmlcollection.forEach(element => {

      const dataColor = useUnion(element.getAttribute('data-color'), [String]));
      const dataHighLight = useUnion(element.getAttribute('data-highlight'), [Boolean]));

      ...
    });
  }
}
const fun = () => {
  return {
    createFun: _createFun(htmlcollection)
  }
}

export default fun;

useStrictUnion(value: any, types: any[], limit: Number)

Similar to useUnion (and also similar to React's hook useState), this will be an array deconstructing assignment. Call the first argument to get the current value. Call the second argument to set a value.

//  String only
const [value, setValue] = useStrictUnion('hello world', [String]);

console.log(value()); //  'hello world'

setValue(7); // this will error

//  String and Number with a limit of 3 sets
const [alphaNum, setAlphaNum] = useStrictUnion('methodule', [String, Number], 2);

console.log(alphaNum()); //  'methodule'

setAlphaNum(7);

console.log(alphaNum()); //  7

setAlphaNum({name: 'javascript'}); //  this will error
setAlphaNum(8);
setAlphaNum('Error'); // this will error due to number of sets has been exceeded

console.log(alphaNum()); //  alphaNum() should still be Number 8

keySearch(object: any{}, key: String)

First argument MUST be an object, and the second will be the search key string.

function Person(name, age) {
  this.name = name;
  this.age = age;
}

const p1 = new Person('Alice', 45);

console.log(keySearch(p1, name); //  'Alice'

keySearchAll(object: any[] | any{}, key: String)

Like keySearch(), this function accepts an array of objects or a complex object, and returns all values of the same key.

function Person(name, age) {
  this.name = name;
  this.age = age;
}

const people = [new Person('John', 35), new Person('Jane', 35)];

console.log(keySearchAll(people, name)); //  ['John', 'Jane']

requestData(url: String, option: Object)

First argument MUST be a URL string STARTING with 'http://' or 'https://'. Second argument is an object of options to add headers, method, body, etc.

It is highly recommend to use async and await keywords for this function due to fetch() method is promise-based. Using these keywords for readability and error handling.

import { requestData } from "../methodules.js"

const data = async () => {
  return await fetchData("https://jsonplaceholder.typicode.com/posts/1", {method: 'GET'});
};

//  Another way
async function getData(url, option) {
  return await fetchData(url, option);
}

keyValueSwitch(object: any{})

Self-explanatory. Pass in an object to switch the keys and values. (This will copy the referrenced object).

const a = { a: 1, b: 2 };
const b = keyValueSwitch(a);

console.log(a);  //  { a: 1, b: 2 }
console.log(b);  //  { 1: a, 2: b }

arrayShuffle(array: any[])

Self-explanatory. Pass in an array to be shuffled. (This will copy the referrenced array).

const arr = [1,2,3,4,5];

console.log(arrayShuffle(arr));  //  possibilities: [2,4,3,5,1], [1,3,4,2,5], ...