1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | "use strict";
|
24 |
|
25 | var logger = require( 'log4js' ).getLogger();
|
26 |
|
27 | var S = require('singleton').get();
|
28 | S.results = [ ];
|
29 | S.deep = [ ];
|
30 |
|
31 | function bonk (los) {
|
32 | return los.filter( function (s) {
|
33 | if (typeof s != 'string') {
|
34 | return JSON.stringify(s, null, 2);
|
35 | }
|
36 | else {
|
37 | return s;
|
38 | }
|
39 | } ).join( '\n\t' );
|
40 | }
|
41 |
|
42 | function testwrapper (thing, target) {
|
43 | if (thing.test && ((typeof thing.test) == 'function')) {
|
44 | return thing.test( target );
|
45 | }
|
46 | else if ((typeof thing) == 'function') {
|
47 | return thing( target );
|
48 | }
|
49 | else {
|
50 | logger.error( 'Failing on confusing object from testwrapper.' );
|
51 | return new Error();
|
52 | }
|
53 | }
|
54 |
|
55 | function arrayhandler (a) {
|
56 |
|
57 |
|
58 | if ( Object.prototype.toString.call( a ).substr(8, 5) === 'Array' ) {
|
59 |
|
60 |
|
61 | logger.debug( 'Encountered array, pushing ' + a.length + ' elements onto stack.' );
|
62 | a.forEach( function (subelement) { S.deep.push( subelement ) } );
|
63 | return true;
|
64 | }
|
65 | }
|
66 |
|
67 | exports.deeply = function ( deep, test, parameters ) {
|
68 | if (S.deep.length > 0) {
|
69 | logger.error( 'Called deeply when sdeep stack not empty.' );
|
70 | }
|
71 | else {
|
72 |
|
73 |
|
74 | S.deep = deep;
|
75 | S.results = [ ];
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | for (; function (t) {
|
85 | if (S.deep.length > 0) {
|
86 | var element = S.deep.shift();
|
87 | logger.debug( 'Shifted sdeep, ' + S.deep.length + ' elements remain.' );
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | if (arrayhandler( element )) { return true }
|
93 |
|
94 |
|
95 |
|
96 | if ((typeof element == 'object') && (Object.keys( element ).length > 0)) {
|
97 |
|
98 |
|
99 |
|
100 | Object.keys( element ).forEach( function (key) {
|
101 | var matched = false;
|
102 | var match = undefined;
|
103 | if (parameters['check-keys']) {
|
104 | if (testwrapper(test, key)) {
|
105 | matched = true;
|
106 | match = key;
|
107 | }
|
108 | }
|
109 | if (parameters['check-values']) {
|
110 | if (testwrapper(test, element[key])) {
|
111 | matched = true;
|
112 | match = element[key];
|
113 | }
|
114 | }
|
115 | if (matched == true) {
|
116 | if (parameters['return-hash-tuples']) {
|
117 | match = element;
|
118 | }
|
119 | logger.debug( 'Found a hashlike match.' );
|
120 | S.results.push( match );
|
121 | }
|
122 | else {
|
123 | logger.debug( 'Discarded a hashlike match.' );
|
124 | }
|
125 | } );
|
126 | }
|
127 |
|
128 | if (testwrapper(test, element)) {
|
129 | logger.debug( 'Test accepted ' , element );
|
130 | S.results.push( element )
|
131 | }
|
132 | else {
|
133 | logger.debug( 'Test rejected ' , element );
|
134 | }
|
135 | }
|
136 | else {
|
137 | logger.debug( 'Finished sdeep.' );
|
138 | return false;
|
139 | }
|
140 | return true;
|
141 | }( test ); ) { }
|
142 |
|
143 | return S.results;
|
144 | }
|
145 |
|
146 |
|
147 |
|
148 | exports.flatten = function ( list, behavior ) {
|
149 | var flat = [ ]
|
150 | , local = list;
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | for (; local.length; ) {
|
158 | var element = local.shift();
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | if ( Object.prototype.toString.call( element ).substr(8, 5) === 'Array' ) {
|
172 | element.forEach( function (subelement) { local.push( subelement ) } );
|
173 | }
|
174 | else {
|
175 | flat.push( element );
|
176 | }
|
177 | }
|
178 |
|
179 | return flat;
|
180 |
|
181 | }
|
182 |
|
183 |
|
184 |
|
185 | exports.unique = function ( list ) {
|
186 | var flist = exports.flatten( list )
|
187 | , utable = { }
|
188 |
|
189 | flist.forEach( function (k) { if (utable[k]) { utable[k]++ } else { utable[k] = 1 } } );
|
190 |
|
191 | return Object.keys( utable );
|
192 | }
|
193 |
|
194 |
|
195 |
|
196 | exports.sync = function ( List, expression ) {
|
197 |
|
198 |
|
199 | var retval = [ ];
|
200 | if ((typeof expression) == 'object') {
|
201 |
|
202 |
|
203 | if (expression.test && ((typeof expression.test) == 'function')) {
|
204 |
|
205 |
|
206 |
|
207 | retval = List.filter( function (t) {
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | return expression.test(t);
|
217 | } );
|
218 | return retval;
|
219 | }
|
220 | }
|
221 | else if ((typeof expression) == 'function') {
|
222 | retval = List.filter( expression );
|
223 | return retval;
|
224 | }
|
225 | else {
|
226 | return new Error( 'Sorry, unclear what ' + expression + ' is.' );
|
227 | }
|
228 | }
|
229 |
|
230 |
|
231 |
|
232 | exports.async = function ( List, expression, Callback ) {
|
233 | var q = require('q')
|
234 | , deferred = q.defer()
|
235 | , retval = [ ];
|
236 |
|
237 | if ((typeof expression) == 'object') {
|
238 |
|
239 |
|
240 | if (expression.test && ((typeof expression.test) == 'function')) {
|
241 | return q.all( function () {
|
242 | retval = List.filter( expression.test )
|
243 | deferred.resolve( retval );
|
244 | return deferred.promise;
|
245 | } ).then( function (results) {
|
246 | return results;
|
247 | } );
|
248 | }
|
249 | }
|
250 | else if (typeof expression === 'function') {
|
251 | return q.all( function () {
|
252 | retval = List.filter( expression );
|
253 | deferred.resolve( retval );
|
254 | return deferred.promise;
|
255 | } ).then( function (results) {
|
256 | return Callback( results );
|
257 | } );
|
258 | }
|
259 | else {
|
260 | return new Error( 'Sorry, unclear what ' + expression + ' is.' );
|
261 | }
|
262 | }
|
263 |
|
264 |
|
265 |
|
266 | exports.in = function ( List, Test ) {
|
267 | var yes = false;
|
268 |
|
269 | List.forEach( function (t) {
|
270 | if (t == Test) {
|
271 | yes = true;
|
272 | }
|
273 | } )
|
274 |
|
275 | return yes;
|
276 |
|
277 | }
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | exports.all_in = function ( List, Test_List ) {
|
284 | var results = [ ];
|
285 | List.forEach( function (record) {
|
286 | exports.sync( Test_List, function (t) { if (t == record) { return true } } )
|
287 | .forEach( function (r) { results.push( r ) } )
|
288 | } );
|
289 | return results;
|
290 | }
|