@ohos-ports/deep-grep 0.0.0-stage → 0.4.2-beta.1

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/Changelog.md ADDED
@@ -0,0 +1,23 @@
1
+ 0.4.x
2
+ ====
3
+
4
+ * Added support for multi-session grep (allows for concurrency)
5
+ * Cleaned up module.exports
6
+ * Added token unit tests
7
+ * Deprecated `sync()` and `async()` methods
8
+ * Added jsdoc
9
+
10
+ 0.3.x
11
+ ====
12
+
13
+ * Add support for hash tuple checks
14
+ * Add strictures
15
+ * Add log4j logging
16
+ * Properly implemented singleton
17
+ * Add substantial edge case checking
18
+ * Add add'l unit tests
19
+
20
+ 0.2.x
21
+ ====
22
+
23
+ * Fold in [jgrep](https://github.com/janearc/jagrep)
package/LICENSE.md ADDED
@@ -0,0 +1,28 @@
1
+ The author waives copyright and related rights in the work
2
+ worldwide through the CC0 1.0 Universal public domain dedication.
3
+
4
+ ## CC0 1.0 Universal Summary
5
+
6
+ This is a human-readable summary of the [Legal Code (read the full text)](https://creativecommons.org/publicdomain/zero/1.0/legalcode).
7
+
8
+ ### No Copyright
9
+
10
+ The person who associated a work with this deed has dedicated the work to
11
+ the public domain by waiving all of his or her rights to the work worldwide
12
+ under copyright law, including all related and neighboring rights, to the
13
+ extent allowed by law.
14
+
15
+ You can copy, modify, distribute and perform the work, even for commercial
16
+ purposes, all without asking permission.
17
+
18
+ ### Other Information
19
+
20
+ In no way are the patent or trademark rights of any person affected by CC0,
21
+ nor are the rights that other persons may have in the work or in how the
22
+ work is used, such as publicity or privacy rights.
23
+
24
+ Unless expressly stated otherwise, the person who associated a work with
25
+ this deed makes no warranties about the work, and disclaims liability for
26
+ all uses of the work, to the fullest extent permitted by applicable law.
27
+ When using or citing the work, you should not imply endorsement by the
28
+ author or the affirmer.
package/README.md CHANGED
@@ -1,3 +1,102 @@
1
- # Temporary Holding Version
1
+ deep-grep
2
+ =========
2
3
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
4
+ Too many haystacks, not enough needles — or, "when you have a grep,
5
+ everything looks like a list."
6
+
7
+ ```javascript
8
+ var needles = dg.deeply( [
9
+ foo, [
10
+ bar, baz, [
11
+ bletch, [
12
+ qip
13
+ ]
14
+ ]
15
+ ] ], function (t) { if t == qip return t }
16
+ )
17
+ ```
18
+
19
+ usage
20
+ ====
21
+
22
+ For grep on simple, flat (non-nested) lists:
23
+
24
+ * `dg.in()`
25
+ - `dg.in( list, 'value' )` - returns `true` or `false` depending upon
26
+ whether `list` contains `value`.
27
+ * `dg.all_in()`
28
+ - `dg.all_in( list_a, list_b )` - returns a list of all values in `list_a`
29
+ that exist in `list_b`.
30
+ * `dg.unique()`
31
+ - `dg.unique( list )` - returns all unique values of `list`. Note: flattens.
32
+ * `dg.flatten( )`
33
+ - `dg.flatten( nested_list )` - returns a list of all the lists contained in
34
+ `nested_list`, concatenated into a single scope.
35
+
36
+ For doing greppy things on nested lists:
37
+
38
+ ```javascript
39
+ var haystack = [
40
+ 'zebra', 'lion', [
41
+ 'tiger', 'unicorn', 'emperor penguin'
42
+ ],
43
+ 'leprechaun',
44
+ 'cockerel'
45
+ ];
46
+
47
+ var needles = dg.deeply( list, function (t) {
48
+ if (new RegExp( ('(unicorn|leprechaun)s?' ).test(t))) return true
49
+ } );
50
+ ```
51
+
52
+ For more complex data, options are available to you:
53
+
54
+ ```javascript
55
+ var needles = dg.deeply( arks['Noah'], function (t) { ... }, {
56
+ // Any of these may be defined
57
+ //
58
+
59
+ // If items in the provided object have methods, check these methods for
60
+ // return values that evaluate as true in the provided function.
61
+ //
62
+ 'check-object-methods': [ 'get_species', 'list_passengers' ],
63
+
64
+ // Provide optionally as argument to calls from check-object-methods, above
65
+ //
66
+ 'object-method-arguments': [ argument, function, 'etc' ],
67
+
68
+ // Return hash keys that evaluate true also?
69
+ // default: false
70
+ //
71
+ 'check-keys': true,
72
+
73
+ // Return hash values that evaluate true?
74
+ // default: false
75
+ //
76
+ 'check-values': true,
77
+
78
+ // Return key/value tuples rather than just keys or just values?
79
+ // default: false
80
+ //
81
+ 'return-hash-tuples': true,
82
+ } );
83
+ ```
84
+
85
+ deprecated methods
86
+ ====
87
+
88
+ * `dg.sync()`
89
+ - `dg.sync( list, expression )` - evaluates `list`, returning a new list of every
90
+ element that matches `expression.test` (like `RegExp`).
91
+ - `dg.sync( list, func )` - same as above, only executes `func` for every element
92
+ of list, returning a new list.
93
+ * `dg.async()`
94
+ - `dg.async( list, expression )` - same as with `sync()`, above, only
95
+ returns a promise to the list of matches.
96
+ - `dg.async( list, func, callback )` - same as `sync()`, above, except the
97
+ callback is called with a promise to the list of matches.
98
+
99
+ author
100
+ ====
101
+
102
+ [@janearc](https://github.com/janearc), jane@cpan.org
package/extend.js ADDED
@@ -0,0 +1,11 @@
1
+ exports._extend = function(origin, add) {
2
+ // Don't do anything if add isn't an object
3
+ if (!add || !isObject(add)) return origin;
4
+
5
+ var keys = Object.keys(add);
6
+ var i = keys.length;
7
+ while (i--) {
8
+ origin[keys[i]] = add[keys[i]];
9
+ }
10
+ return origin;
11
+ };
package/lib/dg.js ADDED
@@ -0,0 +1,499 @@
1
+ /**
2
+ * @file deep grep
3
+ * @author Jane Arc <jane@cpan.org>
4
+ *
5
+ * @overview if you're like me, you like data structures and you don't mind
6
+ * them being nested to various levels and of various types.
7
+ *
8
+ * sometimes, though, when you want something deep in one of these
9
+ * structures, it can be a pain in the ass to ask it for something deeply-
10
+ * nested.
11
+ *
12
+ * sometimes you just want the things and you don't want to have
13
+ * to describe your data structure or nested object methods in excruciating
14
+ * detail to go and find them.
15
+ *
16
+ * also i get sick of the amount of nested scoping node does, and this
17
+ * allows me to abstract some of that away into a lib.
18
+ *
19
+ * @example
20
+ * var haystack = [ foo, [ bar, baz, [ bletch, [ qip ] ] ] ];
21
+ * var needles = dg.deeply( haystack, function (t) {
22
+ * if (t == qip) { return true }
23
+ * } );
24
+ */
25
+
26
+ 'use strict';
27
+
28
+ /**
29
+ * @description Perform a search through an arbitrarily-nested/complex data structure.
30
+ *
31
+ * @param {Array} deep the "target" list to search through; may be nested/contain complex data
32
+ * @param {Function|RegExp} test a function or regular expression to evaluate items in list
33
+ * @param {Object} parameters various configuration options for the operation
34
+ * @param {Boolean} parameters.check-object-methods evalute object method return values
35
+ * @param {Boolean} parameters.object-method-arguments parameters to pass object methods
36
+ * @param {Boolean} parameters.check-keys check object keys
37
+ * @param {Boolean} parameters.check-values check object values
38
+ * @param {Boolean} parameters.return-hash-tuples return tuples instead of keys or values
39
+ * @param {Boolean} parameters.return-promise return a promise to the results instead of a list.
40
+ * @param {Object} session for concurrency, a session object may be supplied
41
+ *
42
+ * @returns {Array|Promise} an promised array of objects with the requested fields/members.
43
+ *
44
+ * @example
45
+ * // finding a user in a big list of users
46
+ * //
47
+ * var user = dg.deeply( list_with_nested_hashes, function (t) {
48
+ * if (t == username) { return true }
49
+ * }, { 'return-hash-tuples': true, 'check-keys': true, 'check-values': false } )
50
+ *
51
+ */
52
+ function deeply (deep, test, parameters, session) { // {{{
53
+ var xact;
54
+
55
+ // Attempt to discern whether we are running multiple deeply() sessions,
56
+ // if we are, check to see that we have a session object. If not, create
57
+ // a new session transparently to the caller.
58
+ //
59
+ if (session) {
60
+ xact = session;
61
+ }
62
+ else {
63
+ xact = singlesession();
64
+ if (!xact) {
65
+ return logwrap.error( 'Please provide a session object if you are going to run multiple deeply() sessions.' );
66
+ }
67
+ }
68
+
69
+ var g = heap.sessions[ xact.serial ];
70
+
71
+ for (; function (t) {
72
+ if (g.deep.length > 0) {
73
+
74
+ // Indicate we have made a first pass through deep
75
+ //
76
+ g.fresh = false;
77
+
78
+ var element = g.deep.shift();
79
+ logwrap.debug( 'Shifted sdeep, '.concat( g.deep.length, ' elements remain.' ) );
80
+
81
+ // Handle arrays, break if this is something to explode and handle
82
+ // later.
83
+ //
84
+ if (arrayhandler( element, xact )) { return true }
85
+
86
+ // Inspect hashlike elements
87
+ //
88
+ if ((typeof element == 'object') && (Object.keys( element ).length > 0)) {
89
+ // Looks like it's a hashlike thingie. Check our parameters, behave
90
+ // accordingly.
91
+ //
92
+ Object.keys( element ).forEach( function (key) {
93
+ var matched = false;
94
+ var match = undefined;
95
+ if (parameters['check-keys']) {
96
+ if (testwrapper(test, key)) {
97
+ matched = true;
98
+ match = key;
99
+ }
100
+ }
101
+ if (parameters['check-values']) {
102
+ if (testwrapper(test, element[key])) {
103
+ matched = true;
104
+ match = element[key];
105
+ }
106
+ }
107
+ if (matched == true) {
108
+ if (parameters['return-hash-tuples']) {
109
+ match = element;
110
+ }
111
+ logwrap.debug( 'Found a hashlike match.' );
112
+ g.results.push( match );
113
+ }
114
+ else {
115
+ logwrap.debug( 'Discarded a hashlike match.' );
116
+ }
117
+ } );
118
+ }
119
+
120
+ if (testwrapper(test, element)) {
121
+ logwrap.debug( 'Test accepted ' , element );
122
+ g.results.push( element )
123
+ }
124
+ else {
125
+ logwrap.debug( 'Test rejected ' , element );
126
+ }
127
+ }
128
+ else {
129
+ if (g.fresh == true) {
130
+ // g.deep is fresh, let's add some values to it
131
+ //
132
+ g.deep.push( deep );
133
+ return true;
134
+ }
135
+ else {
136
+ // g.deep.length is false because it's exhausted
137
+ //
138
+ logwrap.debug( 'Finished deeply().' );
139
+ xit.end( xact );
140
+ delete heap.sessions[ xact.serial ];
141
+ return false;
142
+ }
143
+ }
144
+ return true;
145
+ }( test ); ) { } // for
146
+
147
+ return g.results;
148
+ } // }}}
149
+
150
+ /**
151
+ * @description Flatten the nested elements of a list into a single scope
152
+ *
153
+ * @param {Array} deep the "target" list to flatten; may be nested/contain complex data
154
+ *
155
+ * @returns {Array} a "flattened" array containing the elements in deep
156
+ */
157
+ function flatten (list, behavior) { // {{{
158
+ var flat = [ ]
159
+ , local = list;
160
+
161
+ // Rather than recurse through ourselves over and over, we are going to
162
+ // invoke 'for' here in a possibly-rude way, basically as a 'while' with
163
+ // a break, and just explode everything in list that is also a list back
164
+ // onto the stack (local), and continue to walk it until it is extinguished.
165
+ //
166
+ for (; local.length; ) {
167
+ var element = local.shift();
168
+
169
+ // So reference the 'complex data' documentation here:
170
+ //
171
+ // https://github.com/janearc/deep-grep/blob/master/README.md#usage
172
+ //
173
+ // the 'behavior' hash, above, will determine what we actually do
174
+ // during the flatten operation. For the moment (<= 0.0.2), it is unused
175
+ // and lists are assumed.
176
+ //
177
+
178
+ // This is very suspicious but when I change it to typeof, it breaks, and
179
+ // I CBA to figure out what is wrong with it right now.
180
+ //
181
+ if ( Object.prototype.toString.call( element ).substr(8, 5) === 'Array' ) {
182
+ element.forEach( function (subelement) { local.push( subelement ) } );
183
+ }
184
+ else {
185
+ flat.push( element );
186
+ }
187
+ }
188
+
189
+ return flat;
190
+
191
+ } // }}}
192
+
193
+ /**
194
+ * @description Return the unique elements of a (optionally nested) list
195
+ *
196
+ * @param {Array} deep the "target" list; may be nested/contain complex data
197
+ *
198
+ * @returns {Array} a list of the unique elements in deep
199
+ */
200
+ function unique (list) { // {{{
201
+ var flist = flatten( list )
202
+ , utable = { }
203
+
204
+ flist.forEach( function (k) { if (utable[k]) { utable[k]++ } else { utable[k] = 1 } } );
205
+
206
+ return Object.keys( utable );
207
+ } // }}}
208
+
209
+ // Perform a synchronous (no-promise, blocking) grep
210
+ //
211
+ function sync (list, expression) { // {{{
212
+ var retval = [ ];
213
+ if ((typeof expression) == 'object') {
214
+ // Looks like they sent us an object. Verify it has test()
215
+ //
216
+ if (expression.test && ((typeof expression.test) == 'function')) {
217
+ // We allow them to send us any object that can test. We call this
218
+ // polymorphism. It's a good thing.
219
+ //
220
+ retval = list.filter( function (t) {
221
+ // Without the wrapper above, we get:
222
+ //
223
+ // TypeError: Method RegExp.prototype.test called on incompatible receiver undefined
224
+ //
225
+ // which is .. it seems to be an error in their library. There's
226
+ // implicit coercion to strings with the passed function wrapper
227
+ //
228
+ return expression.test(t);
229
+ } );
230
+ return retval;
231
+ }
232
+ }
233
+ else if ((typeof expression) == 'function') {
234
+ retval = list.filter( expression );
235
+ return retval;
236
+ }
237
+ else {
238
+ return logwrap.error( 'Unclear what '.concat( expression, ' is.' ) );
239
+ }
240
+ } // }}}
241
+
242
+ // Perform an asynchronous (promise, but probably still blocking) grep
243
+ //
244
+ function async (list, expression, callback) { // {{{
245
+ var q = require('q')
246
+ , deferred = q.defer()
247
+ , retval = [ ];
248
+
249
+ if ((typeof expression) == 'object') {
250
+ // Looks like they sent us an object. Verify it has exec()
251
+ //
252
+ if (expression.test && ((typeof expression.test) == 'function')) {
253
+ return q.all( function () {
254
+ retval = list.filter( expression.test )
255
+ deferred.resolve( retval );
256
+ return deferred.promise;
257
+ } ).then( function (results) {
258
+ return results;
259
+ } );
260
+ }
261
+ }
262
+ else if (typeof expression === 'function') {
263
+ return q.all( function () {
264
+ retval = list.filter( expression );
265
+ deferred.resolve( retval );
266
+ return deferred.promise;
267
+ } ).then( function (results) {
268
+ return callback( results );
269
+ } );
270
+ }
271
+ else {
272
+ return logwrap.error( 'Unclear what '.concat( expression, ' is.' ) );
273
+ }
274
+ } // }}}
275
+
276
+ // Just figure out whether test exists in list
277
+ //
278
+ function list_in (list, test) { // {{{
279
+ var yes = false;
280
+
281
+ list.forEach( function (t) {
282
+ if (t == test) {
283
+ yes = true;
284
+ }
285
+ } )
286
+
287
+ return yes;
288
+ } // }}}
289
+
290
+ // Return all the values of list that are also in test_list ('intersection')
291
+ // It's not really super complicated but it is a nice idiom to have a shorthand
292
+ // for.
293
+ //
294
+ function all_in (list, test_list) { // {{{
295
+ var results = [ ];
296
+ list.forEach( function (record) {
297
+ sync( test_list, function (t) { if (t == record) { return true } } )
298
+ .forEach( function (r) { results.push( r ) } )
299
+ } );
300
+ return results;
301
+ } // }}}
302
+
303
+ /**
304
+ * @description Coalesce two objects into one
305
+ *
306
+ * @param {Object} from keys/values to read "from" into the coalesce
307
+ * @param {Object} to the "target" object to coalesce into
308
+ * @param {Object} parameters
309
+ * @param {Boolean} paramaters.return-clone return a cloned object rather than operating on 'to'
310
+ * @param {Array} paramaters.keys the list of keys to operate on (optional)
311
+ *
312
+ * @returns {Object} the coalesced object
313
+ *
314
+ * @example
315
+ * var coalesced = dg.coalesce(
316
+ * from, to,
317
+ * {
318
+ * keys: ['key1', 'key2'],
319
+ * 'return-clone': true
320
+ * }
321
+ * )
322
+ */
323
+ function coalesce_object (from, to, parameters) { // {{{
324
+ var target;
325
+ if (parameters['return-clone']) {
326
+ target = clone( to );
327
+ }
328
+ else {
329
+ target = to;
330
+ }
331
+ if (parameters.keys && parameters.keys.length) {
332
+ parameters.keys.forEach( function (key) {
333
+ target[key] = from[key]
334
+ } )
335
+ }
336
+ else {
337
+ Object.keys( from ).forEach( function (key) {
338
+ target[key] = from[key];
339
+ })
340
+ }
341
+ return target;
342
+ } // }}}
343
+
344
+ module.exports = {
345
+ 'in' : list_in, // 'in' is a reserved word, kids
346
+ 'all_in' : all_in,
347
+ 'async' : async,
348
+ 'sync' : sync,
349
+ 'deeply' : deeply,
350
+ 'unique' : unique,
351
+ 'flatten' : flatten,
352
+
353
+ 'clone' : clone,
354
+ 'extend' : extend,
355
+
356
+ 'coalesce' : coalesce_object,
357
+ 'coalesce_object' : coalesce_object
358
+ }
359
+
360
+
361
+ // NOTHING BELOW THIS LINE EXPORTED
362
+ //
363
+
364
+ function arrayhandler (a, xact) { // {{{
365
+ // This is suspicious but I don't remember why I did it.
366
+ //
367
+ if ( Object.prototype.toString.call( a ).substr(8, 5) === 'Array' ) {
368
+ // Explode arrays
369
+ //
370
+ logwrap.debug( 'Encountered array, pushing '.concat( a.length, ' elements onto stack.' ) );
371
+ a.forEach( function (subelement) { heap.sessions[ xact.serial ].deep.push( subelement ) } );
372
+ return true;
373
+ }
374
+ } // }}}
375
+
376
+ // This is stolen/paraphrased from io.js
377
+ //
378
+ function extend (origin, add) { // {{{
379
+ // Don't do anything if add isn't an object
380
+ //
381
+ if (!add || (typeof add != 'object')) {
382
+ return origin;
383
+ }
384
+
385
+ Object.keys( add ).forEach( function (key) {
386
+ origin[key] = add[key]
387
+ } );
388
+ return origin;
389
+ } // }}}
390
+
391
+ // Sugar on top of extend for clones
392
+ //
393
+ function clone (thing) { // {{{
394
+ return extend( {}, thing );
395
+ } // }}}
396
+
397
+ function testwrapper (thing, target) { // {{{
398
+ if (thing.test && ((typeof thing.test) == 'function')) {
399
+ return thing.test( target );
400
+ }
401
+ else if ((typeof thing) == 'function') {
402
+ return thing( target );
403
+ }
404
+ else {
405
+ logwrap.error( 'Failing on confusing object from testwrapper.' );
406
+ return new Error();
407
+ }
408
+ } // }}}
409
+
410
+ function singlesession () { // {{{
411
+ if (Object.keys( heap.sessions ).length == 1) {
412
+ // There's already a session running, go grab it for the user.
413
+ //
414
+ var solo = Object.keys( heap.sessions )[0]
415
+ , xact = xit.get( solo );
416
+
417
+ return xact;
418
+ }
419
+ else if (Object.keys( heap.sessions ).length == 0) {
420
+ // Set up a new session for them
421
+ //
422
+ var xact = new xit.xact();
423
+
424
+ logwrap.debug( 'New root xid is '.concat( xact.serial ) );
425
+
426
+ var sessions = heap.sessions
427
+ , serial = xact.serial;
428
+
429
+ sessions[serial] = { };
430
+
431
+ heap.sessions[ xact.serial ].results = [ ]; // the results we will be returning
432
+ heap.sessions[ xact.serial ].deep = [ ]; // the to-search pile
433
+ heap.sessions[ xact.serial ].fresh = true // this is a new, fresh, session
434
+
435
+ xit.add( xact );
436
+
437
+ return xact;
438
+ }
439
+ else if (Object.keys( heap.sessions ).length > 1) {
440
+ // This is not a single-session instance
441
+ //
442
+ return false;
443
+ }
444
+ else {
445
+ // Someone is funning us
446
+ //
447
+ return logwrap.error( 'Inconsistent session status.' );
448
+ }
449
+ } // }}}
450
+
451
+ var log4js = require( 'log4js' ); // {{{
452
+
453
+ var config = {
454
+ "appenders": [
455
+ {
456
+ "type": "console",
457
+ "layout": {
458
+ "type": "pattern",
459
+ 'pattern': '%d{ABSOLUTE} [%[%5.5p%]] [%12c] - %m',
460
+ "tokens": {
461
+ "pid" : function() { return process.pid; }
462
+ }
463
+ }
464
+ }
465
+ ]
466
+ };
467
+
468
+ log4js.configure( config, {} );
469
+ var logger = log4js.getLogger( 'deep-grep' )
470
+ , logwrap = {
471
+ debug : function (s) {
472
+ if (process.env.DEBUG != undefined) { logger.debug(s) }
473
+ },
474
+ info : function (s) {
475
+ if (process.env.DEBUG != undefined) { logger.info(s) }
476
+ },
477
+ warn : function (s) {
478
+ if (process.env.DEBUG != undefined) { logger.warn(s) }
479
+ },
480
+ error : function (s) {
481
+ if (process.env.DEBUG != undefined) { logger.error(s); return new Error(s) }
482
+ },
483
+ };
484
+
485
+ // }}}
486
+
487
+ var heap = {
488
+ sessions: { },
489
+ };
490
+
491
+ logwrap.debug( 'Heap initialised.' );
492
+
493
+ var xit = require( 'xact-id-tiny' );
494
+ xit.start();
495
+
496
+ logwrap.debug( 'Transaction book spun up.' );
497
+
498
+
499
+ // @janearc // jane@cpan.org // vim:tw=80:ts=2:noet
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@ohos-ports/deep-grep",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.4.2-beta.1",
4
+ "description": "Scours arbitrarily-nested objects finding the things you want.",
5
+ "main": "lib/dg.js",
6
+ "directories": {
7
+ "test": "test"
8
+ },
9
+ "scripts": {
10
+ "test": "mocha"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/ohos-ports/ohos-ports.git",
15
+ "directory": "ports/deep-grep/0.4.2"
16
+ },
17
+ "keywords": [
18
+ "grep",
19
+ "recursive",
20
+ "recursion",
21
+ "synchronous",
22
+ "nested",
23
+ "map",
24
+ "filter"
25
+ ],
26
+ "author": "Jane Arc <jane.arc@gsa.gov> (http://github.com/janearc)",
27
+ "license": "CC0-1.0",
28
+ "bugs": {
29
+ "url": "https://github.com/ohos-ports/ohos-ports/issues"
30
+ },
31
+ "dependencies": {
32
+ "mocha": "*",
33
+ "chai": "*",
34
+ "q": "^1.5.1",
35
+ "xact-id-tiny": ">=0.0.6",
36
+ "log4js": "^1.1.1"
37
+ },
38
+ "homepage": "https://github.com/janearc/deep-grep"
39
+ }
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+
3
+ var assert = require( 'assert' )
4
+ , chai = require( 'chai' )
5
+ , dg
6
+
7
+ , simple_list = function () { return [ 'foo', 'unicorn', 'bar', 'baz' ] }
8
+ , nested_list = function () { return [ 'foo', [ 'unicorn', 'bar', [ 'leprechaun' ] ], 'baz' ] }
9
+
10
+ , mythical = function () { [ 'unicorn', 'leprechaun', 'griffin', 'sphinx', 'centaur', 'dragon', 'siren', 'banshee' ] }
11
+
12
+ it( 'syntax', function () {
13
+ assert( function () { return dg = require( '../lib/dg.js' ) }, 'require' );
14
+ } );
15
+
16
+ it( 'simple list', function () {
17
+ dg = require( '../lib/dg.js' );
18
+
19
+ assert( function () {
20
+ var uni = dg.sync( simple_list(), function (t) { if (t == 'unicorn') return true } );
21
+ if (uni.length != 1) return false;
22
+ if (uni[0] != 'unicorn') return false;
23
+ return true;
24
+ }, 'returns requisite element' );
25
+
26
+ } );
27
+
@@ -0,0 +1,23 @@
1
+ var assert = require('assert');
2
+ var dg = require( '../lib/dg.js' );
3
+
4
+ var input = [ 'test', 'another test', 1234, 'a string' ]
5
+ , results = [ ];
6
+
7
+ it('Expression (synchronous): object returned', function () {
8
+ var r = dg.sync( input, /^test$/ );
9
+ assert.equal( typeof r, 'object' )
10
+ } );
11
+
12
+ it('Expression (synchronous): correct value returned', function () {
13
+ assert.deepEqual( dg.sync( input, new RegExp( '^test$' ) ) , [ 'test' ] )
14
+ } );
15
+
16
+ it('Function (synchronous): object returned', function () {
17
+ var r = dg.sync( input, function (t) { if ( t == 'test' ) { return 1 } } );
18
+ assert( (typeof r) == 'object', 'object returned' )
19
+ } );
20
+
21
+ it('Function (synchronous): correct value returned', function () {
22
+ assert.deepEqual( dg.sync( input, function (t) { if (t === 'test') { return 1 } } ) , [ 'test' ] )
23
+ } );
@@ -0,0 +1,17 @@
1
+ var assert = require('assert');
2
+ var dg = require( '../lib/dg.js' );
3
+ var test = [ 'test', 'another test', 1234, 'a string' ];
4
+
5
+ it( 'Expression (asynchronous): object returned', function () {
6
+ dg.async( test, new RegExp( '^test$' ) ).then(
7
+ function (r) {
8
+ assert( typeof r, 'object' )
9
+ }
10
+ )
11
+ } );
12
+
13
+ it( 'Function (asynchronous): correct value returned', function () {
14
+ dg.async( test, function (t) { if ( t == 'test' ) return 1 }, function (r) {
15
+ assert.equal( r, [ 'test' ] )
16
+ } ) }
17
+ );
@@ -0,0 +1,34 @@
1
+ var sendak_data = [
2
+ [ '/Users/jane/dev/sendak/bin/js/add-github-project.js',
3
+ '/Users/jane/dev/sendak/bin/js/build-node.js',
4
+ '/Users/jane/dev/sendak/bin/js/check-user.js',
5
+ '/Users/jane/dev/sendak/bin/js/create-iam-group.js',
6
+ '/Users/jane/dev/sendak/bin/js/create-iam-user.js',
7
+ '/Users/jane/dev/sendak/bin/js/create-sendak-group.js',
8
+ '/Users/jane/dev/sendak/bin/js/create-sendak-project.js',
9
+ '/Users/jane/dev/sendak/bin/js/create-sendak-user.js',
10
+ '/Users/jane/dev/sendak/bin/js/list-iam-group-policies.js',
11
+ '/Users/jane/dev/sendak/bin/js/list-iam-groups.js',
12
+ '/Users/jane/dev/sendak/bin/js/list-iam-users.js',
13
+ '/Users/jane/dev/sendak/bin/js/list-sendak-nodes.js',
14
+ '/Users/jane/dev/sendak/bin/js/list-sendak-users.js',
15
+ '/Users/jane/dev/sendak/bin/js/rrm.js' ],
16
+ [ '/Users/jane/dev/sendak/bin/pl/de64',
17
+ '/Users/jane/dev/sendak/bin/pl/de64.pl',
18
+ '/Users/jane/dev/sendak/bin/pl/en64',
19
+ '/Users/jane/dev/sendak/bin/pl/en64.pl' ]
20
+ ];
21
+
22
+ var assert = require( 'assert' )
23
+ , chai = require( 'chai' )
24
+ , dg = require( '../lib/dg.js' )
25
+
26
+ it( 'syntax', function () {
27
+ assert( function () { return dg = require( '../lib/dg.js' ) }, 'require' );
28
+ } );
29
+
30
+ it( 'flatten', function () {
31
+ var flats = dg.flatten( sendak_data );
32
+ assert( flats, 'return looks trueish' );
33
+ assert( flats.length == 18, 'flattened list has right number of elements' );
34
+ } );
package/test/04-in.js ADDED
@@ -0,0 +1,25 @@
1
+ var assert = require('assert')
2
+ , chai = require('chai')
3
+ , dg = require( '../lib/dg.js' );
4
+
5
+ var input = [ 'test', 'another test', 1234, 'a string' ];
6
+
7
+ it('in', function () {
8
+ var r = dg.in( input, 'a string' );
9
+ assert( r, 'returned truth' );
10
+ var s = dg.in( input, 'peace on earth' );
11
+ chai.assert.notOk( s, 'returned false (boo-hoo)' );
12
+ } );
13
+
14
+ it('all_in', function () {
15
+ var r = dg.all_in( input, [ 'a string', 'test' ] );
16
+ assert(r, 'truth returned');
17
+
18
+ // We do not guarantee the return order of lists. Chai has this cool 'equal-ish' assert:
19
+ // .include(haystack, needle, [message])
20
+ //
21
+
22
+ r.forEach( function (test) {
23
+ chai.assert.include( [ 'a string', 'test' ], test, 'correct values returned' );
24
+ } );
25
+ } );
@@ -0,0 +1,47 @@
1
+ var entities = function () { return [
2
+ 'Ishmael', 'Ahab', 'Starbuck', 'Queequeg', 'Stubb', 'Tashtego', 'Flask', 'Daggoo',
3
+ 'Pip', 'Fedallah', 'Peleg', 'Bildad', 'Mapple', 'Boomer', 'Gabriel'
4
+ ] }
5
+
6
+ var duplicates = function () { return [ 'Ahab', 'Starbuck', 'Queequeg', 'Gabriel' ] }
7
+
8
+ var nested = function () { return [ 'Redburn', 'Jones', 'Riga', 'Jackson', 'Lavender', 'Blunt', 'Larry' ] }
9
+
10
+ var assert = require( 'assert' )
11
+ , chai = require( 'chai' )
12
+ , dg = require( '../lib/dg.js' )
13
+
14
+ it( 'syntax', function () {
15
+ assert( function () { return dg = require( '../lib/dg.js' ) }, 'require' );
16
+ } );
17
+
18
+ it( 'unique', function () {
19
+ var test1 = dg.unique(entities())
20
+
21
+ // Test that unique didn't lop off any values
22
+ //
23
+ assert.deepEqual( test1, entities(), 'simple uniqueness' );
24
+
25
+ // Add in some duplicates
26
+ //
27
+ var entities_dupes = dg.flatten( [ entities(), duplicates() ] );
28
+
29
+ // Check to see that unique removes the duplicates we added
30
+ //
31
+ var test2 = dg.unique( entities_dupes );
32
+ assert.deepEqual( test2, entities(), 'uniqueness' );
33
+
34
+ // Throw in some nested elements
35
+ //
36
+ var nested_test = [ entities(), [ duplicates() ], [ nested() ] ];
37
+
38
+ // Create the control list
39
+ //
40
+ var nested_control = dg.flatten( [ entities(), nested() ] );
41
+
42
+ // Run the test
43
+ //
44
+ var test3 = dg.unique( nested_test );
45
+
46
+ assert.deepEqual( test3, nested_control, 'deep uniqueness' );
47
+ } );
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ var assert = require( 'assert' )
4
+ , chai = require( 'chai' )
5
+ , dg = require( '../lib/dg' )
6
+
7
+ , nested_list = function () { return [ 'foo', [ 'unicorn', 'bar', [ 'leprechaun' ] ], 'baz' ] }
8
+
9
+ , mythical = function () { [ 'unicorn', 'leprechaun', 'griffin', 'sphinx', 'centaur', 'dragon', 'siren', 'banshee' ] }
10
+
11
+ it( 'nested list', function () {
12
+
13
+ assert( function () {
14
+ var uni = dg.deeply(
15
+ nested_list(),
16
+ function (t) { if (t == 'unicorn') return true }
17
+ );
18
+ if (uni.length != 1) { return false; }
19
+ if (uni[0] != 'unicorn') { return false; }
20
+ return true;
21
+ }, 'returns requisite element' );
22
+
23
+ var test = function (t) {
24
+ if ( (t == 'unicorn') || (t == 'leprechaun') ) {
25
+ return true;
26
+ }
27
+ return false;
28
+ }
29
+
30
+ var pair = dg.deeply( nested_list(), test );
31
+
32
+ assert(pair.length == 2, 'two elements returned');
33
+
34
+ } );
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ var assert = require('assert');
4
+ var dg = require( '../lib/dg.js' );
5
+
6
+ // inspired by a Real Actual Bug(tm)
7
+ //
8
+ var user_records = [
9
+ { 'user-name': 'jane' }
10
+ , { 'user-name': 'kate' }
11
+ , { 'user-name': 'may' }
12
+ , { 'user-name': 'alouicious everdander evercromby' }
13
+ , { 'user-name': 'mud' }
14
+ , { 'user-name': 'tybalt' }
15
+ , { 'user-name': 'chakotay' }
16
+ ];
17
+
18
+ it('find a username in a hash', function () {
19
+ var users = dg.deeply(
20
+ // Records to search
21
+ //
22
+ user_records,
23
+
24
+ // Test
25
+ //
26
+ function (t) {
27
+ //console.log( t );
28
+ if (t == 'tybalt') {
29
+ return true;
30
+ }
31
+ },
32
+
33
+ // Parameters to deeply
34
+ //
35
+ {
36
+ 'return-hash-tuples': true,
37
+ 'check-keys': false,
38
+ 'check-values': true
39
+ }
40
+ );
41
+ assert.deepEqual( users, [ { 'user-name': 'tybalt' } ] )
42
+ } );
43
+
44
+ // jane@cpan.org // vim:tw=80:ts=2:noet
@@ -0,0 +1,34 @@
1
+ var start = function () { return {
2
+ foo: 'foo_value',
3
+ bar: 'bar_value',
4
+ baz: 'baz_value'
5
+ } }
6
+
7
+ var into = function () { return {
8
+ qip: 'qip_value',
9
+ quux: 'quux_value'
10
+ } };
11
+
12
+ var assert = require( 'assert' )
13
+ , chai = require( 'chai' )
14
+ , dg = require( '../lib/dg.js' )
15
+
16
+ it( 'syntax', function () {
17
+ assert( function () { return dg = require( '../lib/dg.js' ) }, 'require' );
18
+ } );
19
+
20
+ it( 'coalesce', function () {
21
+ var new_obj = dg.coalesce_object( start(), into(), {
22
+ 'keys': [ 'foo', 'bar', 'baz' ],
23
+ 'return-clone': true
24
+ } );
25
+ var merged = {
26
+ foo: 'foo_value',
27
+ bar: 'bar_value',
28
+ baz: 'baz_value',
29
+ qip: 'qip_value',
30
+ quux: 'quux_value'
31
+ }
32
+ assert( new_obj, 'truthy return' );
33
+ assert.deepEqual( new_obj, merged, 'coalesced object' );
34
+ } );