2012-04-16

Struct.js ver. 0.3をリリースしました

欲しかった機能が実装できたのでタグを切りました。

主な機能追加

  • ネストした構造のサポート
  • nullableオプションの追加
  • カスタムバリデーション関数のサポート
  • 設定で全チェックを無効化する機能を追加
  • 初期作成時の値チェックを追加
型チェックのためのコーディングをなるべく増やさない、というポリシーです。なので、素のObjectと同様に扱えるが必要なチェック機構は備えている、という物を目指しています。しかし副作用としてプロパティの有無でそれが何であるかを判定するダックタイピングは不可能になってしまったので、そこだけは Struct.getType(obj) を使います。

リリース用の設定

Proxyオブジェクトを生成しないので、通常のプロパティアクセスのコストだけになる。
1
2
3
4
Struct.configure({
  // 全てのチェック無効化
  'disable any check': true
});

バリデーション関数のサポート

1
2
3
4
5
6
7
8
9
Struct.define('Size', {
  width:  {type: 'number', cond: 'v >= 0'},
  height: {type: 'number', cond: function(v){return v >= 0} /*same as ↑*/}
});
 
var size = Struct.create('Size', {
  width: 0,
  height: 0
});

ネスト構造のサポート

Struct.create にネストしたObjectをそのまま食わせても大丈夫。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Struct.define('Position', {
  x: {type: 'number', nullable: false},
  y: {type: 'number', nullable: false}
});
 
Struct.define('Size', {
  width:  {type: 'number', nullable: false, cond: 'v >= 0'},
  height: {type: 'number', nullable: false, cond: 'v >= 0'}
});
 
Struct.define('Rect', {
  pos:  {type: 'struct:Position', nullable: false},
  size: {type: 'struct:Size', nullable: false},
  createdAt: {type: 'date', writable: false}
});
 
var rect = Struct.create('Rect', {
  pos: {x: 0, y: 0},
  size: {width: 100, height: 100},
  createdAt: new Date()
});
 
rect.pos.x = "200"; // => type mismatch error
rect.size.height = -1; // => condition formula validation error

今後の予定

次は関数呼び出しのパラメータチェックを実装する予定。