|
1 | 1 | # Objectron
|
2 | 2 |
|
3 |
| -| build |  | |
4 |
| -|-------|------------------------------------------------------------------------------------------------------| |
| 3 | +> Compares a set of match rules contained in a tester object with a payload object to determine if the latter conforms to the matching rules. |
5 | 4 |
|
6 |
| -- [Objectron](#objectron) |
7 |
| - - [Synopsis](#synopsis) |
8 |
| - - [Install](#install) |
9 |
| - - [Usage](#usage) |
10 |
| - - [Collaborators](#collaborators) |
| 5 | + |
| 6 | + |
11 | 7 |
|
12 |
| -## Synopsis |
| 8 | +This module provides you with the means to define a **tester object** cotaning a set of **match rules** that will be used against a **payload object**. The `match()` method within the module will return whether the payload object has satisfied all the rules and will return the set of matches. |
13 | 9 |
|
14 |
| -TBD |
| 10 | +Refer to [Usage example](#usage-example) to see it in action. |
15 | 11 |
|
16 |
| -## Install |
| 12 | +> GIF demonstrating the plugin -- TBD |
17 | 13 |
|
18 |
| -**Requires node v10.13.x or above** |
| 14 | +## Installation |
19 | 15 |
|
| 16 | +Windows, macOS, Linux -- **requires node v10.13.x or above** |
| 17 | + |
| 18 | +``` |
| 19 | +$ npm install --save @menadevs/objectron |
20 | 20 | ```
|
21 |
| -$ npm install |
22 | 21 |
|
23 |
| -# Run the tests |
| 22 | +Run tests (optional) |
| 23 | +``` |
24 | 24 | $ npm test
|
25 | 25 | ```
|
26 | 26 |
|
27 |
| -## Usage |
| 27 | +## Usage examples |
| 28 | + |
| 29 | +Keep in mind that the tester object can be a subset of the payload. You don't have to write explicit rules for all the properties of the payload object. |
| 30 | + |
| 31 | +### 1. Simple match (Success) |
| 32 | + |
| 33 | +The tester object will match with the payload provided |
| 34 | + |
| 35 | +```javascript |
| 36 | +const match = require('@menadevs/objectron'); |
| 37 | + |
| 38 | +const payload = { |
| 39 | + 'type': 'message', |
| 40 | + 'text': 'text', |
| 41 | +} |
| 42 | + |
| 43 | +const tester = { |
| 44 | + 'type': 'message', |
| 45 | + 'text': 'text', |
| 46 | +} |
| 47 | + |
| 48 | +const result = match(payload, tester); |
| 49 | + |
| 50 | +console.log(result) |
| 51 | + |
| 52 | +# Output |
| 53 | +> { |
| 54 | + match: true, |
| 55 | + total: 2, |
| 56 | + matches: { type: 'message', text: 'text' }, |
| 57 | + groups: {} |
| 58 | + } |
| 59 | +``` |
| 60 | + |
| 61 | +### 2. Simple match (Fail) |
| 62 | + |
| 63 | +The tester object will not match with the payload provided |
| 64 | + |
| 65 | +```javascript |
| 66 | +const match = require('@menadevs/objectron'); |
| 67 | + |
| 68 | +const payload = { |
| 69 | + 'type': 'message', |
| 70 | + 'text': 'text', |
| 71 | +} |
| 72 | + |
| 73 | +const tester = { |
| 74 | + 'another_key': 'different value', |
| 75 | +} |
| 76 | + |
| 77 | +const result = match(payload, tester); |
| 78 | + |
| 79 | +console.log(result) |
| 80 | + |
| 81 | +# Output |
| 82 | +> { |
| 83 | + match: false, |
| 84 | + total: 0, |
| 85 | + matches: {}, |
| 86 | + groups: {} |
| 87 | + } |
| 88 | +``` |
| 89 | + |
| 90 | +### 3. Simple match with RegEx (Success) |
| 91 | + |
| 92 | +You can use regular expressions to build generic tester objects |
| 93 | + |
| 94 | +```javascript |
| 95 | +const match = require('@menadevs/objectron'); |
28 | 96 |
|
29 |
| -TBD |
| 97 | +const payload = { |
| 98 | + 'type': 'message', |
| 99 | + 'text': 'invite Smith', |
| 100 | +} |
30 | 101 |
|
31 |
| -## Collaborators |
| 102 | +const tester = { |
| 103 | + 'type': 'message', |
| 104 | + 'text': /invite (\S+)/, |
| 105 | +} |
| 106 | + |
| 107 | +const result = match(payload, tester); |
| 108 | + |
| 109 | +console.log(result) |
| 110 | + |
| 111 | +# Output |
| 112 | +> { |
| 113 | + match: true, |
| 114 | + total: 2, |
| 115 | + matches: { type: 'message', text: 'invite Smith' }, |
| 116 | + groups: {} |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +### 4. Match with RegEx and named groups (Success) |
| 121 | + |
| 122 | +You can use regular expression named groups to capture matches separately |
| 123 | + |
| 124 | +```javascript |
| 125 | +const match = require('@menadevs/objectron'); |
| 126 | + |
| 127 | +const payload = { |
| 128 | + 'type': 'message', |
| 129 | + 'text': 'invite (Smith) ([email protected]) (CompanyX) (Engineer)', |
| 130 | +} |
| 131 | + |
| 132 | +const tester = { |
| 133 | + 'type': 'message', |
| 134 | + 'text': /invite \((?<name>\S+)\) \((?<email>\S+)\) \((?<company>\S+)\) \((?<role>\S+)\)/, |
| 135 | +} |
| 136 | + |
| 137 | +const result = match(payload, tester); |
| 138 | + |
| 139 | +console.log(result) |
| 140 | + |
| 141 | +# Output |
| 142 | +> { |
| 143 | + match: true, |
| 144 | + total: 2, |
| 145 | + matches: { |
| 146 | + type: 'message', |
| 147 | + text : 'invite (Smith) ([email protected]) (CompanyX) (Engineer)' |
| 148 | + }, |
| 149 | + groups: { |
| 150 | + name: 'Smith', |
| 151 | + |
| 152 | + company: 'CompanyX', |
| 153 | + role: 'Engineer' |
| 154 | + } |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +### 5. Match with nested Objects, Arrays and RegExp (Success) |
| 159 | + |
| 160 | +You can create complex tester objects with an indefinite nesting depth |
| 161 | + |
| 162 | +```javascript |
| 163 | +const match = require('@menadevs/objectron'); |
| 164 | + |
| 165 | +const payload = { |
| 166 | + 'type': 'message', |
| 167 | + 'level1': [ |
| 168 | + { |
| 169 | + 'level2': [ |
| 170 | + { |
| 171 | + 'text': 'invite (Smith) ([email protected]) (CompanyX) (Engineer)' |
| 172 | + } |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + 'text': 'secondary object' |
| 177 | + } |
| 178 | + ] |
| 179 | +} |
| 180 | + |
| 181 | +const tester = { |
| 182 | + 'level1': [ |
| 183 | + { |
| 184 | + 'level2': [ |
| 185 | + { |
| 186 | + 'text': /invite \((?<name>\S+)\) \((?<email>\S+)\) \((?<company>\S+)\) \((?<role>\S+)\)/, |
| 187 | + } |
| 188 | + ] |
| 189 | + } |
| 190 | + ] |
| 191 | +} |
| 192 | + |
| 193 | +const result = match(payload, tester); |
| 194 | + |
| 195 | +console.dir(result, {depth: null}); |
| 196 | + |
| 197 | +# Output |
| 198 | +> { |
| 199 | + match: true, |
| 200 | + total: 1, |
| 201 | + matches: { |
| 202 | + level1: [ |
| 203 | + { |
| 204 | + level2: [ |
| 205 | + { |
| 206 | + text : 'invite (Smith) ([email protected]) (CompanyX) (Engineer)' |
| 207 | + } |
| 208 | + ] |
| 209 | + } |
| 210 | + ] |
| 211 | + }, |
| 212 | + groups: { |
| 213 | + name: 'Smith', |
| 214 | + |
| 215 | + company: 'CompanyX', |
| 216 | + role: 'Engineer' |
| 217 | + } |
| 218 | + } |
| 219 | +``` |
| 220 | + |
| 221 | +## Meta |
32 | 222 |
|
33 | 223 | - [@Link-](https://github.com/Link-)
|
34 | 224 | - [@aymanfarhat](https://github.com/aymanfarhat)
|
35 | 225 | - [@omaraboumrad](https://github.com/omaraboumrad)
|
36 | 226 |
|
| 227 | +## Contributing |
| 228 | + |
37 | 229 | This project does not require a Contributor License Agreement.
|
0 commit comments