Comparing version 0.0.7 to 0.0.8
@@ -14,2 +14,3 @@ /** | ||
mergeStatements, | ||
replaceStatements, | ||
printStatements; | ||
@@ -179,2 +180,32 @@ | ||
/** | ||
* ## Replace Statement | ||
* | ||
* Recursively find a matching statement, and replace it | ||
* | ||
* E.g. statements = replaceStatements([..statements], {prop: 'primary_tag'}, function (statement) { | ||
* statement = {} | ||
* return statement; | ||
* }); | ||
* | ||
* @param {Array} statements - an array of statements from a GQL JSON object | ||
* @param {Object} match - an object full of key-value pairs to match against | ||
* @param {Function} func - function to call when a match is found | ||
* @returns {Array} statements - resulting array | ||
*/ | ||
replaceStatements = function replaceStatements(statements, match, func) { | ||
return _.map(statements, function (statement) { | ||
if (_.has(statement, 'group')) { | ||
statement.group = replaceStatements(statement.group, match, func); | ||
return statement; | ||
} | ||
if (matchStatement(statement, match)) { | ||
return func(statement); | ||
} | ||
return statement; | ||
}); | ||
}; | ||
/** | ||
* ## Print statements | ||
@@ -208,3 +239,4 @@ * | ||
mergeStatements: mergeStatements, | ||
replaceStatements: replaceStatements, | ||
printStatements: printStatements | ||
}; |
{ | ||
"name": "ghost-gql", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Filter query language for Ghost", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -449,2 +449,108 @@ var should = require('should'), | ||
describe('replaceStatements', function () { | ||
var replaceStatements = lodashStmt.replaceStatements, | ||
testFunction = function testFunction() { | ||
return {prop: 'magic', op: '=', value: true}; | ||
}, | ||
testFunctionGroup = function testFunctionGroup(stmt) { | ||
stmt.prop = 'magic'; | ||
return { | ||
group: [ | ||
stmt, | ||
{prop: 'foo', op: '=', value: 'bar'} | ||
] | ||
}; | ||
}; | ||
it('should replace inside a simple flat array', function () { | ||
var statements = [ | ||
{prop: 'page', op: '=', value: false}, | ||
{prop: 'status', op: '=', value: 'published', func: 'and'} | ||
]; | ||
replaceStatements(statements, {prop: 'page'}, testFunction).should.eql([ | ||
{prop: 'magic', op: '=', value: true}, | ||
{prop: 'status', op: '=', value: 'published', func: 'and'} | ||
]); | ||
}); | ||
it('should replace with group inside a simple flat array', function () { | ||
var statements = [ | ||
{prop: 'page', op: '=', value: false}, | ||
{prop: 'status', op: '=', value: 'published', func: 'and'} | ||
]; | ||
replaceStatements(statements, {prop: 'page'}, testFunctionGroup).should.eql([ | ||
{ | ||
group: [ | ||
{prop: 'magic', op: '=', value: false}, | ||
{prop: 'foo', op: '=', value: 'bar'} | ||
] | ||
}, | ||
{prop: 'status', op: '=', value: 'published', func: 'and'} | ||
]); | ||
}); | ||
it('should replace with regex', function () { | ||
var statements = [ | ||
{prop: 'tags.slug', op: 'IN', value: false}, | ||
{prop: 'status', op: '=', value: 'published', func: 'and'} | ||
]; | ||
replaceStatements(statements, {prop: /^tags/, op: 'IN'}, testFunction).should.eql([ | ||
{prop: 'magic', op: '=', value: true}, | ||
{prop: 'status', op: '=', value: 'published', func: 'and'} | ||
]); | ||
}); | ||
it('should replace a statement from a group', function () { | ||
var statements = [ | ||
{op: '!=', value: 'joe', prop: 'author'}, | ||
{ | ||
group: [ | ||
{op: '=', value: 'photo', prop: 'tag'}, | ||
{op: '=', value: 'video', prop: 'tag', func: 'or'} | ||
], func: 'and' | ||
} | ||
]; | ||
replaceStatements(statements, {value: 'video'}, testFunction).should.eql([ | ||
{op: '!=', value: 'joe', prop: 'author'}, | ||
{ | ||
group: [ | ||
{op: '=', value: 'photo', prop: 'tag'}, | ||
{op: '=', value: true, prop: 'magic'} | ||
], func: 'and' | ||
} | ||
]); | ||
}); | ||
it('should replace a statement from a group with a group', function () { | ||
var statements = [ | ||
{op: '!=', value: 'joe', prop: 'author'}, | ||
{ | ||
group: [ | ||
{op: '=', value: 'photo', prop: 'tag'}, | ||
{op: '=', value: 'video', prop: 'tag', func: 'or'} | ||
], func: 'and' | ||
} | ||
]; | ||
replaceStatements(statements, {value: 'video'}, testFunctionGroup).should.eql([ | ||
{op: '!=', value: 'joe', prop: 'author'}, | ||
{ | ||
group: [ | ||
{op: '=', value: 'photo', prop: 'tag'}, | ||
{ | ||
group: [ | ||
{prop: 'magic', op: '=', value: 'video', func: 'or'}, | ||
{prop: 'foo', op: '=', value: 'bar'} | ||
] | ||
} | ||
], func: 'and' | ||
} | ||
]); | ||
}); | ||
}); | ||
describe('mergeStatements', function () { | ||
@@ -451,0 +557,0 @@ var mergeStatements = lodashStmt.mergeStatements; |
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
137075
2964