-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #358 from microstates/cl/abstract-relationships
Abstract relationships.
- Loading branch information
Showing
6 changed files
with
159 additions
and
11 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
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,56 @@ | ||
import { Any } from './types'; | ||
import { view, Path } from './lens'; | ||
import { valueOf } from './meta'; | ||
|
||
export class Relationship { | ||
|
||
constructor(traverse) { | ||
this.traverse = traverse; | ||
} | ||
|
||
flatMap(sequence) { | ||
return new Relationship(edge => { | ||
let cell = this.traverse(edge); | ||
let next = sequence(cell); | ||
return next.traverse(edge); | ||
}); | ||
} | ||
|
||
map(fn) { | ||
return this.flatMap(cell => new Relationship(() => fn(cell))); | ||
} | ||
} | ||
|
||
export function relationship(definition) { | ||
if (typeof definition === 'function') { | ||
return new Relationship(definition); | ||
} else { | ||
return relationship(({ value }) => { | ||
if (value != null) { | ||
return { Type: Any, value }; | ||
} else { | ||
return { Type: Any, value: definition }; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export class Edge { | ||
constructor(parent, path) { | ||
this.parent = parent; | ||
this.path = path; | ||
} | ||
|
||
get name() { | ||
let [ name ] = this.path.slice(-1); | ||
return name; | ||
} | ||
|
||
get value() { | ||
return view(Path(this.path), this.parentValue); | ||
} | ||
|
||
get parentValue() { | ||
return valueOf(this.parent); | ||
} | ||
} |
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,73 @@ | ||
/* global describe, it, beforeEach */ | ||
|
||
import expect from 'expect'; | ||
|
||
import { Any } from '../src/types'; | ||
import { relationship, Edge } from '../src/relationship'; | ||
|
||
describe('relationships', () => { | ||
let rel; | ||
let e = (value) => new Edge(value, []); | ||
|
||
describe('with absolutely nothing specified', () => { | ||
beforeEach(() => { | ||
rel = relationship(10); | ||
}); | ||
|
||
it('reifies to using Any for the type and the passed value, }', () => { | ||
expect(rel.traverse(e(5))).toEqual({ Type: Any, value: 5 }); | ||
}); | ||
|
||
it('uses the supplied value if non is given', () => { | ||
expect(rel.traverse(e())).toEqual({ Type: Any, value: 10 }); | ||
}); | ||
}); | ||
|
||
describe('depending on the parent microstate that they are a part of', () => { | ||
beforeEach(() => { | ||
rel = relationship((value, parent) => { | ||
if (typeof parent === 'number') { | ||
return { Type: Number, value: Number(value) }; | ||
} else if (typeof parent === 'string') { | ||
return { Type: String, value: String(value) }; | ||
} else { | ||
return { Type: Object, value: Object(value) }; | ||
} | ||
}); | ||
}); | ||
it('generates numbers when the parent is a number', () => { | ||
expect(rel.traverse(5, 'parent')).toEqual({ Type: String, value: '5'}); | ||
}); | ||
|
||
it('generates strings when the parent is a string', () => { | ||
expect(rel.traverse(5, 0)).toEqual({ Type: Number, value: 5}); | ||
}); | ||
}); | ||
|
||
describe('that are mapped', () => { | ||
beforeEach(() => { | ||
rel = relationship() | ||
.map(({ value }) => ({Type: Number, value: value * 2 })); | ||
}); | ||
|
||
it('executes the mapping as part of the traversal', () => { | ||
expect(rel.traverse(e(3))).toEqual({Type: Number, value: 6}); | ||
}); | ||
}); | ||
|
||
describe('Edges', () => { | ||
let edge; | ||
beforeEach(() => { | ||
edge = new Edge({one: {two: 2}}, ['one', 'two']); | ||
}); | ||
it('knows its value', () => { | ||
expect(edge.value).toEqual(2); | ||
}); | ||
it('knows its parent value', () => { | ||
expect(edge.parentValue).toEqual({one: {two: 2}}); | ||
}); | ||
it('has a name provided it is not anonymous', () => { | ||
expect(edge.name).toEqual('two'); | ||
}); | ||
}); | ||
}); |