sparqlalgebrajs
Advanced tools
Comparing version 4.0.5 to 4.1.0
import { Wildcard } from 'sparqljs'; | ||
import * as A from "./algebra"; | ||
import { TypedOperation } from './algebra'; | ||
import { TypedOperation, TypedExpression } from './algebra'; | ||
import Factory from "./factory"; | ||
@@ -43,3 +43,3 @@ import { BaseQuad, Variable } from '@rdfjs/types'; | ||
* @param {Operation} op - The Operation to recurse on. | ||
* @param { [type: string]: (op: Operation, factory: Factory) => RecurseResult } callbacks - A map of required callback Operations. | ||
* @param callbacks - A map of required callback Operations. | ||
* @param {Factory} factory - Factory used to create new Operations. Will use default factory if none is provided. | ||
@@ -50,2 +50,4 @@ * @returns {Operation} - The copied result. | ||
[T in A.types]?: (op: TypedOperation<T>, factory: Factory) => RecurseResult; | ||
} & { | ||
[T in A.expressionTypes]?: (expr: TypedExpression<T>, factory: Factory) => ExpressionRecurseResult; | ||
}, factory?: Factory): A.Operation; | ||
@@ -57,7 +59,25 @@ /** | ||
* @param {Expression} expr - The Operation to recurse on. | ||
* @param { [type: string]: (op: Operation, factory: Factory) => RecurseResult } callbacks - A map of required callback Operations. | ||
* @param callbacks - A map of required callback Operations. | ||
* @param {Factory} factory - Factory used to create new Operations. Will use default factory if none is provided. | ||
* @returns {Operation} - The copied result. | ||
*/ | ||
private static mapExpression; | ||
static mapExpression(expr: A.Expression, callbacks: { | ||
[T in A.types]?: (op: TypedOperation<T>, factory: Factory) => RecurseResult; | ||
} & { | ||
[T in A.expressionTypes]?: (expr: TypedExpression<T>, factory: Factory) => ExpressionRecurseResult; | ||
}, factory?: Factory): A.Expression; | ||
/** | ||
* Creates a deep clone of the operation. | ||
* This is syntactic sugar for calling {@link mapOperation} without callbacks. | ||
* @param {Operation} op - The operation to copy. | ||
* @returns {Operation} - The deep copy. | ||
*/ | ||
static cloneOperation(op: A.Operation): A.Operation; | ||
/** | ||
* Creates a deep clone of the expression. | ||
* This is syntactic sugar for calling {@link mapExpression} without callbacks. | ||
* @param {Expression} expr - The operation to copy. | ||
* @returns {Expression} - The deep copy. | ||
*/ | ||
static cloneExpression(expr: A.Expression): A.Expression; | ||
static createUniqueVariable(label: string, variables: { | ||
@@ -79,1 +99,10 @@ [vLabel: string]: boolean; | ||
} | ||
/** | ||
* @interface ExpressionRecurseResult | ||
* @property {Expression} result - The resulting A.Expression. | ||
* @property {boolean} recurse - Whether to continue with recursion. | ||
*/ | ||
export interface ExpressionRecurseResult { | ||
result: A.Expression; | ||
recurse: boolean; | ||
} |
@@ -286,3 +286,3 @@ "use strict"; | ||
* @param {Operation} op - The Operation to recurse on. | ||
* @param { [type: string]: (op: Operation, factory: Factory) => RecurseResult } callbacks - A map of required callback Operations. | ||
* @param callbacks - A map of required callback Operations. | ||
* @param {Factory} factory - Factory used to create new Operations. Will use default factory if none is provided. | ||
@@ -395,3 +395,3 @@ * @returns {Operation} - The copied result. | ||
* @param {Expression} expr - The Operation to recurse on. | ||
* @param { [type: string]: (op: Operation, factory: Factory) => RecurseResult } callbacks - A map of required callback Operations. | ||
* @param callbacks - A map of required callback Operations. | ||
* @param {Factory} factory - Factory used to create new Operations. Will use default factory if none is provided. | ||
@@ -401,15 +401,23 @@ * @returns {Operation} - The copied result. | ||
static mapExpression(expr, callbacks, factory) { | ||
let recurse = (op) => Util.mapOperation(op, callbacks, factory); | ||
let result = expr; | ||
let doRecursion = true; | ||
factory = factory || new factory_1.default(); | ||
const callback = callbacks[expr.expressionType]; | ||
if (callback) | ||
({ result, recurse: doRecursion } = callback(expr, factory)); | ||
if (!doRecursion) | ||
return result; | ||
let mapOp = (op) => Util.mapOperation(op, callbacks, factory); | ||
switch (expr.expressionType) { | ||
case algebra_1.expressionTypes.AGGREGATE: | ||
if (expr.variable) { | ||
return factory.createBoundAggregate(expr.variable, expr.aggregator, recurse(expr.expression), expr.distinct, expr.separator); | ||
return factory.createBoundAggregate(expr.variable, expr.aggregator, mapOp(expr.expression), expr.distinct, expr.separator); | ||
} | ||
return factory.createAggregateExpression(expr.aggregator, recurse(expr.expression), expr.distinct, expr.separator); | ||
return factory.createAggregateExpression(expr.aggregator, mapOp(expr.expression), expr.distinct, expr.separator); | ||
case algebra_1.expressionTypes.EXISTENCE: | ||
return factory.createExistenceExpression(expr.not, recurse(expr.input)); | ||
return factory.createExistenceExpression(expr.not, mapOp(expr.input)); | ||
case algebra_1.expressionTypes.NAMED: | ||
return factory.createNamedExpression(expr.name, expr.args.map(recurse)); | ||
return factory.createNamedExpression(expr.name, expr.args.map(mapOp)); | ||
case algebra_1.expressionTypes.OPERATOR: | ||
return factory.createOperatorExpression(expr.operator, expr.args.map(recurse)); | ||
return factory.createOperatorExpression(expr.operator, expr.args.map(mapOp)); | ||
case algebra_1.expressionTypes.TERM: | ||
@@ -422,2 +430,20 @@ return factory.createTermExpression(expr.term); | ||
} | ||
/** | ||
* Creates a deep clone of the operation. | ||
* This is syntactic sugar for calling {@link mapOperation} without callbacks. | ||
* @param {Operation} op - The operation to copy. | ||
* @returns {Operation} - The deep copy. | ||
*/ | ||
static cloneOperation(op) { | ||
return Util.mapOperation(op, {}); | ||
} | ||
/** | ||
* Creates a deep clone of the expression. | ||
* This is syntactic sugar for calling {@link mapExpression} without callbacks. | ||
* @param {Expression} expr - The operation to copy. | ||
* @returns {Expression} - The deep copy. | ||
*/ | ||
static cloneExpression(expr) { | ||
return Util.mapExpression(expr, {}); | ||
} | ||
static createUniqueVariable(label, variables, dataFactory) { | ||
@@ -424,0 +450,0 @@ let counter = 0; |
{ | ||
"name": "sparqlalgebrajs", | ||
"version": "4.0.5", | ||
"version": "4.1.0", | ||
"description": "Convert SPARQL to SPARQL algebra", | ||
@@ -31,3 +31,3 @@ "author": "Joachim Van Herwegen", | ||
"@types/minimist": "^1.2.2", | ||
"@types/node": "^16.11.3", | ||
"@types/node": "^18.0.0", | ||
"chai": "^4.3.6", | ||
@@ -37,3 +37,3 @@ "jest": "^28.1.3", | ||
"ts-jest": "^28.0.8", | ||
"typescript": "^4.7.2" | ||
"typescript": "^5.0.0" | ||
}, | ||
@@ -40,0 +40,0 @@ "pre-commit": [ |
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
318212
2773