Skip to content

Commit 9bd963f

Browse files
committed
add grepArray and Array.prototype.grep
1 parent fc1c5b2 commit 9bd963f

13 files changed

Lines changed: 716 additions & 55 deletions

File tree

meta/examples/array-grep.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env yavascript
2+
/// <reference path="../../yavascript.d.ts" />
3+
4+
echo(["abc", "bcd", "def", "cat"].grep(/c/i));
5+
6+
// non-strings are coerced to strings
7+
echo([2, "ab2", "b2d", "def", "2at"].grep(/2/i));

meta/generated-docs/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<!-- meta/generated-docs/README.md is generated from meta/scripts/lib/generated-doc-index.md. Do not make edits to meta/generated-docs/README.md; instead, make them to meta/scripts/lib/generated-doc-index.md and then re-run the build. -->
2+
13
# YavaScript API Documentation Index
24

35
Here is a list of all of YavaScript's builtin APIs, aside from those normally found in ECMAScript.
@@ -46,6 +48,7 @@ Here is a list of all of YavaScript's builtin APIs, aside from those normally fo
4648
- [`which`]: Find the path to a program on the system
4749
- [`grepFile`]: Search for matches in a file
4850
- [`grepString`]: Search for matches in a string
51+
- [`grepArray`]: Search for matches in an Array
4952
- [`whoami`]: Get user name/uid/gid
5053
- [`openUrl`]: Open a file/url using your operating system's default application for that file/url
5154

@@ -201,6 +204,7 @@ For convenience, two of the builtin modules from QuickJS are also available as g
201204
[`which`]: /meta/generated-docs/which.md#which-function
202205
[`grepFile`]: /meta/generated-docs/grep.md#grepfile-function
203206
[`grepString`]: /meta/generated-docs/grep.md#grepstring-function
207+
[`grepArray`]: /meta/generated-docs/grep.md#greparray-function
204208
[`whoami`]: /meta/generated-docs/whoami.md#whoami-function
205209
[`openUrl`]: /meta/generated-docs/open-url.md#openurl-function
206210
[`Path`]: /meta/generated-docs/path.md#path-class
@@ -257,6 +261,7 @@ For convenience, two of the builtin modules from QuickJS are also available as g
257261
[`JSX`]: /meta/generated-docs/jsx.md#jsx-namespace
258262
[`yavascript.compilers`]: /meta/generated-docs/yavascript.md#yavascriptcompilers-object-property
259263
[`String.prototype.grep`]: /meta/generated-docs/grep.md#stringgrep-function-property
264+
[`Array.prototype.grep`]: /meta/generated-docs/grep.md#arraygrep-function-property
260265
[`RegExp.escape`]: /meta/generated-docs/regexp-escape.md#regexpconstructorescape-method
261266
[`String.dedent`]: /meta/generated-docs/string-dedent.md#stringconstructordedent-function-property
262267
[`bigint`]: /meta/generated-docs/others.md#bigint-bigintconstructor

meta/generated-docs/grep.md

Lines changed: 136 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
- [grepString (function)](#grepstring-function)
22
- [grepString(...) (call signature)](#grepstring-call-signature)
33
- [grepString(...) (call signature)](#grepstring-call-signature-1)
4+
- [grepArray (function)](#greparray-function)
5+
- [grepArray(...) (call signature)](#greparray-call-signature)
6+
- [grepArray(...) (call signature)](#greparray-call-signature-1)
47
- [grepFile (function)](#grepfile-function)
58
- [grepFile(...) (call signature)](#grepfile-call-signature)
69
- [grepFile(...) (call signature)](#grepfile-call-signature-1)
710
- [String (interface)](#string-interface)
811
- [String.grep (function property)](#stringgrep-function-property)
912
- [String.grep(...) (call signature)](#stringgrep-call-signature)
1013
- [String.grep(...) (call signature)](#stringgrep-call-signature-1)
14+
- [Array (interface)](#array-interface)
15+
- [Array.grep (function property)](#arraygrep-function-property)
16+
- [Array.grep(...) (call signature)](#arraygrep-call-signature)
17+
- [Array.grep(...) (call signature)](#arraygrep-call-signature-1)
1118
- [GrepOptions (interface)](#grepoptions-interface)
1219
- [GrepOptions.inverse (boolean property)](#grepoptionsinverse-boolean-property)
1320
- [GrepOptions.details (boolean property)](#grepoptionsdetails-boolean-property)
1421
- [GrepMatchDetail (interface)](#grepmatchdetail-interface)
1522
- [GrepMatchDetail.lineNumber (number property)](#grepmatchdetaillinenumber-number-property)
16-
- [GrepMatchDetail.lineContent (string property)](#grepmatchdetaillinecontent-string-property)
23+
- [GrepMatchDetail.lineContent (ItemType property)](#grepmatchdetaillinecontent-itemtype-property)
1724
- [GrepMatchDetail.matches (RegExpMatchArray property)](#grepmatchdetailmatches-regexpmatcharray-property)
25+
- [GrepMatchDetail.index (number property)](#grepmatchdetailindex-number-property)
26+
- [GrepMatchDetail.content (ItemType property)](#grepmatchdetailcontent-itemtype-property)
1827

1928
# grepString (function)
2029

@@ -25,7 +34,8 @@ the specified pattern, as an array of strings or detail objects.
2534
- `@param` _pattern_ — The pattern to find. Can be a string or a RegExp.
2635
- `@param` _options_ — Options which control matching behavior.
2736

28-
See also [grepFile](/meta/generated-docs/grep.md#grepfile-function) and [String.prototype.grep](/meta/generated-docs/grep.md#stringgrep-function-property).
37+
See also [grepFile](/meta/generated-docs/grep.md#grepfile-function), [grepArray](/meta/generated-docs/grep.md#greparray-function), [String.prototype.grep](/meta/generated-docs/grep.md#stringgrep-function-property),
38+
and [Array.prototype.grep](/meta/generated-docs/grep.md#arraygrep-function-property).
2939

3040
```ts
3141
const grepString: {
@@ -54,16 +64,61 @@ const grepString: {
5464
(str: string, pattern: string | RegExp, options?: GrepOptions): Array<string>;
5565
```
5666
67+
# grepArray (function)
68+
69+
Returns those Array items matching the specified pattern, as either an
70+
Array of items or an Array of detail objects.
71+
72+
- `@param` _targetArray_ — The Array of strings to search through. Non-strings will be coerced to string during matching.
73+
- `@param` _pattern_ — The pattern to find. Can be a string or a RegExp.
74+
- `@param` _options_ — Options which control matching behavior.
75+
76+
See also [grepString](/meta/generated-docs/grep.md#grepstring-function), [grepFile](/meta/generated-docs/grep.md#grepfile-function), [String.prototype.grep](/meta/generated-docs/grep.md#stringgrep-function-property),
77+
and [Array.prototype.grep](/meta/generated-docs/grep.md#arraygrep-function-property).
78+
79+
```ts
80+
const grepArray: {
81+
<T>(
82+
targetArray: Array<T>,
83+
pattern: string | RegExp,
84+
options: GrepOptions & {
85+
details: true;
86+
},
87+
): Array<GrepMatchDetail<T>>;
88+
<T>(
89+
targetArray: Array<T>,
90+
pattern: string | RegExp,
91+
options?: GrepOptions,
92+
): Array<T>;
93+
};
94+
```
95+
96+
## grepArray(...) (call signature)
97+
98+
```ts
99+
<T>(targetArray: Array<T>, pattern: string | RegExp, options: GrepOptions & {
100+
details: true;
101+
}): Array<GrepMatchDetail<T>>;
102+
```
103+
104+
## grepArray(...) (call signature)
105+
106+
```ts
107+
<T>(targetArray: Array<T>, pattern: string | RegExp, options?: GrepOptions): Array<T>;
108+
```
109+
57110
# grepFile (function)
58111
59112
Reads the file content at `path`, splits it on `\n`, and then returns the
60-
lines matching the specified pattern, as an array of strings or detail objects.
113+
lines matching the specified pattern, as an array of strings or detail
114+
objects.
61115
62116
- `@param` _str_ — The string to search through.
63117
- `@param` _pattern_ — The pattern to find. Can be a string or a RegExp.
64118
- `@param` _options_ — Options which control matching behavior.
65119
66-
See also [grepString](/meta/generated-docs/grep.md#grepstring-function) and [String.prototype.grep](/meta/generated-docs/grep.md#stringgrep-function-property).
120+
See also [grepArray](/meta/generated-docs/grep.md#greparray-function), [grepString](/meta/generated-docs/grep.md#grepstring-function),
121+
[String.prototype.grep](/meta/generated-docs/grep.md#stringgrep-function-property), and [Array.prototype.grep](/meta/generated-docs/grep.md#arraygrep-function-property).
67122
68123
```ts
69124
const grepFile: {
@@ -121,7 +176,8 @@ specified pattern, as an array of strings or detail objects.
121176
- `@param` _pattern_ — The pattern to find. Can be a string or a RegExp.
122177
- `@param` _options_ — Options which control matching behavior.
123178

124-
See also [grepString](/meta/generated-docs/grep.md#grepstring-function) and [grepFile](/meta/generated-docs/grep.md#grepfile-function).
179+
See also [grepString](/meta/generated-docs/grep.md#grepstring-function), [grepArray](/meta/generated-docs/grep.md#greparray-function), [grepFile](/meta/generated-docs/grep.md#grepfile-function), and
180+
[Array.prototype.grep](/meta/generated-docs/grep.md#arraygrep-function-property).
125181

126182
```ts
127183
grep: {
@@ -146,6 +202,56 @@ grep: {
146202
(pattern: string | RegExp, options?: GrepOptions): Array<string>;
147203
```
148204
205+
# Array (interface)
206+
207+
```ts
208+
interface Array<T> {
209+
grep: {
210+
(
211+
pattern: string | RegExp,
212+
options: GrepOptions & {
213+
details: true;
214+
},
215+
): Array<GrepMatchDetail<T>>;
216+
(pattern: string | RegExp, options?: GrepOptions): Array<T>;
217+
};
218+
}
219+
```
220+
221+
## Array.grep (function property)
222+
223+
Returns those Array items matching the specified pattern, as either an
224+
Array of items or an Array of detail objects.
225+
226+
- `@param` _pattern_ — The pattern to find. Can be a string or a RegExp.
227+
- `@param` _options_ — Options which control matching behavior.
228+
229+
See also [grepString](/meta/generated-docs/grep.md#grepstring-function), [grepArray](/meta/generated-docs/grep.md#greparray-function), [grepFile](/meta/generated-docs/grep.md#grepfile-function), and
230+
[String.prototype.grep](/meta/generated-docs/grep.md#stringgrep-function-property).
231+
232+
```ts
233+
grep: {
234+
(pattern: string | RegExp, options: GrepOptions & {
235+
details: true;
236+
}): Array<GrepMatchDetail<T>>;
237+
(pattern: string | RegExp, options?: GrepOptions): Array<T>;
238+
};
239+
```
240+
241+
### Array.grep(...) (call signature)
242+
243+
```ts
244+
(pattern: string | RegExp, options: GrepOptions & {
245+
details: true;
246+
}): Array<GrepMatchDetail<T>>;
247+
```
248+
249+
### Array.grep(...) (call signature)
250+
251+
```ts
252+
(pattern: string | RegExp, options?: GrepOptions): Array<T>;
253+
```
254+
149255
# GrepOptions (interface)
150256
151257
```ts
@@ -176,15 +282,17 @@ details?: boolean;
176282
177283
# GrepMatchDetail (interface)
178284
179-
When `grepString`, `grepFile`, or `String.prototype.grep` are called with the
180-
`{ details: true }` option set, an Array of `GrepMatchDetail` objects is
181-
returned.
285+
When `grepString`, `grepArray`, `grepFile`, or `String.prototype.grep` are
286+
called with the `{ details: true }` option set, an Array of `GrepMatchDetail`
287+
objects is returned.
182288
183289
```ts
184-
declare interface GrepMatchDetail {
290+
declare interface GrepMatchDetail<ItemType = string> {
185291
lineNumber: number;
186-
lineContent: string;
292+
lineContent: ItemType;
187293
matches: RegExpMatchArray;
294+
index: number;
295+
content: ItemType;
188296
}
189297
```
190298
@@ -194,14 +302,30 @@ declare interface GrepMatchDetail {
194302
lineNumber: number;
195303
```
196304
197-
## GrepMatchDetail.lineContent (string property)
305+
## GrepMatchDetail.lineContent (ItemType property)
198306
199307
```ts
200-
lineContent: string;
308+
lineContent: ItemType;
201309
```
202310
203311
## GrepMatchDetail.matches (RegExpMatchArray property)
204312
205313
```ts
206314
matches: RegExpMatchArray;
207315
```
316+
317+
## GrepMatchDetail.index (number property)
318+
319+
Same as lineNumber - 1.
320+
321+
```ts
322+
index: number;
323+
```
324+
325+
## GrepMatchDetail.content (ItemType property)
326+
327+
Alias for lineContent.
328+
329+
```ts
330+
content: ItemType;
331+
```

meta/scripts/lib/generated-doc-index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<!-- meta/generated-docs/README.md is generated from meta/scripts/lib/generated-doc-index.md. Do not make edits to meta/generated-docs/README.md; instead, make them to meta/scripts/lib/generated-doc-index.md and then re-run the build. -->
2+
13
# YavaScript API Documentation Index
24

35
Here is a list of all of YavaScript's builtin APIs, aside from those normally found in ECMAScript.
@@ -46,6 +48,7 @@ Here is a list of all of YavaScript's builtin APIs, aside from those normally fo
4648
- [`which`]: Find the path to a program on the system
4749
- [`grepFile`]: Search for matches in a file
4850
- [`grepString`]: Search for matches in a string
51+
- [`grepArray`]: Search for matches in an Array
4952
- [`whoami`]: Get user name/uid/gid
5053
- [`openUrl`]: Open a file/url using your operating system's default application for that file/url
5154

meta/scripts/lib/generated-doc-links.json5

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
which: "/meta/generated-docs/which.md#which-function",
4242
grepFile: "/meta/generated-docs/grep.md#grepfile-function",
4343
grepString: "/meta/generated-docs/grep.md#grepstring-function",
44+
grepArray: "/meta/generated-docs/grep.md#greparray-function",
4445
whoami: "/meta/generated-docs/whoami.md#whoami-function",
4546
openUrl: "/meta/generated-docs/open-url.md#openurl-function",
4647

@@ -112,6 +113,7 @@
112113

113114
// ECMAScript Extensions
114115
"String.prototype.grep": "/meta/generated-docs/grep.md#stringgrep-function-property",
116+
"Array.prototype.grep": "/meta/generated-docs/grep.md#arraygrep-function-property",
115117
"RegExp.escape": "/meta/generated-docs/regexp-escape.md#regexpconstructorescape-method",
116118
"String.dedent": "/meta/generated-docs/string-dedent.md#stringconstructordedent-function-property",
117119

meta/tests/src/globals.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ test("globals", async () => {
242242
__dirname: string set throws error (GS)
243243
grepFile: function (CWE)
244244
grepString: function (CWE)
245+
grepArray: function (CWE)
245246
global: object (CW)
246247
process: object (CW)
247248
",

0 commit comments

Comments
 (0)