Skip to content

Commit 52e1aa7

Browse files
author
Steve Thompson
committed
-------
Instead of loading dependency classes via import statements at the top of the PublicArray.ts file, now we load them via require() statements inside getter functions, so the resource is only loaded and instantiated if we access the property that the instance will be stored inside.
1 parent 4420eb0 commit 52e1aa7

File tree

6 files changed

+86
-106
lines changed

6 files changed

+86
-106
lines changed

dist/privy/PublicArray.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ var array_append_prepend_1 = require("@writetome51/array-append-prepend");
1717
var set_array_1 = require("@writetome51/set-array");
1818
var di_factory_1 = require("@writetome51/di-factory");
1919
var public_array_content_1 = require("@writetome51/public-array-content");
20-
var public_array_filter_1 = require("@writetome51/public-array-filter");
21-
var public_array_getter_converter_1 = require("@writetome51/public-array-getter-converter");
22-
var public_array_getter_1 = require("@writetome51/public-array-getter");
23-
var public_array_getter_remover_1 = require("@writetome51/public-array-getter-remover");
24-
var public_array_inserter_1 = require("@writetome51/public-array-inserter");
25-
var public_array_remover_1 = require("@writetome51/public-array-remover");
26-
var public_array_sorter_1 = require("@writetome51/public-array-sorter");
27-
var public_array_replacer_1 = require("@writetome51/public-array-replacer");
2820
/***********************
2921
This class is for general array manipulation. It's called PublicArray because it
3022
contains an array in a public property: 'data' .
@@ -56,26 +48,31 @@ var PublicArray = /** @class */ (function (_super) {
5648
) {
5749
if (data === void 0) { data = []; }
5850
var _this = _super.call(this, data) || this;
51+
_this._dependencyClasses = [
52+
{ path: '@writetome51/public-array-filter', name: 'PublicArrayFilter' },
53+
{ path: '@writetome51/public-array-getter-converter', name: 'PublicArrayGetterConverter' },
54+
{ path: '@writetome51/public-array-getter', name: 'PublicArrayGetter' },
55+
{ path: '@writetome51/public-array-getter-remover', name: 'PublicArrayGetterRemover' },
56+
{ path: '@writetome51/public-array-inserter', name: 'PublicArrayInserter' },
57+
{ path: '@writetome51/public-array-remover', name: 'PublicArrayRemover' },
58+
{ path: '@writetome51/public-array-replacer', name: 'PublicArrayReplacer' },
59+
{ path: '@writetome51/public-array-sorter', name: 'PublicArraySorter' }
60+
];
5961
_this._createGetterAndOrSetterForEach(
60-
// each of these represents a public property:
61-
[
62-
{ name: 'filter', class: public_array_filter_1.PublicArrayFilter },
63-
{ name: 'getConverted', class: public_array_getter_converter_1.PublicArrayGetterConverter },
64-
{ name: 'get', class: public_array_getter_1.PublicArrayGetter },
65-
{ name: 'getAndRemove', class: public_array_getter_remover_1.PublicArrayGetterRemover },
66-
{ name: 'insert', class: public_array_inserter_1.PublicArrayInserter },
67-
{ name: 'remove', class: public_array_remover_1.PublicArrayRemover },
68-
{ name: 'replace', class: public_array_replacer_1.PublicArrayReplacer },
69-
{ name: 'sort', class: public_array_sorter_1.PublicArraySorter }
70-
], {
71-
get_getterFunction: function (property) {
72-
// Lazy-Loading is used to instantiate these properties:
73-
if (!(_this["_" + property.name])) { // if property not set...
74-
_this["_" + property.name] = di_factory_1.DIFactory.getInstance(property.class);
75-
}
62+
// each of these is a public property:
63+
['filter', 'getConverted', 'get', 'getAndRemove', 'insert',
64+
'remove', 'replace', 'sort'], {
65+
get_getterFunction: function (property, index) {
7666
return function () {
77-
_this["_" + property.name].data = _this.data;
78-
return _this["_" + property.name];
67+
// Lazy-Loading is used to instantiate these properties:
68+
if (!(_this["_" + property])) { // if property not set...
69+
var dependencyClass = _this._dependencyClasses[index];
70+
// @ts-ignore
71+
var modul = require(dependencyClass.path);
72+
_this["_" + property] = di_factory_1.DIFactory.getInstance(modul[dependencyClass.name]);
73+
}
74+
_this["_" + property].data = _this.data;
75+
return _this["_" + property];
7976
};
8077
}
8178
});

dist/tests.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
var index_1 = require("./index");
4-
var arr = index_1.getPublicArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
3+
var arrays_match_1 = require("@writetome51/arrays-match");
4+
var PublicArray_1 = require("./privy/PublicArray");
5+
var arr = new PublicArray_1.PublicArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
56
// Test 1: getPublicArray() must return instance of PublicArray:
67
if (arr.className && arr.className === 'PublicArray')
78
console.log('test 1 passed');
89
else
910
console.log('test 1 FAILED');
11+
//Test 1A: the instance must contain the array passed into getPublicArray():
12+
if (arrays_match_1.arraysMatch(arr.data, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
13+
console.log('test 1A passed');
14+
else
15+
console.log('test 1A FAILED');
16+
// Test 2: PublicArray.filter must be instance of PublicArrayFilter:
17+
if (arr.filter.className && arr.filter.className === 'PublicArrayFilter')
18+
console.log('test 2 passed');
19+
else
20+
console.log('test 2 FAILED');
1021
/*******************
11-
// Test 1A: the instance must contain the array passed into getPublicArray():
12-
if (arraysMatch(arr.data, [1, 2, 3, 4, 5, 6, 7, 8, 9])) console.log('test 1A passed');
13-
else console.log('test 1A FAILED');
1422
15-
// Test 2: :
16-
if (arr.className && arr.className === 'PublicArray') console.log('test 2 passed');
17-
else console.log('test 2 FAILED');
23+
24+
1825
1926
// Test 2A: the instance must contain the array passed into getPublicArray():
2027
if (arraysMatch(arr.data, [1, 2, 3, 4, 5, 6, 7, 8, 9])) console.log('test 1A passed');
@@ -37,8 +44,10 @@ else
3744
console.log(otherArr);
3845
3946
console.log(arr.data);
40-
*************/
41-
/************
47+
48+
49+
50+
4251
arr.remove.allAfterFirst(6);
4352
arr.remove.allBeforeFirst(3);
4453
arr.prepend([100]);
@@ -65,4 +74,4 @@ else
6574
console.log(arr.sort.shuffle());
6675
6776
console.log(arr.sort);
68-
**************/
77+
*************/

lib/index.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

lib/privy/PublicArray.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ import { append, prepend } from '@writetome51/array-append-prepend';
22
import { setArray } from '@writetome51/set-array';
33
import { DIFactory } from '@writetome51/di-factory';
44
import { PublicArrayContent } from '@writetome51/public-array-content';
5-
import { PublicArrayFilter } from '@writetome51/public-array-filter';
6-
import { PublicArrayGetterConverter } from '@writetome51/public-array-getter-converter';
7-
import { PublicArrayGetter } from '@writetome51/public-array-getter';
8-
import { PublicArrayGetterRemover } from '@writetome51/public-array-getter-remover';
9-
import { PublicArrayInserter } from '@writetome51/public-array-inserter';
10-
import { PublicArrayRemover } from '@writetome51/public-array-remover';
11-
import { PublicArraySorter } from '@writetome51/public-array-sorter';
12-
import { PublicArrayReplacer } from '@writetome51/public-array-replacer';
135

146

157
/***********************
@@ -30,6 +22,7 @@ import { PublicArrayReplacer } from '@writetome51/public-array-replacer';
3022

3123
export class PublicArray extends PublicArrayContent {
3224

25+
private _dependencyClasses: { path: string, name: string }[];
3326
private _filter; // PublicArrayFilter
3427
private _getConverted; // PublicArrayGetterConverter
3528
private _get; // PublicArrayGetter
@@ -61,27 +54,33 @@ export class PublicArray extends PublicArrayContent {
6154

6255
super(data);
6356

57+
this._dependencyClasses = [
58+
{path: '@writetome51/public-array-filter', name: 'PublicArrayFilter'},
59+
{path: '@writetome51/public-array-getter-converter', name: 'PublicArrayGetterConverter'},
60+
{path: '@writetome51/public-array-getter', name: 'PublicArrayGetter'},
61+
{path: '@writetome51/public-array-getter-remover', name: 'PublicArrayGetterRemover'},
62+
{path: '@writetome51/public-array-inserter', name: 'PublicArrayInserter'},
63+
{path: '@writetome51/public-array-remover', name: 'PublicArrayRemover'},
64+
{path: '@writetome51/public-array-replacer', name: 'PublicArrayReplacer'},
65+
{path: '@writetome51/public-array-sorter', name: 'PublicArraySorter'}
66+
];
67+
6468
this._createGetterAndOrSetterForEach(
65-
// each of these represents a public property:
66-
[
67-
{name: 'filter', class: PublicArrayFilter},
68-
{name: 'getConverted', class: PublicArrayGetterConverter},
69-
{name: 'get', class: PublicArrayGetter},
70-
{name: 'getAndRemove', class: PublicArrayGetterRemover},
71-
{name: 'insert', class: PublicArrayInserter},
72-
{name: 'remove', class: PublicArrayRemover},
73-
{name: 'replace', class: PublicArrayReplacer},
74-
{name: 'sort', class: PublicArraySorter}
75-
],
69+
// each of these is a public property:
70+
['filter', 'getConverted', 'get', 'getAndRemove', 'insert',
71+
'remove', 'replace', 'sort'],
7672
{
77-
get_getterFunction: (property) => {
78-
// Lazy-Loading is used to instantiate these properties:
79-
if (!(this[`_${property.name}`])) { // if property not set...
80-
this[`_${property.name}`] = DIFactory.getInstance(property.class);
81-
}
73+
get_getterFunction: (property, index) => {
8274
return () => {
83-
this[`_${property.name}`].data = this.data;
84-
return this[`_${property.name}`];
75+
// Lazy-Loading is used to instantiate these properties:
76+
if (!(this[`_${property}`])) { // if property not set...
77+
let dependencyClass = this._dependencyClasses[index];
78+
// @ts-ignore
79+
let modul = require(dependencyClass.path);
80+
this[`_${property}`] = DIFactory.getInstance(modul[dependencyClass.name]);
81+
}
82+
this[`_${property}`].data = this.data;
83+
return this[`_${property}`];
8584
};
8685
}
8786
}

lib/tests.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
import { getPublicArray } from './index';
21
import { arraysMatch } from '@writetome51/arrays-match';
2+
import { PublicArray } from './privy/PublicArray';
33

44

5-
let arr = getPublicArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
5+
let arr = new PublicArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
66

77
// Test 1: getPublicArray() must return instance of PublicArray:
88
if (arr.className && arr.className === 'PublicArray') console.log('test 1 passed');
99
else console.log('test 1 FAILED');
1010

11+
//Test 1A: the instance must contain the array passed into getPublicArray():
12+
if (arraysMatch(arr.data, [1, 2, 3, 4, 5, 6, 7, 8, 9])) console.log('test 1A passed');
13+
else console.log('test 1A FAILED');
14+
15+
// Test 2: PublicArray.filter must be instance of PublicArrayFilter:
16+
if (arr.filter.className && arr.filter.className === 'PublicArrayFilter') console.log('test 2 passed');
17+
else console.log('test 2 FAILED');
18+
1119
/*******************
12-
// Test 1A: the instance must contain the array passed into getPublicArray():
13-
if (arraysMatch(arr.data, [1, 2, 3, 4, 5, 6, 7, 8, 9])) console.log('test 1A passed');
14-
else console.log('test 1A FAILED');
1520
16-
// Test 2: :
17-
if (arr.className && arr.className === 'PublicArray') console.log('test 2 passed');
18-
else console.log('test 2 FAILED');
21+
22+
1923
2024
// Test 2A: the instance must contain the array passed into getPublicArray():
2125
if (arraysMatch(arr.data, [1, 2, 3, 4, 5, 6, 7, 8, 9])) console.log('test 1A passed');
@@ -40,10 +44,10 @@ else console.log('test 1 FAILED');
4044
console.log(otherArr);
4145
4246
console.log(arr.data);
43-
*************/
4447
4548
46-
/************
49+
50+
4751
arr.remove.allAfterFirst(6);
4852
arr.remove.allBeforeFirst(3);
4953
arr.prepend([100]);
@@ -70,4 +74,4 @@ else console.log('test 1 FAILED');
7074
console.log(arr.sort.shuffle());
7175
7276
console.log(arr.sort);
73-
**************/
77+
*************/

package-lock.json

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

0 commit comments

Comments
 (0)