Skip to content

Instantly share code, notes, and snippets.

@codingedgar
Last active August 30, 2018 14:51
Show Gist options
  • Select an option

  • Save codingedgar/4920a158d5f5384522ac4bb749661711 to your computer and use it in GitHub Desktop.

Select an option

Save codingedgar/4920a158d5f5384522ac4bb749661711 to your computer and use it in GitHub Desktop.
until transducer: transducer
interface title {
title: String
}
interface task extends title {
type: type
due_date: DueDate
}
interface in_two_hours_analysis {
['in 2 hours']: [
{
title: String
}
]
}
type in_two_hours_analysis_any = in_two_hours_analysis | any // 😉 for readability purposes
type DueDate = 'in 1 hours' | 'in 2 hours' | 'in 3 hours'
type type = 'task' | 'date'
import { times } from "ramda";
import { random } from 'faker';
import { tasks2inTwoHoursAnalysis } from "./taskList2inTwoHoursAnalysis";
describe('tasks2inTwoHoursAnalysis', () => {
it('should return basic structure', () => {
const input: task[] = [
{
title: 'some dope task',
type: 'task',
due_date: 'in 2 hours'
}
]
const output = {
'in 2 hours': [
{
title: 'SOME DOPE TASK'
}
]
}
expect(
tasks2inTwoHoursAnalysis(input)
)
.toEqual(output)
})
describe('when due date its of interest', () => {
let input: task[]
let output: in_two_hours_analysis
beforeAll(() => {
const input_times =
times(
() =>
({
title: random.words(),
due_date: random.arrayElement<DueDate>(['in 2 hours']),
type: random.arrayElement<type>(['task', 'date'])
})
)
input = input_times(100)
output = tasks2inTwoHoursAnalysis(input)
})
it('should have the same length as input', () => {
expect(
output['in 2 hours']
)
.toHaveLength(100)
})
it('should have same title as input but uppercase', () => {
input
.forEach(
(task, index) => {
expect(output['in 2 hours'][index].title === task.title.toUpperCase())
}
)
})
})
it('should filter not desired due dates', () => {
const input_times =
times(
() =>
({
title: random.words(),
due_date: random.arrayElement<DueDate>(['in 1 hours', 'in 3 hours']),
type: random.arrayElement<type>(['task', 'date'])
})
)
const input = input_times(100)
expect(
tasks2inTwoHoursAnalysis(input)['in 2 hours']
)
.toHaveLength(0)
})
})
import { compose, map, filter, transduce } from 'ramda'
export function tasks2inTwoHoursAnalysis(input: task[]): in_two_hours_analysis {
return transduce(
// Morphisms (transformations)
// can be compose and are executed from top to bottom (or left to right),
// and must use the transduce protocol
compose(
filter(({ due_date }: task) => due_date.includes('in 2 hours')),
map(({ title }) => ({ title })),
map<any, any>(({ title }: title) => ({ title: title.toUpperCase() })),
),
// Reducer
(accumulator: in_two_hours_analysis_any, current: title) => (
{
...accumulator,
['in 2 hours']: [
...accumulator['in 2 hours'],
current
]
}
),
// Initial value of reducer
{
['in 2 hours']: []
},
// Input
input
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment