qtools-functional-library 1.1.11 → 1.1.13
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 +76 -26
- 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-template-replace/qtools-template-replace.js +4 -1
- package/package.json +1 -1
- package/qtools-functional-library.js +20 -9
package/README.md
CHANGED
|
@@ -1,28 +1,78 @@
|
|
|
1
|
-
**qtools-functional-library**
|
|
2
|
-
|
|
3
|
-
This is a collection of tools that allow chained operations on various Javascript datatypes.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
-
|
|
26
|
-
-
|
|
1
|
+
## **qtools-functional-library**
|
|
2
|
+
|
|
3
|
+
This is a collection of tools that allow chained operations on various Javascript datatypes.
|
|
4
|
+
|
|
5
|
+
In each case, the primary operand is `this`. In most cases, the function takes an argument with parameters to guide the operation.
|
|
6
|
+
|
|
7
|
+
**EG,**
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
const qt=require('qtools-functional-library');
|
|
11
|
+
|
|
12
|
+
qt.help(); // this information and more
|
|
13
|
+
|
|
14
|
+
const demo=({a:{b:"Hi from b"}})
|
|
15
|
+
|
|
16
|
+
const one=demo
|
|
17
|
+
.qtGetSurePath('a.b')
|
|
18
|
+
.qtDump();//produces 'Hi from b [thisFilename.js]'
|
|
19
|
+
|
|
20
|
+
console.log(one); //produces 'Hi from b'
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### **Non-chain utilities:**
|
|
24
|
+
|
|
25
|
+
- **qt.help**: Shows this information and more.
|
|
26
|
+
- **qt.log**: Writes log entry with suffix identifying the file that generated the output. (See qtLog below for chained version.)
|
|
27
|
+
|
|
28
|
+
### **Get/Set values from an Array:**
|
|
29
|
+
|
|
30
|
+
- **qtGetByProperty**: Operates on an array of objects. Retrieves one or more elements based on the value of an object property specified by a dotted path.
|
|
31
|
+
|
|
32
|
+
- **qtPop**: 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({}).
|
|
33
|
+
|
|
34
|
+
- **qtPush**: Pushes a value onto an array and returns the array (unlike normal .push() which returns the length of the array)
|
|
35
|
+
|
|
36
|
+
- **qtLast**: Delivers the element that would have been pop'd but does not change the array. Default if none exists.
|
|
37
|
+
|
|
38
|
+
- **qtFirst**: Delivers the element zero if it exists but does not change the array. Default if none exists.
|
|
39
|
+
|
|
40
|
+
- **qtIncludesRegex**: Operations on a simple array of strings. Retrieves one or more elements based on an regEx match.
|
|
41
|
+
|
|
42
|
+
### **Get/Set values in an Object (or array of Objects)**
|
|
43
|
+
|
|
44
|
+
- **qtGetSurePath**: Gets 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.
|
|
45
|
+
|
|
46
|
+
- **qtSetSurePath** Sets the value of a property based on a dotted string path. Creates intermediate elements if they are missing.
|
|
47
|
+
|
|
48
|
+
- **qtSelectProperties**: Given an Object and an Array of Strings, filters the properties into a new Object with defaults for missing elements. Does the same for an Array of Objects.
|
|
49
|
+
|
|
50
|
+
- **qtMapProperties**: Given an Object and a one level Object whose properties are functions, applies each function to the corresponding element. Does the same for an array of Objects.
|
|
51
|
+
|
|
52
|
+
### **Array/Object Conversions:**
|
|
53
|
+
|
|
54
|
+
- **qtClone**: Does a deep clone of an object or array.- qtNumberKeysToArray: Converts an object with properties that are numbers to an array, including subordinate properties.
|
|
55
|
+
|
|
56
|
+
- **qtToKeyValueArray**: Converts an object to an array of objects of the form, ie, `{["1"]:'one', ["2"]:'two'} => ['one', 'two']`
|
|
57
|
+
|
|
58
|
+
- **qtMerge**: Deep merge of two objects including array propterties.
|
|
59
|
+
|
|
60
|
+
- **qtToString**: Joins an array with prefix, suffix and separator. Pretty much the same as .join().
|
|
61
|
+
|
|
62
|
+
### **Array/Object Template Replacement:**
|
|
63
|
+
|
|
64
|
+
- **qtTemplateReplace**: Takes Object and a `<!template!>` String ()either can be 'this' or the argument). Substitutes from the object properties into the string. If the 'this' is an array of Objects, it does it to all of them.
|
|
65
|
+
|
|
66
|
+
### **Output:**
|
|
67
|
+
|
|
68
|
+
- **qtDump**: Prints 'this' and returns it. Good for debugging complicated chains.
|
|
69
|
+
|
|
70
|
+
- **qtLog**: Logs message by applying 'this' into the argument which is a <!template!> string. Appends calling file name.
|
|
71
|
+
|
|
72
|
+
### **Misc:**
|
|
73
|
+
|
|
74
|
+
- **qtPassThrough**: 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.
|
|
75
|
+
|
|
76
|
+
- **qtIterate**: Operates on a number. EG, (5).qtStart(7).qtIncrement(3).qtIterate(item=>item*100)
|
|
27
77
|
|
|
28
78
|
DESCRIPTION
|
|
@@ -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
|
|
|
@@ -8,7 +8,6 @@ var moduleFunction = function(args = {}) {
|
|
|
8
8
|
|
|
9
9
|
const baseReplacementFunction = (replaceObject, template, options) => {
|
|
10
10
|
const { transformations = {}, leaveUnmatchedTagsIntact = true } = options;
|
|
11
|
-
|
|
12
11
|
replaceObject = replaceObject.qtClone().qtMerge(transformations);
|
|
13
12
|
|
|
14
13
|
const tmp = template.match(/<!.*?!>/g);
|
|
@@ -45,6 +44,8 @@ var moduleFunction = function(args = {}) {
|
|
|
45
44
|
// prettier-ignore
|
|
46
45
|
const stringCallsObject = (item, arg) => arg instanceof Object && typeof item == 'string';
|
|
47
46
|
// prettier-ignore
|
|
47
|
+
const stringCallsArrayOfObjects = (item, arg) => arg instanceof Array && typeof item == 'string';
|
|
48
|
+
// prettier-ignore
|
|
48
49
|
const arrayOfStringsCallsObject = (item, arg) => arg instanceof Object && item instanceof Array && item.filter(item => typeof item == 'string').qtPop(false) ? true : false;
|
|
49
50
|
|
|
50
51
|
//first method definition function ==========================
|
|
@@ -69,6 +70,8 @@ var moduleFunction = function(args = {}) {
|
|
|
69
70
|
return this.map(item => baseReplacementFunction(item, arg, options));
|
|
70
71
|
} else if (objectCallsString(this, arg)) {
|
|
71
72
|
return baseReplacementFunction(this, arg, options);
|
|
73
|
+
} else if (stringCallsArrayOfObjects(this, arg)) {
|
|
74
|
+
return arg.map(item => baseReplacementFunction(item, this, options));
|
|
72
75
|
} else if (stringCallsObject(this, arg)) {
|
|
73
76
|
return baseReplacementFunction(arg, this, options);
|
|
74
77
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qtools-functional-library",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.13",
|
|
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": {
|
|
@@ -19,7 +19,7 @@ const commonFunctions = {
|
|
|
19
19
|
methods.push(methodName);
|
|
20
20
|
documentation.push({
|
|
21
21
|
name: methodName,
|
|
22
|
-
description: `${
|
|
22
|
+
description: `${functionItem.description}`,
|
|
23
23
|
supportedTypeList
|
|
24
24
|
});
|
|
25
25
|
|
|
@@ -226,21 +226,32 @@ const addMorePrototypes = () => {
|
|
|
226
226
|
|
|
227
227
|
addMorePrototypes();
|
|
228
228
|
|
|
229
|
-
const helpActual = docList => (
|
|
230
|
-
|
|
231
|
-
|
|
229
|
+
const helpActual = docList => (options={}) => {
|
|
230
|
+
|
|
231
|
+
const {printOutput=true, sendJson=false, queryString='.*'}=options;
|
|
232
|
+
|
|
233
|
+
let rawList;
|
|
232
234
|
if (!queryString) {
|
|
233
|
-
|
|
235
|
+
rawList = docList;
|
|
234
236
|
} else {
|
|
235
|
-
|
|
237
|
+
rawList = docList.filter(item => {
|
|
236
238
|
const regex = new RegExp(queryString, 'i');
|
|
237
239
|
const result = JSON.stringify(item).match(regex);
|
|
238
240
|
return result;
|
|
239
241
|
});
|
|
240
242
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
243
|
+
|
|
244
|
+
let outArray=[];
|
|
245
|
+
rawList.forEach(item=>{
|
|
246
|
+
outArray=outArray.concat(item);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
const outString='<!name!>: <!description!> (<!supportedTypeList!>) '.qtTemplateReplace(outArray).join('\n');
|
|
250
|
+
|
|
251
|
+
if (printOutput){
|
|
252
|
+
console.log(outString);
|
|
253
|
+
}
|
|
254
|
+
return outString;
|
|
244
255
|
};
|
|
245
256
|
|
|
246
257
|
const testActual = testList => (args={}) => {
|