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
54 changes: 54 additions & 0 deletions src/methods/series/filtering/between.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Between method for Series
* Returns a new Series with values between lower and upper bounds (inclusive)
*/

/**
* Creates a between method for Series
* @returns {Function} - Function to be attached to Series prototype
*/
export function between() {
/**
* Returns a new Series with values between lower and upper bounds (inclusive)
* @param {number} lower - Lower bound
* @param {number} upper - Upper bound
* @param {Object} [options] - Options object
* @param {boolean} [options.inclusive=true] - Whether bounds are inclusive
* @returns {Series} - New Series with filtered values
*/
return function(lower, upper, options = {}) {
const { inclusive = true } = options;

if (lower === undefined || upper === undefined) {
throw new Error('Both lower and upper bounds must be provided');
}

if (lower > upper) {
throw new Error('Lower bound must be less than or equal to upper bound');
}

if (inclusive) {
return this.filter((x) => {
const numX = Number(x);
return !isNaN(numX) && numX >= lower && numX <= upper;
});
} else {
return this.filter((x) => {
const numX = Number(x);
return !isNaN(numX) && numX > lower && numX < upper;
});
}
};
}

/**
* Registers the between method on Series prototype
* @param {Class} Series - Series class to extend
*/
export function register(Series) {
if (!Series.prototype.between) {
Series.prototype.between = between();
}
}

export default between;
51 changes: 51 additions & 0 deletions src/methods/series/filtering/contains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Contains method for Series
* Returns a new Series with string values that contain the specified substring
*/

/**
* Creates a contains method for Series
* @returns {Function} - Function to be attached to Series prototype
*/
export function contains() {
/**
* Returns a new Series with string values that contain the specified substring
* @param {string} substring - Substring to search for
* @param {Object} [options] - Options object
* @param {boolean} [options.caseSensitive=true] - Whether the search is case sensitive
* @returns {Series} - New Series with filtered values
*/
return function(substring, options = {}) {
const { caseSensitive = true } = options;

if (substring === undefined || substring === null) {
throw new Error('Substring must be provided');
}

return this.filter((value) => {
if (value === null || value === undefined) {
return false;
}

const strValue = String(value);

if (caseSensitive) {
return strValue.includes(substring);
} else {
return strValue.toLowerCase().includes(substring.toLowerCase());
}
});
};
}

/**
* Registers the contains method on Series prototype
* @param {Class} Series - Series class to extend
*/
export function register(Series) {
if (!Series.prototype.contains) {
Series.prototype.contains = contains();
}
}

export default contains;
52 changes: 52 additions & 0 deletions src/methods/series/filtering/endsWith.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* EndsWith method for Series
* Returns a new Series with string values that end with the specified suffix
*/

/**
* Creates an endsWith method for Series
* @returns {Function} - Function to be attached to Series prototype
*/
export function endsWith() {
/**
* Returns a new Series with string values that end with the specified suffix
* @param {string} suffix - Suffix to search for
* @param {Object} [options] - Options object
* @param {boolean} [options.caseSensitive=true] - Whether the search is case sensitive
* @returns {Series} - New Series with filtered values
*/
return function(suffix, options = {}) {
const { caseSensitive = true } = options;

if (suffix === undefined || suffix === null) {
throw new Error('Suffix must be provided');
}

return this.filter((value) => {
if (value === null || value === undefined) {
return false;
}

const strValue = String(value);

if (caseSensitive) {
// В режиме чувствительности к регистру проверяем точное совпадение
return strValue.endsWith(suffix);
} else {
return strValue.toLowerCase().endsWith(suffix.toLowerCase());
}
});
};
}

/**
* Registers the endsWith method on Series prototype
* @param {Class} Series - Series class to extend
*/
export function register(Series) {
if (!Series.prototype.endsWith) {
Series.prototype.endsWith = endsWith();
}
}

export default endsWith;
16 changes: 9 additions & 7 deletions src/methods/series/filtering/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
* @param {Function} predicate - Function that takes a value and returns true/false
* @returns {Series} - New Series with filtered values
*/
export const filter = (series, predicate) => {
export function filter(series, predicate) {
const values = series.toArray();
const filteredValues = values.filter(predicate);
return new series.constructor(filteredValues);
};
}

/**
* Registers the filter method on Series prototype
* @param {Class} Series - Series class to extend
*/
export const register = (Series) => {
Series.prototype.filter = function(predicate) {
return filter(this, predicate);
};
};
export function register(Series) {
if (!Series.prototype.filter) {
Series.prototype.filter = function(predicate) {
return filter(this, predicate);
};
}
}

export default { filter, register };
30 changes: 30 additions & 0 deletions src/methods/series/filtering/isNull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* IsNull method for Series
* Returns a new Series with only null or undefined values
*/

/**
* Creates an isNull method for Series
* @returns {Function} - Function to be attached to Series prototype
*/
export function isNull() {
/**
* Returns a new Series with only null or undefined values
* @returns {Series} - New Series with filtered values
*/
return function() {
return this.filter((x) => x === null || x === undefined);
};
}

/**
* Registers the isNull method on Series prototype
* @param {Class} Series - Series class to extend
*/
export function register(Series) {
if (!Series.prototype.isNull) {
Series.prototype.isNull = isNull();
}
}

export default isNull;
51 changes: 51 additions & 0 deletions src/methods/series/filtering/matches.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Matches method for Series
* Returns a new Series with string values that match the specified regular expression
*/

/**
* Creates a matches method for Series
* @returns {Function} - Function to be attached to Series prototype
*/
export function matches() {
/**
* Returns a new Series with string values that match the specified regular expression
* @param {RegExp|string} pattern - Regular expression pattern to match
* @param {Object} [options] - Options object
* @param {boolean} [options.flags] - Flags for the RegExp if pattern is a string
* @returns {Series} - New Series with filtered values
*/
return function(pattern, options = {}) {
const { flags = '' } = options;

if (pattern === undefined || pattern === null) {
throw new Error('Regular expression pattern must be provided');
}

// Convert string pattern to RegExp if needed
const regex = pattern instanceof RegExp
? pattern
: new RegExp(pattern, flags);

return this.filter((value) => {
if (value === null || value === undefined) {
return false;
}

const strValue = String(value);
return regex.test(strValue);
});
};
}

/**
* Registers the matches method on Series prototype
* @param {Class} Series - Series class to extend
*/
export function register(Series) {
if (!Series.prototype.matches) {
Series.prototype.matches = matches();
}
}

export default matches;
Loading
Loading