# is-like

> Functions to check if a value is something alike.

Latest version **0.1.13** (published 2023-09-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install is-like
pnpm add is-like
yarn add is-like
bun add is-like
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.13 |
| Published | 2023-09-22 |
| First published | 2019-10-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | A-yon Lee |
| Maintainers | ayonli |
| Keywords | is, like, array, set, map, buffer, TypedArray, array-like |

## Links

- npm: https://www.npmjs.com/package/is-like
- Repository: https://github.com/ayonli/is-like
- Homepage: https://github.com/ayonli/is-like#readme
- Issues: https://github.com/ayonli/is-like/issues
- npm.io page: https://npm.io/package/is-like

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 0.1.13 (latest) — 2023-09-22
- 0.1.12 — 2023-09-15
- 0.1.11 — 2023-06-03
- 0.1.9 — 2021-11-27
- 0.1.8 — 2020-11-28
- 0.1.7 — 2020-11-28
- 0.1.6 — 2020-06-29
- 0.1.5 — 2020-05-20
- 0.1.4 — 2020-02-20
- 0.1.3 — 2020-01-24
- 0.1.2 — 2019-10-12
- 0.1.1 — 2019-10-12
- 0.1.0 — 2019-10-12

## README

# is-like

**Functions to check if a value is something alike.**

## Install

### Node.js

```sh
npm i is-like
```

### Deno

```ts
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.*

```js
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.*

```js
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.*

```js
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.

```js
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.*

```js
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.*

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

---
_Source: https://npm.io/package/is-like · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
