Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ parse(input)
{ scope: 'world', children: [] } ]
```

The maximum number of children is 100 by default. Additional children will be dropped. To allow more children, you can specify `maxNodes` in the second arguments:

```
parse(input, Infinity)
```

### Deep

We don't care about blank lines or the [type of
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const collapse = (list, maxNodes = 100) => {
tail = list.slice(i + 1)
const nextScopeIndex = findNextEquivalentScope(scope, tail)
const children = findLinesWithinScope(scope, tail)
const collapsedChildren = collapse(children)
const collapsedChildren = collapse(children, maxNodes)
result.push({
scope,
children: collapsedChildren,
Expand All @@ -55,11 +55,11 @@ export const collapse = (list, maxNodes = 100) => {
} else {
break
}
} while (1 && count++ < maxNodes)
} while (++count < maxNodes)

return result
}

export const parse = (text) => {
return collapse(text.split('\n').filter(x => x))
export const parse = (text, maxNodes) => {
return collapse(text.split('\n').filter(x => x), maxNodes)
}
60 changes: 58 additions & 2 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as mod from '.'

const log = (x) =>
console.log(JSON.stringify(x, null, ' '))
// const log = (x) =>
// console.log(JSON.stringify(x, null, ' '))

describe('isWithinScope', () => {
it('should is', () => {
Expand Down Expand Up @@ -163,6 +163,62 @@ describe('collapse', () => {
})
})

describe('maxNodes', () => {
const input = `
- a
- b
- 1
- 2
- 3
- 4
- 5
- c
- d
- e`

const result = mod.parse(input, 3)
expect(result).toEqual(
[
{
'scope': '- a',
'children': [
{
'scope': ' - b',
'children': [
{
'scope': ' - 1',
'children': []
},
{
'scope': ' - 2',
'children': []
},
{
'scope': ' - 3',
'children': []
},
]
}
]
},
{
'scope': '- c',
'children': [
{
'scope': ' - d',
'children': [
{
'scope': ' - e',
'children': []
}
]
}
]
}
]
)
})

describe('mac daddy', () => {
it('should blow away all expectations for awesomeness at every tier', () => {
const input = `
Expand Down