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

Make editCond to work with all kinds (specific or abstract) #132

Merged
merged 1 commit into from
Mar 12, 2019
Merged
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
7 changes: 4 additions & 3 deletions packages/core/src/zip/__tests__/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Editing a Zipper', () => {
name: 'andon',
children: [
{
kind: 'tm',
kind: ['tm', 'yoga'],
name: 'blagoja',
},
{
Expand Down Expand Up @@ -60,6 +60,7 @@ describe('Editing a Zipper', () => {
item => item.get('name') === 'andon',
item => item.set('name', 'sikavica'),
],
[['tm', 'yoga'], item => item.update('name', name => `yoga-${name}`)],
['tm', item => item.update('name', name => `member-${name}`)],
],
elementZipper
Expand Down Expand Up @@ -90,8 +91,8 @@ describe('Editing a Zipper', () => {
name: 'sikavica', // changed
children: [
{
kind: 'tm',
name: 'member-blagoja', // changed
kind: ['tm', 'yoga'],
name: 'yoga-member-blagoja', // changed
},
{
kind: 'tm',
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/zip/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { postWalk } from '../zip'
import * as zip from './impl'
import { curry } from 'ramda'

import Registry from '../registry/Registry'
import MultivalueRegistry from '../registry/MultivalueRegistry'

export const editCond = (patterns, loc) => {
const kinds = new Registry()
const kinds = new MultivalueRegistry()
const preds = []

patterns.forEach(([pred, f]) => {
Expand All @@ -22,9 +22,7 @@ export const editCond = (patterns, loc) => {
const editFn = l =>
postWalk(el => {
const kind = kindOf(el)
const f = kind != null && kinds.get(kind)

if (f != null) el = f(el)
el = kinds.get(kind).reduce((el, f) => f(el), el)

const after = preds.reduce((el, [pred, f]) => (pred(el) ? f(el) : el), el)
return after
Expand Down