@enonic/js-utils
Version:
Enonic XP JavaScript Utils
162 lines (136 loc) • 7.14 kB
JavaScript
// value/isBasicObject.ts
var isBasicObject = (value) => typeof value === "object";
// value/isNumber.ts
function isNumber(value) {
return typeof value === "number" && isFinite(value);
}
// value/isStringLiteral.ts
var isStringLiteral = (value) => typeof value === "string";
// value/isStringObject.ts
var isStringObject = (value) => value instanceof String;
// value/isString.ts
var isString = (value) => isStringLiteral(value) || isStringObject(value);
// value/isSymbol.ts
var isSymbol = (value) => typeof value === "symbol";
// value/isPropertyKey.ts
var isPropertyKey = (value) => isString(value) || isNumber(value) || isSymbol(value);
// value/toStr.ts
function toStr(value, replacer, space = 4) {
return JSON.stringify(value, replacer, space);
}
// object/hasOwnProperty.ts
function hasOwnProperty(obj, propKey) {
if (!isBasicObject(obj)) {
throw new Error(`First parameter to hasOwnProperty must be a basic Object! ${toStr(obj)}`);
}
if (!isPropertyKey(propKey)) {
throw new Error(`Second parameter to hasOwnProperty must be a PropertyKey (string|number|symbol)! ${toStr(propKey)}`);
}
return obj.hasOwnProperty(propKey);
}
// array/forceArray.ts
function forceArray(data) {
return Array.isArray(data) ? data : [data];
}
// value/isObject.ts
var isObject = (value) => Object.prototype.toString.call(value).slice(8, -1) === "Object";
// storage/query/dsl/isDslQueryType.ts
var DSL_EXPRESSION_VALUE_TYPE_DATE_TIME = "dateTime";
var DSL_EXPRESSION_VALUE_TYPE_TIME = "time";
function isDslQueryType(value) {
return isString(value) && (value === DSL_EXPRESSION_VALUE_TYPE_DATE_TIME || value === DSL_EXPRESSION_VALUE_TYPE_TIME);
}
// storage/query/dsl/isInDslExpression.ts
function isInDslExpression(value) {
return isObject(value) && hasOwnProperty(value, "field") && hasOwnProperty(value, "values") && isString(value.field) && Array.isArray(value.values) && (hasOwnProperty(value, "type") ? isDslQueryType(value.type) : true) && (hasOwnProperty(value, "boost") ? isNumber(value.boost) : true);
}
// storage/query/dsl/isExistsDslExpression.ts
function isExistsDslExpression(value) {
return isObject(value) && hasOwnProperty(value, "field") && isString(value.field) && (hasOwnProperty(value, "boost") ? isNumber(value.boost) : true);
}
// array/includes.ts
function sameValueZero(x, y) {
return x === y || typeof x === "number" && typeof y === "number" && isNaN(x) && isNaN(y);
}
function includes(array, searchElement, fromIndex = 0) {
if (array == null) {
throw new TypeError('"array" is null or not defined');
}
const o = Object(array);
const len = o.length >>> 0;
if (len === 0) {
return false;
}
const n = fromIndex | 0;
let k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
while (k < len) {
if (sameValueZero(o[k], searchElement)) {
return true;
}
k++;
}
return false;
}
// storage/query/constants.ts
var QUERY_OPERATOR_AND = "AND";
var QUERY_OPERATOR_OR = "OR";
var QUERY_OPERATORS = [
QUERY_OPERATOR_AND,
QUERY_OPERATOR_OR
];
// storage/query/dsl/isDslOperator.ts
function isDslOperator(value) {
return isString(value) && includes(QUERY_OPERATORS, value);
}
// storage/query/dsl/isFulltextDslExpression.ts
function isFulltextDslExpression(value) {
return isObject(value) && hasOwnProperty(value, "fields") && hasOwnProperty(value, "query") && Array.isArray(value.fields) && value.fields.every(isString) && isString(value.query) && (hasOwnProperty(value, "operator") ? isDslOperator(value.operator) : true) && (hasOwnProperty(value, "boost") ? isNumber(value.boost) : true);
}
// storage/query/dsl/isLikeDslExpression.ts
function isLikeDslExpression(value) {
return isObject(value) && hasOwnProperty(value, "field") && hasOwnProperty(value, "value") && isString(value.field) && isString(value.value) && (hasOwnProperty(value, "type") ? isDslQueryType(value.type) : true) && (hasOwnProperty(value, "boost") ? isNumber(value.boost) : true);
}
// storage/query/dsl/isMatchAllDslExpression.ts
function isMatchAllDslExpression(value) {
return isObject(value) && (hasOwnProperty(value, "boost") ? isNumber(value.boost) : true);
}
// storage/query/dsl/isNgramDslExpression.ts
function isNgramDslExpression(value) {
return isFulltextDslExpression(value);
}
// storage/query/dsl/isPathMatchDslExpression.ts
function isPathMatchDslExpression(value) {
return isObject(value) && hasOwnProperty(value, "field") && hasOwnProperty(value, "path") && isString(value.field) && isString(value.path) && (hasOwnProperty(value, "minimumMatch") ? isNumber(value.minimumMatch) : true) && (hasOwnProperty(value, "boost") ? isNumber(value.boost) : true);
}
// storage/query/dsl/isRangeDslExpression.ts
function isRangeDslExpression(value) {
return isObject(value) && hasOwnProperty(value, "field") && isString(value.field) && ["lt", "lte", "gt", "gte"].some((key) => hasOwnProperty(value, key)) && (hasOwnProperty(value, "boost") ? isNumber(value.boost) : true);
}
// storage/query/dsl/isStemmedDslExpression.ts
function isStemmedDslExpression(value) {
return isObject(value) && hasOwnProperty(value, "fields") && hasOwnProperty(value, "language") && hasOwnProperty(value, "query") && Array.isArray(value.fields) && value.fields.every(isString) && isString(value.language) && isString(value.query) && (hasOwnProperty(value, "operator") ? isDslOperator(value.operator) : true) && (hasOwnProperty(value, "boost") ? isNumber(value.boost) : true);
}
// storage/query/dsl/isTermDslExpression.ts
function isTermDslExpression(value) {
return isObject(value) && hasOwnProperty(value, "field") && hasOwnProperty(value, "value") && isString(value.field) && (hasOwnProperty(value, "type") ? isDslQueryType(value.type) : true) && (hasOwnProperty(value, "boost") ? isNumber(value.boost) : true);
}
// storage/query/dsl/isQueryDsl.ts
var COMPOUND_PROPERTIES = [
"must",
"mustNot",
"should",
"filter"
];
function isBooleanDslExpression(value) {
return isObject(value) && COMPOUND_PROPERTIES.every(
(prop) => hasOwnProperty(value, prop) ? forceArray(value[prop]).every((item) => isQueryDsl(item)) : true
// prop is optional
) && (hasOwnProperty(value, "boost") ? isNumber(value.boost) : true);
}
function isQueryDsl(value) {
return isObject(value) && (hasOwnProperty(value, "boolean") && isBooleanDslExpression(value.boolean) || hasOwnProperty(value, "exists") && isExistsDslExpression(value.exists) || hasOwnProperty(value, "fulltext") && isFulltextDslExpression(value.fulltext) || hasOwnProperty(value, "in") && isInDslExpression(value.in) || hasOwnProperty(value, "like") && isLikeDslExpression(value.like) || hasOwnProperty(value, "matchAll") && isMatchAllDslExpression(value.matchAll) || hasOwnProperty(value, "ngram") && isNgramDslExpression(value.ngram) || hasOwnProperty(value, "pathMatch") && isPathMatchDslExpression(value.pathMatch) || hasOwnProperty(value, "range") && isRangeDslExpression(value.range) || hasOwnProperty(value, "stemmed") && isStemmedDslExpression(value.stemmed) || hasOwnProperty(value, "term") && isTermDslExpression(value.term));
}
export {
isQueryDsl as default,
isBooleanDslExpression
};