Skip to content

Commit

Permalink
Added a tuple type
Browse files Browse the repository at this point in the history
Support was already pretty much built-in, but was not surfaced.
  • Loading branch information
ddsol committed Jun 22, 2016
1 parent b4d3fde commit 2f4ab88
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//Export types
export { default as union } from './types/union';
export { default as tuple } from './types/tuple';
export { default as Any } from './types/any';
export { default as Nil } from './types/nil';
export { default as reference } from './types/reference';
Expand Down
4 changes: 3 additions & 1 deletion src/parse/parse-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function parseObjectType(options, type, arrayType) {
, virtuals = {}
, methods = {}
, meta = {}
, kind = arrayType ? 'array' : 'object'
, kind = arrayType ? ( type.length === 1 ? 'array' : 'tuple' ) : 'object'
, prototype
, thisType
, restType
Expand Down Expand Up @@ -290,6 +290,8 @@ export default function parseObjectType(options, type, arrayType) {
, packed
;

name = String(name);

if (propNames.indexOf(name) !== -1) {
type = properties[name];
} else {
Expand Down
22 changes: 22 additions & 0 deletions src/types/tuple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import parseObjectType from '../parse/parse-object';
import { isArray } from '../utils';

export default function tuple(...types) {
if (!types.length) throw new TypeError('Tuple requires item types.');

if (types.length === 1) {
if (!types[0] || !isArray(types[0]) || types[0].length<2) throw new TypeError('Tuple requires multiple item types.');
types = types[0];
}

function Tuple(options) {
let type = parseObjectType(options, types, true);

type.name = `tuple(${Object.keys(type.properties).map(prop => type.properties[prop].kind).join(', ')})`;
return type;
}

Tuple.isType = true;
return Tuple;
}

0 comments on commit 2f4ab88

Please sign in to comment.