@enonic/js-utils
Version:
Enonic XP JavaScript Utils
121 lines (110 loc) • 2.85 kB
JavaScript
// storage/query/aggregation/stats.ts
function statsParams(field) {
const stats2 = {
field
};
return stats2;
}
function stats(field) {
const statsAggregation = {
stats: statsParams(
field
)
};
return statsAggregation;
}
// value/isFunction.ts
function isFunction(value) {
return Object.prototype.toString.call(value).slice(8, -1) === "Function";
}
// 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/isInt.ts
function isInt(value) {
return typeof value === "number" && isFinite(value) && // TODO Is isFinite() available in Enonic XP?
Math.floor(value) === value;
}
// value/isInteger.ts
var isInteger = "isInteger" in Number && isFunction(Number.isInteger) ? Number.isInteger : isInt;
// value/isObject.ts
var isObject = (value) => Object.prototype.toString.call(value).slice(8, -1) === "Object";
// value/isSet.ts
function isSet(value) {
if (typeof value === "boolean") {
return true;
}
return value !== null && typeof value !== "undefined";
}
// storage/query/aggregation/terms.ts
function termsParams(field, order, size, minDocCount) {
const terms2 = {
field
};
if (order) {
terms2.order = order;
}
if (size) {
terms2.size = size;
}
if (minDocCount) {
terms2.minDocCount = minDocCount;
}
return terms2;
}
function terms(field, ...optionalArgs) {
let order;
let size;
let minDocCount;
let aggregations;
for (let i = 0; i < optionalArgs.length; i++) {
const optinalArg = optionalArgs[i];
if (isString(optinalArg)) {
if (order) {
throw new Error(`terms: You can only provide one optional order parameter!`);
}
order = optinalArg;
} else if (isNumber(optinalArg)) {
if (isSet(minDocCount)) {
throw new Error(`terms: You can only provide one or two optional number parameters!`);
}
if (isSet(size)) {
minDocCount = optinalArg;
} else {
size = optinalArg;
}
} else if (isObject(optinalArg)) {
if (aggregations) {
throw new Error(`terms: You can only provide one optional aggregations parameter!`);
}
aggregations = optinalArg;
} else {
throw new Error(`terms: Unknown optional parameter type!`);
}
}
const termsAggregation = {
terms: termsParams(
field,
order,
size,
minDocCount
)
};
if (aggregations) {
termsAggregation.aggregations = aggregations;
}
return termsAggregation;
}
export {
stats,
statsParams,
terms,
termsParams
};