Skip to content

Latest commit

 

History

History
104 lines (68 loc) · 1.75 KB

arraySpecs.md

File metadata and controls

104 lines (68 loc) · 1.75 KB

Specs for arrays

1. Specifying that a variable is an array

Use ARRAY passing it no arguments:

import s from "./speco";

const arraySpec = s.ARRAY();
 
s.isValid(arraySpec, []);
// => true

s.isValid(arraySpec, ["a", 1, {}]);
// => true

s.isValid(arraySpec, [1, 2]);
// => true

s.isValid(arraySpec, 1);
// => false

s.isValid(arraySpec, "a");
// => false

s.isValid(arraySpec, {});
// => false

function SomeObject() {
  this.a = 1;
}
s.isValid(arraySpec, new SomeObject());
// => false

s.isValid(arraySpec, () => {});
// => false

2. Specifying arrays with any number of elements of the same type

Use ARRAY_OF passing it the spec that all its elements follow:

import s from "./speco";

const arraySpec = s.ARRAY_OF(s.STRING);
 
s.isValid(arraySpec, []);
// => true

s.isValid(arraySpec, ["a", "b"]);
// => true

s.isValid(arraySpec, ["a", 1, {}]);
// => false

s.isValid(arraySpec, [1, "l"]);
// => false

3. Specifying arrays with any number of elements of the same type and no duplicates

Use ARRAY_OF_DISTINCT passing it the spec that all its elements follow:

import s from "./speco";

const arraySpec = s.ARRAY_OF_DISTINCT(s.STRING);

s.isValid(arraySpec, ["a"]);
// => true

s.isValid(arraySpec, ["a", "b"]);
// => true

s.isValid(arraySpec, ["a", "a"]);
// => false

s.isValid(arraySpec, ["a", 1, {}]);
// => false

4. Specifying arrays with a given number of elements with given types

Use ARRAY passing it the specs for each of the elements of the array:

import s from "./speco";

const arraySpec = s.ARRAY(s.STRING, s.NUM);
 
s.isValid(arraySpec, ["a", 1]):
// => true

s.isValid(arraySpec, ["a", 1, {}]);
// => false

s.isValid(arraySpec, [3, 1]);
// => false

s.isValid(arraySpec, [1, 2, []]);
// => false