Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"node": ">=20.0.0"
},
"dependencies": {
"lodash": "^4.17.21"
},
"devDependencies": {
"lodash": "^4.17.21",
"@babel/cli": "^7.25.9",
"@babel/core": "^7.25.9",
"@babel/eslint-parser": "^7.25.9",
Expand Down
102 changes: 102 additions & 0 deletions src/_/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
'use strict';

/**
* Checks if value is null or undefined.
*
* @param {*} value The value to check.
* @returns {boolean} Returns true if value is null or undefined, else false.
*/
exports.isNil = function isNil(value) {
return value === null || value === undefined;
};

/**
* Checks if value is a string.
*
* @param {*} value The value to check.
* @returns {boolean} Returns true if value is a string, else false.
*/
exports.isString = function isString(value) {
return typeof value === 'string';
};

/**
* Checks if value is an object type (not null, not an array).
*
* @param {*} value The value to check.
* @returns {boolean} Returns true if value is an object, else false.
*/
exports.isObject = function isObject(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value);
};

/**
* Checks if object has a direct property (own property).
*
* @param {Object} object The object to check.
* @param {string} key The property name to check.
* @returns {boolean} Returns true if object has the property, else false.
*/
exports.has = function has(object, key) {
return object != null && Object.prototype.hasOwnProperty.call(object, key);
};

/**
* Checks if object has a property (including inherited properties).
*
* @param {Object} object The object to check.
* @param {string} key The property name to check.
* @returns {boolean} Returns true if object has the property, else false.
*/
exports.hasIn = function hasIn(object, key) {
return object != null && key in object;
};

/**
* Creates a new object excluding specified keys.
*
* @param {Object} object The source object.
* @param {Array<string>} keys The keys to exclude.
* @returns {Object} Returns the new object with specified keys omitted.
*/
exports.omit = function omit(object, keys) {
if (object == null) return {};
const result = {};
const keysToOmit = new Set(keys);
for (const key in object) {
if (
Object.prototype.hasOwnProperty.call(object, key) &&
!keysToOmit.has(key)
) {
result[key] = object[key];
}
}
return result;
};

/**
* Checks if value is empty (null, undefined, empty string, empty array, or empty object).
*
* @param {*} value The value to check.
* @returns {boolean} Returns true if value is empty, else false.
*/
exports.isEmpty = function isEmpty(value) {
if (value == null) return true;
if (typeof value === 'string' || Array.isArray(value)) {
return value.length === 0;
}
if (typeof value === 'object') {
return Object.keys(value).length === 0;
}
return false;
};

/**
* Gets the first element of an array.
*
* @param {Array} array The array to query.
* @returns {*} Returns the first element of array.
*/
exports.head = function head(array) {
return array != null ? array[0] : undefined;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isNil } = require('lodash');
const _ = require('../../_');

const BucketAggregationBase = require('./bucket-aggregation-base');

