# f-matches

> Composable version of Lodash.matches()

Latest version **1.1.0** (published 2018-04-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install f-matches
pnpm add f-matches
yarn add f-matches
bun add f-matches
```

## 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 | 1.1.0 |
| Published | 2018-04-25 |
| First published | 2018-04-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 1 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Rene Saarsoo |
| Maintainers | nene |
| Keywords | lodash, utility, pattern-matching |

## Links

- npm: https://www.npmjs.com/package/f-matches
- Repository: https://github.com/nene/f-matches
- Issues: https://github.com/nene/f-matches/issues
- npm.io page: https://npm.io/package/f-matches

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.5

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2018-04-25
- 1.0.0 — 2018-04-01

## README

# f-matches: Composable version of Lodash.matches()

An utility used by [Lebab][] for complex matching of AST nodes.

The package exports 4 curried functions:

- `matches :: Pattern -> Obj -> (Bool|Obj)`
- `extract :: Name -> Pattern -> Obj -> (Bool|Obj)`
- `extractAny :: Name -> Obj -> (Bool|Obj)`
- `matchesLength :: Pattern -> Array -> (Bool|Obj)`

For details, just read the source, it's really small.

Alternatively, [read how Lebab uses this for patterns in syntax trees.][lebab-blog]

## Example

```js
import {matches, matchesLength, extract} from "f-matches";

// Function for matching string literal
const isStringLiteral = matches({
    "type": "Literal",
    // Unlike in Lodash.matches(),
    // we can provide a function to assert if object field matches.
    "value": (v) => typeof v === 'string',
});

// Function for matching: <local> = require(<source>)
const isRequireDeclarator = matches({
    "type": "VariableDeclarator",
    // Store the matching identifier under key: "local"
    "id": extract("local", {
        "type": "Identifier",
    }),
    "init": {
        "type": "CallExpression",
        "callee": {
            "type": "Identifier",
            "name": "require"
        },
        "arguments": matchesLength([
            // Store the matching string literal under key: "source"
            extract("source", isStringLiteral),
        ]),
    },
});

// Function for matching: var <local> = require(<source>)
const isRequire = matches({
    "type": "VariableDeclaration",
    // Match array of exactly 1 element (not 1 or more elements, which is the default)
    "declarations": matchesLength([
        isRequireDeclarator,
    ]),
    "kind": "var",
});

// Transform require() call to ES6 import statement.
estraverse.replace(ast, {
    enter(node) {
        const match = isRequire(node);
        if (match) {
            return {
                "type": "ImportDeclaration",
                "specifiers": [
                    {
                        "type": "ImportDefaultSpecifier",
                        "local": match.local
                    }
                ],
                "source": match.source
            };
        }
    }
});
```

[Lebab]: https://github.com/lebab/lebab
[lebab-blog]: http://nene.github.io/2016/04/02/matches-ast

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