@clubedaentrega/test-spec
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -0,1 +1,4 @@ | ||
# 1.1.0 | ||
* Added: support for Node.js v0.12 | ||
# 1.0.1 | ||
@@ -2,0 +5,0 @@ * Added: docs |
@@ -11,6 +11,6 @@ /** @module */ | ||
module.exports.randomStr = function (len, alphabet) { | ||
let str = '' | ||
var str = '' | ||
len = len || 8 | ||
alphabet = alphabet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' | ||
for (let i = 0; i < len; i++) { | ||
for (var i = 0; i < len; i++) { | ||
str += alphabet[Math.floor(Math.random() * alphabet.length)] | ||
@@ -17,0 +17,0 @@ } |
/** @module */ | ||
'use strict' | ||
let compileValue = require('./compileValue'), | ||
var compileValue = require('./compileValue'), | ||
parse = require('./parse') | ||
@@ -23,3 +23,3 @@ | ||
function compile(blocks) { | ||
blocks.forEach((block, i) => { | ||
blocks.forEach(function (block, i) { | ||
if (block.type === 'section') { | ||
@@ -26,0 +26,0 @@ compile(block.children) |
'use strict' | ||
let getSnippet = require('./getSnippet'), | ||
var getSnippet = require('./getSnippet'), | ||
mixin = require('./mixin'), | ||
@@ -14,3 +14,3 @@ numericRegex = /^\d+$/ | ||
module.exports = function (source, value) { | ||
let code = 'with (__context) {', | ||
var code = 'with (__context) {', | ||
n = 0 | ||
@@ -21,3 +21,3 @@ | ||
let fun | ||
var fun | ||
try { | ||
@@ -31,4 +31,4 @@ /*jshint evil:true*/ | ||
let run = function (context) { | ||
let pos = { | ||
var run = function (context) { | ||
var pos = { | ||
line: 1, | ||
@@ -40,4 +40,4 @@ size: 1 | ||
} catch (err) { | ||
let snippet = getSnippet(source, pos.line, pos.size) | ||
err.message = `${err.message}\n${snippet}` | ||
var snippet = getSnippet(source, pos.line, pos.size) | ||
err.message = err.message + '\n' + snippet | ||
throw err | ||
@@ -71,3 +71,3 @@ } | ||
} | ||
throw new Error(`Invalid subtype: ${value.subtype}`) | ||
throw new Error('Invalid subtype: ' + value.subtype) | ||
} | ||
@@ -81,9 +81,9 @@ | ||
function compileArray(value) { | ||
let name = `__${n++}` | ||
code += `\nvar ${name} = new Array(${value.elements.length})` | ||
code += `\n${name}.isUnordered = ${value.isUnordered}` | ||
var name = '__' + (n++) | ||
code += '\nvar ' + name + ' = new Array(' + value.elements.length + ')' | ||
code += '\n' + name + '.isUnordered = ' + value.isUnordered | ||
value.elements.forEach((subValue, i) => { | ||
let subName = compileValue(subValue) | ||
code += `\n${name}[${i}] = ${subName}` | ||
value.elements.forEach(function (subValue, i) { | ||
var subName = compileValue(subValue) | ||
code += '\n' + name + '[' + i + '] = ' + subName | ||
}) | ||
@@ -100,8 +100,8 @@ | ||
function compileObject(value) { | ||
let name = `__${n++}` | ||
code += `\nvar ${name} = {}` | ||
var name = '__' + (n++) | ||
code += '\nvar ' + name + ' = {}' | ||
value.keys.forEach(each => { | ||
let subName = compileValue(each.value) | ||
code += `\n${name}['${escape(each.name)}'] = ${subName}` | ||
value.keys.forEach(function (each) { | ||
var subName = compileValue(each.value) | ||
code += '\n' + name + '[\'' + escape(each.name) + '\'] = ' + subName | ||
}) | ||
@@ -118,17 +118,17 @@ | ||
function compileMixin(value) { | ||
let name = `__${n++}`, | ||
nameAdditions = `__${n++}`, | ||
var name = '__' + (n++), | ||
nameAdditions = '__' + (n++), | ||
base = value.base.replace(/\.(\d+)(?=\.|$)/g, '[$1]'), | ||
removals = value.removals.map(preparePath).join(', ') | ||
code += `\nvar ${nameAdditions} = new Array(${value.additions.length})` | ||
code += '\nvar ' + nameAdditions + ' = new Array(' + value.additions.length + ')' | ||
value.additions.forEach((addition, i) => { | ||
let path = preparePath(addition.name), | ||
value.additions.forEach(function (addition, i) { | ||
var path = preparePath(addition.name), | ||
subName = compileValue(addition.value) | ||
code += `\n${nameAdditions}[${i}] = {path: ${path}, value: ${subName}}` | ||
code += '\n' + nameAdditions + '[' + i + '] = {path: ' + path + ', value: ' + subName + '}' | ||
}) | ||
code += `\n__pos.line = ${value.line}, __pos.size = ${value.size}` | ||
code += `\nvar ${name} = __mixin(${base}, [${removals}], ${nameAdditions})` | ||
code += '\n__pos.line = ' + value.line + ', __pos.size = ' + value.size | ||
code += '\nvar ' + name + ' = __mixin(' + base + ', [' + removals + '], ' + nameAdditions + ')' | ||
@@ -144,6 +144,6 @@ return name | ||
function compileJS(value) { | ||
let name = `__${n++}` | ||
var name = '__' + (n++) | ||
code += `\n__pos.line = ${value.line}, __pos.size = ${value.size}` | ||
code += `\nvar ${name} = (${value.code})` | ||
code += '\n__pos.line = ' + value.line + ', __pos.size = ' + value.size | ||
code += '\nvar ' + name + ' = (' + value.code + ')' | ||
return name | ||
@@ -160,5 +160,7 @@ } | ||
function preparePath(path) { | ||
let parts = path.split('.') | ||
parts = parts.map(part => numericRegex.test(part) ? part : `'${escape(part)}'`) | ||
return `[${parts.join(', ')}]` | ||
var parts = path.split('.') | ||
parts = parts.map(function (part) { | ||
return numericRegex.test(part) ? part : '\'' + escape(part) + '\'' | ||
}) | ||
return '[' + parts.join(', ') + ']' | ||
} | ||
@@ -165,0 +167,0 @@ } |
@@ -12,12 +12,12 @@ /** @module */ | ||
module.exports = function (source, line, size) { | ||
let lines = source.split(/\r?\n/), | ||
var lines = source.split(/\r?\n/), | ||
fromLine = Math.max(1, line - 2) - 1, | ||
toLine = Math.min(lines.length, line + size + 2) | ||
return lines.slice(fromLine, toLine).map((str, i) => { | ||
let lineNum = i + 1 + fromLine | ||
return lines.slice(fromLine, toLine).map(function (str, i) { | ||
var lineNum = i + 1 + fromLine | ||
if (lineNum >= line && lineNum < line + size) { | ||
return ` ${lineNum} >> | ${str}` | ||
return ' ' + lineNum + ' >> | ' + str | ||
} | ||
return ` ${lineNum} | ${str}` | ||
return ' ' + lineNum + ' | ' + str | ||
}).join('\n') | ||
} |
@@ -15,5 +15,9 @@ 'use strict' | ||
let result = copyDeep(base) | ||
removals.forEach(path => remove(result, path)) | ||
additions.forEach(each => add(result, each.path, each.value)) | ||
var result = copyDeep(base) | ||
removals.forEach(function (path) { | ||
remove(result, path) | ||
}) | ||
additions.forEach(function (each) { | ||
add(result, each.path, each.value) | ||
}) | ||
@@ -34,4 +38,4 @@ return result | ||
// Map | ||
let r = {} | ||
for (let key in x) { | ||
var r = {} | ||
for (var key in x) { | ||
r[key] = copyDeep(x[key]) | ||
@@ -56,7 +60,7 @@ } | ||
let key = path[i], | ||
var key = path[i], | ||
last = i === path.length - 1 | ||
if (!obj || typeof obj !== 'object') { | ||
throw new Error(`Can't remove key ${key} from non-object`) | ||
throw new Error('Can\'t remove key ' + key + ' from non-object') | ||
} | ||
@@ -76,7 +80,7 @@ | ||
} else { | ||
throw new Error(`Can't remove index ${key} from an array with ${obj.length} elements`) | ||
throw new Error('Can\'t remove index ' + key + ' from an array with ' + obj.length + ' elements') | ||
} | ||
} else { | ||
if (typeof key !== 'string') { | ||
throw new Error(`Can't remove the numeric key ${key} from an object`) | ||
throw new Error('Can\'t remove the numeric key ' + key + ' from an object') | ||
} else if (key in obj) { | ||
@@ -89,3 +93,3 @@ if (last) { | ||
} else { | ||
throw new Error(`Can't remove key ${key} from the object`) | ||
throw new Error('Can\'t remove key ' + key + ' from the object') | ||
} | ||
@@ -107,7 +111,7 @@ } | ||
let key = path[i], | ||
var key = path[i], | ||
last = i === path.length - 1 | ||
if (!obj || typeof obj !== 'object') { | ||
throw new Error(`Can't add key ${key}' to non-object`) | ||
throw new Error('Can\'t add key ' + key + ' to non-object') | ||
} | ||
@@ -127,7 +131,7 @@ | ||
} else { | ||
throw new Error(`Can't add index ${key} to an array with ${obj.length} elements`) | ||
throw new Error('Can\'t add index ' + key + ' to an array with ' + obj.length + ' elements') | ||
} | ||
} else { | ||
if (typeof key !== 'string') { | ||
throw new Error(`Can't add the numeric key ${key} to an object`) | ||
throw new Error('Can\'t add the numeric key ' + key + ' to an object') | ||
} else { | ||
@@ -134,0 +138,0 @@ if (last) { |
/** @module */ | ||
'use strict' | ||
let getSnippet = require('./getSnippet'), | ||
var getSnippet = require('./getSnippet'), | ||
parseValue = require('./parseValue'), | ||
@@ -51,3 +51,3 @@ emptyRegex = /^[ \t]*$/, | ||
// Split in lines | ||
let lines = source.split(/\r?\n/), | ||
var lines = source.split(/\r?\n/), | ||
// Current section | ||
@@ -75,7 +75,7 @@ section = { | ||
// Whether the next block should not amend the previous | ||
let newBlock = true | ||
var newBlock = true | ||
// Pre parse each line, without filling text content or parsing values | ||
for (line = 2; line <= lines.length; line++) { | ||
let str = lines[line - 1] | ||
var str = lines[line - 1] | ||
@@ -99,8 +99,8 @@ if (emptyRegex.test(str)) { | ||
// Gather text content and parse values | ||
let finalParse = function (section) { | ||
section.children.forEach(child => { | ||
var finalParse = function (section) { | ||
section.children.forEach(function (child) { | ||
if (child.type === 'section') { | ||
finalParse(child) | ||
} else if (child.type === 'text' || child.type === 'value') { | ||
let sliceStart = child.line - 1, | ||
var sliceStart = child.line - 1, | ||
sliceEnd = child.line + child.size - 1, | ||
@@ -112,6 +112,8 @@ subLines = lines.slice(sliceStart, sliceEnd) | ||
// Remove starting tab character | ||
parseValue(child, subLines.map((e, i) => ({ | ||
line: child.line + i, | ||
str: e.substr(1) | ||
})), throwSyntaxError) | ||
parseValue(child, subLines.map(function (e, i) { | ||
return { | ||
line: child.line + i, | ||
str: e.substr(1) | ||
} | ||
}), throwSyntaxError) | ||
} | ||
@@ -141,6 +143,6 @@ } | ||
function takeHeader(level, name) { | ||
let currLevel = sectionStack.length | ||
var currLevel = sectionStack.length | ||
if (level > currLevel + 1) { | ||
throwSyntaxError(`Unexpected header level ${level} on section level ${currLevel}`) | ||
throwSyntaxError('Unexpected header level ' + level + ' on section level ' + currLevel) | ||
} else if (level === 1) { | ||
@@ -175,4 +177,4 @@ throwSyntaxError('There can be only one header level 1, the first line of the file') | ||
size = size === undefined ? 1 : size | ||
let snippet = getSnippet(source, errorLine, size), | ||
err = new SyntaxError(`${message}\n${snippet}`) | ||
var snippet = getSnippet(source, errorLine, size), | ||
err = new SyntaxError(message + '\n' + snippet) | ||
err.line = errorLine | ||
@@ -189,3 +191,3 @@ throw err | ||
function appendLine(type) { | ||
let len = section.children.length | ||
var len = section.children.length | ||
if (newBlock || | ||
@@ -192,0 +194,0 @@ !len || |
'use strict' | ||
let objectKeyRegex = /^(?:(\w+\??)|(['"])((\\\\|\\\2|.)*?)\2):\s*/i, | ||
var objectKeyRegex = /^(?:(\w+\??)|(['"])((\\\\|\\\2|.)*?)\2):\s*/i, | ||
objectPathRegex = /^(\w+(\.\w+)*):\s*/i, | ||
@@ -52,6 +52,6 @@ mixinRegex = /^(\w+(?:\.\w+)*) (without|with)( .*)?$/, | ||
let subValue, subLines | ||
var subValue, subLines | ||
lines.forEach(each => { | ||
let line = each.line, | ||
lines.forEach(function (each) { | ||
var line = each.line, | ||
str = each.str | ||
@@ -62,3 +62,3 @@ | ||
if (str[1] !== '\t') { | ||
throwSyntaxError(`Expected a tab after ${str[0]}`, line) | ||
throwSyntaxError('Expected a tab after ' + str[0], line) | ||
} | ||
@@ -106,3 +106,3 @@ if (subValue) { | ||
function tryParseObject(block, lines, allowPath) { | ||
let regex = allowPath ? objectPathRegex : objectKeyRegex | ||
var regex = allowPath ? objectPathRegex : objectKeyRegex | ||
@@ -118,6 +118,6 @@ if (!lines.length || !regex.test(lines[0].str)) { | ||
let subValue, subLines | ||
var subValue, subLines | ||
lines.forEach(each => { | ||
let line = each.line, | ||
lines.forEach(function (each) { | ||
var line = each.line, | ||
str = each.str, | ||
@@ -133,3 +133,3 @@ match | ||
// 1 for path and simple key; 3 for escaped key | ||
let key = allowPath ? match[1] : match[1] || match[3] | ||
var key = allowPath ? match[1] : match[1] || match[3] | ||
subValue = { | ||
@@ -170,3 +170,3 @@ type: 'value', | ||
function tryParseMixin(block, lines) { | ||
let match | ||
var match | ||
@@ -186,6 +186,6 @@ if (!lines.length || | ||
// Without | ||
let preposition = match[2], | ||
var preposition = match[2], | ||
substr = (match[3] || '').trimLeft() | ||
if (preposition === 'without') { | ||
let subMatch = substr.match(mixinRemovalsRegex) | ||
var subMatch = substr.match(mixinRemovalsRegex) | ||
@@ -196,6 +196,6 @@ if (!subMatch) { | ||
block.removals = subMatch[1].split(',').map(each => { | ||
block.removals = subMatch[1].split(',').map(function (each) { | ||
each = each.trim() | ||
if (!each.match(pathRegex)) { | ||
throwSyntaxError(`Invalid removal path: ${each}`, lines[0].line) | ||
throwSyntaxError('Invalid removal path: ' + each, lines[0].line) | ||
} | ||
@@ -217,3 +217,3 @@ return each | ||
if (preposition === 'with') { | ||
let subValue = { | ||
var subValue = { | ||
type: 'value', | ||
@@ -228,4 +228,4 @@ line: lines[0].line, | ||
lines.forEach((each, i) => { | ||
let line = each.line, | ||
lines.forEach(function (each, i) { | ||
var line = each.line, | ||
str = each.str | ||
@@ -283,3 +283,3 @@ if (!i) { | ||
function cleanLines(block, lines) { | ||
let removeFirst = false | ||
var removeFirst = false | ||
if (lines.length && emptyRegex.test(lines[0].str)) { | ||
@@ -291,3 +291,3 @@ block.line += 1 | ||
return lines.filter((each, i) => { | ||
return lines.filter(function (each, i) { | ||
return !(!i && removeFirst) && !commentRegex.test(each.str) | ||
@@ -294,0 +294,0 @@ }) |
{ | ||
"name": "@clubedaentrega/test-spec", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"author": "Sitegui <sitegui@sitegui.com.br>", | ||
@@ -15,3 +15,3 @@ "description": "A parser and runtime for markdown-inspired documentation and testing files", | ||
"engines": { | ||
"node": ">=4.2" | ||
"node": ">=0.12" | ||
}, | ||
@@ -24,5 +24,5 @@ "scripts": { | ||
"jsdoc": "^3.4.0", | ||
"mocha": "^2.3.4", | ||
"should": "^8.0.2" | ||
"mocha": "^2.4.5", | ||
"should": "^8.2.2" | ||
} | ||
} |
@@ -13,6 +13,6 @@ # Test Spec Parser | ||
```js | ||
let spec = require('@clubedaentrega/test-spec') | ||
var spec = require('@clubedaentrega/test-spec') | ||
// Usually, the source is read from some *.md file | ||
let source = '# Title\n'+ | ||
var source = '# Title\n'+ | ||
'## Sub section\n'+ | ||
@@ -24,3 +24,3 @@ 'Some textual content\n'+ | ||
// Compile the source to a tree of section, text and value blocks | ||
let mainSection = spec.compile(source), | ||
var mainSection = spec.compile(source), | ||
subSection = mainSection.children[0], | ||
@@ -27,0 +27,0 @@ valueBlock = subSection.children[1], |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
32255
897