qtools-functional-library 1.1.11 → 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 CHANGED
@@ -4,17 +4,18 @@ 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-new-method-template
13
- - qtools-number-iterator
14
- - qtools-number-keys-to-array
15
- - qtools-object-sure-path
16
- - qtools-pass-through
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
@@ -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 = ({verbose=false, logErrors=false, moduleName}) => (
9
- methodName,
10
- thisStepMessage,
11
- thisStepResult,
12
- thisStepEvalFunction
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(`Result for: ${methodName} (${thisStepPass?'PASS':'FAIL'}):`);
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:{['0']:'zeroX',
58
- ['1']:'oneX',
59
- ['4']:'fourX'},
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 = "test #1, result is wrong, holes or length are wrong";
69
- thisStepResult = testArray[methodName]({collapseHoles:false, omitNonNumeric:true});
70
- thisStepEvalFunction = item => item=(item.length==5 && typeof(item[2])=='undefined' && typeof(item[3])=='undefined');
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(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
73
- passingTests;
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 = "test #2, result is wrong, has holes or length is wrong";
79
- thisStepResult = testArray[methodName]({collapseHoles:true, omitNonNumeric:true});
80
- thisStepEvalFunction = item => item=(item.length==3 && item.reduce((item, result)=>(typeof(item!='undefined') && result), true));
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(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
83
- passingTests;
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 = "test #3, result is wrong, non-numeric element is missing or length is wrong";
91
- thisStepResult = testArray[methodName]({collapseHoles:true, omitNonNumeric:false});
92
- thisStepEvalFunction = item => item=(item.length==4 && typeof(item[3])=='object');
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(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
95
- passingTests;
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 = "test #4, subordinate element was not converted properly";
104
- thisStepResult = testArray2[methodName]({collapseHoles:true, omitNonNumeric:false});
105
- thisStepEvalFunction = item => item=(item.length==5 && typeof(item[4])=='object' && item[4].value.length==3);
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(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
108
- passingTests;
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 = "test #5, empty object returns empty array";
114
- thisStepResult = emptyArray[methodName]({collapseHoles:true, omitNonNumeric:false});
115
- thisStepEvalFunction = item => item=(item.length===0);
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(methodName, thisStepMessage, thisStepResult, thisStepEvalFunction) &&
118
- passingTests;
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
 
@@ -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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qtools-functional-library",
3
- "version": "1.1.11",
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": {