Comparing version 1.3.1 to 1.4.0
@@ -6,8 +6,2 @@ 'use strict' | ||
/** | ||
* @typedef {Object} Mixin~Addition | ||
* @property {string[]} path | ||
* @property {string[]} value | ||
*/ | ||
/** | ||
* @typedef {string[]} Mixin~Removal | ||
@@ -20,4 +14,4 @@ */ | ||
function Mixin() { | ||
/** @member {Mixin~Addition[]} */ | ||
this.additions = [] | ||
/** @member {Obj} */ | ||
this.additions = null | ||
@@ -41,3 +35,3 @@ /** @member {Mixin~Removal[]} */ | ||
Mixin.prototype.execute = function (context, name) { | ||
var base, i | ||
var base, i, additions | ||
@@ -58,7 +52,10 @@ // Execute base | ||
}) | ||
this.additions.forEach(function (addition) { | ||
var value = _eval(addition.value, context, name + '<with ' + addition.path.join('.') + '>') | ||
add(base, value, addition.path) | ||
}) | ||
if (this.additions) { | ||
additions = this.additions.execute(context, name + '.<with>') | ||
Object.keys(additions).forEach(function (path) { | ||
add(base, additions[path], path.split('.')) | ||
}, this) | ||
} | ||
return base | ||
@@ -65,0 +62,0 @@ } |
@@ -151,2 +151,3 @@ 'use strict' | ||
/** | ||
* @param {boolean} [acceptPath=false] whether the object keys can be paths | ||
* @returns {boolean} false if it's probably not an object | ||
@@ -156,6 +157,8 @@ * @throws {ParseError} if invalid syntax | ||
*/ | ||
Obj.prototype._parseObject = function () { | ||
var i, line, obj, match, key | ||
Obj.prototype._parseObject = function (acceptPath) { | ||
var i, line, obj, match, key, regex | ||
if (!this.lines.length || !this.lines[0].match(/^[a-z$_][a-z0-9$_]*:/i)) { | ||
regex = acceptPath ? /^((\d+|[a-z$_][a-z0-9$_]*)(\.(\d+|[a-z$_][a-z0-9$_]*))*):/i : /^([a-z$_][a-z0-9$_]*):/i | ||
if (!this.lines.length || !regex.test(this.lines[0])) { | ||
// An object must start with '_key_:' | ||
@@ -169,3 +172,3 @@ return false | ||
line = this.lines[i] | ||
if ((match = line.match(/^([a-z$_][a-z0-9$_]*):/i))) { | ||
if ((match = line.match(regex))) { | ||
// A new key | ||
@@ -195,7 +198,7 @@ if (obj) { | ||
Obj.prototype._parseMixin = function () { | ||
var path, str, pos, value | ||
var path, str, i, line, additions | ||
if (this.lines.length !== 1 || | ||
if (!this.lines.length || | ||
!(path = readPath(this.lines[0])) || | ||
!path.newStr.match(/^with(out)? /)) { | ||
!path.newStr.match(/^with(out)?( |$)/)) { | ||
// Must start with a path followed by 'with' or 'without' | ||
@@ -211,3 +214,3 @@ return false | ||
// Without | ||
if (str.indexOf('without ') === 0) { | ||
if (str.indexOf('without') === 0) { | ||
str = eat(str, 7) | ||
@@ -231,23 +234,23 @@ while ((path = readPath(str))) { | ||
// With | ||
while (str.indexOf('with ') === 0) { | ||
if (str.indexOf('with') === 0) { | ||
str = eat(str, 4) | ||
path = readPath(str) | ||
if (!path) { | ||
throw new ParseError('Expected a path after "with"', this) | ||
additions = new Obj(this.source.begin) | ||
additions.push(str) | ||
for (i = 1; i < this.lines.length; i++) { | ||
line = this.lines[i] | ||
if (line[0] === '\t') { | ||
additions.push(line.substr(1)) | ||
} else { | ||
throw new ParseError('Expected the line to start with "\t"', this) | ||
} | ||
} | ||
str = path.newStr | ||
pos = str.indexOf(';') | ||
value = pos === -1 ? str : str.substr(0, pos) | ||
if (!value) { | ||
throw new ParseError('Expected a value for path ' + path.name, this) | ||
if (!additions._parseObject(true)) { | ||
throw new ParseError('Expected an object sub-document', additions) | ||
} | ||
this.value.additions.push({ | ||
path: path.parts, | ||
value: value | ||
}) | ||
str = pos === -1 ? '' : eat(str, pos + 1) | ||
} | ||
additions.parsed = true | ||
if (str) { | ||
throw new ParseError('Could not parse as mixin: "' + str + '"', this) | ||
this.value.additions = additions | ||
} else if (this.lines.length !== 1) { | ||
throw new ParseError('Could not parse as mixin', this) | ||
} | ||
@@ -261,3 +264,3 @@ | ||
* @param {string} str | ||
* @returns {Object} with keys 'path' and 'newStr' or null if no path could be read | ||
* @returns {Object} with keys 'name', 'parts' and 'newStr' or null if no path could be read | ||
*/ | ||
@@ -264,0 +267,0 @@ function readPath(str) { |
@@ -62,3 +62,3 @@ # Doc Syntax | ||
user with pass '1234' | ||
user with pass: '1234' | ||
@@ -73,5 +73,7 @@ will create the object `{name: 'John', pass: '1234'}` | ||
user without name, pass; with age 36; with token randomStr(16) | ||
user without name, pass; with | ||
age: 36 | ||
token: randomStr(16) | ||
`without` must appear before any `with` | ||
`without` must appear before `with` | ||
@@ -89,3 +91,3 @@ ### Paths | ||
order with items.ok true | ||
order with items.ok: true | ||
@@ -92,0 +94,0 @@ will create the object `{items: [{name: 'a', price: 60, ok: true}, {name: 'b', price: 63, ok: true}], price: 123}` |
{ | ||
"name": "api-test", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"author": "Sitegui <sitegui@sitegui.com.br>", | ||
@@ -5,0 +5,0 @@ "description": "API testing made simple", |
@@ -116,3 +116,3 @@ # API Test | ||
``` | ||
user with name.first 'Unhappy' | ||
user with name.first: 'Unhappy' | ||
``` | ||
@@ -185,2 +185,3 @@ | ||
## TODO | ||
* Make keys less restrictive | ||
* Make keys less restrictive | ||
* Make type check in finds (like `{name: String}`) (covert to `$type` operator) |
@@ -22,5 +22,5 @@ # user/login | ||
### Post | ||
user: user with password randomStr(5) | ||
user: user with password: randomStr(5) | ||
### Out | ||
error with error.code 200 | ||
error with error.code: 200 | ||
@@ -27,0 +27,0 @@ ## Valid |
@@ -12,3 +12,3 @@ # user/signup | ||
### Post | ||
user: user with name '' | ||
user: user with name: '' | ||
### Out | ||
@@ -25,3 +25,3 @@ error: | ||
user: | ||
user with password '1' | ||
user with password: '1' | ||
### Out | ||
@@ -39,3 +39,3 @@ error: | ||
### Find in users | ||
user with token out.token | ||
user with token: out.token | ||
@@ -42,0 +42,0 @@ ## Using the same username again |
@@ -25,2 +25,2 @@ # Signup + Login | ||
### Find in users | ||
user with token out.token | ||
user with token: out.token |
@@ -130,12 +130,14 @@ /*globals describe, it*/ | ||
it('should work for simple mixins', function () { | ||
check(['user with pass "1234"'], { | ||
check(['user with pass: "1234"'], { | ||
name: 'John', | ||
pass: '1234' | ||
}) | ||
check(['user without name'], { | ||
pass: '123' | ||
}) | ||
check(['user without name, pass; with age 36; with token randomStr(16)'], { | ||
check([ | ||
'user without name, pass; with', | ||
' age: 36', | ||
' token: randomStr(16)' | ||
], { | ||
age: 36, | ||
@@ -165,3 +167,3 @@ token: 'hi' | ||
check(['order with items.ok true'], { | ||
check(['order with items.ok: true'], { | ||
items: [{ | ||
@@ -168,0 +170,0 @@ name: 'a', |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
241755
2051
185