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

@kuoki/anymap

v1.0.0

Published

A helper class for testing use cases where the value is of type any

Downloads

1

Readme

Anymap

A helper class for testing use cases where the value is of type any

npm Documentation Coverage Quality Gate Status GitHub GitHub issues anymap

About The Project

The Anymap object is useful to test functions or methods that uses arguments with the types any or undefined because contains all the standard JavaScript data types in a dictionary, allowing filtering them using the data type keys or using a filter function. In this way, the code can be strengthened by testing all existing types and their behavior.

Getting Started

Installation

Using NPM

npm install --save-dev @kuoki/anymap

Using Yarn

yarn add --dev @kuoki/anymap

Usage

Create an instace of Anymap and use the methods to chain operations to get the desired result.

const objectValues = new Anymap().exclude('null').filter((value) => typeof value === 'object').values;

The class exposes four types of ways to get the final desired data. The dictionary is a plain object with the key and the list of values that represents.

type dictionary = Record<string, any[]>;
const anymap = new Anymap().include('boolean', 'null');
anymap.dict; // {boolean:[true,false],null:[null]}
anymap.keys; // ['boolean','null']
anymap.values; // [true,false,null]
anymap.entries; // [['boolean':true],['boolean':false],['null',null]]

Available keys

anonymousFunction, Array, ArrayBuffer, Atomics, BigInt64Array, bigint, BigUint64Array, binary, Boolean, DataView, Date, decimal, Document, DocumentFragment, Element, Error, EvalError, Event, exponent, Float32Array, Float64Array, hexadecimal, Infinity, Int16Array, Int32Array, Int8Array, integer, JSON, Map, Math, namedFunction, namedObject, NaN, null, Number, octal, plainObject, RangeError, ReferenceError, RegExp, Set, SharedArrayBuffer, string, String, Symbol, SyntaxError, TypeError, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray, undefined, URIError, WeakMap, WeakSet, Window,boolean

Use cases

Add

new Anymap().include('string').add({ string: [' '] }).dict;
// {string:['',' ']}
new Anymap().include('null').add({ myObject: [{ a: 'a' }] }).dict;
// {null:[null],myObject:[{a:'a'}]}

Clear

new Anymap().include('null').dict;
// {null:[null]}
new Anymap().include('null').clear().dict;
// {}

Include

new Anymap().include('null').dict;
// {null:[null]}
new Anymap().include('null', 'boolean').dict;
// {null:[null],boolean:[true,false]}

Exclude

const test = new Anymap().include('null', 'boolean');

test.dict;
// {null:[null],boolean:[true,false]}
test.exclude('null').dict;
// {boolean:[true,false]}

Union

const a = new Anymap().include('null');
const b = new Anymap().include('boolean');

a.union(b).dict;
// {null:[null],boolean:[true,false]}

Except

const a = new Anymap().include('null', 'boolean');
const b = new Anymap().include('boolean');

a.except(b).dict;
// {null:[null]}

Intersect

const a = new Anymap().include('null', 'boolean');
const b = new Anymap().include('boolean');

a.intersect(b).dict;
// {boolean:[true,false]}

Filter

new Anymap().include('null', 'boolean').filter((value) => Boolean(value)).dict;
// {boolean:[true]}

Use in a test

import { Anymap } from '@kuoki/anymap';

function isFalsy(arg: any): boolean {
  return Boolean(arg) === false;
}

describe('isFalsy', () => {
  const falsy = new Anymap().filter((v) => Boolean(v) === false);
  const truthy = new Anymap().except(falsy);

  falsy.entries.forEach(([key, value]) => {
    it(`isFalsy(${key} ${String(value)}) returns true`, () => {
      expect(isFalsy(value)).toEqual(true);
    });
  });

  truthy.entries.forEach(([key, value]) => {
    it(`isFalsy(${key} ${String(value)}) returns false`, () => {
      expect(isFalsy(value)).toEqual(false);
    });
  });
});

Common filters

import { Anymap } from '@kuoki/anymap';

const numbers = new Anymap().filter((value) => typeof value === 'number');
const falsies = new Anymap().filter((value) => Boolean(value) === false);
const iterables = new Anymap().filter((value) => typeof value[Symbol.iterator] === 'function');