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

is-like

v0.1.13

Published

Functions to check if a value is something alike.

Downloads

10,023

Readme

is-like

Functions to check if a value is something alike.

Install

Node.js

npm i is-like

Deno

import { isDictLike, isArrayLike, /* ... */ } from "https://deno.land/x/is_like/index.js";

API

Currently, this module includes the following functions.

isDictLike(value: any): boolean

Checks if the input value is a dict object, which includes key-value pairs.

console.assert(isDictLike({}));
console.assert(isDictLike({ foo: "bar" }));

class Test {
    constructor(name) {
        this.name = name;
    }
}
console.assert(isDictLike(new Test("Test")));
console.assert(!isDictLike(new Test())); // an empty custom object will fail

console.assert(!isDictLike(new Date()));
console.assert(!isDictLike(/[a-z]/));
console.assert(!isDictLike(["hello", "world"]));
console.assert(!isDictLike(new Map([["foo", "Hello"], ["bar", "World"]])));
console.assert(!isDictLike(new Error("something went wrong")));
console.assert(!isDictLike(Promise.resolve("Hello, World!")));

isArrayLike(value: any): boolean

Checks if the input value is an object with length property or a string.

console.assert(isArrayLike([1, 2, 3]));
console.assert(isArrayLike(Int8Array.from([1, 2, 3])));
console.assert(isArrayLike(Uint8Array.from([1, 2, 3])));
console.assert(isArrayLike(Int16Array.from([1, 2, 3])));
console.assert(isArrayLike(Uint16Array.from([1, 2, 3])));
console.assert(isArrayLike(Int32Array.from([1, 2, 3])));
console.assert(isArrayLike(Uint32Array.from([1, 2, 3])));
console.assert(isArrayLike(Float32Array.from([1, 2, 3])));
console.assert(isArrayLike(Float64Array.from([1, 2, 3])));
console.assert(isArrayLike(Buffer.from([1, 2, 3])));

let args = null;
(function () { args = arguments; })("Hello, World!");
console.assert(isArrayLike(args));

console.assert(isArrayLike("Hello, World!"));
console.assert(isArrayLike(new String("Hello, World!")));

class MyArray extends Array { }
console.assert(isArrayLike(new MyArray(8)));

class ArrayLike {
    get length() {
        return 0;
    }
    [Symbol.iterator]() { }
}
console.assert(isArrayLike(new ArrayLike()));

isCollectionLike(value: any, excludeWeakOnes?: boolean): boolean

Checks if the input value is an object with size property and [Symbol.iterator]() method, or is an instance of WeakMap or WeakSet if excludeWeakOnes is not set.

console.assert(isCollectionLike(new Map));
console.assert(isCollectionLike(new Set));
console.assert(isCollectionLike(new WeakMap));
console.assert(!isCollectionLike(new WeakMap, true));
console.assert(isCollectionLike(new WeakSet));
console.assert(!isCollectionLike(new WeakSet, true));

class MyMap extends Map { }
console.assert(isCollectionLike(new MyMap));

class MySet extends Set { }
console.assert(isCollectionLike(new MySet));

class CollectionLike {
    get size() {
        return 0;
    }
    [Symbol.iterator]() { }
}
console.assert(isCollectionLike(new CollectionLike));

isTypedArrayLike(value: any): boolean

Checks if the input value is an object with byteLength property and slice() method.

Alias: isBufferLike, deprecated.

console.assert(isTypedArrayLike(Buffer.from([1, 2, 3])));
console.assert(isTypedArrayLike(new ArrayBuffer(8)));
console.assert(isTypedArrayLike(Int8Array.from([1, 2, 3])));
console.assert(isTypedArrayLike(Uint8Array.from([1, 2, 3])));
console.assert(isTypedArrayLike(Int16Array.from([1, 2, 3])));
console.assert(isTypedArrayLike(Uint16Array.from([1, 2, 3])));
console.assert(isTypedArrayLike(Int32Array.from([1, 2, 3])));
console.assert(isTypedArrayLike(Uint32Array.from([1, 2, 3])));
console.assert(isTypedArrayLike(Float32Array.from([1, 2, 3])));
console.assert(isTypedArrayLike(Float64Array.from([1, 2, 3])));

isErrorLike(value: any): boolean

Checks if the input value is an object with name, message and stack properties.

console.assert(isErrorLike(new Error("something went wrong")));
console.assert(isErrorLike(new EvalError("something went wrong")));
console.assert(isErrorLike(new RangeError("something went wrong")));
console.assert(isErrorLike(new RangeError("something went wrong")));
console.assert(isErrorLike(new ReferenceError("something went wrong")));
console.assert(isErrorLike(new SyntaxError("something went wrong")));
console.assert(isErrorLike(new TypeError("something went wrong")));
console.assert(isErrorLike(new URIError("something went wrong")));
console.assert(isErrorLike(new assert.AssertionError({ message: "something went wrong" })));

class MyError extends Error { }
console.assert(isErrorLike(new MyError("something went wrong")));

let err = { name: "CustomError", message: "something went wrong" };
Error.captureStackTrace(err);
console.assert(isErrorLike(err));

isPromiseLike(value: any): boolean

Checks if the input is an object with then() method.

console.assert(isPromiseLike(Promise.resolve(123)));
console.assert(isPromiseLike({ then() { } }));
console.assert(isPromiseLike({ then: () => { } }));
console.assert(!isPromiseLike({ then: 123 }));