UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

197 lines (196 loc) 7.52 kB
"use strict"; /* * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.btrim = btrim; exports.left = left; exports.lower = lower; exports.ltrim = ltrim; exports.normalize = normalize; exports.replace = replace; exports.right = right; exports.rtrim = rtrim; exports.split = split; exports.substring = substring; exports.toLower = toLower; exports.toString = toString; exports.toStringOrNull = toStringOrNull; exports.toUpper = toUpper; exports.trim = trim; exports.upper = upper; const filter_truthy_1 = require("../../utils/filter-truthy"); const normalize_variable_1 = require("../../utils/normalize-variable"); const CypherFunctions_1 = require("./CypherFunctions"); /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/#functions-btrim | Cypher Documentation} * @group Functions * @category String * @since Neo4j 5.20 */ function btrim(original, trimCharacter) { const normalizedOriginal = (0, normalize_variable_1.normalizeExpr)(original); const normalizedTrimCharacter = trimCharacter ? (0, normalize_variable_1.normalizeExpr)(trimCharacter) : undefined; return new CypherFunctions_1.CypherFunction("btrim", (0, filter_truthy_1.filterTruthy)([normalizedOriginal, normalizedTrimCharacter])); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/#functions-left | Cypher Documentation} * @group Functions * @category String */ function left(original, length) { return new CypherFunctions_1.CypherFunction("left", [original, length]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/#functions-lower | Cypher Documentation} * @group Functions * @category String * @since Neo4j 5.21 */ function lower(original) { return new CypherFunctions_1.CypherFunction("lower", [original]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/ | Cypher Documentation} * @group Functions * @category String */ function ltrim(original, trimCharacter) { const normalizedOriginal = (0, normalize_variable_1.normalizeExpr)(original); const normalizedTrimCharacter = trimCharacter ? (0, normalize_variable_1.normalizeExpr)(trimCharacter) : undefined; return new CypherFunctions_1.CypherFunction("ltrim", (0, filter_truthy_1.filterTruthy)([normalizedOriginal, normalizedTrimCharacter])); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/ | Cypher Documentation} * @group Functions * @category String * @param normalForm - A string with the normal form to use or a Cypher expression * @since Neo4j 5.17 * @example `Cypher.normalize(param, "NFC")` */ function normalize(input, normalForm) { const normalFormExpr = normalForm ? (0, normalize_variable_1.normalizeExpr)(normalForm) : undefined; return new CypherFunctions_1.CypherFunction("normalize", (0, filter_truthy_1.filterTruthy)([input, normalFormExpr])); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/ | Cypher Documentation} * @group Functions * @category String */ function replace(original, search, replace) { return new CypherFunctions_1.CypherFunction("replace", [original, search, replace]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/ | Cypher Documentation} * @group Functions * @category List * @category String */ function right(original, length) { return new CypherFunctions_1.CypherFunction("right", [original, length]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/ | Cypher Documentation} * @group Functions * @category String */ function rtrim(original, trimCharacter) { const normalizedOriginal = (0, normalize_variable_1.normalizeExpr)(original); const normalizedTrimCharacter = trimCharacter ? (0, normalize_variable_1.normalizeExpr)(trimCharacter) : undefined; return new CypherFunctions_1.CypherFunction("rtrim", (0, filter_truthy_1.filterTruthy)([normalizedOriginal, normalizedTrimCharacter])); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/ | Cypher Documentation} * @group Functions * @category String */ function split(original, delimiter) { return new CypherFunctions_1.CypherFunction("split", [original, delimiter]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/ | Cypher Documentation} * @group Functions * @category String */ function substring(original, start, length) { return new CypherFunctions_1.CypherFunction("substring", (0, filter_truthy_1.filterTruthy)([original, start, length])); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/ | Cypher Documentation} * @group Functions * @category String */ function toLower(original) { return new CypherFunctions_1.CypherFunction("toLower", [original]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/ | Cypher Documentation} * @group Functions * @category String */ function toString(expression) { return new CypherFunctions_1.CypherFunction("toString", [expression]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/ | Cypher Documentation} * @group Functions * @category String */ function toStringOrNull(expression) { return new CypherFunctions_1.CypherFunction("toStringOrNull", [expression]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/ | Cypher Documentation} * @group Functions * @category String */ function toUpper(original) { return new CypherFunctions_1.CypherFunction("toUpper", [original]); } /** Implements a trim function with a trim expression `trim(BOTH 'x' FROM 'xxxhelloxxx')` */ class TrimFunction extends CypherFunctions_1.CypherFunction { constructor(typeOrInput, trimChar, input) { super("trim"); this.typeOrInput = typeOrInput; this.trimChar = trimChar; this.input = input; } serializeParams(env) { const trimStr = this.trimChar.getCypher(env); const inputStr = this.input.getCypher(env); return `${this.typeOrInput} ${trimStr} FROM ${inputStr}`; } } function trim(typeOrInput, trimChar, input) { if (typeof typeOrInput === "string") { if (!trimChar || !input) { throw new Error("Invalid parameters in trim. trimChar and input must be valid Expr"); } return new TrimFunction(typeOrInput, trimChar, input); } return new CypherFunctions_1.CypherFunction("trim", [typeOrInput]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/string/#functions-upper | Cypher Documentation} * @group Functions * @category String * @since Neo4j 5.21 */ function upper(original) { return new CypherFunctions_1.CypherFunction("upper", [original]); }