Skip to content

Commit c01a5cc

Browse files
author
Steve Thompson
committed
added new method .moveByIndex() .
Removed DIFactory from dependencies. made .forEach() return void. Incremented package version to 4.0.0
1 parent f258ffc commit c01a5cc

File tree

8 files changed

+52
-14
lines changed

8 files changed

+52
-14
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ new PublicArray(array = [])
4848
<summary>view properties</summary>
4949

5050
#### data: any[] (read-writable)
51-
###### This is the array to be operated on.
51+
&nbsp;&nbsp;&nbsp;&nbsp;<small>This is the array to be operated on.</small>
5252

5353
#### copy: PublicArray (read-only)
54-
###### a copy of the PublicArray instance, containing an independent copy of this.data that can be manipulated separately.
54+
&nbsp;&nbsp;&nbsp;&nbsp;<small>a copy of the PublicArray instance, containing an independent copy of this.data that can be manipulated separately.</small>
5555

5656
#### length: number (read-writable)
5757
###### length of this.data
@@ -558,7 +558,11 @@ append(values: any[]): this
558558
prepend(values: any[]): this
559559
// attaches values to beginning of this.data.
560560
561-
forEach(iterationFunction): this
561+
moveByIndex(currentIndex, newIndex): this
562+
// moves an item, identified by currentIndex, to newIndex.
563+
// Both currentIndex and newIndex can be negative or positive.
564+
565+
forEach(iterationFunction): void
562566
// Behaves same as Array.forEach()
563567
// iterationFunction = function(currentValue, currentIndex?, entireArray?){...}
564568

dist/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ export declare class PublicArray extends PublicArrayContent {
4343
prepend(values: any[]): this;
4444

4545

46+
moveByIndex(currentIndex: number, newIndex: number): this;
47+
48+
4649
forEach(
4750
iterationFunction: (currentValue: any, currentIndex?: number, entireArray?: any[]) => any
48-
): this;
51+
): void;
4952

5053
}

dist/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ var __extends = (this && this.__extends) || (function () {
1414
})();
1515
Object.defineProperty(exports, "__esModule", { value: true });
1616
var array_append_prepend_1 = require("@writetome51/array-append-prepend");
17+
var array_move_by_index_1 = require("@writetome51/array-move-by-index");
1718
var set_array_1 = require("@writetome51/set-array");
18-
var di_factory_1 = require("@writetome51/di-factory");
1919
var public_array_content_1 = require("@writetome51/public-array-content");
2020
/***********************
2121
This class is for general array manipulation. It's called PublicArray because it
@@ -53,7 +53,7 @@ var PublicArray = /** @class */ (function (_super) {
5353
var dependencyClass = dependencyClasses[index];
5454
// @ts-ignore
5555
var modul = require(dependencyClass.path);
56-
_this["_" + property] = di_factory_1.DIFactory.getInstance(modul[dependencyClass.name]);
56+
_this["_" + property] = new modul[dependencyClass.name];
5757
}
5858
_this["_" + property].data = _this.data;
5959
return _this["_" + property];
@@ -72,8 +72,11 @@ var PublicArray = /** @class */ (function (_super) {
7272
PublicArray.prototype.prepend = function (values) {
7373
return this._returnThis_after(array_append_prepend_1.prepend(values, this.data));
7474
};
75+
PublicArray.prototype.moveByIndex = function (currentIndex, newIndex) {
76+
return this._returnThis_after(array_move_by_index_1.moveByIndex(currentIndex, newIndex, this.data));
77+
};
7578
PublicArray.prototype.forEach = function (iterationFunction) {
76-
return this._returnThis_after(this.data.forEach(iterationFunction));
79+
this.data.forEach(iterationFunction);
7780
};
7881
return PublicArray;
7982
}(public_array_content_1.PublicArrayContent));

dist/tests.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,10 @@ if (arrays_match_1.arraysMatch(indexes, [1, 3]))
240240
console.log('test 22 passed');
241241
else
242242
console.log('test 22 FAILED');
243+
// Test 23: make sure .moveByIndex() works:
244+
// arr.data is [1,2,3,4,5]
245+
arr.moveByIndex(-1, 1);
246+
if (arrays_match_1.arraysMatch(arr.data, [1, 5, 2, 3, 4]))
247+
console.log('test 23 passed');
248+
else
249+
console.log('test 23 FAILED');

lib/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { append, prepend } from '@writetome51/array-append-prepend';
2+
import { moveByIndex } from '@writetome51/array-move-by-index';
23
import { setArray } from '@writetome51/set-array';
3-
import { DIFactory } from '@writetome51/di-factory';
44
import { PublicArrayContent } from '@writetome51/public-array-content';
55

66

@@ -73,7 +73,7 @@ export class PublicArray extends PublicArrayContent {
7373
let dependencyClass = dependencyClasses[index];
7474
// @ts-ignore
7575
let modul = require(dependencyClass.path);
76-
this[`_${property}`] = DIFactory.getInstance(modul[dependencyClass.name]);
76+
this[`_${property}`] = new modul[dependencyClass.name];
7777
}
7878
this[`_${property}`].data = this.data;
7979
return this[`_${property}`];
@@ -101,8 +101,13 @@ export class PublicArray extends PublicArrayContent {
101101
}
102102

103103

104-
forEach(iterationFunction: (currentValue, currentIndex?, entireArray?) => any): this {
105-
return this._returnThis_after(this.data.forEach(iterationFunction));
104+
moveByIndex(currentIndex, newIndex): this {
105+
return this._returnThis_after(moveByIndex(currentIndex, newIndex, this.data));
106+
}
107+
108+
109+
forEach(iterationFunction: (currentValue, currentIndex?, entireArray?) => any): void {
110+
this.data.forEach(iterationFunction);
106111
}
107112

108113

lib/tests.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,9 @@ let indexes = arr.indexesThatPass((item) => (item % 2 === 0));
211211
if (arraysMatch(indexes, [1, 3])) console.log('test 22 passed');
212212
else console.log('test 22 FAILED');
213213

214+
215+
// Test 23: make sure .moveByIndex() works:
216+
// arr.data is [1,2,3,4,5]
217+
arr.moveByIndex(-1, 1);
218+
if (arraysMatch(arr.data, [1, 5, 2, 3, 4])) console.log('test 23 passed');
219+
else console.log('test 23 FAILED');

package-lock.json

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@writetome51/public-array",
3-
"version": "3.0.1",
3+
"version": "4.0.0",
44
"description": "A TypeScript/JavaScript class for general array manipulation",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -47,7 +47,7 @@
4747
"@writetome51/public-array-getter-remover": "~2.0.0",
4848
"@writetome51/public-array-filter": "~2.0.0",
4949
"@writetome51/array-append-prepend": "~1.0.3",
50-
"@writetome51/di-factory": "~1.0.0",
50+
"@writetome51/array-move-by-index": "~1.0.0",
5151
"@writetome51/set-array": "~1.0.1"
5252
}
5353
}

0 commit comments

Comments
 (0)