Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump minimatch from 3.0.4 to 3.1.2 #14

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Most functions, such as `getChildren`, `predicate`, `onEnter`, and `onLeave`, ar
- [find](#find)
- [findAll](#findAll)
- [findIndexPath](#findIndexPath)
- [flat](#flat)
- [visit](#visit)
- [withOptions](#withOptions)

Expand Down Expand Up @@ -83,7 +84,7 @@ const rootNode = {
],
}

access(rootNode, [1, 0], { getChildren })
accessPath(rootNode, [1, 0], { getChildren })
// #=> [{ name: 'a', children: [...] }, { name: 'c', children: [...] }, { name: 'd' }]
```

Expand Down Expand Up @@ -245,6 +246,38 @@ findAllIndexPaths(rootNode, {

---

### `flat`

Returns an array containing the root node and all of its descendants.

This is analogous to `Array.prototype.flat` for flattening arrays.

**Type**: `function flat<T>(node: T, options: BaseOptions<T>): T[]`

#### Example

```js
import { flat } from 'tree-visit'

const getChildren = (node) => node.children || []

const rootNode = {
name: 'a',
children: [
{ name: 'b' },
{
name: 'c',
children: [{ name: 'd' }],
},
],
}

flat(rootNode, { getChildren }).map((node) => node.name)
// #=> ['a', 'b', 'c', 'd']
```

---

### `visit`

Visit each node in the tree, calling an optional `onEnter` and `onLeave` for each.
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": "tree-visit",
"version": "0.1.0",
"version": "0.1.3",
"description": "A tree traversal library.",
"main": "lib/index.js",
"files": [
Expand Down
42 changes: 21 additions & 21 deletions src/__tests__/__snapshots__/index.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ exports[`diagram generates box diagram 1`] = `
exports[`diagram generates folder diagram 1`] = `
"a
├── b
   ├── b1
   └── b2
├── b1
└── b2
└── c
├── c1
└── c2"
Expand All @@ -27,8 +27,8 @@ exports[`diagram generates folder diagram 1`] = `
exports[`diagram generates folder diagram 2`] = `
"a
├── b
   ├── b1
   └── b2
├── b1
└── b2
└── c
├── c1
└── c2"
Expand All @@ -46,15 +46,15 @@ exports[`diagram generates folder diagram with multiline label 1`] = `
"name: a
path:
├── name: b
   path: 0
   ├── name: b1
   │   path: 0.0
   └── name: b2
   path: 0.1
path: 0
├── name: b1
path: 0.0
└── name: b2
path: 0.1
└── name: c
path: 1
├── name: c1
   path: 1.0
path: 1.0
└── name: c2
path: 1.1"
`;
Expand All @@ -63,15 +63,15 @@ exports[`diagram generates folder diagram with multiline label 2`] = `
"name: a
path:
├── name: b
   path: 0
   ├── name: b1
   │   path: 0.0
   └── name: b2
   path: 0.1
path: 0
├── name: b1
path: 0.0
└── name: b2
path: 0.1
└── name: c
path: 1
├── name: c1
   path: 1.0
path: 1.0
└── name: c2
path: 1.1"
`;
Expand All @@ -97,7 +97,7 @@ path:
exports[`diagram generates folder diagram with non-root single child case 1`] = `
"a
├── b
   └── b1
└── b1
└── c
└── c1"
`;
Expand Down Expand Up @@ -154,8 +154,8 @@ exports[`diagram generates uneven box diagram 1`] = `
exports[`withOptions supports overloaded calls 1`] = `
"a
├── b
   ├── b1
   └── b2
├── b1
└── b2
└── c
├── c1
└── c2"
Expand All @@ -164,8 +164,8 @@ exports[`withOptions supports overloaded calls 1`] = `
exports[`withOptions supports overloaded calls 2`] = `
"a
├── b
   ├── b1
   └── b2
├── b1
└── b2
└── c
├── c1
└── c2"
Expand Down
143 changes: 138 additions & 5 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { visit, IndexPath } from '../index'
import { access, accessPath } from '../access'
import { find, findIndexPath, findAll, findAllIndexPaths } from '../find'
import { withOptions } from '../withOptions'
import { diagram } from '../diagram'
import { find, findAll, findAllIndexPaths, findIndexPath } from '../find'
import { flat } from '../flat'
import { flatMap } from '../flatMap'
import { IndexPath, visit } from '../index'
import { map } from '../map'
import { reduce } from '../reduce'
import { withOptions } from '../withOptions'

type Node = {
name: string
Expand Down Expand Up @@ -261,6 +265,95 @@ describe('find', () => {
})
})

describe('flat', () => {
it('flattens a tree', () => {
const nodes = flat(example, {
getChildren,
})

expect(nodes.map((node) => node.name)).toEqual([
'a',
'b',
'b1',
'b2',
'c',
'c1',
'c2',
])
})
})

describe('flatMap', () => {
it('flatMaps a tree', () => {
const items = flatMap(example, {
getChildren,
transform: (node) => [{ name: node.name, depth: node.indexPath.length }],
})

expect(items).toEqual([
{ depth: 0, name: 'a' },
{ depth: 1, name: 'b' },
{ depth: 2, name: 'b1' },
{ depth: 2, name: 'b2' },
{ depth: 1, name: 'c' },
{ depth: 2, name: 'c1' },
{ depth: 2, name: 'c2' },
])
})
})

describe('reduce', () => {
it('reduces a tree', () => {
const result = reduce(example, {
getChildren,
initialResult: 0,
nextResult: (result) => result + 1,
})

expect(result).toEqual(7)
})
})

describe('map', () => {
it('maps a tree', () => {
type ResultNode = { id: string; items: ResultNode[] }

const result: ResultNode = map(example, {
getChildren,
transform: (node, children) => ({
id: node.name,
items: children,
}),
})

expect(
flat(result, {
getChildren: (node) => node.items,
}).map((node) => node.id)
).toEqual(['a', 'b', 'b1', 'b2', 'c', 'c1', 'c2'])
})

it('maps a tree and omits nodes', () => {
type ResultNode = { id: string; items: ResultNode[] }

const result: ResultNode = map(example, {
getChildren,
transform: (node, children) => ({
id: node.name,
items: children.filter(
(child) => child.id !== 'b1' && child.id !== 'c'
),
}),
})

expect(
flat(result, {
getChildren: (node) => node.items,
}).map((node) => node.id)
).toEqual(['a', 'b', 'b2'])
})
})

describe('diagram', () => {
const getLabel = (node: Node): string => node.name
const getMultilineLabel = (node: Node): string =>
Expand Down Expand Up @@ -447,7 +540,9 @@ describe('diagram', () => {

describe('withOptions', () => {
it('binds options', () => {
const { find, access, visit } = withOptions({ getChildren })
const { find, access, visit, reduce, flatMap, map } = withOptions({
getChildren,
})

let enterNames: string[] = []

Expand All @@ -466,6 +561,34 @@ describe('withOptions', () => {
).toEqual('b1')

expect(access(example, [0, 0]).name).toEqual('b1')

expect(
reduce(
example,
(result, node) => (result ? result + ' ' + node.name : node.name),
''
)
).toEqual('a b b1 b2 c c1 c2')

expect(flatMap(example, (node) => node.name.split(''))).toEqual([
'a',
'b',
'b',
'1',
'b',
'2',
'c',
'c',
'1',
'c',
'2',
])

expect(
map<{ id: string }>(example, (node, transformedChildren) => ({
id: `${node.name}:${transformedChildren.length}`,
})).id
).toEqual('a:2')
})

it('supports typed finding', () => {
Expand All @@ -485,7 +608,7 @@ describe('withOptions', () => {
})

it('supports overloaded calls', () => {
const { find, findAllIndexPaths, visit, diagram } = withOptions({
const { find, findAllIndexPaths, visit, diagram, flat } = withOptions({
getChildren,
})

Expand All @@ -497,6 +620,16 @@ describe('withOptions', () => {

expect(enterNames).toEqual(['a', 'b', 'b1', 'b2', 'c', 'c1', 'c2'])

expect(flat(example).map((node) => node.name)).toEqual([
'a',
'b',
'b1',
'b2',
'c',
'c1',
'c2',
])

expect(find(example, (node) => node.name === 'b1')?.name).toEqual('b1')

expect(
Expand Down
2 changes: 1 addition & 1 deletion src/diagram/directoryDiagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IndexPath } from '../indexPath'
enum LinePrefix {
Child = `├── `,
LastChild = `└── `,
NestedChild = `│   `,
NestedChild = `│ `,
LastNestedChild = ` `,
}

Expand Down
4 changes: 2 additions & 2 deletions src/find.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IndexPath } from './indexPath'
import { BaseOptions } from './options'
import { visit, STOP } from './visit'
import { STOP, visit } from './visit'

export type FindOptions<T> = BaseOptions<T> & {
/**
Expand Down Expand Up @@ -28,13 +28,13 @@ export function find<T>(node: T, options: FindOptions<T>): T | undefined {
let found: T | undefined

visit(node, {
...options,
onEnter: (child, indexPath) => {
if (options.predicate(child, indexPath)) {
found = child
return STOP
}
},
getChildren: options.getChildren,
})

return found
Expand Down
18 changes: 18 additions & 0 deletions src/flat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { BaseOptions } from './options'
import { reduce } from './reduce'

/**
* Returns an array containing the root node and all of its descendants.
*
* This is analogous to `Array.prototype.flat` for flattening arrays.
*/
export function flat<T>(node: T, options: BaseOptions<T>): T[] {
return reduce<T, T[]>(node, {
...options,
initialResult: [],
nextResult: (result, child) => {
result.push(child)
return result
},
})
}
Loading