Skip to content

Commit d5d63ed

Browse files
committed
Fixing bug in building and updated README.
1 parent 61f10e0 commit d5d63ed

File tree

4 files changed

+159
-41
lines changed

4 files changed

+159
-41
lines changed

README.md

Lines changed: 155 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ To ensure the smooth operation of the middleware, your web application must be b
1010

1111
## Installing
1212
Use the npm command to install this library into your project:
13-
```
14-
npm i --save query-strings-parser
15-
```
13+
```shell
14+
npm i query-strings-parser --save
15+
```
1616

1717
### Usage Examples
18-
#### 1. Using default configurations
19-
```js
18+
```js
2019
const express = require('express')
2120
const qs = require('query-strings-parser')
2221
const app = express()
@@ -46,32 +45,9 @@ app.get('/', (req, res) => {
4645
* }
4746
*/
4847
```
49-
#### The middleware uses the following defaults:
50-
```js
51-
options = {
52-
use_page: false,
53-
client_db: 'mongodb',
54-
date_field: {
55-
start_at: 'created_at',
56-
end_at: 'created_at'
57-
},
58-
default: {
59-
fields: {},
60-
sort: {},
61-
filters: {},
62-
pagination: {
63-
limit: Number.MAX_SAFE_INTEGER,
64-
skip: 0,
65-
page: 1
66-
}
67-
}
68-
}
69-
```
70-
If the options are not provided, the default values will be used for the treatment of queries strings.
7148

72-
73-
### 2. Using custom configurations:
74-
```js
49+
### Using custom configurations:
50+
```js
7551
const express = require('express')
7652
const qs = require('query-strings-parser')
7753
const app = express()
@@ -109,17 +85,159 @@ app.use(qs({
10985
* }
11086
*/
11187
```
88+
89+
The middleware uses the following defaults:
90+
```js
91+
options = {
92+
use_page: false,
93+
client_db: 'mongodb',
94+
date_field: {
95+
start_at: 'created_at',
96+
end_at: 'created_at'
97+
},
98+
default: {
99+
fields: {},
100+
sort: {},
101+
filters: {},
102+
pagination: {
103+
limit: Number.MAX_SAFE_INTEGER,
104+
skip: 0,
105+
page: 1
106+
}
107+
}
108+
}
109+
```
110+
111+
If the options are not provided, the default values will be used for the treatment of queries strings.
112112

113113
For more details, access the [wiki](https://github.com/nutes-uepb/query-strings-parser/wiki/2.-Usage-Examples) page.
114-
115-
## Supported Query Strings
114+
115+
### Parsers Functions
116+
To use these functions, simply call them through the middleware instance and pass them the query string to be converted and its default values. If you pass the default values ​​a merge will be performed with the result of the query strings. Here are some examples of each analyzer:
117+
118+
- `parser()`
119+
120+
```js
121+
const qs = require('query-strings-parser')
122+
123+
const query = '?fields=name,age&page=1&limit=10&sort=created_at'
124+
console.log(qs.parseFields(query, {}, { use_page: true }))
125+
126+
/**
127+
* Result:
128+
* {
129+
* fields: { name: 1, age: 1 },
130+
* sort: { created_at: 'asc' },
131+
* filters: {},
132+
* pagination: { limit: 10, page: 1 },
133+
* original: '?fields=name,age&page=1&limit=10&sort=created_at'
134+
* }
135+
*/
136+
```
137+
For more [details >>](https://github.com/nutes-uepb/query-strings-parser/wiki/4.-Parsers#parser)
138+
139+
- `parseFields()`
140+
141+
```js
142+
143+
const qs = require('query-strings-parser')
144+
145+
const query = '?fields=name,age'
146+
console.log(qs.parseFields(query))
147+
148+
/**
149+
* Result:
150+
* {
151+
* name: 1,
152+
* age: 1
153+
* }
154+
*/
155+
```
156+
For more [details >>](https://github.com/nutes-uepb/query-strings-parser/wiki/4.-Parsers#parsefields)
157+
158+
- `parseSort()`
159+
160+
```js
161+
const qs = require('query-strings-parser')
162+
163+
const query = '?sort=name,-age,created_at'
164+
console.log(qs.parseSort(query))
165+
166+
/**
167+
* Result:
168+
* {
169+
* name: 'asc',
170+
* age: 'desc',
171+
* created_at: 'asc'
172+
* }
173+
*/
174+
```
175+
For more [details >>](https://github.com/nutes-uepb/query-strings-parser/wiki/4.-Parsers#parsesort)
176+
177+
- `parsePagination()`
178+
179+
```js
180+
const qs = require('query-strings-parser')
181+
182+
const query = '?limit=20&page=3'
183+
console.log(qs.parsePagination(query, {}, true))
184+
185+
/**
186+
* Result:
187+
* {
188+
* limit: 20,
189+
* page: 3
190+
* }
191+
*/
192+
```
193+
For more [details >>](https://github.com/nutes-uepb/query-strings-parser/wiki/4.-Parsers#parsepagination)
194+
195+
- `parseFilter()`
196+
197+
```js
198+
const qs = require('query-strings-parser')
199+
200+
const query = '?name=elvis&age=80'
201+
console.log(qs.parseFilter(query))
202+
203+
/**
204+
* Result:
205+
* {
206+
* name: 'elvis',
207+
* age: 80
208+
* }
209+
*/
210+
```
211+
For more [details >>](https://github.com/nutes-uepb/query-strings-parser/wiki/4.-Parsers#parsefilter)
212+
213+
- `parseDate()`
214+
215+
```js
216+
const qs = require('query-strings-parser')
217+
218+
const query = '?start_at=2019-02-05T00:00:00&end_at=2019-02-05T23:59:59'
219+
console.log(qs.parseDate(query))
220+
221+
/**
222+
* Result:
223+
* {
224+
* $and: [
225+
* { created_at: { lt: 2019-02-05T23:59:59 }},
226+
* { created_at: { gte: 2019-02-05T00:00:00 }}
227+
* ]}
228+
* }
229+
*/
230+
```
231+
For more [details >>](https://github.com/nutes-uepb/query-strings-parser/wiki/4.-Parsers#parsedate)
232+
233+
### Supported Query Strings
116234
For informations and details about the supported query strings, access the [wiki](https://github.com/nutes-uepb/query-strings-parser/wiki/3.-Supported-Query-Strings) page.
117-
118-
## New Features
119-
- Support for parser functions. For informations and details about parser functions, access the [wiki](https://github.com/nutes-uepb/query-strings-parser/wiki/4.-Parsers) page.
120235

121-
## Future Features
122-
- ¹Support for relational databases such as MySQL, PostgreSQL and SQLite.
236+
----------
237+
238+
### Future Features
239+
- ¹Support for relational databases such as MySQL, PostgreSQL and SQLite.
240+
123241

124242
[//]: # (These are reference links used in the body of this note.)
125243
[node.js]: <https://nodejs.org>

lib/mapper/filters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function buildRegEx(value) {
8282
value = value.replace(/(^\*{2,})|\*{2,}$/gi, '*')
8383
const result = {'$options': 'i'}
8484
if (!value.startsWith('*') && value.endsWith('*')) result.$regex = '^'.concat(value.replace(/([*])/gi, ''))
85-
else if (value.startsWith('*') && !value.endsWith('*')) result.$regex = value.replace(/([*])/gi, '').concat('&')
85+
else if (value.startsWith('*') && !value.endsWith('*')) result.$regex = value.replace(/([*])/gi, '').concat('$')
8686
else result.$regex = value.replace(/([*])/gi, '')
8787
return result
8888
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "query-strings-parser",
3-
"version": "2.1.2",
3+
"version": "2.1.3",
44
"description": "Middleware to transform query strings in a format that is recognized by the MongoDB, MySQL and other databases...",
55
"license": "MIT",
66
"main": "index.js",
@@ -44,7 +44,7 @@
4444
"devDependencies": {
4545
"chai": "^4.2.0",
4646
"express": "^4.17.1",
47-
"mocha": "^6.2.0",
47+
"mocha": "^6.2.1",
4848
"nyc": "^14.1.1",
4949
"supertest": "^4.0.2"
5050
}

test/integration/index.default.config.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe('queryFilter()', function () {
135135
name: {'$options': 'i', '$regex': '^lucas'}
136136
},
137137
{
138-
name: {'$options': 'i', '$regex': 'douglas&'}
138+
name: {'$options': 'i', '$regex': 'douglas$'}
139139
},
140140
{
141141
name: {'$options': 'i', '$regex': 'jorge'}

0 commit comments

Comments
 (0)