javascript icon indicating copy to clipboard operation
javascript copied to clipboard

Implement new Concept Exercise: type-checking

Open SleeplessByte opened this issue 5 years ago • 1 comments

This issue describes how to implement the type-checking concept exercise for the javascript track.

Getting started

Here you can read about what Concept Exercises are and how they are structured:

If you have not done so yet, it is probably also helpful to do a couple of "Learning Exercises" (this is how they are called on the site) yourself. You can also look at the code of an existing concept exercise like bird-watcher (concept for-loops) for reference.

See the documentation above (general documentation), as well as How to implement a Concept Exercise in JavaScript.

Goal

The goal of this exercise is to teach the student how they can type-check their values at run-time.

JavaScript specificness

ℹ Many languages have a way to check a value against a type .is_a? instance of etc. JavaScript is special:

  • typeof only work for primitives
  • typeof null is object
  • instanceof only works for prototype inheritance
  • Array.isArray is not the same as instanceof Array
  • 'property' in object
  • object.hasOwnProperty('property')
  • duck typing (array like, partial shapes, etc.)

Learning objectives

  • How to use typeof for primitives (e.g. implement isNumeric,isString)
  • How to create a sane type-checking function:
    • isNumber should check if the value is finite (ie not NaN)
    • isObject should check if the value is actually an object (so not null)
    • isNumericString should check if the value is a string but only consists of numbers
    • etc.
  • How to check for emptiness:
    • isEmptyArray should check if the value is an array and empty
    • isNonEmptyArray should check if the value is an array and not empty
    • etc.

You should have the student create a large number of very simple functions. So the learning objectives are all around the same object (usage of typeof, instanceof and property checking), but we want to give the student something they can use in real life.

Consider also having a few assertXXX variants: These use the check above (boolean result) and return self if valid, or throw an Error when invalid.

Take your inspiration from this library. Please add to references if you end up using the code to create your implementation.

Out of scope

  • Boxed types (use after.md to teach type checking boxed primitives, e.g. isString(val) => typeof val === 'string or val instanceof String)
  • JSON Schema or similar

Concepts

  • type-checking

Prerequisites

  • basics
  • ... (do you think something is missing?)

After

Boxed types, common mistakes, and why typeof null returns object.

Help

You can choose to do this solo-style, or collaborate with multiple people on this. My suggestion would be is to

  1. first accept this issue by saying "I'd like to work on this" (you will not get a response from me. Just go with it) and optionally request that someone works with you (and wait for a second person to accept your request).
  2. Use this issue to write down what you need (collect reference material) and discuss, if necessary, what to write and what not to write.
  1. Create a PR and set "exercism/javascript" as reviewers. Additionally you can write in #maintaining-javascript that your PR is ready for review. Once you incorporated any critical feedback that the reviewer might give you and the PR is approved, it will be merged by a maintainer.

SleeplessByte avatar Jul 10 '20 14:07 SleeplessByte

I'll take a shot at this.

rishiosaur avatar Aug 04 '20 16:08 rishiosaur