Skip to content

Commit 15ded84

Browse files
committed
Merge branch 'develop'
2 parents 2aec821 + 76f3eb8 commit 15ded84

41 files changed

Lines changed: 5109 additions & 15987 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
indent_style = space
8+
indent_size = 2
9+
10+
[*.md]
11+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
.DS_Store
2-
node_modules
3-
*.log
4-
coverage
5-
.vscode
6-
.idea
1+
/coverage
2+
/lib
3+
/node_modules

.npmignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/build
2+
/coverage
3+
/test
4+
.*
5+
logo-vuex-orm.png
6+
tsconfig.build.json
7+
tsconfig.json
8+
tslint.json

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
node_js:
3+
- "10"
4+
- "12"
5+
script:
6+
- npm run lint
7+
- npm run coverage
8+
- cat ./coverage/lcov.info | ./node_modules/.bin/codecov

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Conan Crawford
3+
Copyright (c) 2019 Conan Crawford and Kia Ishii
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 113 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,105 @@
1-
# Vuex Orm Plugin: Search
2-
3-
This plugin adds a **search()** method to the vuex-orm query methods to easily filter matched records using fuzzy search logic from the [Fuse.js](http://fusejs.io/) library.
1+
<p align="center">
2+
<img width="192" src="https://github.com/vuex-orm/plugin-search/raw/master/logo-vuex-orm.png" alt="Vuex ORM">
3+
</p>
4+
5+
<h1 align="center">Vuex Orm Plugin: Search</h1>
6+
7+
<p align="center">
8+
<a href="https://travis-ci.org/vuex-orm/plugin-search">
9+
<img src="https://travis-ci.org/vuex-orm/plugin-search.svg?branch=master" alt="Travis CI">
10+
</a>
11+
<a href="https://codecov.io/gh/vuex-orm/plugin-search">
12+
<img src="https://codecov.io/gh/vuex-orm/plugin-search/branch/master/graph/badge.svg" alt="codecov">
13+
</a>
14+
<a href="https://standardjs.com">
15+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="JavaScript Style Guide">
16+
</a>
17+
<a href="https://github.com/vuex-orm/plugin-search/blob/master/LICENSE.md">
18+
<img src="https://img.shields.io/npm/l/@vuex-orm/plugin-search.svg" alt="License">
19+
</a>
20+
</p>
21+
22+
Vuex ORM Search plugin adds a **search()** method to the vuex-orm query methods to easily filter matched records using fuzzy search logic from the [Fuse.js](http://fusejs.io/) library.
423

524
A simple example to search for '_john_' within your query:
625

7-
```javascript
8-
const data = this.$store.getters['entities/users/query']()
9-
.search('john')
10-
.orderBy('name', 'asc')
11-
.offset(0)
12-
.limit(10)
13-
.get()
14-
```
15-
16-
### API
17-
The search plugin method accepts two parameters
18-
19-
- **term** : any string or an array of strings
20-
- **options** : see the _Fuse.js Default Options_ below
21-
22-
**Note:** If passing an array of search terms, the results assume every element in the array must be matched.
23-
24-
```typescript
25-
search(term: string | Array<string>, options: Object<any>): Array<Records>
26+
```js
27+
const users = User.query().search('john').get()
2628
```
2729

28-
## Requirements:
30+
## Installation
2931

30-
The search plugin requires **@vuex-orm/core** package version **0.23.3**
32+
Install `@vuex-orm/plugin-search` alongside with Vuex ORM Core.
3133

32-
To upgrade the **vuex-orm package** simply run
3334
```bash
34-
npm install @vuex-orm/core
35-
```
35+
# Via NPM.
36+
npm install @vuex-orm/core @vuex-orm/plugin-search --save
3637

37-
## Installation
38-
```bash
39-
npm install @vuex-orm/plugin-search --save
38+
# Via Yarn.
39+
yarn add @vuex-orm/core @vuex-orm/plugin-search
4040
```
4141

42-
## Plugin Import Directions
42+
## Setup
4343

4444
Import the search plugin in the Vuex Store entry file.
4545

46-
```javascript
47-
// ex: store/index.js
46+
```js
4847
import VuexORM from '@vuex-orm/core'
4948
import VuexORMSearch from '@vuex-orm/plugin-search'
50-
````
51-
52-
Tell VuexORM to install the plugin
5349

54-
```javascript
5550
VuexORM.use(VuexORMSearch, {
56-
// configure default fuse.js options here (see below)
51+
// Configure default fuse.js options here (see "Configuration" section below).
5752
})
5853
```
5954

60-
## Fuse.js Default Options
55+
### API
56+
57+
The search plugin method accepts two parameters.
58+
59+
```ts
60+
search(terms: string | string[], options: object): this
61+
```
62+
63+
- **terms** – any string or an array of strings.
64+
- **options** – see the "Configurations" section below.
65+
66+
> **NOTE:** If passing an array of search terms, the results assume some element in the array to be matched.
6167
62-
The plugin provides opinionated default fuse.js options for token based matching for optimum performance. These options are easily changed at two stages of the plugin lifecycle:
68+
## Configurations
6369

64-
- Plugin installation (sets the global default options)
65-
- Runtime within the **search()** query chain
70+
The plugin provides opinionated default Fuse.js options for token-based matching for optimum performance. These options can be easily changed at two stages of the plugin lifecycle:
6671

67-
see: [Fuse.js](http://fusejs.io/) for demo
72+
- Plugin installation (sets the global default options).
73+
- Runtime within the **search()** query chain.
74+
75+
See: [Fuse.js](http://fusejs.io/) for demo.
6876

6977
| Property | Description | Default |
7078
| --- | --- | --- |
71-
| searchPrimaryKey | Also search the primary key | false |
72-
| location | Approximately where in the text is the pattern expected to be found | 0 |
73-
| distance | Determines how close the match must be to the fuzzy location | 100 |
74-
| threshold | **0.0** requires a perfect match, and a threshold of **1.0** would match anything | 0.3 |
75-
| maxPatternLength | Machine word size | 32 |
76-
| caseSensitive | Indicates whether comparisons should be case sensitive | false |
77-
| tokenSeparator | Regex used to separate words when searching. Only applicable when **tokenize** is **true** | / +/g |
78-
| findAllMatches | When true, the algorithm continues searching to even if a perfect match is found | false |
79-
| minMatchCharLength | Minimum number of characters that must be matched before a result is considered a match | 1 |
80-
| keys | An array of fields (columns) to be included in the search | Model.$fields() |
81-
| shouldSort | Whether to sort the result list, by score | false |
82-
| tokenize | When true, the search algorithm will search individual words **and** the full string, computing the final score as a function of both. **Note**: that when _tokenize_ is _true_, the **threshold**, **distance**, and **location** are inconsequential for individual tokens | false |
83-
| matchAllTokens | When true, the result set will only include records that match all tokens. Will only work if **tokenize** is also true. **Note**: It is better to use multiple **.search()** query chains if you have multiple terms that need to match. | false |
84-
| verbose | Will print to the console. Useful for debugging. | false |
85-
86-
## Option Use Examples
87-
88-
Some examples on how to use the search plugin with case specific options
79+
| searchPrimaryKey | Also search the primary key | `false` |
80+
| location | Approximately where in the text is the pattern expected to be found | `0` |
81+
| distance | Determines how close the match must be to the fuzzy location | `100` |
82+
| threshold | **0.0** requires a perfect match, and a threshold of **1.0** would match anything | `0.3` |
83+
| maxPatternLength | Machine word size | `32` |
84+
| caseSensitive | Indicates whether comparisons should be case sensitive | `false` |
85+
| tokenSeparator | Regex used to separate words when searching. Only applicable when **tokenize** is **true** | `/ +/g` |
86+
| findAllMatches | When true, the algorithm continues searching to even if a perfect match is found | `false` |
87+
| minMatchCharLength | Minimum number of characters that must be matched before a result is considered a match | `1` |
88+
| keys | An array of fields (columns) to be included in the search | Keys from `Model.fields()` |
89+
| shouldSort | Whether to sort the result list, by score | `false` |
90+
| tokenize | When true, the search algorithm will search individual words **and** the full string, computing the final score as a function of both. **NOTE**: that when _tokenize_ is _true_, the **threshold**, **distance**, and **location** are inconsequential for individual tokens | `false` |
91+
| matchAllTokens | When true, the result set will only include records that match all tokens. Will only work if **tokenize** is also true. **NOTE**: It is better to use multiple **.search()** query chains if you have multiple terms that need to match. | `false` |
92+
| verbose | Will print to the console. Useful for debugging. | `false` |
93+
94+
## Option Examples
95+
96+
Here are some examples on how to use the search plugin with case specific options.
8997

9098
### During Plugin Install
9199

92-
For example, if we want to match based on case sensitive and no fuzzy search logic (perfect match)
93-
```javascript
100+
For example, if we want to match based on case sensitive and no fuzzy search logic (perfect match).
101+
102+
```js
94103
VuexORM.use(VuexORMSearch, {
95104
caseSensitive: true,
96105
threshold: 0
@@ -103,35 +112,60 @@ The global install options will now default to case sensitive and no fuzzy logic
103112

104113
Let's also specify the need to only search the **firstName** and **lastName** fields.
105114

106-
```javascript
107-
const options = {
115+
```js
116+
const users = User.query().search('john', {
108117
caseSensitive: false,
109118
threshold: 0.3,
110119
keys: ['firstName', 'lastName']
111-
}
112-
const data = this.$store.getters['entities/users/query']()
113-
.search('john', options)
114-
.orderBy('firstName', 'asc')
115-
.offset(0)
116-
.limit(10)
117-
.get()
120+
}).get()
118121
```
119122

120123
### Finding Results Matching Multiple Terms
121124

122125
Let's find all matches where both **pat** and **male** exist in our records, and sort by the date added.
123126

124127
```javascript
125-
const terms = ['pat', 'male']
126-
const options = {
128+
const data = User.query().search(['pat', 'male'], {
127129
keys: ['firstName', 'gender']
128-
}
129-
const data = this.$store.getters['entities/users/query']()
130-
.search(terms, options)
131-
.orderBy('createdAt', 'desc')
132-
.get()
130+
}).get()
131+
```
132+
133+
## Contribution
134+
135+
We are excited that you are interested in contributing to Vuex ORM Search! Anything from raising an issue, submitting an idea of a new feature, or making a pull request is welcome!
136+
137+
### Development
138+
139+
```bash
140+
$ yarn build
141+
```
142+
143+
Compile files and generate bundles in `dist` directory.
144+
145+
```bash
146+
$ yarn lint
147+
```
148+
149+
Lint files using a rule of Standard JS.
150+
151+
```bash
152+
$ yarn test
153+
```
154+
155+
Run the test using [Jest](https://jestjs.io/).
156+
157+
```bash
158+
$ yarn test:watch
159+
```
160+
161+
Run the test in watch mode.
162+
163+
```bash
164+
$ yarn coverage
133165
```
134166

167+
Generate test coverage in `coverage` directory.
168+
135169
## License
136170

137171
The Vuex ORM Plugin Search is open-sourced software licensed under the [MIT license](LICENSE.md).

build/build.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const zlib = require('zlib')
4+
const rollup = require('rollup')
5+
const uglify = require('uglify-js')
6+
const configs = require('./configs')
7+
8+
if (!fs.existsSync('dist')) {
9+
fs.mkdirSync('dist')
10+
}
11+
12+
build(Object.keys(configs).map(key => configs[key]))
13+
14+
function build (builds) {
15+
let built = 0
16+
17+
const total = builds.length
18+
19+
const next = () => {
20+
buildEntry(builds[built]).then(() => {
21+
built++
22+
23+
if (built < total) {
24+
next()
25+
}
26+
}).catch(logError)
27+
}
28+
29+
next()
30+
}
31+
32+
function buildEntry ({ input, output }) {
33+
const isProd = /min\.js$/.test(output.file)
34+
35+
return rollup.rollup(input)
36+
.then(bundle => bundle.generate(output))
37+
.then(({ output: [{ code }] }) => {
38+
if (isProd) {
39+
var minified = uglify.minify(code, {
40+
output: {
41+
ascii_only: true
42+
}
43+
}).code
44+
45+
return write(output.file, minified, true)
46+
}
47+
48+
return write(output.file, code)
49+
})
50+
}
51+
52+
function write (dest, code, zip) {
53+
return new Promise((resolve, reject) => {
54+
function report (extra) {
55+
console.log(blue(path.relative(process.cwd(), dest)) + ' ' + getSize(code) + (extra || ''))
56+
57+
resolve()
58+
}
59+
60+
fs.writeFile(dest, code, err => {
61+
if (err) {
62+
return reject(err)
63+
}
64+
65+
if (zip) {
66+
zlib.gzip(code, (err, zipped) => {
67+
if (err) {
68+
return reject(err)
69+
}
70+
71+
report(' (gzipped: ' + getSize(zipped) + ')')
72+
})
73+
74+
return
75+
}
76+
77+
report()
78+
})
79+
})
80+
}
81+
82+
function getSize (code) {
83+
return (code.length / 1024).toFixed(2) + 'kb'
84+
}
85+
86+
function logError (e) {
87+
console.log(e)
88+
}
89+
90+
function blue (str) {
91+
return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
92+
}

0 commit comments

Comments
 (0)