Normally Pleasant Mixture

npm

Need private packages and team management tools?Check out npm Orgs. »

@scuba-squad/type-array

1.0.1 • Public • Published

TypeArray

Status

Build Status Coverage Status

Table of Content

Purpose

TypeArray class definition

Installation

Via npm

npm install @scuba-squad/type-array

API

TypeArray(validator: function | string | RegExp | Array, message?: string | null)

Added in: v1.0.0

Factory method to get a function to create a Typed|Validated Array

arguments:

  1. validator: function | string | RegExp | Array
  2. message: string | null = 'invalid value'

returns: Function

throws: TypeError

const TypeArray = require('@scuba-squad/type-array');
const NumberArray = TypeArray((value) => {
  return typeof value === 'number';
});
const test = NumberArray(1, 5.4, -9);
console.log(test.length); // 3
console.log(test[0]); // 1
test.push(0); // 4
console.log(test[3]); // 0
test.shift(); // 3
console.log(test[0]); // 5.4
test.push('5'); // TypeError: invalid value
const TypeArray = require('@scuba-squad/type-array');
const NumberArray = TypeArray('isFloat');
const test = NumberArray(1, 5.4, -9);
console.log(test.length); // 3
console.log(test[0]); // 1
test.push(0); // 4
console.log(test[3]); // 0
test.shift(); // 3
console.log(test[0]); // 5.4
test.push('5'); // 4 (value is castable to float so passes isFloat validation)
test.push('asd'); // TypeError: invalid value
const TypeArray = require('@scuba-squad/type-array');
const NumberArray = TypeArray(/^-?\d+(?:\.\d+)?$/);
const test = NumberArray(1, 5.4, -9);
console.log(test.length); // 3
console.log(test[0]); // 1
test.push(0); // 4
console.log(test[3]); // 0
test.shift(); // 3
console.log(test[0]); // 5.4
test.push('5'); // 4 (value passes validation)
test.push('asd'); // TypeError: invalid value
const TypeArray = require('@scuba-squad/type-array');
const NumberArray = TypeArray(['isFloat', {min: -10, max: 10}]);
const test = NumberArray(1, 5.4, -9);
console.log(test.length); // 3
console.log(test[0]); // 1
test.push(0); // 4
console.log(test[3]); // 0
test.shift(); // 3
console.log(test[0]); // 5.4
test.push('5'); // 4 (value is castable to float so passes isFloat validation)
test.push(11); // TypeError: invalid value

TypeArray.ErrorArray(...args: Error)

Added in: v1.0.0

Function to create a predefined ErrorArray

arguments:

  1. ...args: Error

returns: Array

throws: TypeError

const {ErrorArray} = require('@scuba-squad/type-array');
const test = ErrorArray(new Error('a'));
console.log(test.length); // 1
console.log(test[0]); // Error: a
test.push(new TypeError('invalid'));
console.log(test[1]); // TypeError: invalid
test.push('hello'); // TypeError: value must be an Error

TypeArray.FunctionArray(...args: Function)

Added in: v1.0.0

Function to create a predefined FunctionArray

arguments:

  1. ...args: Function

returns: Array

throws: TypeError

const {FunctionArray} = require('@scuba-squad/type-array');
const test = FunctionArray(Number);
console.log(test.length); // 1
console.log(test[0]); // [Function: Number]
test.push(() => {});
console.log(test[1]); // [Function]
test.push('hello'); // TypeError: value must be a Function

TypeArray.PromiseArray(...args: Promise)

Added in: v1.0.0

Function to create a predefined PromiseArray

arguments:

  1. ...args: Promise

returns: Array

throws: TypeError

const {PromiseArray} = require('@scuba-squad/type-array');
const test = PromiseArray(Promise.resolve(5));
console.log(test.length); // 1
console.log(test[0]); // Promise { 5 }
test.push(Promise.reject('fail'));
console.log(test[1]); // Promise { <rejected> 'fail' }
test.push('hello'); // TypeError: value must be a Promise

Test

tests

npm install
npm test

License

MIT

install

npm i @scuba-squad/type-array

Downloadsweekly downloads

2

version

1.0.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability