Socket
Book a DemoInstallSign in
Socket

@scuba-squad/type-array

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@scuba-squad/type-array

TypeArray is a Proxy allowing you to define validation of values that may be set

latest
Source
npmnpm
Version
1.0.8
Version published
Maintainers
1
Created
Source

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:

  • validator: function | string | RegExp | Array
  • 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:

  • ...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:

  • ...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:

  • ...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

Keywords

scuba-squad

FAQs

Package last updated on 02 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts