Comparing version 1.0.1 to 2.0.0
@@ -0,1 +1,11 @@ | ||
<a name="2.0.0"></a> | ||
# [2.0.0](https://github.com/poppinss/haye/compare/v1.0.1...v2.0.0) (2017-12-01) | ||
### Features | ||
* rewrite ([d825339](https://github.com/poppinss/haye/commit/d825339)) | ||
<a name="1.0.1"></a> | ||
@@ -2,0 +12,0 @@ ## [1.0.1](https://github.com/poppinss/haye/compare/v1.0.0...v1.0.1) (2016-09-30) |
22
index.js
@@ -1,3 +0,1 @@ | ||
'use strict' | ||
/* | ||
@@ -12,2 +10,20 @@ * haye | ||
module.exports = require('./src/Haye') | ||
const Pipe = require('./src/Parsers/Pipe') | ||
const Qs = require('./src/Parsers/Qs') | ||
const ArrayPresenter = require('./src/Presenters/ArrayPresenter') | ||
const JsonPresenter = require('./src/Presenters/JsonPresenter') | ||
module.exports = { | ||
fromPipe (expression) { | ||
return { | ||
toArray: () => Pipe(expression, new ArrayPresenter()), | ||
toJSON: () => Pipe(expression, new JsonPresenter()) | ||
} | ||
}, | ||
fromQS (expression) { | ||
return { | ||
toArray: () => Qs(expression, new ArrayPresenter()), | ||
toJSON: () => Qs(expression, new JsonPresenter()) | ||
} | ||
} | ||
} |
{ | ||
"name": "haye", | ||
"version": "1.0.1", | ||
"description": "Haye is an expressive string syntax to Array/Object and Vice-Versa parser for Javascript. Quite often you want your users to define some values using an expressive string expression instead of nesting values in an array or object.", | ||
"version": "2.0.0", | ||
"description": "Haye is a super fast string based expression parser for Node.js", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "istanbul cover _mocha --report lcovonly -- -R spec test && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage", | ||
"coverage": "istanbul cover _mocha test", | ||
"lint": "standard" | ||
"pretest": "npm run lint", | ||
"test": "japa", | ||
"lint": "standard src/**/*.js index.js test/**/*.js", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
"benchmark": "matcha benchmarks/index.js" | ||
}, | ||
@@ -16,15 +18,12 @@ "keywords": [ | ||
], | ||
"author": "amanvirk,adonisjs", | ||
"author": "virk", | ||
"license": "MIT", | ||
"dependencies": { | ||
"lodash": "^4.16.2" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"coveralls": "^2.11.14", | ||
"cz-conventional-changelog": "^1.2.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.1.0", | ||
"mocha-lcov-reporter": "^1.2.0", | ||
"standard": "^8.3.0" | ||
"benchmark": "^2.1.4", | ||
"coveralls": "^3.0.0", | ||
"cz-conventional-changelog": "^2.1.0", | ||
"japa": "^1.0.5", | ||
"japa-cli": "^1.0.1", | ||
"standard": "^10.0.3" | ||
}, | ||
@@ -31,0 +30,0 @@ "config": { |
314
README.md
# Haye | ||
![](http://i1117.photobucket.com/albums/k594/thetutlage/poppins-1_zpsg867sqyl.png) | ||
Haye is a simple super fast string expression parser. In support `pipe` and `qs` string expressions ( explained below ). | ||
[![Version](https://img.shields.io/npm/v/haye.svg?style=flat-square)](https://www.npmjs.com/package/haye) | ||
[![Build Status](https://img.shields.io/travis/poppinss/haye/master.svg?style=flat-square)](https://travis-ci.org/poppinss/haye) | ||
[![Coverage Status](https://img.shields.io/coveralls/poppinss/haye/master.svg?style=flat-square)](https://coveralls.io/github/poppinss/haye?branch=master) | ||
[![Downloads](https://img.shields.io/npm/dt/haye.svg?style=flat-square)](https://www.npmjs.com/package/haye) | ||
[![License](https://img.shields.io/npm/l/haye.svg?style=flat-square)](https://opensource.org/licenses/MIT) | ||
[![Support AdonisJs](https://img.shields.io/badge/support-adonisjs-brightgreen.svg?style=flat-square)](https://www.patreon.com/adonisframework) | ||
> **Limitations** | ||
The keys/values inside the expression cannot have any of the reserved keywords, otherwise parser will mis-behave. | ||
Haye is an expressive string syntax to Array/Object and Vice-Versa parser for Javascript. Quite often you want your users to define some values using an **expressive string expression** instead of nesting values in an **array** or **object**. | ||
--- | ||
> Want to build production ready apps with joy similar to ROR and Laravel. Try [AdonisJs](http://adonisjs.com/) an opinionated MVC framework for Node.js. | ||
<details> | ||
<summary> Benchmarks </summary> | ||
<pre> | ||
<code> | ||
haye #pipeToArray x 741,030 ops/sec ±0.97% (90 runs sampled) | ||
haye #pipeToJson x 313,101 ops/sec ±0.95% (87 runs sampled) | ||
haye #qsToArray x 698,688 ops/sec ±0.74% (91 runs sampled) | ||
haye #qsToJson x 303,482 ops/sec ±1.10% (89 runs sampled) | ||
</code> | ||
</pre> | ||
</details> | ||
Expressive strings are great for Humans but not for machines/programs. Using `haye` you can convert them to machine readable objects/arrays. | ||
<details> | ||
<summary> Comparison with 1.0.1 </summary> | ||
<pre> | ||
<code> | ||
219,138 op/s » haye #pipeToArray #legacy | ||
170,068 op/s » haye #pipeToJson #legacy | ||
147,594 op/s » haye #qsToArray #legacy | ||
121,094 op/s » haye #qsToJson #legacy | ||
## Expressions | ||
747,298 op/s » haye #pipeToArray | ||
363,152 op/s » haye #pipeToJson | ||
742,310 op/s » haye #qsToArray | ||
349,075 op/s » haye #qsToJson | ||
</code> | ||
</pre> | ||
</details> | ||
Their are 2 types of string expressions `haye` will parse and return an array or object (based on your preference). | ||
<details> | ||
<summary> Upgrading from 1.0.1 </summary> | ||
<p> There are couple of breaking changes from 1.0.1 to 2.x.x </p> | ||
### Pipe Expression | ||
<ol> | ||
<li> | ||
All methods to convert <code>Arrays</code> and <code>Objects</code> have been removed. | ||
</li> | ||
The pipe expression is quite popular by [Laravel](https://laravel.com/docs/validation) validation engine and also adopted by [Indicative](http://indicative.adonisjs.com/) a validation engine for Node.js | ||
<li> | ||
The `args` property in `toArray` methods is always an array. Earlier it used to be string for single values and array for multiple. | ||
</li> | ||
#### Pipe To Array | ||
```javascript | ||
const haye = require('haye') | ||
<li> | ||
The value in `key/value` pair is always an array. Earlier it used to be string for single values and array for multiple. | ||
</li> | ||
</ol> | ||
const expression = 'required|min:10|max:20' | ||
const parsedExp = haye.fromPipe(expression).toArray() | ||
``` | ||
</details> | ||
Returns | ||
### Pipe expression | ||
The pipe based expression is very popular in Laravel community, due to their [Validation engine](https://laravel.com/docs/validation), and same is adopted by [Indicative](http://indicative.adonisjs.com). | ||
```javascript | ||
[ | ||
{ | ||
name: 'required', | ||
args: null | ||
}, { | ||
name: 'min', | ||
args: '10' | ||
}, { | ||
name: 'max', | ||
args: '20' | ||
} | ||
] | ||
#### Syntax example: | ||
```js | ||
required|email|max:4|range:10,30 | ||
``` | ||
#### Pipe To JSON | ||
1. Each item is separated by `|` | ||
2. The values are defined after `:` | ||
3. Multiple values are separated by `,`. | ||
4. White spaces in keys are trimmed. | ||
Also you can change the output format from an array to an object. | ||
--- | ||
### Qs expression | ||
The query string expression is almost similar to the URL query string, with couple of small modifications to make the expression readable. | ||
```javascript | ||
const haye = require('haye') | ||
const expression = 'required|min:10|max:20' | ||
const parsedExp = haye.fromPipe(expression).toJSON() | ||
#### Syntax example: | ||
``` | ||
Returns | ||
```javascript | ||
{ | ||
required: null, | ||
min: '10', | ||
max: '20' | ||
} | ||
required,email,max=4,range=[1, 10] | ||
``` | ||
#### How about multiple values next to key? | ||
1. Each item is separated by `,` | ||
2. The values are defined after `=` | ||
3. Multiple values are separated by `,` inside `[]`. | ||
4. White spaces in keys are trimmed. | ||
```javascript | ||
const haye = require('haye') | ||
## Installation | ||
const expression = 'ranger:10,20' | ||
const parsedExp = haye.fromPipe(expression).toArray() | ||
``` | ||
The module is available on npm | ||
Returns | ||
```bash | ||
npm i haye | ||
```javascript | ||
[{ | ||
name: 'range', | ||
args: ['10', '20'] | ||
}] | ||
# yarn | ||
yarn add haye | ||
``` | ||
The `toJSON` method on the same expression will return | ||
## Usage | ||
Below is the bunch of usage examples | ||
```javascript | ||
{ | ||
range: ['10', '20'] | ||
} | ||
``` | ||
#### Pipe -> Array | ||
### QS Expression | ||
The reason I call it a `QS` expression, since it is quite similar to the query string instead you use `,` as a seperator instea of `&`. | ||
#### QS To Array | ||
```javascript | ||
```js | ||
const haye = require('haye') | ||
const expression = 'required|email:unique,users' | ||
const expression = 'username=virk,firstname=harminder,lastname=virk' | ||
const parsedExp = haye.fromQS(expression).toArray() | ||
const parsed = haye.fromPipe(expression).toArray() | ||
``` | ||
Returns | ||
```javascript | ||
Output | ||
```js | ||
[ | ||
{ | ||
name: 'username', | ||
args: 'virk' | ||
}, { | ||
name: 'firstname', | ||
args: 'harminder' | ||
}, { | ||
name: 'lastname', | ||
args: 'virk' | ||
} | ||
{ name: 'required', args: [] }, | ||
{ name: 'email', args: ['unique', 'users'] } | ||
] | ||
``` | ||
#### QS To JSON | ||
#### Pipe -> JSON | ||
Ofcourse you can get values back to a flat Object too. | ||
```javascript | ||
```js | ||
const haye = require('haye') | ||
const expression = 'required|email:unique,users' | ||
const expression = 'username=virk,firstname=harminder,lastname=virk' | ||
const parsedExp = haye.fromQS(expression).toJSON() | ||
const parsed = haye.fromPipe(expression).toJSON() | ||
``` | ||
Returns | ||
```javascript | ||
Output | ||
```js | ||
{ | ||
username: 'virk', | ||
firstname: 'harminder', | ||
lastname: 'virk' | ||
required: [], | ||
email: [ 'unique', 'users' ] | ||
} | ||
``` | ||
#### Multiple Values | ||
You can also define multiple values and they will be returned back as array. | ||
#### Qs -> Array | ||
```javascript | ||
```js | ||
const haye = require('haye') | ||
const expression = 'required,email=[unique,users]' | ||
const expression = 'username=virk,likes=[javascript,ruby,haskell]' | ||
const parsedExp = haye.fromQS(expression).toArray() | ||
const parsed = haye.fromQS(expression).toArray() | ||
``` | ||
Returns | ||
```javascript | ||
Output | ||
```js | ||
[ | ||
{ | ||
name: 'username', | ||
args: 'virk' | ||
}, { | ||
name: 'likes', | ||
args: ['javascript', 'ruby', 'haskell'] | ||
} | ||
{ name: 'required', args: [] }, | ||
{ name: 'email', args: ['unique', 'users'] } | ||
] | ||
``` | ||
And `toJSON` will return a flat object with likes as an array. | ||
#### Qs -> JSON | ||
```javascript | ||
{ | ||
username: 'virk', | ||
likes: ['javascriot', 'ruby', 'haskell'] | ||
} | ||
``` | ||
## Values To Expression (VICE-VERSA) | ||
Also you can convert your arrays and objects back to `pipe` or `QS` expression. | ||
### Pipe Expression | ||
#### Array To Pipe | ||
```javascript | ||
```js | ||
const haye = require('haye') | ||
const rules = [ | ||
{ | ||
name: 'min', | ||
args: 4 | ||
}, { | ||
name: 'max', | ||
args: 10 | ||
} | ||
] | ||
const expression = 'required,email=[unique,users]' | ||
haye.fromArray(rules).toPipe() | ||
const parsed = haye.fromQS(expression).toJSON() | ||
``` | ||
Returns | ||
``` | ||
'min:4|max:10' | ||
``` | ||
#### JSON To Pipe | ||
```javascript | ||
const haye = require('haye') | ||
const rules = { | ||
min: 4, | ||
max:10 | ||
Output | ||
```js | ||
{ | ||
required: [], | ||
email: [ 'unique', 'users' ] | ||
} | ||
haye.fromJSON(rules).toPipe() | ||
``` | ||
Returns | ||
``` | ||
'min:4|max:10' | ||
``` | ||
### QS Expression | ||
Just like the pipe expression, you can convert your arrays and objects to QS expression too. | ||
#### Array To QS | ||
```javascript | ||
const haye = require('haye') | ||
const user = [ | ||
{ | ||
name: 'username', | ||
args: 'virk' | ||
}, { | ||
name: 'likes', | ||
args: ['js', 'ruby'] | ||
} | ||
] | ||
haye.fromArray(rules).toQS() | ||
``` | ||
Returns | ||
``` | ||
username=virk,likes=[js,ruby] | ||
``` | ||
#### JSON To QS | ||
```javascript | ||
const haye = require('haye') | ||
const user = { | ||
username: 'virk', | ||
likes: ['js', 'ruby'] | ||
} | ||
haye.fromJSON(rules).toQS() | ||
``` | ||
Returns | ||
``` | ||
username=virk,likes=[js,ruby] | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
0
6
9
9150
170
170
2
- Removedlodash@^4.16.2
- Removedlodash@4.17.21(transitive)