-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
An element can include a property `@@girders-elements/children` which denotes *which of it's properties contain children elements*. This is an optional property, and can contain: - a *string* denoting a single property name, (e.g. `'@@girders-elements/children': 'children'`) - an *array of strings* denoting which properties contain child elements *A property containing child elements* can either: - contain a single element - contain an array of elements E.g. ```javascript { kind: ['container', 'two-column'], '@@girders-elements/children': ['main', 'aside'], main: [ { kind: 'tile', content: [ ], }, // ... ], aside: { kind: 'weather', content: [], } } ``` This feature enhances the `zip.elementZipper`
- Loading branch information
Showing
14 changed files
with
1,536 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules/ | ||
dist/ | ||
coverage/ | ||
packages/core/src/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,182 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
import { fromJS } from 'immutable'; | ||
import { zip } from '..'; | ||
import { fromJS } from 'immutable' | ||
import { zip, data } from '..' | ||
|
||
describe('Zipper', () => { | ||
const childCollectionKind = '@@girders-elements/child-collection' | ||
|
||
describe('Zipper', () => { | ||
const singleChild = { | ||
kind: 'parent', | ||
children: [ | ||
{ | ||
kind: 'lvl1', | ||
children: [ | ||
{ | ||
kind: 'lvl2' | ||
} | ||
] | ||
} | ||
] | ||
kind: 'lvl2', | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
|
||
it('zipper should correctly navigate up and down', () => { | ||
const zipper = zip.elementZipper(fromJS(singleChild), 'children') | ||
expect(zipper.value().get('kind')).toEqual('parent') | ||
expect(zipper.down().value().size).toEqual(1) // the children collection level | ||
expect(data.isOfKind(childCollectionKind, zipper.down().value())).toBe(true) | ||
expect(zipper.down().down().value().get('kind')).toEqual('lvl1') | ||
expect(zipper.down().down().down().value().size).toEqual(1) // the children collection level | ||
expect(zipper.down().down().down().down().value().get('kind')).toEqual('lvl2') | ||
expect( | ||
data.isOfKind(childCollectionKind, zipper.down().down().down().value()) | ||
).toBe(true) | ||
expect(zipper.down().down().down().down().value().get('kind')).toEqual( | ||
'lvl2' | ||
) | ||
expect(zipper.down().down().down().down().down()).toBeNull() | ||
expect(zipper.down().up().value().get('kind')).toEqual('parent') | ||
expect(zipper.down().down().down().up().value().get('kind')).toEqual('lvl1') | ||
}); | ||
}) | ||
|
||
const multipleChildren = { | ||
id: 1, | ||
kind: 't', | ||
children: [ | ||
{ | ||
id: 2, | ||
kind: 't', | ||
children: [ | ||
{ | ||
id: 3 | ||
id: 3, | ||
kind: 't', | ||
}, | ||
{ | ||
id: 4 | ||
} | ||
] | ||
kind: 't', | ||
id: 4, | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 5, | ||
kind: 't', | ||
children: [ | ||
{ | ||
id: 6 | ||
kind: 't', | ||
id: 6, | ||
}, | ||
{ | ||
id: 7 | ||
} | ||
] | ||
kind: 't', | ||
id: 7, | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 8, | ||
kind: 't', | ||
children: [ | ||
{ | ||
id: 9 | ||
kind: 't', | ||
id: 9, | ||
}, | ||
{ | ||
id: 10 | ||
} | ||
] | ||
} | ||
] | ||
kind: 't', | ||
id: 10, | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
|
||
it('zipper should correctly navigate up down left and right', () => { | ||
const zipper = zip.elementZipper(fromJS(multipleChildren), 'children') | ||
|
||
expect(zipper.value().get('id')).toEqual(1) | ||
expect(zipper.down().value().size).toEqual(3) // children collection level | ||
expect(data.isOfKind(childCollectionKind, zipper.down().value())).toBe(true) | ||
expect(zipper.down().down().value().get('id')).toEqual(2) | ||
expect(zipper.down().down().right().value().get('id')).toEqual(5) | ||
expect(zipper.down().down().right().right().value().get('id')).toEqual(8) | ||
expect(zipper.down().down().right().right().left().value().get('id')).toEqual(5) | ||
expect(zipper.down().down().right().right().left().up().value().size).toEqual(3) | ||
expect(zipper.down().down().right().right().left().up().up().value().get('id')).toEqual(1) | ||
}); | ||
expect( | ||
zipper.down().down().right().right().left().value().get('id') | ||
).toEqual(5) | ||
expect( | ||
data.isOfKind( | ||
childCollectionKind, | ||
zipper.down().down().right().right().left().up().value() | ||
) | ||
).toBe(true) | ||
expect( | ||
zipper.down().down().right().right().left().up().up().value().get('id') | ||
).toEqual(1) | ||
}) | ||
|
||
const multipleChildrenElements = { | ||
id: 1, | ||
kind: 't', | ||
left: [ | ||
{ | ||
id: 2 | ||
} | ||
kind: 't', | ||
id: 2, | ||
}, | ||
], | ||
right: [ | ||
{ | ||
id: 3 | ||
kind: 't', | ||
id: 3, | ||
}, | ||
{ | ||
id: 4 | ||
} | ||
] | ||
kind: 't', | ||
id: 4, | ||
}, | ||
], | ||
} | ||
|
||
it('zipper multiple children elements', () => { | ||
const zipper = zip.elementZipper(fromJS(multipleChildrenElements), ['left', 'right']) | ||
const zipper = zip.elementZipper(fromJS(multipleChildrenElements), [ | ||
'left', | ||
'right', | ||
]) | ||
|
||
expect(zipper.value().get('id')).toEqual(1) | ||
expect(zipper.down().value().size).toEqual(1) // children collection node for left | ||
expect(zipper.down().right().value().size).toEqual(2) // children collection node for right | ||
expect(zipper.down().value().get('propertyName')).toEqual('left') | ||
|
||
expect(zipper.down().right().value().get('propertyName')).toEqual('right') | ||
|
||
expect(zipper.down().down().value().get('id')).toEqual(2) | ||
expect(zipper.down().right().down().value().get('id')).toEqual(3) | ||
expect(zipper.down().right().down().right().value().get('id')).toEqual(4) | ||
}) | ||
|
||
const withChildrenPositions = { | ||
id: 1, | ||
kind: 'tX', | ||
'@@girders-elements/children': ['left', 'right'], | ||
|
||
left: [ | ||
{ | ||
kind: 't', | ||
id: 2, | ||
}, | ||
], | ||
right: [ | ||
{ | ||
kind: 't', | ||
id: 3, | ||
}, | ||
{ | ||
kind: 't', | ||
id: 4, | ||
}, | ||
], | ||
} | ||
|
||
it('supports the @@girders-elements/children hint for child positions', () => { | ||
const zipper = zip.elementZipper(fromJS(withChildrenPositions)) | ||
|
||
expect(zipper.value().get('id')).toEqual(1) | ||
expect(zipper.down().value().get('propertyName')).toEqual('left') | ||
|
||
expect(zipper.down().right().value().get('propertyName')).toEqual('right') | ||
|
||
expect(zipper.down().down().value().get('id')).toEqual(2) | ||
expect(zipper.down().right().down().value().get('id')).toEqual(3) | ||
expect(zipper.down().right().down().right().value().get('id')).toEqual(4) | ||
}); | ||
}); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
'use strict'; | ||
|
||
import * as element from './element'; | ||
import * as element from './element' | ||
export * from './element' | ||
|
||
export { | ||
element | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Tommi Kaikkonen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { makeZipper } from './zipper'; | ||
|
||
/** | ||
* Zipper for nested Arrays. | ||
* | ||
* Don't use with new keyword - use the function plainly | ||
* or with `ArrayZipper.from([1, 2, 3])`. | ||
* | ||
* @param {Array} arr - the data structure to make a zipper for | ||
* @return {Zipper} | ||
*/ | ||
export const ArrayZipper = makeZipper( | ||
arr => !!arr.length, | ||
arr => arr, | ||
(_, children) => children, | ||
); | ||
|
||
export default ArrayZipper; |
Oops, something went wrong.