Skip to content

Commit

Permalink
Expose parser as well (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraklin authored Oct 5, 2020
1 parent f56db16 commit 87e734b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ npm install elm-debug-transformer

There is a nice summary of the usage in Alex Korban's article [Get improved Debug.log output in the browser console](https://korban.net/posts/elm/2019-07-02-improved-debug-log-output-browser-console/)

### `register()`
Register the console debugger in your main JS file before you initialize Elm application:

```
Expand Down Expand Up @@ -91,6 +92,16 @@ Here's a sample HTML for your reference:
</html>
```

### `parse()`
Since version 1.1.0 the parser function is exposed as well, so you can use it to parse Debug.log output into JSON structure and work with it later as you wish.

```
import {parse} ElmDebugger from 'elm-debug-transformer';
const parsedValue = parse("debug tag: [1,2,3]");
```


### Enable custom formatters in Chrome dev tools
Available in Chrome 47 and higher.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elm-debug-transformer",
"version": "1.1.0-beta",
"version": "1.1.0",
"description": "Transform Elm Debug.log output into nice log object with custom formatter",
"main": "dist/elm-console-debug.js",
"author": "Tomas Latal <[email protected]>",
Expand Down
6 changes: 4 additions & 2 deletions src/elm-debug.pegjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Set.toList, Array.toList
{
function toStr(chars) {return chars.join("")};
function flat(arr) { return arr.reduce((acc, val) => acc.concat(val), []);}
function toStr(chars) {console.log("chars", chars); return chars.join("")};
}

DebugString
Expand Down Expand Up @@ -67,7 +68,8 @@ VariableName =
chars:[a-zA-Z0-9_]+ {return toStr(chars);}

Type =
type:[a-zA-Z]+ {return {type: "Type", name: toStr(type)};}
// type returns array in array -> "Yes" -> ["Y",["e", "s"]] -- need to flat that array
type: ([A-Z][a-zA-Z0-9_]+) {return {type: "Type", name: toStr(flat(type))};}

Tag =
tag:TagChar+ { return toStr(tag); }
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as _ from 'lodash';
import { IElmDebugValue, IThemeOption } from './CommonTypes';
import { parse } from './elm-debug-parser';
import { IElmDebugValue, IThemeOption } from './CommonTypes';
import { parse as elmDebugParse } from './elm-debug-parser';
import { darkTheme, lightTheme } from './formatters/elements/Styles';
import JsonMLFormatter from './formatters/JsonMLFormatter';
import SimpleFormatter from './formatters/SimpleFormatter';
Expand Down Expand Up @@ -30,6 +30,10 @@ const defaultOptions: IOptions = {
theme: "light"
}

export function parse(message: string) : IElmDebugValue {
return elmDebugParse(message) as IElmDebugValue;
}

export function register(opts: IOptions | undefined): IOptions {

if(window.__ELM_DEBUG_TRANSFORM_OPTIONS__){
Expand Down Expand Up @@ -76,7 +80,7 @@ export function register(opts: IOptions | undefined): IOptions {
log.call(console, 'Original message:', msg);
}

const parsed = parse(msg) as IElmDebugValue;
const parsed = parse(msg);

log.call(
console,
Expand Down
18 changes: 18 additions & 0 deletions test/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,24 @@ describe('Parsing', () => {
});

describe('Custom types', () => {
it('Custom type name with number', () => {
expect(
parser.parse('custom type: User1 "Adam"').value
).to.deep.equal({
name: 'User1',
type: 'Custom',
value: [B.str('Adam')],
});
});
it('Custom type name with underscore', () => {
expect(
parser.parse('custom type: User_name "Adam"').value
).to.deep.equal({
name: 'User_name',
type: 'Custom',
value: [B.str('Adam')],
});
});
it('Custom type with one value', () => {
expect(
parser.parse('custom type: User "Adam"').value
Expand Down

0 comments on commit 87e734b

Please sign in to comment.