# masa-search

> Search indexer for TypeScript and JavaScript

Latest version **0.4.0** (published 2018-08-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install masa-search
pnpm add masa-search
yarn add masa-search
bun add masa-search
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2018-08-09 |
| First published | 2018-04-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 889.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sandro.Maggi |
| Maintainers | cabadath |
| Keywords | search, indexed |

## Links

- npm: https://www.npmjs.com/package/masa-search
- Repository: https://github.com/Phyrra/masa-search
- Homepage: https://github.com/Phyrra/masa-search#readme
- Issues: https://github.com/Phyrra/masa-search/issues
- npm.io page: https://npm.io/package/masa-search

## Dependencies (2)

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

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 0.4.0 (latest) — 2018-08-09
- 0.3.1 — 2018-06-28
- 0.3.0 — 2018-05-25
- 0.2.0 — 2018-04-19
- 0.1.4 — 2018-04-14
- 0.1.3 — 2018-04-13
- 0.1.2 — 2018-04-13
- 0.1.1 — 2018-04-12
- 0.1.0 — 2018-04-12

## README

Installation
============

The module is registered on [npmjs.org](https://www.npmjs.com/package/masa-search), to install run
```
npm i --save masa-search
```
or
```
yarn add masa-search
```

Usage
=====

## Node

Once installed, you can pull components from the module and use them in your code.
```
const { Search, Type, Match } = require('masa-search');

const search = new Search();

search.addIndex({
	key: 'name',
	type: Type.WORD
});

search.addData([
	{ name: 'Albert' },
	{ name: 'Berta' },
	{ name: 'Charlie' }
]);

console.log(
	search.find({
		condition: {
			index: {
				key: 'name',
				type: Type.WORD
			},
			match: Match.EQ,
			value: 'ALBERT'
		}
	})
);
```

## TypeScript

The module was written in TypeScript and comes with its own types.
```
import * from 'masa-search';

const search = new Search();

...
```

## Browser

The module is converted to a browser-library using [browserify](http://browserify.org/).

The dependencies are exported to the `window` global.

```
<html>
	<script src="node_modules/masa-search/dist/masa-search.browser.js">
	<script>
		const search = new Search();
		
		// ...
	</script>
</html>
```

API
===

## addIndex

Add an index definition. All indexes should be added before adding data.
Already processed data will not be re-indexed, unless manually triggered.

The index should match the following interface
```
interface Index {
	key: string;
	type: Type;
}
```

* 	`key` being the field to be indexed. The fields may be nested, refer to [_.get](https://lodash.com/docs#get).

* 	`type` being the description of how to index the field. `Type` is an enum of
	```
	enum Type {
		WORD = 'word',
		TEXT = 'text',
		NUMBER = 'number',
		DATE = 'date'
	}
	```

## addData

Add new data for future searches. The data can be anything and nested as is required.

## reIndex

Clears the current indexed data store and re-indexes all data.

## find

Find a set of results, based on the provided search query.

### Query

```
export interface Query {
	and?: Query[],
	or?: Query[],
	condition?: Condition
}
```

*	`and` will combine all queries with `&&`

*	`or` will combine all queries with `||`

*	`condition` is the final condition (stops recursion)

The query should only ever have one of the keys filled and can be nested as required.

### Condition

```
interface Condition {
	index: Index,
	match ?: Match,
	value: any
}
```

*	`index` being the index, see definition above

*	`match` being the match type where `Match` is an enum of
	```
	enum Match {
		EQ = '=',
		GT = '>',
		LT = '<',
		GTE = '>=',
		LTE = '<=',
		FUZZY = 'fuzzy',
		PREFIX = 'prefix',
		WILDCARD = 'wildcard'
	}
	```

*	`value` being the value that should be compared against

#### FUZZY

The `FUZZY` search matches words with an auto-determined maximum distance.

* up to 4 letters: 1
* up to 8 letters: 2
* longer words: 4

#### WILDCARD

The `WILDCARD` search mathes exact words supporting wildcards.

* `.` a single character wildcard
* `?` the previous character is not required, but may occur
* `+` the previous character may occur repeatedly, but at least once
* `*` the previous character may occur repeatedly or not at all
* `[]` defines a selection of characters, example: `[ab]` matches either a or b
	* `[^]` defines a negative selection of characters, example: `[^ab]` matches everything except a and b
	* `[-]` defines a range, example: `[a-d]` matches every character between a and d

Examples
========

```
const { Search, Type, Match } = require('../index');

const search = new Search();

search.addIndex({
	key: 'name',
	type: Type.WORD
});

search.addIndex({
	key: 'comment',
	type: Type.TEXT
});

search.addIndex({
	key: 'age',
	type: Type.NUMBER
})

search.addData([
	{ name: 'Albert', comment: 'He is cool', age: 25 },
	{ name: 'Berta', comment: 'She is cool', age: 50 },
	{ name: 'Charlie', comment: 'He is cool too', age: 35 }
]);

const query = {
	and: [
		{
			condition: {
				index: {
					key: 'age',
					type: Type.NUMBER
				},
				match: Match.GT,
				value: 30
			}
		}, {
			or: [
				{
					condition: {
						index: {
							key: 'comment',
							type: Type.TEXT
						},
						value: 'cool too'
					}
				}, {
					condition: {
						index: {
							key: 'name',
							type: Type.WORD
						},
						value: 'Albert'
					}
				}
			]
		}
	]
};

console.log(search.find(query));
```

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