Type and Form

typeof is the only native tool for reflection in javascript and it is poorly aging.

Collect has a broad set of type and form description. form is mainly a description of compound data types -- iterable multi-value containers like Array and Map. Everything which is not a container is described as scalar; Collecting a scalar will make most of the Collect methods throw error.

💡

The one exception is that strings, given their importance, are treated as collections of characters that are iterable. For now think of a String collection as a collection around an array of single characters.

All the Types

Type string

Form string

The value (string) of the read-only .type property are:

  • string
  • number
  • date
  • null
  • symbol
  • undefined
  • (all the return values of Form below - except scalar)

The returnable values of the read-only .form property are:

  • array
  • map
  • object
  • set
  • scalar
  • function

What can I put in a Collection?

You can put anything into a collection. However -- outside of these type - reflection properties almost ALL of the properties / methods that require iterative procedures will fail (probably throw) if your value is not an iterable.

Currently, arrays, Objects, Maps, and Sets can be used as iterable collection stores. You can however put anything into a collection, and you can use the type/form values to analyze it.

💡

Looking at the source you may notice an "any" type; this is used in other contexts to disable type constraints, but is not a returnable value in Collect.

import collect from '@wonderlandlabs/collect';
const stringC = collect('along time ago');
const objC = collect({x: 1, y: 2, z: 3});
const numC = collect(42);
console.log('stringC type:', stringC.type) // 'string';
console.log('objC.type', objC.type) // object;
console.log('numC.type', numC.type) // number;
console.log('stringC form:', stringC.form) // 'scalar';
console.log('objC.form', objC.form) // 'object';
console.log('numC.form', numC.form) // 'scalar';
console.log('stringC.get(1)', stringC.get(1)); // 'l';
console.log('objC.get("x")', objC.get('x')); // 1;
try {
numC.get(1);
} catch (err) {
console.log('numC is not iterable:', err.message);
}