UNPKG

2.81 kBMarkdownView Raw
1deep-grep
2=========
3
4Too many haystacks, not enough needles — or, "when you have a grep,
5everything looks like a list."
6
7```
8var 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
19usage
20====
21
22For simple grep:
23
24* `dg.sync()`
25 - `dg.sync( list, expression )` - evaluates `list`, returning a new list of every
26 element that matches `expression.test` (like `RegExp`).
27 - `dg.sync( list, func )` - same as above, only executes `func` for every element
28 of list, returning a new list.
29* `dg.async()`
30 - `dg.async( list, expression )` - same as with `sync()`, above, only
31 returns a promise to the list of matches.
32 - `dg.async( list, func, callback )` - same as `sync()`, above, except the
33 callback is called with a promise to the list of matches.
34* `dg.in()`
35 - `dg.in( list, 'value' )` - returns `true` or `false` depending upon
36 whether `list` contains `value`.
37* `dg.all_in()`
38 - `dg.all_in( list_a, list_b )` - returns a list of all values in `list_a`
39 that exist in `list_b`.
40* `dg.unique()`
41 - `dg.unique( list )` - returns all unique values of `list`. Note: flattens.
42* `dg.flatten( )`
43 - `dg.flatten( nested_list )` - returns a list of all the lists contained in
44 `nested_list`, concatenated into a single scope.
45
46For doing greppy things on nested structures:
47
48For simple lists (that is, lists with nested lists of arbitrary depth but no
49complex datatypes — like objects — and no hashes), `dg.deeply` is
50invoked simply:
51
52```
53var haystack = [
54 'zebra', 'lion', [
55 'tiger', 'unicorn', 'emperor penguin'
56 ],
57 'leprechaun',
58 'cockerel'
59];
60
61var needles = dg.deeply( list, function (t) {
62 if (new RegExp( ('(unicorn|leprechaun)s?' ).test(t))) return true
63} );
64```
65
66For more complex data, options are available to you:
67
68```
69var needles = dg.deeply( arks['Noah'], function (t) { ... }, {
70 // Any of these may be defined
71 //
72
73 // If items in the provided object have methods, check these methods for
74 // return values that evaluate as true in the provided function.
75 //
76 'check-object-methods': [ 'get_species', 'list_passengers' ],
77
78 // Provide optionally as argument to calls from check-object-methods, above
79 //
80 'object-method-arguments': [ argument, function, 'etc' ],
81
82 // Return hash keys that evaluate true also?
83 // default: false
84 //
85 'check-keys': true,
86
87 // Return hash values that evaluate true?
88 // default: false
89 //
90 'check-values': true,
91
92 // Return key/value tuples rather than just keys or just values?
93 // default: false
94 //
95 'return-hash-tuples': true,
96
97 // Return just keys or just values from hashlike structures encountered
98 // default: true
99 'return-hash-elements': false
100} );
101```
102
103author
104====
105
106[@janearc](https://github.com/janearc), jane@cpan.org