-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtools-functional-library.js
271 lines (225 loc) · 6.43 KB
/
qtools-functional-library.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const commonFunctions = {
universalAddToPrototype: (commonFunctions, functionObject) => {
let methods = [];
let documentation = [];
let methodName;
let testList = [];
functionObject.forEach((functionItem, inx) => {
methodName = inx;
const supportedTypeList = functionItem.supportedTypeList
.map(item => item.toString().replace(/^function (.*?)\(.*$/, '$1'))
.join(', ')
.replace(/, $/, '');
if (functionItem.test && typeof functionItem.test == 'function') {
testList.push({ methodName, test: functionItem.test });
}
methods.push(methodName);
documentation.push({
name: methodName,
description: `${functionItem.description}`,
supportedTypeList
});
functionItem.supportedTypeList.forEach(target => {
if (typeof target.prototype[methodName] == 'undefined') {
Object.defineProperty(target.prototype, methodName, {
value: functionItem.method(commonFunctions),
writable: false,
enumerable: false
});
}
});
});
const testActual=methodName=>(args) => {
const tmp = testList.reduce((passingTests, testItem, inx) => {
if (typeof(args.methodList)!='undefined' && args.methodList.length>0 && !args.methodList.includes(methodName)){
return true;
}
const result=passingTests && testItem.test(args) ? true : false;
if (args.listTests){
console.log(`${methodName} ${result?'passed':'failed'}`);
}
return result //calls test method passed from functional module
}, true);
return tmp;
};
const test = testActual(methodName);
return {
methods,
documentation,
test
};
},
callerName: (calledFromQtLib = false, options={}) => {
if (typeof(options)!='object'){
throw(`qtools-functional-library.js internal error: options must be an object, got '${options}'`);
}
options.suffixCallStackDepth=options.suffixCallStackDepth?options.suffixCallStackDepth:3;
const index = calledFromQtLib ? options.suffixCallStackDepth : 2; //the 1 might need adjusting once this is made available to applications
return new Error().stack
.split(/\n/)[index]
.trim()
.replace(/at new moduleFunction/, "called from")
.replace(/Object\.<anonymous>/, 'not_in_function');
},
toType: function(obj) {
if (obj instanceof Map) {
return 'map';
}
if (obj instanceof Set) {
return 'set';
}
if (typeof obj == 'string') {
return 'string';
}
if (typeof obj == 'number') {
return 'number';
}
if (typeof obj == 'obj' && typeof length == 'number') {
return 'array';
}
if (obj === null) {
return 'null';
}
if (typeof obj == 'undefined') {
return 'undefined';
}
if (typeof obj == 'string') {
return 'string';
}
return {}.toString
.call(obj)
.match(/\s([a-z|A-Z]+)/)[1]
.toLowerCase();
},
byObjectProperty: function(fieldName, transformer) {
//called: resultArray=someArray.sort(qtools.byObjectProperty('somePropertyName'));
//based on closure of fieldName
var fullNameSort;
return (fullNameSort = function(a, b) {
var localFieldName = fieldName;
var localTransformer = transformer;
//for debug
if (typeof fieldName == 'function') {
var aa = a;
var bb = b;
transformer = fieldName;
} else {
var aa = a.qtGetSurePath(fieldName);
var bb = b.qtGetSurePath(fieldName);
}
if (typeof transformer == 'function') {
aa = transformer(aa);
bb = transformer(bb);
} else if (transformer) {
switch (transformer) {
case 'caseInsensitive':
aa = aa.toLowerCase();
bb = bb.toLowerCase();
break;
default:
console.log(
'qt.byObjectProperty says, No such transformer as: ' + transformer
);
break;
}
}
if (!bb && !aa) {
return 0;
}
if (!bb) {
return -1;
}
if (!aa) {
return 1;
}
if (aa > bb) {
return 1;
}
if (aa < bb) {
return -1;
}
return 0;
});
},
isSupportedType: (input, supportedTypeList) =>
supportedTypeList.reduce((result, supportedTypeItem) => {
return (
result || Object.getPrototypeOf(input) === supportedTypeItem.prototype
); //instoanceof does not work for strings and numbers
}, false)
};
// Array.prototype.remove = function(from, to) {
// var rest = this.slice((to || from) + 1 || this.length);
// this.length = from < 0 ? this.length + from : from;
// return this.push.apply(this, rest);
// };
String.prototype.toCamelCase = function(delimiter, pascalCase) {
var firstCharFunction = pascalCase
? function(v) {
return v.toUpperCase();
}
: function(v) {
return v.toLowerCase();
};
delimiter = delimiter ? delimiter : ' ';
return this.split(delimiter)
.map(function(word) {
var first = word.substring(0, 1);
word = word.replace(new RegExp(first), first.toUpperCase());
return word;
})
.join('')
.replace(/^(.)/, firstCharFunction);
};
const docList = [];
const testList = [];
const addMorePrototypes = () => {
const fs = require('fs');
const path = require('path');
const libDir = path.join(path.dirname(module.filename), 'lib');
const dirList = fs.readdirSync(libDir);
dirList.forEach(item => {
if (item.match(/^qtools/)) {
const moduleGen = require(path.join(libDir, item));
const module = new moduleGen({ commonFunctions });
const result = module.addToPrototype();
result.documentation && docList.push(result.documentation);
result.test && testList.push(result.test);
}
});
};
addMorePrototypes();
const helpActual = docList => (options={}) => {
const {printOutput=true, sendJson=false, queryString='.*'}=options;
let rawList;
if (!queryString) {
rawList = docList;
} else {
rawList = docList.filter(item => {
const regex = new RegExp(queryString, 'i');
const result = JSON.stringify(item).match(regex);
return result;
});
}
let outArray=[];
rawList.forEach(item=>{
outArray=outArray.concat(item);
});
const outString='<!name!>: <!description!> (<!supportedTypeList!>) '.qtTemplateReplace(outArray).join('\n');
if (printOutput){
console.log(outString);
}
return outString;
};
const testActual = testList => (args={}) => {
return testList.reduce((result, test) => {
if (typeof test == 'function') {
result = test(args) && result; //executes the framework testItem.test() above
}
return result;
}, true);
};
commonFunctions.qtLog_AddCallableMethods("what part of 'not for civilian use' is unclear?");
commonFunctions.help = helpActual(docList);
commonFunctions.test = testActual(testList);
module.exports = commonFunctions;