Expand All @@ -26,7 +26,7 @@ class AutoDateHistogramAggregation extends BucketAggregationBase {
// eslint-disable-next-line require-jsdoc
constructor(name, field, buckets) {
super(name, 'auto_date_histogram', field);
if (!isNil(buckets)) this._aggsDef.buckets = buckets;
if (!_.isNil(buckets)) this._aggsDef.buckets = buckets;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isNil } = require('lodash');
const _ = require('../../_');

const {
Aggregation,
Expand All @@ -27,7 +27,7 @@ class BucketAggregationBase extends Aggregation {
constructor(name, aggType, field) {
super(name, aggType);

if (!isNil(field)) this._aggsDef.field = field;
if (!_.isNil(field)) this._aggsDef.field = field;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isNil } = require('lodash');
const _ = require('../../../_');

const ValuesSourceBase = require('./values-source-base');

Expand Down Expand Up @@ -32,7 +32,7 @@ class DateHistogramValuesSource extends ValuesSourceBase {
constructor(name, field, interval) {
super('date_histogram', REF_URL, name, field);

if (!isNil(interval)) this._opts.interval = interval;
if (!_.isNil(interval)) this._opts.interval = interval;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isNil } = require('lodash');
const _ = require('../../../_');

const ValuesSourceBase = require('./values-source-base');

Expand Down Expand Up @@ -32,7 +32,7 @@ class HistogramValuesSource extends ValuesSourceBase {
constructor(name, field, interval) {
super('histogram', REF_URL, name, field);

if (!isNil(interval)) this._opts.interval = interval;
if (!_.isNil(interval)) this._opts.interval = interval;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isEmpty, isNil } = require('lodash');
const _ = require('../../../_');

const {
util: { invalidParam, recursiveToJSON }
Expand All @@ -24,7 +24,7 @@ const invalidOrderParam = invalidParam('', 'order', "'asc' or 'desc'");
class ValuesSourceBase {
// eslint-disable-next-line require-jsdoc
constructor(valueSrcType, refUrl, name, field) {
if (isEmpty(valueSrcType))
if (_.isEmpty(valueSrcType))
throw new Error('ValuesSourceBase `valueSrcType` cannot be empty');

this._name = name;
Expand All @@ -34,7 +34,7 @@ class ValuesSourceBase {
this._body = {};
this._opts = this._body[valueSrcType] = {};

if (!isNil(field)) this._opts.field = field;
if (!_.isNil(field)) this._opts.field = field;
}

/**
Expand Down Expand Up @@ -81,7 +81,7 @@ class ValuesSourceBase {
* @returns {ValuesSourceBase} returns `this` so that calls can be chained.
*/
order(order) {
if (isNil(order)) invalidOrderParam(order, this._refUrl);
if (_.isNil(order)) invalidOrderParam(order, this._refUrl);

const orderLower = order.toLowerCase();
if (orderLower !== 'asc' && orderLower !== 'desc') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isNil } = require('lodash');
const _ = require('../../_');

const {
util: { invalidParam },
Expand Down Expand Up @@ -106,7 +106,7 @@ class DiversifiedSamplerAggregation extends BucketAggregationBase {
* @throws {Error} If Execution Hint is outside the accepted set.
*/
executionHint(hint) {
if (isNil(hint)) invalidExecutionHintParam(hint);
if (_.isNil(hint)) invalidExecutionHintParam(hint);

const hintLower = hint.toLowerCase();
if (!EXECUTION_HINT_SET.has(hintLower)) {
Expand Down
4 changes: 2 additions & 2 deletions src/aggregations/bucket-aggregations/filter-aggregation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isNil } = require('lodash');
const _ = require('../../_');

const {
Query,
Expand Down Expand Up @@ -39,7 +39,7 @@ class FilterAggregation extends BucketAggregationBase {
constructor(name, filterQuery) {
super(name, 'filter');

if (!isNil(filterQuery)) this.filter(filterQuery);
if (!_.isNil(filterQuery)) this.filter(filterQuery);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/aggregations/bucket-aggregations/filters-aggregation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isEmpty } = require('lodash');
const _ = require('../../_');

const {
Query,
Expand Down Expand Up @@ -198,7 +198,7 @@ class FiltersAggregation extends BucketAggregationBase {
otherBucket(enable, otherBucketKey) {
this._aggsDef.other_bucket = enable;

!isEmpty(otherBucketKey) && this.otherBucketKey(otherBucketKey);
!_.isEmpty(otherBucketKey) && this.otherBucketKey(otherBucketKey);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isNil } = require('lodash');
const _ = require('../../_');

const {
GeoPoint,
Expand Down Expand Up @@ -121,7 +121,7 @@ class GeoDistanceAggregation extends RangeAggregationBase {
* @throws {Error} If `type` is neither `plane` nor `arc`.
*/
distanceType(type) {
if (isNil(type)) invalidDistanceTypeParam(type);
if (_.isNil(type)) invalidDistanceTypeParam(type);

const typeLower = type.toLowerCase();
if (typeLower !== 'plane' && typeLower !== 'arc')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isNil } = require('lodash');
const _ = require('../../_');

const BucketAggregationBase = require('./bucket-aggregation-base');

Expand Down Expand Up @@ -55,7 +55,7 @@ class GeoHashGridAggregation extends BucketAggregationBase {
* @throws {Error} If precision is not between 1 and 12.
*/
precision(precision) {
if (isNil(precision) || precision < 1 || precision > 12) {
if (_.isNil(precision) || precision < 1 || precision > 12) {
throw new Error('`precision` can only be value from 1 to 12.');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isNil } = require('lodash');
const _ = require('../../_');

const BucketAggregationBase = require('./bucket-aggregation-base');

Expand Down Expand Up @@ -57,7 +57,7 @@ class GeoHexGridAggregation extends BucketAggregationBase {
* @throws {Error} If precision is not between 0 and 15.
*/
precision(precision) {
if (isNil(precision) || precision < 0 || precision > 15) {
if (_.isNil(precision) || precision < 0 || precision > 15) {
throw new Error('`precision` can only be value from 0 to 15.');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isNil } = require('lodash');
const _ = require('../../_');

const {
GeoPoint,
Expand Down Expand Up @@ -64,7 +64,7 @@ class GeoTileGridAggregation extends BucketAggregationBase {
* @throws {Error} If precision is not between 0 and 29.
*/
precision(precision) {
if (isNil(precision) || precision < 0 || precision > 29) {
if (_.isNil(precision) || precision < 0 || precision > 29) {
throw new Error('`precision` can only be value from 0 to 29.');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { has, isNil } = require('lodash');
const _ = require('../../_');

const {
util: { invalidParam }
Expand Down Expand Up @@ -31,7 +31,7 @@ class HistogramAggregationBase extends BucketAggregationBase {
constructor(name, aggType, field, interval) {
super(name, aggType, field);

if (!isNil(interval)) this._aggsDef.interval = interval;
if (!_.isNil(interval)) this._aggsDef.interval = interval;
}

/**
Expand Down Expand Up @@ -106,14 +106,14 @@ class HistogramAggregationBase extends BucketAggregationBase {
* @returns {HistogramAggregationBase} returns `this` so that calls can be chained
*/
order(key, direction = 'desc') {
if (isNil(direction)) invalidDirectionParam(direction);
if (_.isNil(direction)) invalidDirectionParam(direction);

const directionLower = direction.toLowerCase();
if (directionLower !== 'asc' && directionLower !== 'desc') {
invalidDirectionParam(direction);
}

if (has(this._aggsDef, 'order')) {
if (_.has(this._aggsDef, 'order')) {
if (!Array.isArray(this._aggsDef.order)) {
this._aggsDef.order = [this._aggsDef.order];
}
Expand Down
4 changes: 2 additions & 2 deletions src/aggregations/bucket-aggregations/nested-aggregation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { isNil } = require('lodash');
const _ = require('../../_');

const BucketAggregationBase = require('./bucket-aggregation-base');

Expand Down Expand Up @@ -32,7 +32,7 @@ class NestedAggregation extends BucketAggregationBase {
constructor(name, path) {
super(name, 'nested');

if (!isNil(path)) this._aggsDef.path = path;
if (!_.isNil(path)) this._aggsDef.path = path;
}

/**
Expand Down
Loading
Loading