Comparing version 0.4.4 to 0.4.5
{ | ||
"name": "mediaxml", | ||
"version": "0.4.4", | ||
"version": "0.4.5", | ||
"description": "A general purpose module for working with XML that includes first class support for media manifests like ADI, mRSS, and SCTE-236.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -1030,5 +1030,6 @@ const { normalizeValue } = require('../normalize') | ||
while (backlog.length) { | ||
const item = backlog.shift() | ||
let item = backlog.shift() | ||
if ('string' === typeof item) { | ||
result = await Expression.from(context, item, opts).evaluate() || item | ||
item = item.trim() | ||
result = defined(await Expression.from(context, item, opts).evaluate(), item) | ||
if ('string' === typeof result && item !== result) { | ||
@@ -1044,3 +1045,2 @@ backlog.push(result) | ||
const outputs = context.output.splice(0, context.output.length) | ||
@@ -1061,3 +1061,3 @@ | ||
} else { | ||
resolve(result || null) | ||
resolve(defined(result, null)) | ||
} | ||
@@ -1064,0 +1064,0 @@ }) |
@@ -1,13 +0,156 @@ | ||
const REGEX = /if\s*\((.*?)\)\s*{\n?(\s*|.*?)\n?}\s*?(\s*?else\s*{\n?(\s*|.*?)\n?})*/mg | ||
const REGEX = /\bif(\s|\n|.*)*}/mg | ||
function transform(queryString, ctx) { | ||
const result = queryString.replace(REGEX, replace) | ||
return result | ||
return queryString.replace(REGEX, replace) | ||
function replace(_, condition, body, elseStatement, elseBody) { | ||
return compile(ctx, { condition, body, elseStatement, elseBody }) | ||
.replace(REGEX, replace) | ||
function replace(source) { | ||
const value = parse(ctx, source).replace(REGEX, replace) | ||
return value | ||
} | ||
} | ||
function parse(ctx, source) { | ||
const statements = { | ||
if: null, | ||
elseif: [], | ||
else: null | ||
} | ||
let i = 0 | ||
while (i < source.length) { | ||
const expr = scan('}') | ||
if ('string' === typeof expr) { | ||
consume(expr) | ||
continue | ||
} | ||
switch (expr.name) { | ||
case 'if': statements.if = expr; break | ||
case 'else if': statements.elseif.push(expr); break | ||
case 'else': statements.else = expr; break | ||
default: continue | ||
} | ||
} | ||
if (!statements.if) { | ||
throw new Error('Missing initial `if (condition) { ... }` expression.') | ||
} | ||
if (ctx.evaluate(statements.if.condition)) { | ||
return statements.if.body.slice(1, -1).trim() | ||
} | ||
for (const elseif of statements.elseif) { | ||
if (ctx.evaluate(elseif.condition)) { | ||
return elseif.body.slice(1, -1).trim() | ||
} | ||
} | ||
if (statements.else) { | ||
return statements.else.body.slice(1, -1).trim() | ||
} | ||
return '' | ||
function consume(string) { | ||
i += string.length | ||
} | ||
function peek(start, stop) { | ||
return source.slice((start || i), (start + stop) || source.length) | ||
} | ||
function scan(stop) { | ||
const scopes = { parens: [], brackets: [] } | ||
const buffer = [] | ||
for (; i < source.length; ++i) { | ||
const ch = source[i] | ||
if ('(' === ch) { | ||
scopes.parens.push(ch) | ||
} | ||
if ('{' === ch) { | ||
scopes.brackets.push(ch) | ||
} | ||
if (')' === ch) { | ||
scopes.parens.shift() | ||
} | ||
if ('}' === ch) { | ||
scopes.brackets.shift() | ||
} | ||
if (stop && ch === stop) { | ||
if (')' === ch && 0 === scopes.parens.length) { | ||
buffer.push(ch) | ||
break | ||
} else if ('}' === ch && 0 === scopes.brackets.length) { | ||
buffer.push(ch) | ||
break | ||
} | ||
} | ||
const tmp = buffer.join('').trim() | ||
switch (tmp) { | ||
case 'if': { | ||
const ptr = shift() | ||
const condition = scan(')') | ||
const body = scan('}') | ||
return { | ||
name: ptr.trim(), | ||
body: body.trim(), | ||
condition: condition.trim() | ||
} | ||
} | ||
case 'else if': { | ||
const ptr = shift() | ||
const condition = scan(')') | ||
const body = scan('}') | ||
return { | ||
name: ptr.trim(), | ||
body: body.trim(), | ||
condition: condition.trim() | ||
} | ||
} | ||
case 'else': { | ||
const ptr = shift() | ||
const next = peek().trim() | ||
if ('{' !== next[0]) { | ||
buffer.push(...ptr, ch) | ||
break | ||
} | ||
const body = scan('}') | ||
return { | ||
name: ptr.trim(), | ||
body: body.trim() | ||
} | ||
} | ||
default: | ||
buffer.push(ch) | ||
} | ||
} | ||
i += 1 | ||
return shift() | ||
function shift(offset, length) { | ||
return buffer.splice(offset || 0, length || buffer.length).join('') | ||
} | ||
} | ||
} | ||
function compile(ctx, { condition, body, elseStatement, elseBody }) { | ||
@@ -14,0 +157,0 @@ if (condition) { |
const { AbstractDocument } = require('./document') | ||
const { normalizeValue } = require('./normalize') | ||
const { Entity } = require('./entity') | ||
const assert = require('nanoassert') | ||
@@ -6,0 +5,0 @@ /** |
@@ -51,2 +51,3 @@ const fxp = require('fast-xml-parser') | ||
* @throws ValidationError | ||
* @memberof validate | ||
*/ | ||
@@ -53,0 +54,0 @@ function validate(xml, opts) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
2613944
122
14544