Skip to content

Commit

Permalink
feat(core): zip.select and assorted predicates (#113)
Browse files Browse the repository at this point in the history
Implements the function

const select: (...arguments) => location => Iterable<locations>

The function is in curried form. It expects a variable arg-set that is used to select/filter starting from the current location.

Each argument can be:
- a 'string' which is interpreted as descending into a property. The property should have an element (or child elements)
- an array of strings which is interpreted as the predicate `ofKind` 
- a function returning a list of locations (selector or motion function)
- a predicate function used to filter out nodes

Also implements the functions that can me used in the `select` function, in some of the following categories:
- selectors
  - `ancestors` an iterable of the location's ancestors; nearest parent first
  - `descendants`, an iterable of all descendants, depth-first, right-to-left
  - `children` an iterable of the location's children
  - `childrenAt(key)` an iterable of the location's children for the specified key
- predicates
  - `ofKind(kind)` a predicate function which will return true if the location points to an element the given kind

Closes #53
  • Loading branch information
andon authored Jan 12, 2019
1 parent 3667c11 commit 6abab00
Show file tree
Hide file tree
Showing 11 changed files with 820 additions and 2 deletions.
18 changes: 17 additions & 1 deletion packages/core/src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import { data, log, registry, zip, propNames } from '../'
import { data, log, registry, zip, skeleZip, propNames } from '../'

const anyFunction = expect.any(Function)
const anyString = expect.any(String)
Expand Down Expand Up @@ -63,6 +63,7 @@ test('core.zip interface', () => {

// edit
expect(zip.editCond).toEqual(anyFunction)
expect(zip.editAt).toEqual(anyFunction)

// walk
expect(zip.preWalk).toEqual(anyFunction)
Expand All @@ -87,6 +88,21 @@ test('core.zip interface', () => {
expect(zip.replace).toEqual(anyFunction)
expect(zip.edit).toEqual(anyFunction)
expect(zip.getChildren).toEqual(anyFunction)

// predicates
expect(zip.ofKind).toEqual(anyFunction)
expect(zip.propEq).toEqual(anyFunction)

// selectors
expect(zip.ancestors).toEqual(anyFunction)
expect(zip.descendants).toEqual(anyFunction)

// selectors (skele)
expect(skeleZip.children).toEqual(anyFunction)
expect(skeleZip.childrenAt).toEqual(anyFunction)

// select
expect(zip.select).toEqual(anyFunction)
})

test('core.log interface', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as data from './data'
import * as registry from './registry'
import * as zip from './zip'
import skeleZip from './zip/skele'
import * as skeleZip from './zip/skele'
import * as log from './log'
import * as propNames from './propNames'
import Cursor from './vendor/cursor'
Expand Down
60 changes: 60 additions & 0 deletions packages/core/src/zip/__tests__/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict'

import I from 'immutable'

import * as zip from '../'
import { childrenProperty, flow } from '../../data'

import { propEq } from '../predicate'

const zipper = data => zip.elementZipper({})(data)

const data = {
kind: ['navigation', 'tabs'],
[childrenProperty]: ['tabs', 'settings'],
title: 'App',
tabs: [
{
kind: ['tab'],
title: 'Murder Bot',
},
{
kind: ['tab'],
title: 'Dr Mensah',
[childrenProperty]: ['elements', 'deeplink'],
elements: [
{
kind: ['element'],
title: 'robot',
},
{
kind: ['element'],
title: 'human',
},
],
deeplink: {
kind: ['link'],
title: 'goodreads',
},
},
],
settings: {
kind: ['settings'],
title: 'Martha Wells',
},
}

describe('zipper predicates', () => {
test('propEq', () => {
const root = zipper(I.fromJS(data))
expect(
flow(
root,
propEq(
'settings',
I.fromJS({ kind: ['settings'], title: 'Martha Wells' })
)
)
).toBeTruthy()
})
})
Loading

0 comments on commit 6abab00

Please sign in to comment.