# panda-generics

> Generic functions (multi-argument dispatch) for JavaScript.

Latest version **6.0.3** (published 2020-09-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install panda-generics
pnpm add panda-generics
yarn add panda-generics
bun add panda-generics
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.0.3 |
| Published | 2020-09-11 |
| First published | 2018-09-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | dyoder, freeformflow |
| Keywords | functional programming, generic functions, multimethods |

## Links

- npm: https://www.npmjs.com/package/panda-generics
- Repository: https://github.com/pandastrike/panda-generics
- Homepage: https://github.com/pandastrike/panda-generics#readme
- Issues: https://github.com/pandastrike/panda-generics/issues
- npm.io page: https://npm.io/package/panda-generics

## Recent versions

- 6.0.3 (latest) — 2020-09-11
- 6.0.2 — 2019-08-17
- 6.0.1 — 2019-08-17
- 6.0.0 — 2019-08-17
- 5.0.0 — 2018-12-18
- 4.3.0 — 2018-12-17
- 4.2.0 — 2018-10-27
- 4.1.0 — 2018-10-23
- 4.0.2 — 2018-09-20
- 4.0.1 — 2018-09-20
- 4.0.0 — 2018-09-07
- 1.0.0 — 2018-09-07

## README

# Panda Generics

Panda Generics brings generics, also known as multi-methods, to JavaScript. Generics are great for functional programming because they offer multi-argument dispatch. That is, you aren't limited to an implicit first argument as you are with object-oriented methods.

## Installation

`npm i panda-generics`

## Usage

```coffee
import Generics from "panda-generics"
import {isObject} from "panda-parchment"

equal = Generics.create
  name: "equal"
  description: "'Deep' equality operator"
  default: (a, b) -> a == b # fallback to shallow equality

# when comparing objects, recursively check the values
# corresponding to the union of their properties—
# return false on the first inequality
Generics.define equal, isObject, isObject, (a, b) ->
  (a == b) || do ->
    keys = new Set (Object.keys a)..., (Object.keys b)...
    for key from keys
      if ! equal a[key], b[key]
        return false
    true

# when comparing arrays, recursively check values
# after making sure they're the same length
# return false on the first inequality
Generics.define equal, isArray, isArray, (ax, bx) ->
  (ax == bx) || do ->
    return false if ax.length != bx.length
    for i in [0..ax.length]
      if !equal ax[i], bx[i]
        return false
    true

equal "this", "this"  # => true, shallow equality works here
equal { x: 1, y: 2 }, { x: 1, y: 2 } # => true, deep equality
equal [1..5], [1..5] # true, deep equality
equal { x: 1, y: 2}, [1..5] # false
```

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