@scuba-squad/type-array
v1.0.8
Published
TypeArray is a Proxy allowing you to define validation of values that may be set
Downloads
4
Maintainers
Readme
TypeArray
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
npm install
npm test