Practical

intermediate6 of 6

Learn about practical in fp-ts nonemptyarray

Code Editor

06-practical.exercise.ts

💻
Loading editor...
Preparing Monaco Editor with TypeScript support

Test Results

Requirements

describe('NonEmptyArray practical examples', () => {
  const logs: LogEntry[] = [
    { timestamp: 1, message: 'Started', level: 'info' },
    { timestamp: 2, message: 'Warning!', level: 'warn' },
    { timestamp: 3, message: 'Failed', level: 'error' },
    { timestamp: 4, message: 'Completed', level: 'info' },
  ]

  it('gets latest log', () => {
    const result = getLatestLog(logs)
    expect(O.isSome(result)).toBe(true)
    if (O.isSome(result)) {
      expect(result.value.timestamp).toBe(4)
    }
  })

  it('returns None for empty logs', () => {
    const result = getLatestLog([])
    expect(O.isNone(result)).toBe(true)
  })

  it('filters error logs', () => {
    const nonEmptyLogs = NEA.fromArray(logs)
    expect(O.isSome(nonEmptyLogs)).toBe(true)
    if (O.isSome(nonEmptyLogs)) {
      const result = getErrorLogs(nonEmptyLogs.value)
      expect(O.isSome(result)).toBe(true)
      if (O.isSome(result)) {
        expect(result.value.length).toBe(1)
        expect(result.value[0].level).toBe('error')
      }
    }
  })

  it('gets most recent error', () => {
    const nonEmptyLogs = NEA.fromArray(logs)
    expect(O.isSome(nonEmptyLogs)).toBe(true)
    if (O.isSome(nonEmptyLogs)) {
      const result = getMostRecentError(nonEmptyLogs.value)
      expect(O.isSome(result)).toBe(true)
      if (O.isSome(result)) {
        expect(result.value.message).toBe('Failed')
      }
    }
  })
})
🧪

Ready to Test?

Click "Run Tests" to see how your code performs

Pro Tips

💡 Stuck? Here's what to try:

  • • Read the comments in the code carefully
  • • Run tests frequently to get feedback
  • • Check the fp-ts documentation
  • • Use the solution if you need help

🚀 Learning Approach:

  • • Focus on understanding, not just solving
  • • Experiment with different approaches
  • • Think about real-world applications
  • • Build on previous exercises