<commit_before><p align="center">
  <img src="https://cdn.rawgit.com/fullstackio/cq/master/doc/readme/code-query-image.png" alt="Code Query - extract code snippets using selectors" />
</p>

# cq: Code Query [![npm package](https://img.shields.io/npm/v/@fullstackio/cq.svg?maxAge=2592000?style=flat-square)](https://www.npmjs.com/package/@fullstackio/cq) [![Dolpins](https://cdn.rawgit.com/fullstackio/cq/master/doc/readme/dolphins-badge-ff00ff.svg)](https://www.fullstackreact.com) 

> A tool to extract code snippets using selectors (instead of line numbers)
>
> Supports JavaScript ES5, ES6, JSX, and TypeScript

## Install

```
$ npm install --global @fullstackio/cq
```

## Usage

```
$ cq <query> <file>

# or

$ cat file | cq <query>
```

## Examples

Say we have a file `examples/basics.js` with the following code:


```javascript
// examples/basics.js
{lang=javascript,crop-query=.bye-EOF,format=raw}
<<[](examples/basics.js)
```

Get the `bye()` function:

```javascript
$ cq '.bye' examples/basics.js

{lang=javascript,crop-query=.bye,format=raw}
<<[](examples/basics.js)
```

Get the `calcArea()` function on the `Barn` class:

```javascript
$ cq '.Barn .calcArea' examples/basics.js

{lang=javascript,crop-query=.Barn .calcArea,format=raw}
<<[](examples/basics.js)
```

Get the `bye()` function plus the line after:

```javascript
// `context(identifier, linesBefore, linesAfter)`
$ cq 'context(.bye,0,1)' examples/basics.js

{lang=javascript,crop-query=context(.bye,0,1),format=raw}
<<[](examples/basics.js)
```

Get the _range_ of `constructor` through `calcArea`, inclusive, of the `Barn` class

```javascript
$ cq '.Barn .constructor-.calcArea' examples/basics.js

{lang=javascript,crop-query=.Barn .constructor-.calcArea,format=raw}
<<[](examples/basics.js)
```

If you pass `--json` you'll get the results in JSON, which can be useful for further processing:

```javascript
$ cq --json 'context(.bye,0,1)' examples/basics.js

    {
      "code": "const bye = function() {\n  return 'bye';\n}\nbye(); // -> 'bye'",
      "start": 598,
      "end": 659,
      "start_line": 25,
      "end_line": 28
    }
```

`cq` works with TypeScript as well. Say we had the following TypeScript File:

```typescript
// AuthService.ts
{lang=typescript,crop-query=.Injectable-EOF,format=raw}
<<[](examples/AuthService.ts)
```

Get the `AUTH_PROVIDERS` export:

```typescript
$ cq '.AUTH_PROVIDERS' examples/AuthService.ts

export var AUTH_PROVIDERS: Array<any> = [
  provide(AuthService, {useClass: AuthService})
];
```

Get the `isLoggedIn()` function through `AUTH_PROVIDERS`

```typescript
$ cq '(.AuthService .isLoggedIn)-.AUTH_PROVIDERS' examples/AuthService.ts

  isLoggedIn(): boolean {
    return this.getUser() !== null;
  }
}

export var AUTH_PROVIDERS: Array<any> = [
  provide(AuthService, {useClass: AuthService})
];
```

`cq` can search for strings as well as identifiers. Say we have the following test:

```javascript
import chai from 'chai';
const assert = chai.assert;

describe('My First Test', () => {
  it('basic assert', () => {
    assert.equal(1, 1);
  });
});

describe('My Second Test', () => {
  it('basic assert', () => {
    // this is the second one
    assert.equal(2, 2);
  });
});
```
 
We can get the first test:

