qtools-functional-library 1.1.10 → 1.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -11
- package/lib/qtools-map-properties/package.json +11 -0
- package/lib/qtools-map-properties/qtools-map-properties.js +99 -0
- package/lib/qtools-map-properties/test.js +118 -0
- package/lib/qtools-merge/test.js +0 -4
- package/lib/qtools-number-keys-to-array/test.js +107 -56
- package/lib/qtools-pop/qtools-pop.js +36 -0
- package/lib/qtools-pop/test.js +9 -0
- package/lib/qtools-select-properties/package.json +11 -0
- package/lib/qtools-select-properties/qtools-select-properties.js +132 -0
- package/lib/qtools-select-properties/test.js +156 -0
- package/package.json +2 -2
- package/test.js +22 -2
package/README.md
CHANGED
|
@@ -4,20 +4,26 @@ This is a collection of tools that allow chained operations on various Javascrip
|
|
|
4
4
|
|
|
5
5
|
Most of the functions included in this library (except, eg, qt.log()) are implemented on the prototype of the data type in question. For example, almost any element can take the .qtDump() function.
|
|
6
6
|
|
|
7
|
-
- qtools-clone
|
|
8
|
-
- qtools-get-by-property
|
|
9
|
-
- qtools-includes-regex
|
|
10
|
-
- qtools-log-output
|
|
11
|
-
- qtools-merge
|
|
12
|
-
- qtools-
|
|
13
|
-
- qtools-number-
|
|
14
|
-
- qtools-
|
|
15
|
-
- qtools-
|
|
16
|
-
- qtools-
|
|
17
|
-
- qtools-pop
|
|
7
|
+
- qtools-clone: Does a deep clone of an object or array.
|
|
8
|
+
- qtools-get-by-property: Operates on an array of objects. Retrieves one or more elements based on the value of an object property specified by a dotted path.
|
|
9
|
+
- qtools-includes-regex: Operations on a simple array of strings. Retrieves one or more elements based on an regEx match.
|
|
10
|
+
- qtools-log-output: Substitutes properties into a <!template!> string and send to log. (Note to self, verify array operation)
|
|
11
|
+
- qtools-merge: Deep merge of two objects including array propterties.
|
|
12
|
+
- qtools-number-iterator: Operates on a number. EG, (5).qtStart(7).qtIncrement(3).qtIterate(item=>item*100)
|
|
13
|
+
- qtools-number-keys-to-array: Converts an object with properties that are numbers to an array, including subordinate properties.
|
|
14
|
+
- qtools-object-sure-path: Gets or Sets the value of a property based on a dotted string path. No crash if intermediate elements are missing. Provides default. Predates and is prettier to use than optional chaining and supports setting the value.
|
|
15
|
+
- qtools-pass-through: Sits in a declarative chain and executes an arbitrary function. It returns 'this' regardless of what the function does. 'this' is available to the function.
|
|
16
|
+
- qtools-pop: Pops the last value off an array. If a default is supplied and there is nothing left on the list, that will be returned. Really good for ArrayOfObjects.qtPop({}).
|
|
17
|
+
- qtools-qtLast: Delivers the element that would have been pop'd but does not change the array.
|
|
18
|
+
- qtoolspush: Pushes a value onto an array and returns the array (unlike normal .push() which returns the length of the array)
|
|
18
19
|
- qtools-print-debug
|
|
19
20
|
- qtools-template-replace
|
|
20
21
|
- qtools-to-key-value-array
|
|
21
22
|
- qtools-to-string
|
|
22
23
|
|
|
24
|
+
NEW:
|
|
25
|
+
|
|
26
|
+
- qtools-select-properties
|
|
27
|
+
- qtools-map-properties
|
|
28
|
+
|
|
23
29
|
DESCRIPTION
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "qtools-map-properties",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": " Transform and add properties to a javascript object or array of objects",
|
|
5
|
+
"main": "qtools-map-properties.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"author": "TQ White II",
|
|
10
|
+
"license": "ISC"
|
|
11
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
//START OF moduleFunction() ============================================================
|
|
3
|
+
var moduleFunction = function(args = {}) {
|
|
4
|
+
const { commonFunctions } = args;
|
|
5
|
+
|
|
6
|
+
const addToPrototypeActual = functionObject => () =>
|
|
7
|
+
commonFunctions.universalAddToPrototype(commonFunctions, functionObject);
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
//first method definition function ==========================
|
|
12
|
+
const firstMethodFunction = commonFunctions => {
|
|
13
|
+
const methodName = 'qtMapProperties';
|
|
14
|
+
const description = `operand.qtMapProperties() transforms and adds properties to a javascript object or array of objects`;
|
|
15
|
+
const supportedTypeList = [Array, Object];
|
|
16
|
+
|
|
17
|
+
const method = () =>
|
|
18
|
+
function(mappings = {}, options = {}) {
|
|
19
|
+
const mappingFunction = item => {
|
|
20
|
+
const outObj = item.qtClone();
|
|
21
|
+
const originalThis = item.qtClone();
|
|
22
|
+
|
|
23
|
+
// mappingElementFunction=(item, name, entire)=>{}; //note: synthetic elements have item==undefined
|
|
24
|
+
|
|
25
|
+
Object.keys(mappings).forEach(name => {
|
|
26
|
+
if (typeof mappings[name] == 'function') {
|
|
27
|
+
outObj[name] = mappings[name](
|
|
28
|
+
originalThis[name],
|
|
29
|
+
name,
|
|
30
|
+
originalThis
|
|
31
|
+
);
|
|
32
|
+
} else {
|
|
33
|
+
outObj[name] = mappings[name];
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return outObj;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if (this instanceof Array) {
|
|
42
|
+
return this.map(item => mappingFunction(item));
|
|
43
|
+
} else {
|
|
44
|
+
return mappingFunction(this);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const test = args => {
|
|
50
|
+
return require('./test.js')({
|
|
51
|
+
...args,
|
|
52
|
+
...{
|
|
53
|
+
moduleName: module.id.replace(module.path, '')
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
methodName,
|
|
60
|
+
description,
|
|
61
|
+
supportedTypeList,
|
|
62
|
+
method,
|
|
63
|
+
test
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
const functionObject = new Map(); // prettier-ignore
|
|
69
|
+
const addFunction = definition => {
|
|
70
|
+
const {
|
|
71
|
+
methodName,
|
|
72
|
+
description,
|
|
73
|
+
supportedTypeList,
|
|
74
|
+
method,
|
|
75
|
+
test
|
|
76
|
+
} = definition;
|
|
77
|
+
functionObject.set(methodName, {
|
|
78
|
+
description,
|
|
79
|
+
supportedTypeList,
|
|
80
|
+
method,
|
|
81
|
+
test: args => {
|
|
82
|
+
return require('./test.js')({
|
|
83
|
+
...args,
|
|
84
|
+
...{
|
|
85
|
+
moduleName: module.id.replace(module.path, '')
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
addFunction(firstMethodFunction());
|
|
93
|
+
|
|
94
|
+
this.addToPrototype = addToPrototypeActual(functionObject);
|
|
95
|
+
|
|
96
|
+
};
|
|
97
|
+
//END OF moduleFunction() ============================================================
|
|
98
|
+
module.exports = moduleFunction;
|
|
99
|
+
//module.exports = new moduleFunction();
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
//START OF moduleFunction() ============================================================
|
|
4
|
+
|
|
5
|
+
const moduleFunction = function(args) {
|
|
6
|
+
//this module is initialized with parameters spec'd in the main function module
|
|
7
|
+
|
|
8
|
+
const completeStepActual = ({verbose=false, logErrors=false, moduleName}) => (
|
|
9
|
+
methodName,
|
|
10
|
+
thisStepMessage,
|
|
11
|
+
thisStepResult,
|
|
12
|
+
thisStepEvalFunction
|
|
13
|
+
) => {
|
|
14
|
+
|
|
15
|
+
const thisStepPass = thisStepEvalFunction(thisStepResult);
|
|
16
|
+
|
|
17
|
+
if (logErrors && !thisStepPass) {
|
|
18
|
+
console.log(`FAIL TEST: ${thisStepMessage} in ...${moduleName}`);
|
|
19
|
+
}
|
|
20
|
+
if (verbose) {
|
|
21
|
+
console.log(`Result for: ${methodName} (${thisStepPass?'PASS':'FAIL'}):`);
|
|
22
|
+
console.dir(thisStepResult);
|
|
23
|
+
console.log('\n\n');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return passingTests && thisStepPass;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const completeStep = completeStepActual(args);
|
|
30
|
+
|
|
31
|
+
let passingTests = true;
|
|
32
|
+
|
|
33
|
+
let thisStepMessage;
|
|
34
|
+
let thisStepResult;
|
|
35
|
+
let thisStepEvalFunction;
|
|
36
|
+
let methodName;
|
|
37
|
+
|
|
38
|
+
//TESTS ============================================================
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
const testObject={
|
|
43
|
+
one: 1,
|
|
44
|
+
two: 2
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const testArray=[
|
|
48
|
+
{
|
|
49
|
+
alpha: 11,
|
|
50
|
+
beta: 12
|
|
51
|
+
},{
|
|
52
|
+
alpha: 21,
|
|
53
|
+
beta: 22
|
|
54
|
+
}
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
const testOneArgument={
|
|
58
|
+
one: (item, name, entire)=>item*1000
|
|
59
|
+
}
|
|
60
|
+
const testTwoArgument={
|
|
61
|
+
one: (item, name, entire)=>item*1000,
|
|
62
|
+
addedValue:(item, name, entire)=>`${name} says, item:${item} becomes ${Object.keys(entire).join(', ')}`
|
|
63
|
+
}
|
|
64
|
+
const testThreeArgument={
|
|
65
|
+
alpha: (item, name, entire)=>item*1000,
|
|
66
|
+
addedValue:(item, name, entire)=>`${name} says, item:${item} becomes ${Object.keys(entire).join(', ')}`
|
|
67
|
+
}
|
|
68
|
+
// map one object
|
|
69
|
+
// map one with added property
|
|
70
|
+
// map array
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
//TEST ITEM ------------------------------------------------------
|
|
75
|
+
methodName='qtMapProperties'
|
|
76
|
+
thisStepMessage = "did not get map one object ";
|
|
77
|
+
thisStepResult = testObject[methodName](testOneArgument);
|
|
78
|
+
thisStepEvalFunction = item => (item.one==1000 && item.two==2);
|
|
79
|
+
passingTests =
|
|
80
|
+
completeStep(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
|
|
81
|
+
passingTests;
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
//TEST ITEM ------------------------------------------------------
|
|
85
|
+
methodName='qtMapProperties'
|
|
86
|
+
thisStepMessage = "did not map one with added property";
|
|
87
|
+
thisStepResult = testObject[methodName](testTwoArgument);
|
|
88
|
+
thisStepEvalFunction = item => (item.one==1000 && item.two==2 && item.addedValue=='addedValue says, item:undefined becomes one, two');
|
|
89
|
+
passingTests =
|
|
90
|
+
completeStep(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
|
|
91
|
+
passingTests;
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
//TEST ITEM ------------------------------------------------------
|
|
95
|
+
methodName='qtMapProperties'
|
|
96
|
+
thisStepMessage = "did not map array woth added property";
|
|
97
|
+
thisStepResult = testArray[methodName](testThreeArgument);
|
|
98
|
+
thisStepEvalFunction = item => (
|
|
99
|
+
item[0].alpha==11000 &&
|
|
100
|
+
item[0].beta==12 &&
|
|
101
|
+
item[0].addedValue=='addedValue says, item:undefined becomes alpha, beta' &&
|
|
102
|
+
item[1].alpha==21000 &&
|
|
103
|
+
item[1].beta==22 &&
|
|
104
|
+
item[1].addedValue=='addedValue says, item:undefined becomes alpha, beta'
|
|
105
|
+
);
|
|
106
|
+
passingTests =
|
|
107
|
+
completeStep(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
|
|
108
|
+
passingTests;
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
return passingTests;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
//END OF moduleFunction() ============================================================
|
|
115
|
+
|
|
116
|
+
module.exports = moduleFunction;
|
|
117
|
+
//module.exports = new moduleFunction();
|
|
118
|
+
|
package/lib/qtools-merge/test.js
CHANGED
|
@@ -123,10 +123,6 @@ const moduleFunction = function(args) {
|
|
|
123
123
|
methodName='qtMerge'
|
|
124
124
|
thisStepMessage = "Failed to replace base type with complex one or vice versa";
|
|
125
125
|
thisStepResult = arrayTestA[methodName](arrayTestE, {mergeNonExistent:true});
|
|
126
|
-
thisStepResult.qtDump({label:"thisStepResult"});
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
126
|
thisStepEvalFunction = item =>
|
|
131
127
|
item.qtGetSurePath('c.arrayOfBasicTypes[0]')=='replacesFirst' &&
|
|
132
128
|
item.qtGetSurePath('c.arrayOfBasicTypes[1]')=='second';
|
|
@@ -5,24 +5,24 @@
|
|
|
5
5
|
const moduleFunction = function(args) {
|
|
6
6
|
//this module is initialized with parameters spec'd in the main function module
|
|
7
7
|
|
|
8
|
-
const completeStepActual = ({
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
) => {
|
|
14
|
-
|
|
8
|
+
const completeStepActual = ({
|
|
9
|
+
verbose = false,
|
|
10
|
+
logErrors = false,
|
|
11
|
+
moduleName
|
|
12
|
+
}) => (methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) => {
|
|
15
13
|
const thisStepPass = thisStepEvalFunction(thisStepResult);
|
|
16
|
-
|
|
14
|
+
|
|
17
15
|
if (logErrors && !thisStepPass) {
|
|
18
16
|
console.log(`FAIL TEST: ${thisStepMessage} in ...${moduleName}`);
|
|
19
17
|
}
|
|
20
18
|
if (verbose) {
|
|
21
|
-
console.log(
|
|
19
|
+
console.log(
|
|
20
|
+
`Result for: ${methodName} (${thisStepPass ? 'PASS' : 'FAIL'}):`
|
|
21
|
+
);
|
|
22
22
|
console.dir(thisStepResult);
|
|
23
23
|
console.log('\n\n');
|
|
24
24
|
}
|
|
25
|
-
|
|
25
|
+
|
|
26
26
|
return passingTests && thisStepPass;
|
|
27
27
|
};
|
|
28
28
|
|
|
@@ -40,82 +40,133 @@ const moduleFunction = function(args) {
|
|
|
40
40
|
|
|
41
41
|
|
|
42
42
|
const testArray = {
|
|
43
|
-
['0']:'zero',
|
|
44
|
-
['1']:'one',
|
|
45
|
-
['4']:'four',
|
|
46
|
-
a:'b'
|
|
47
|
-
|
|
48
|
-
}
|
|
43
|
+
['0']: 'zero',
|
|
44
|
+
['1']: 'one',
|
|
45
|
+
['4']: 'four',
|
|
46
|
+
a: 'b'
|
|
47
|
+
};
|
|
49
48
|
|
|
50
49
|
|
|
51
50
|
|
|
52
51
|
const testArray2 = {
|
|
53
|
-
['0']:'zero',
|
|
54
|
-
['1']:'one',
|
|
55
|
-
['4']:'four',
|
|
56
|
-
a:'b',
|
|
57
|
-
subArray:{
|
|
58
|
-
['
|
|
59
|
-
['
|
|
60
|
-
|
|
61
|
-
}
|
|
52
|
+
['0']: 'zero',
|
|
53
|
+
['1']: 'one',
|
|
54
|
+
['4']: 'four',
|
|
55
|
+
a: 'b',
|
|
56
|
+
subArray: {
|
|
57
|
+
['0']: 'zeroX',
|
|
58
|
+
['1']: 'oneX',
|
|
59
|
+
['4']: 'fourX'
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
62
|
|
|
63
|
-
const emptyArray={}
|
|
63
|
+
const emptyArray = {};
|
|
64
64
|
|
|
65
65
|
|
|
66
66
|
//TEST ITEM ------------------------------------------------------
|
|
67
|
-
methodName='qtNumberKeysToArray'
|
|
68
|
-
thisStepMessage =
|
|
69
|
-
thisStepResult = testArray[methodName]({
|
|
70
|
-
|
|
67
|
+
methodName = 'qtNumberKeysToArray';
|
|
68
|
+
thisStepMessage = 'test #1, result is wrong, holes or length are wrong';
|
|
69
|
+
thisStepResult = testArray[methodName]({
|
|
70
|
+
collapseHoles: false,
|
|
71
|
+
omitNonNumeric: true
|
|
72
|
+
});
|
|
73
|
+
thisStepEvalFunction = item =>
|
|
74
|
+
(item =
|
|
75
|
+
item.length == 5 &&
|
|
76
|
+
typeof item[2] == 'undefined' &&
|
|
77
|
+
typeof item[3] == 'undefined');
|
|
71
78
|
passingTests =
|
|
72
|
-
completeStep(
|
|
73
|
-
|
|
79
|
+
completeStep(
|
|
80
|
+
methodName,
|
|
81
|
+
thisStepMessage,
|
|
82
|
+
thisStepResult,
|
|
83
|
+
thisStepEvalFunction
|
|
84
|
+
) && passingTests;
|
|
74
85
|
|
|
75
86
|
|
|
76
87
|
//TEST ITEM ------------------------------------------------------
|
|
77
|
-
methodName='qtNumberKeysToArray'
|
|
78
|
-
thisStepMessage =
|
|
79
|
-
thisStepResult = testArray[methodName]({
|
|
80
|
-
|
|
88
|
+
methodName = 'qtNumberKeysToArray';
|
|
89
|
+
thisStepMessage = 'test #2, result is wrong, has holes or length is wrong';
|
|
90
|
+
thisStepResult = testArray[methodName]({
|
|
91
|
+
collapseHoles: true,
|
|
92
|
+
omitNonNumeric: true
|
|
93
|
+
});
|
|
94
|
+
thisStepEvalFunction = item =>
|
|
95
|
+
(item =
|
|
96
|
+
item.length == 3 &&
|
|
97
|
+
item.reduce(
|
|
98
|
+
(item, result) => typeof (item != 'undefined') && result,
|
|
99
|
+
true
|
|
100
|
+
));
|
|
81
101
|
passingTests =
|
|
82
|
-
completeStep(
|
|
83
|
-
|
|
102
|
+
completeStep(
|
|
103
|
+
methodName,
|
|
104
|
+
thisStepMessage,
|
|
105
|
+
thisStepResult,
|
|
106
|
+
thisStepEvalFunction
|
|
107
|
+
) && passingTests;
|
|
84
108
|
|
|
85
109
|
|
|
86
110
|
|
|
87
111
|
|
|
88
112
|
//TEST ITEM ------------------------------------------------------
|
|
89
|
-
methodName='qtNumberKeysToArray'
|
|
90
|
-
thisStepMessage =
|
|
91
|
-
|
|
92
|
-
|
|
113
|
+
methodName = 'qtNumberKeysToArray';
|
|
114
|
+
thisStepMessage =
|
|
115
|
+
'test #3, result is wrong, non-numeric element is missing or length is wrong';
|
|
116
|
+
thisStepResult = testArray[methodName]({
|
|
117
|
+
collapseHoles: true,
|
|
118
|
+
omitNonNumeric: false
|
|
119
|
+
});
|
|
120
|
+
thisStepEvalFunction = item =>
|
|
121
|
+
(item = item.length == 4 && typeof item[3] == 'object');
|
|
93
122
|
passingTests =
|
|
94
|
-
completeStep(
|
|
95
|
-
|
|
123
|
+
completeStep(
|
|
124
|
+
methodName,
|
|
125
|
+
thisStepMessage,
|
|
126
|
+
thisStepResult,
|
|
127
|
+
thisStepEvalFunction
|
|
128
|
+
) && passingTests;
|
|
96
129
|
|
|
97
130
|
|
|
98
131
|
|
|
99
132
|
|
|
100
133
|
|
|
101
134
|
//TEST ITEM ------------------------------------------------------
|
|
102
|
-
methodName='qtNumberKeysToArray'
|
|
103
|
-
thisStepMessage =
|
|
104
|
-
thisStepResult = testArray2[methodName]({
|
|
105
|
-
|
|
135
|
+
methodName = 'qtNumberKeysToArray';
|
|
136
|
+
thisStepMessage = 'test #4, subordinate element was not converted properly';
|
|
137
|
+
thisStepResult = testArray2[methodName]({
|
|
138
|
+
collapseHoles: true,
|
|
139
|
+
omitNonNumeric: false
|
|
140
|
+
});
|
|
141
|
+
thisStepEvalFunction = item =>
|
|
142
|
+
(item =
|
|
143
|
+
item.length == 5 &&
|
|
144
|
+
typeof item[4] == 'object' &&
|
|
145
|
+
item[4].value.length == 3);
|
|
106
146
|
passingTests =
|
|
107
|
-
completeStep(
|
|
108
|
-
|
|
147
|
+
completeStep(
|
|
148
|
+
methodName,
|
|
149
|
+
thisStepMessage,
|
|
150
|
+
thisStepResult,
|
|
151
|
+
thisStepEvalFunction
|
|
152
|
+
) && passingTests;
|
|
109
153
|
|
|
110
154
|
|
|
111
155
|
//TEST ITEM ------------------------------------------------------
|
|
112
|
-
methodName='qtNumberKeysToArray'
|
|
113
|
-
thisStepMessage =
|
|
114
|
-
thisStepResult = emptyArray[methodName]({
|
|
115
|
-
|
|
156
|
+
methodName = 'qtNumberKeysToArray';
|
|
157
|
+
thisStepMessage = 'test #5, empty object returns empty array';
|
|
158
|
+
thisStepResult = emptyArray[methodName]({
|
|
159
|
+
collapseHoles: true,
|
|
160
|
+
omitNonNumeric: false
|
|
161
|
+
});
|
|
162
|
+
thisStepEvalFunction = item => (item = item.length === 0);
|
|
116
163
|
passingTests =
|
|
117
|
-
completeStep(
|
|
118
|
-
|
|
164
|
+
completeStep(
|
|
165
|
+
methodName,
|
|
166
|
+
thisStepMessage,
|
|
167
|
+
thisStepResult,
|
|
168
|
+
thisStepEvalFunction
|
|
169
|
+
) && passingTests;
|
|
119
170
|
|
|
120
171
|
|
|
121
172
|
|
|
@@ -102,6 +102,41 @@ var moduleFunction = function(args = {}) {
|
|
|
102
102
|
test
|
|
103
103
|
};
|
|
104
104
|
};
|
|
105
|
+
//fourth method definition function ==========================
|
|
106
|
+
const fourthMethodFunction = commonFunctions => {
|
|
107
|
+
const methodName = 'qtFirst';
|
|
108
|
+
const description = `operand.qtFirst(value) gets element zero or default`;
|
|
109
|
+
const supportedTypeList = [Array];
|
|
110
|
+
|
|
111
|
+
const method = () =>
|
|
112
|
+
function(value) {
|
|
113
|
+
const inData = this;
|
|
114
|
+
if (typeof inData.length == 'undefined') {
|
|
115
|
+
throw 'array.qtFirst(defaultValue=undefined) works for Arrays only';
|
|
116
|
+
}
|
|
117
|
+
const arrayValue = inData[0]?inData[0]:void('');
|
|
118
|
+
const result =
|
|
119
|
+
typeof arrayValue != 'undefined' ? arrayValue : defaultValue;
|
|
120
|
+
return result;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const test = args => {
|
|
124
|
+
return require('./test.js')({
|
|
125
|
+
...args,
|
|
126
|
+
...{
|
|
127
|
+
moduleName: module.id.replace(module.path, '')
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
methodName,
|
|
134
|
+
description,
|
|
135
|
+
supportedTypeList,
|
|
136
|
+
method,
|
|
137
|
+
test
|
|
138
|
+
};
|
|
139
|
+
};
|
|
105
140
|
|
|
106
141
|
const functionObject = new Map(); // prettier-ignore
|
|
107
142
|
const addFunction = definition => {
|
|
@@ -130,6 +165,7 @@ var moduleFunction = function(args = {}) {
|
|
|
130
165
|
addFunction(firstMethodFunction());
|
|
131
166
|
addFunction(secondMethodFunction());
|
|
132
167
|
addFunction(thirdMethodFunction());
|
|
168
|
+
addFunction(fourthMethodFunction());
|
|
133
169
|
|
|
134
170
|
this.addToPrototype = addToPrototypeActual(functionObject);
|
|
135
171
|
|
package/lib/qtools-pop/test.js
CHANGED
|
@@ -96,6 +96,15 @@ const moduleFunction = function(args) {
|
|
|
96
96
|
completeStep(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
|
|
97
97
|
passingTests;
|
|
98
98
|
|
|
99
|
+
//TEST ITEM ------------------------------------------------------
|
|
100
|
+
methodName='qtFirst'
|
|
101
|
+
thisStepMessage = "element was not added";
|
|
102
|
+
thisStepResult = testArray[methodName]();
|
|
103
|
+
thisStepEvalFunction = item => (item=='allocute' && item.length==2);
|
|
104
|
+
passingTests =
|
|
105
|
+
completeStep(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
|
|
106
|
+
passingTests;
|
|
107
|
+
|
|
99
108
|
|
|
100
109
|
|
|
101
110
|
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "qtools-select-properties",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Create new objects (or list of objects) containing only the enumerated properties. Allows defaults to be supplied for missing properties. If an enumerated property name does not exist at all, a property with the default value is created.",
|
|
5
|
+
"main": "qtools-select-properties.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"author": "TQ White II",
|
|
10
|
+
"license": "ISC"
|
|
11
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
//START OF moduleFunction() ============================================================
|
|
3
|
+
var moduleFunction = function(args = {}) {
|
|
4
|
+
const { commonFunctions } = args;
|
|
5
|
+
|
|
6
|
+
const addToPrototypeActual = functionObject => () =>
|
|
7
|
+
commonFunctions.universalAddToPrototype(commonFunctions, functionObject);
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
//first method definition function ==========================
|
|
12
|
+
const firstMethodFunction = commonFunctions => {
|
|
13
|
+
const methodName = 'qtSelectProperties';
|
|
14
|
+
const description = `operand.qtSelectProperties(value) create snew objects (or list of objects) containing only the enumerated properties. Allows defaults to be supplied for missing properties. If an enumerated property name does not exist at all, a property with the default value is created.`;
|
|
15
|
+
const supportedTypeList = [Array, Object];
|
|
16
|
+
|
|
17
|
+
const method = () =>
|
|
18
|
+
function(keepers, options = {}) {
|
|
19
|
+
|
|
20
|
+
const {
|
|
21
|
+
moreDefaultValues = {},
|
|
22
|
+
omitUndefined = true,
|
|
23
|
+
excludeMode = false
|
|
24
|
+
} = options;
|
|
25
|
+
|
|
26
|
+
if (!(keepers instanceof Object)) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
'qtSelectProperties() argument one must be instance of Object or Array'
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let inclusions = keepers;
|
|
33
|
+
let defaultValues = {};
|
|
34
|
+
if (!(keepers instanceof Array)) {
|
|
35
|
+
inclusions = Object.keys(keepers);
|
|
36
|
+
defaultValues = keepers;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
Object.keys(moreDefaultValues).forEach(
|
|
40
|
+
name => (defaultValues[name] = moreDefaultValues[name])
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const selectingFunction = currentObject => {
|
|
44
|
+
const getDefault = (currentObject, keeperName) =>
|
|
45
|
+
typeof defaultValues[keeperName] == 'function'
|
|
46
|
+
? defaultValues[keeperName](currentObject.qtClone(), keeperName)
|
|
47
|
+
: defaultValues[keeperName];
|
|
48
|
+
|
|
49
|
+
// defaultFunction=(currentObject, elementName)=>{}
|
|
50
|
+
|
|
51
|
+
let outObj = {};
|
|
52
|
+
|
|
53
|
+
if (!excludeMode) {
|
|
54
|
+
inclusions.forEach(keeperName => {
|
|
55
|
+
const newValue =
|
|
56
|
+
typeof currentObject[keeperName] != 'undefined'
|
|
57
|
+
? currentObject[keeperName]
|
|
58
|
+
: getDefault(currentObject, keeperName);
|
|
59
|
+
if (typeof newValue != 'undefined' || !omitUndefined) {
|
|
60
|
+
outObj[keeperName] = newValue;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
} else {
|
|
64
|
+
outObj = currentObject;
|
|
65
|
+
inclusions.forEach(keeperName => {
|
|
66
|
+
if (typeof (outObj[keeperName] == 'undefined')) {
|
|
67
|
+
delete outObj[keeperName];
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return outObj;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
if (this instanceof Array) {
|
|
77
|
+
return this.map(item => selectingFunction(item));
|
|
78
|
+
} else {
|
|
79
|
+
return selectingFunction(this);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const test = args => {
|
|
84
|
+
return require('./test.js')({
|
|
85
|
+
...args,
|
|
86
|
+
...{
|
|
87
|
+
moduleName: module.id.replace(module.path, '')
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
methodName,
|
|
94
|
+
description,
|
|
95
|
+
supportedTypeList,
|
|
96
|
+
method,
|
|
97
|
+
test
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const functionObject = new Map(); // prettier-ignore
|
|
102
|
+
const addFunction = definition => {
|
|
103
|
+
const {
|
|
104
|
+
methodName,
|
|
105
|
+
description,
|
|
106
|
+
supportedTypeList,
|
|
107
|
+
method,
|
|
108
|
+
test
|
|
109
|
+
} = definition;
|
|
110
|
+
functionObject.set(methodName, {
|
|
111
|
+
description,
|
|
112
|
+
supportedTypeList,
|
|
113
|
+
method,
|
|
114
|
+
test: args => {
|
|
115
|
+
return require('./test.js')({
|
|
116
|
+
...args,
|
|
117
|
+
...{
|
|
118
|
+
moduleName: module.id.replace(module.path, '')
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
addFunction(firstMethodFunction());
|
|
126
|
+
|
|
127
|
+
this.addToPrototype = addToPrototypeActual(functionObject);
|
|
128
|
+
|
|
129
|
+
};
|
|
130
|
+
//END OF moduleFunction() ============================================================
|
|
131
|
+
module.exports = moduleFunction;
|
|
132
|
+
//module.exports = new moduleFunction();
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
//START OF moduleFunction() ============================================================
|
|
4
|
+
|
|
5
|
+
const moduleFunction = function(args) {
|
|
6
|
+
//this module is initialized with parameters spec'd in the main function module
|
|
7
|
+
|
|
8
|
+
const completeStepActual = ({verbose=false, logErrors=false, moduleName}) => (
|
|
9
|
+
methodName,
|
|
10
|
+
thisStepMessage,
|
|
11
|
+
thisStepResult,
|
|
12
|
+
thisStepEvalFunction
|
|
13
|
+
) => {
|
|
14
|
+
|
|
15
|
+
const thisStepPass = thisStepEvalFunction(thisStepResult);
|
|
16
|
+
|
|
17
|
+
if (logErrors && !thisStepPass) {
|
|
18
|
+
console.log(`FAIL TEST: ${thisStepMessage} in ...${moduleName}`);
|
|
19
|
+
}
|
|
20
|
+
if (verbose) {
|
|
21
|
+
console.log(`Result for: ${methodName} (${thisStepPass?'PASS':'FAIL'}):`);
|
|
22
|
+
console.dir(thisStepResult);
|
|
23
|
+
console.log('\n\n');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return passingTests && thisStepPass;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const completeStep = completeStepActual(args);
|
|
30
|
+
|
|
31
|
+
let passingTests = true;
|
|
32
|
+
|
|
33
|
+
let thisStepMessage;
|
|
34
|
+
let thisStepResult;
|
|
35
|
+
let thisStepEvalFunction;
|
|
36
|
+
let methodName;
|
|
37
|
+
|
|
38
|
+
//TESTS ============================================================
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
const testObject={
|
|
42
|
+
one: 1,
|
|
43
|
+
two: 2
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const testArray=[
|
|
47
|
+
{
|
|
48
|
+
alpha: 11,
|
|
49
|
+
beta: 12,
|
|
50
|
+
gamma:13
|
|
51
|
+
},{
|
|
52
|
+
alpha: 21,
|
|
53
|
+
gamma:23
|
|
54
|
+
}
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
const testOneArgument=[ 'one'];
|
|
58
|
+
const testTwoArgument=['alpha'];
|
|
59
|
+
|
|
60
|
+
const testThreeArgument=['alpha', 'beta'];
|
|
61
|
+
const testThreeMoreValues={moreDefaultValues:{beta:(currentObject, elementName)=>`default function ${elementName} produces ${Object.keys(currentObject).join(', ')}`}}
|
|
62
|
+
|
|
63
|
+
const testFourArgument={
|
|
64
|
+
alpha: (item, name, entire)=>item*1000,
|
|
65
|
+
addedValue:(item, name, entire)=>`${name} says, item:${item} becomes ${Object.keys(entire).join(', ')}`
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const testFiveArgument={
|
|
69
|
+
alpha: 'eleven',
|
|
70
|
+
beta: 'twelve'
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// array of strings for one object
|
|
74
|
+
// array of strings for array of objects
|
|
75
|
+
// array of strings for one object with moreDefaultValues
|
|
76
|
+
// object with defaults for object
|
|
77
|
+
// EXCLUDE MODE
|
|
78
|
+
|
|
79
|
+
//TEST ITEM ------------------------------------------------------
|
|
80
|
+
methodName='qtSelectProperties'
|
|
81
|
+
thisStepMessage = "did not get array of strings for one object ";
|
|
82
|
+
thisStepResult = testObject[methodName](testOneArgument);
|
|
83
|
+
thisStepEvalFunction = item => (item.one==1 && typeof(item.two)=='undefined');
|
|
84
|
+
passingTests =
|
|
85
|
+
completeStep(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
|
|
86
|
+
passingTests;
|
|
87
|
+
|
|
88
|
+
//TEST ITEM ------------------------------------------------------
|
|
89
|
+
methodName='qtSelectProperties'
|
|
90
|
+
thisStepMessage = "did not get array of strings for array of objects";
|
|
91
|
+
thisStepResult = testArray[methodName](testTwoArgument);
|
|
92
|
+
thisStepEvalFunction = item => (
|
|
93
|
+
item[0].alpha==11 &&
|
|
94
|
+
typeof(item[0].beta)=='undefined' &&
|
|
95
|
+
item[1].alpha==21 &&
|
|
96
|
+
typeof(item[1].beta)=='undefined'
|
|
97
|
+
);
|
|
98
|
+
passingTests =
|
|
99
|
+
completeStep(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
|
|
100
|
+
passingTests;
|
|
101
|
+
|
|
102
|
+
//TEST ITEM ------------------------------------------------------
|
|
103
|
+
methodName='qtSelectProperties'
|
|
104
|
+
thisStepMessage = "did not get array of strings for one object with moreDefaultValues";
|
|
105
|
+
thisStepResult = testArray[methodName](testThreeArgument, testThreeMoreValues);
|
|
106
|
+
thisStepEvalFunction = item => (
|
|
107
|
+
item[0].alpha==11 &&
|
|
108
|
+
item[0].beta==12 &&
|
|
109
|
+
item[1].alpha==21 &&
|
|
110
|
+
item[1].beta=='default function beta produces alpha, gamma'
|
|
111
|
+
);
|
|
112
|
+
passingTests =
|
|
113
|
+
completeStep(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
|
|
114
|
+
passingTests;
|
|
115
|
+
|
|
116
|
+
//TEST ITEM ------------------------------------------------------
|
|
117
|
+
methodName='qtSelectProperties'
|
|
118
|
+
thisStepMessage = "did not get object argument containing defaults";
|
|
119
|
+
thisStepResult = testArray[methodName](testFiveArgument);
|
|
120
|
+
thisStepEvalFunction = item => (
|
|
121
|
+
item[0].alpha==11 &&
|
|
122
|
+
item[0].beta==12 &&
|
|
123
|
+
item[1].alpha==21 &&
|
|
124
|
+
item[1].beta=='twelve'
|
|
125
|
+
);
|
|
126
|
+
passingTests =
|
|
127
|
+
completeStep(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
|
|
128
|
+
passingTests;
|
|
129
|
+
|
|
130
|
+
//TEST ITEM ------------------------------------------------------
|
|
131
|
+
methodName='qtSelectProperties'
|
|
132
|
+
thisStepMessage = "did not get EXCLUDE MODE";
|
|
133
|
+
thisStepResult = testArray[methodName](testThreeArgument, {excludeMode:true});
|
|
134
|
+
thisStepEvalFunction = item => (
|
|
135
|
+
typeof(item[0].alpha)=='undefined' &&
|
|
136
|
+
typeof(item[0].beta)=='undefined' &&
|
|
137
|
+
item[0].gamma==13 &&
|
|
138
|
+
typeof(item[1].alpha)=='undefined' &&
|
|
139
|
+
typeof(item[1].beta)=='undefined' &&
|
|
140
|
+
item[1].gamma==23
|
|
141
|
+
);
|
|
142
|
+
passingTests =
|
|
143
|
+
completeStep(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
|
|
144
|
+
passingTests;
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
return passingTests;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
//END OF moduleFunction() ============================================================
|
|
153
|
+
|
|
154
|
+
module.exports = moduleFunction;
|
|
155
|
+
//module.exports = new moduleFunction();
|
|
156
|
+
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qtools-functional-library",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.12",
|
|
4
4
|
"description": "Useful additions to JS prototypes to facilliate chainable, functional (mostly) style. Also make life more convenient.\"",
|
|
5
5
|
"main": "qtools-functional-library.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"sample": "node test.js -listTests -logErrors -verbose --methodList=qtClone,qtToString,qtPop",
|
|
8
8
|
"sample2": "node test.js -listTests -logErrors --methodList=qtClone,qtToString,qtPop",
|
|
9
9
|
"logTest": "node --trace-uncaught test.js -listTests -logErrors --methodList=qtLog",
|
|
10
|
-
"test": "node test.js -
|
|
10
|
+
"test": "node test.js -logErrors",
|
|
11
11
|
"quiet": "node test.js -listTests -logErrors"
|
|
12
12
|
},
|
|
13
13
|
"repository": {
|
package/test.js
CHANGED
|
@@ -8,7 +8,6 @@ const verbose=process.argv.filter(item=>item.match(/verbose/i))[0]?true:false
|
|
|
8
8
|
const logErrors=process.argv.filter(item=>item.match(/logErrors/i))[0]?true:false
|
|
9
9
|
const listTests=process.argv.filter(item=>item.match(/listTests/i))[0]?true:false
|
|
10
10
|
|
|
11
|
-
|
|
12
11
|
const methodList=process.argv
|
|
13
12
|
.qtClone() //create a deep copy so changes can't leak back to the source
|
|
14
13
|
.filter(item=>item.match(/^--methodList/)) //['--methodList=qtClone,qtToString,qtPop']
|
|
@@ -21,7 +20,28 @@ const methodList=process.argv
|
|
|
21
20
|
|
|
22
21
|
const result=qt.test({logErrors, verbose, methodList, listTests});
|
|
23
22
|
console.log(result?"PASSED ALL TESTS":"");
|
|
24
|
-
console.log("note: npm test runs all tests. For subset, type, eg, node test.js -listTests -logErrors -verbose --methodList=qtNumberKeysToArray")
|
|
25
23
|
|
|
24
|
+
const showAdditionalInfo=(verbose, result)=>{
|
|
25
|
+
console.log(`result=${result}`);
|
|
26
|
+
|
|
27
|
+
if (!verbose && result){
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
console.log(`
|
|
31
|
+
|
|
32
|
+
Additional Usage:
|
|
33
|
+
npm logTest; #node --trace-uncaught test.js -listTests -logErrors --methodList=qtLog
|
|
34
|
+
npm test; #node test.js -verbose -logErrors
|
|
35
|
+
npm quiet; #node test.js -listTests -logErrors
|
|
36
|
+
|
|
37
|
+
# -logErrors shows the message from any failing tests
|
|
38
|
+
|
|
39
|
+
or find .../qtools-functional-library/test.js and execute, eg,
|
|
40
|
+
cd .../qtools-functional-library/test.js;
|
|
41
|
+
node test.js --methodList=qtMapProperties
|
|
42
|
+
`)
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
showAdditionalInfo(verbose, result)
|
|
26
46
|
|
|
27
47
|
process.exit(result?0:1);
|