array-type

0.1.0 • Public • Published

array-type npm

Build Status Code Climate Test Coverage downloads license

The array type. See the type-info for type info collections.

Usage

//register the array type to the TypeInfo..
require('array-type')
var TypeInfo = require('abstract-type')
//get the array type info:
var ArrayType = TypeInfo('array')
/*
//Just load array type only:
var ArrayTypeInfo = require('array-type')
var ArrayType = ArrayTypeInfo()
 */
var LimitedArrayType = TypeInfo('array', min:1, max:3)
 
var arr = ArrayType.create([1,2,3])
//var arr = ArrayType.createValue([1,2,3]) //it's the same.
 
console.log(arr)
//<type "array": "value":[1,2,3]>
console.log(arr.isValid())
//=true
console.log(arr.toJson()))
//='[1,2,3]'
console.log(arr.toJson({withType:true})))
//='{"value":[1,2,3],"name":"array"}'
console.log(LimitedArrayType.isValid([]))
//=false
console.log(LimitedArrayType.isValid([1]))
//=true
var n = arr.clone();
n.assign('aaa')
//=TypeError: "aaa" is an invalid array

API

  • The ArrayType Class
    • options:
      • min: the minimum length of the array. null or undefined means unlimited.
      • max: the maximum length of the array.

See abstract-type

TODO

  • of option to limit the element's type.
    • eg, ArrayType of: 'String'

License

MIT

Package Sidebar

Install

npm i array-type

Weekly Downloads

2

Version

0.1.0

License

MIT

Last publish

Collaborators

  • riceball