qtools-functional-library 1.1.10 → 1.1.11

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
@@ -20,4 +20,9 @@ Most of the functions included in this library (except, eg, qt.log()) are implem
20
20
  - qtools-to-key-value-array
21
21
  - qtools-to-string
22
22
 
23
+ NEW:
24
+
25
+ - qtools-select-properties
26
+ - qtools-map-properties
27
+
23
28
  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
+
@@ -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';
@@ -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.10",
3
+ "version": "1.1.11",
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 -verbose -logErrors",
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);