```javascript
$ cq "'My First Test'" examples/mocha.test.js<commit_msg>added more cq queries to source README<commit_after><p align="center">
  <img src="https://cdn.rawgit.com/fullstackio/cq/master/doc/readme/code-query-image.png" alt="Code Query - extract code snippets using selectors" />
</p>

# cq: Code Query [![npm package](https://img.shields.io/npm/v/@fullstackio/cq.svg?maxAge=2592000?style=flat-square)](https://www.npmjs.com/package/@fullstackio/cq) [![Dolpins](https://cdn.rawgit.com/fullstackio/cq/master/doc/readme/dolphins-badge-ff00ff.svg)](https://www.fullstackreact.com) 

> A tool to extract code snippets using selectors (instead of line numbers)
>
> Supports JavaScript ES5, ES6, JSX, and TypeScript

## Install

```
$ npm install --global @fullstackio/cq
```

## Usage

```
$ cq <query> <file>

# or

$ cat file | cq <query>
```

## Examples

Say we have a file `examples/basics.js` with the following code:


```javascript
// examples/basics.js
{lang=javascript,crop-query=.bye-EOF,format=raw}
<<[](examples/basics.js)
```

Get the `bye()` function:

```javascript
$ cq '.bye' examples/basics.js

{lang=javascript,crop-query=.bye,format=raw}
<<[](examples/basics.js)
```

Get the `calcArea()` function on the `Barn` class:

```javascript
$ cq '.Barn .calcArea' examples/basics.js

{lang=javascript,crop-query=.Barn .calcArea,format=raw}
<<[](examples/basics.js)
```

Get the `bye()` function plus the line after:

```javascript
// `context(identifier, linesBefore, linesAfter)`
$ cq 'context(.bye,0,1)' examples/basics.js

{lang=javascript,crop-query=context(.bye,0,1),format=raw}
<<[](examples/basics.js)
```

Get the _range_ of `constructor` through `calcArea`, inclusive, of the `Barn` class

```javascript
$ cq '.Barn .constructor-.calcArea' examples/basics.js

{lang=javascript,crop-query=.Barn .constructor-.calcArea,format=raw}
<<[](examples/basics.js)
```

If you pass `--json` you'll get the results in JSON, which can be useful for further processing:

```javascript
$ cq --json 'context(.bye,0,1)' examples/basics.js

    {
      "code": "const bye = function() {\n  return 'bye';\n}\nbye(); // -> 'bye'",
      "start": 598,
      "end": 659,
      "start_line": 25,
      "end_line": 28
    }
```

`cq` works with TypeScript as well. Say we had the following TypeScript File:

```typescript
// AuthService.ts
{lang=typescript,crop-query=.Injectable-EOF,format=raw}
<<[](examples/AuthService.ts)
```

Get the `AUTH_PROVIDERS` export:

```typescript
$ cq '.AUTH_PROVIDERS' examples/AuthService.ts

{lang=typescript,crop-query=.AUTH_PROVIDERS,format=raw}
<<[](examples/AuthService.ts)
```

Get the `isLoggedIn()` function through `AUTH_PROVIDERS`

```typescript
$ cq '(.AuthService .isLoggedIn)-.AUTH_PROVIDERS' examples/AuthService.ts

{lang=typescript,crop-query=(.AuthService .isLoggedIn)-.AUTH_PROVIDERS,format=raw}
<<[](examples/AuthService.ts)
```

`cq` can search for strings as well as identifiers. Say we have the following test:

```javascript
import chai from 'chai';
const assert = chai.assert;

describe('My First Test', () => {
  it('basic assert', () => {
    assert.equal(1, 1);
  });
});

describe('My Second Test', () => {
  it('basic assert', () => {
    // this is the second one
    assert.equal(2, 2);
  });
});
```
 
We can get the first test:

```javascript
$ cq "'My First Test'" examples/mocha.test.js
{'fraction_non_alphanumeric': 0.18212070791105733, 'fraction_numerical': 0.009378308879140826, 'mean_word_length': 4.5376884422110555, 'pattern_counts': {'":': 10, '<': 45, '<?xml version=': 0, '>': 33, 'https://': 10, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2088963', 'n_tokens_mistral': 2508, 'n_tokens_neox': 2329, 'n_words': 543}