https://github.com/writetome51/public-array-getter-remover
An array-manipulating typescript/javascript class that removes and returns items in the array
https://github.com/writetome51/public-array-getter-remover
array-manipulations elements items javascript remove remover return typescript
Last synced: 2 months ago
JSON representation
An array-manipulating typescript/javascript class that removes and returns items in the array
- Host: GitHub
- URL: https://github.com/writetome51/public-array-getter-remover
- Owner: writetome51
- License: mit
- Created: 2018-10-20T01:46:20.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-10T02:27:45.000Z (almost 7 years ago)
- Last Synced: 2025-02-03T11:51:21.311Z (about 1 year ago)
- Topics: array-manipulations, elements, items, javascript, remove, remover, return, typescript
- Language: TypeScript
- Size: 57.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PublicArrayGetterRemover
An array-manipulating TypeScript/JavaScript class with methods that both remove
and return items from the array.
## Constructor
```ts
constructor(data? = []) // 'data' is assigned to this.data .
```
You can reset the array by accessing the class `.data` property:
```ts
this.data = [1,2,3,4];
```
## Properties
```ts
data : any[] // the actual array
className: string (read-only)
```
## Methods
view methods
```ts
byIndex(index): any
// removes and returns item identified by index. index can be negative or positive.
byIndexes(indexes): any[]
// removes and returns items identified by indexes. indexes can be negative or positive.
head(numItemsToRemove): any[]
// removes and returns numItemsToRemove from beginning of this.data
tail(numItemsToRemove): any[]
// removes and returns numItemsToRemove from end of this.data
between(numItemsToKeepAtEachEnd): any[]
// removes and returns middle of this.data, between numItemsToIgnoreAtEachEnd
adjacentAt(startingIndex, numItemsToRemove): any[]
// Beginning at startingIndex, removes and returns adjacent numItemsToRemove.
// startingIndex can be negative or positive.
```
NOTICE: For all the functions below, the parameter `value` cannot be an object.
It can be an array, as long as the array doesn't contain objects.
```ts
adjacentToValue(info): any[]
/**************
removes and returns adjacent items including, or near, a particular value.
Only applies to the first instance of value found in array.
The parameter 'info' is an object that looks like this:
{
value: any except object (the value to search for in the array),
offset: integer (tells function where, in relation to value, to begin selecting adjacent
items to remove/return. If offset is zero, the selection will begin with value.)
howMany: integer greater than zero (it's how many adjacent items to remove/return)
}
Example:
let getAndRemove = new PublicArrayGetterRemover( [1,2,3,4,5,6,7,8,9,10] );
let numbers = getAndRemove.adjacentToValue({value:5, offset: -2, howMany:3});
// numbers is now [3,4,5]. getAndRemove.data is now [1,2,6,7,8,9,10]
**************/
allAfterFirst(value): any[]
// removes and returns everything after first instance of value
allBeforeFirst(value): any[]
// removes and returns everything before first instance of value
allAfterLast(value): any[]
// removes and returns everything after last instance of value
allBeforeLast(value): any[]
// removes and returns everything before last instance of value
duplicates(): any[]
// removes and returns every instance of a duplicate, so you may get multiple instances.
```
The next 2 methods return an array of IValueIndexPairs.
A IValueIndexPair looks like this: `{value: any, index: integer}`
It represents an array item.
```ts
byTest(testFunction: (currentValue, currentIndex?, array?) => boolean): IValueIndexPair[]
// removes and returns any item that passes testFunction.
byType(
type: 'object' | 'array' | 'number' | 'string' | 'boolean' | 'function' | 'undefined' | 'null'
): IValueIndexPair[]
// removes and returns any item of the passed type.
// Here, 'null' is considered its own type, separate from 'object'.
// You can also pass 'array' as a type. Passing 'object' will match with objects and arrays.
```
The methods below are not important to know about in order to use this
class. They're inherited from [BaseClass](https://github.com/writetome51/typescript-base-class#baseclass) .
```ts
protected _createGetterAndOrSetterForEach(
propertyNames: string[],
configuration: IGetterSetterConfiguration
) : void
/*********************
Use this method when you have a bunch of properties that need getter and/or
setter functions that all do the same thing. You pass in an array of string
names of those properties, and the method attaches the same getter and/or
setter function to each property.
IGetterSetterConfiguration is this object:
{
get_setterFunction?: (
propertyName: string, index?: number, propertyNames?: string[]
) => Function,
// get_setterFunction takes the property name as first argument and
// returns the setter function. The setter function must take one
// parameter and return void.
get_getterFunction?: (
propertyName: string, index?: number, propertyNames?: string[]
) => Function
// get_getterFunction takes the property name as first argument and
// returns the getter function. The getter function must return something.
}
*********************/
protected _returnThis_after(voidExpression: any) : this
// voidExpression is executed, then function returns this.
// Even if voidExpression returns something, the returned data isn't used.
protected _errorIfPropertyHasNoValue(
property: string, // can contain dot-notation, i.e., 'property.subproperty'
propertyNameInError? = ''
) : void
// If value of this[property] is undefined or null, it triggers fatal error:
// `The property "${propertyNameInError}" has no value.`
```
## Inheritance Chain
PublicArrayGetterRemover<--[PublicArrayContainer](https://github.com/writetome51/public-array-container#publicarraycontainer)<--[BaseClass](https://github.com/writetome51/typescript-base-class#baseclass)
## Installation
```bash
npm i @writetome51/public-array-getter-remover
```
## Loading
```ts
// if using TypeScript:
import {PublicArrayGetterRemover} from '@writetome51/public-array-getter-remover';
// if using ES5 JavaScript:
var PublicArrayGetterRemover =
require('@writetome51/public-array-getter-remover').PublicArrayGetterRemover;
```
## License
[MIT](https://choosealicense.com/licenses/mit/)