Comparing version 0.4.3 to 0.4.4
@@ -17,3 +17,5 @@ 'use strict'; | ||
var factory = function factory(def) { | ||
var factory = function factory() { | ||
var def = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var prop_info = Object.keys(def).map(function (key) { | ||
@@ -35,2 +37,5 @@ var _ref = def[key] instanceof _mutability.Mutability ? { type: def[key].data, mutable: def[key].mutable } : { type: def[key], mutable: false }; | ||
if (!(this instanceof struct)) { | ||
return new struct(props); | ||
} | ||
prop_info.forEach(function (_ref2) { | ||
@@ -58,7 +63,3 @@ var name = _ref2.name; | ||
var creator = function creator(props) { | ||
return new struct(props); | ||
}; | ||
creator.toString = function () { | ||
struct.toString = function () { | ||
var props = prop_info.map(function (prop) { | ||
@@ -70,7 +71,5 @@ return prop.name + ': ' + prop.type; | ||
struct.prototype = creator.prototype; | ||
return creator; | ||
return struct; | ||
}; | ||
exports.default = factory; |
{ | ||
"name": "rusted", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "Rust like enum, Result and Option for javascript", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -19,2 +19,4 @@ [![npm version](https://badge.fury.io/js/rusted.svg)](https://badge.fury.io/js/rusted) | ||
+ [x] mutable property | ||
+ [ ] tuple struct | ||
+ [ ] unit struct | ||
- [x] `impl` | ||
@@ -21,0 +23,0 @@ - [x] static method (associated function) |
@@ -7,3 +7,3 @@ import panic from './panic'; | ||
let factory=function(def){ | ||
let factory=function(def={}){ | ||
let prop_info=Object.keys(def).map((key)=>{ | ||
@@ -21,2 +21,5 @@ let {type,mutable}=def[key] instanceof Mutability | ||
let struct=function(props){ | ||
if(!(this instanceof struct)){ | ||
return new struct(props); | ||
} | ||
prop_info.forEach(({name,type,mutable})=>{ | ||
@@ -39,16 +42,10 @@ name in props | ||
let creator=(props)=>{ | ||
return new struct(props); | ||
}; | ||
creator.toString=()=>{ | ||
let props=prop_info.map((prop)=>`${prop.name}: ${prop.type}`).join(','); | ||
struct.toString=function(){ | ||
let props=prop_info.map(prop=>`${prop.name}: ${prop.type}`).join(','); | ||
return `rusted struct type: {${props} }`; | ||
}; | ||
struct.prototype=creator.prototype; | ||
return creator; | ||
return struct; | ||
}; | ||
export default factory; |
@@ -5,2 +5,4 @@ import {expect} from 'chai'; | ||
import impl from '../src/impl'; | ||
import {imm,mut} from '../src/mutability'; | ||
@@ -91,2 +93,17 @@ | ||
}); | ||
it('should works properly if no arguments (Unit-like struct)',()=>{ | ||
let Unit=struct(); | ||
impl(Unit,{ | ||
foo(){ | ||
return 3; | ||
}, | ||
bar(self){ | ||
return 4; | ||
} | ||
}); | ||
let value=Unit(); | ||
expect(value.bar()).to.equal(4); | ||
expect(Unit.foo()).to.equal(3); | ||
}); | ||
it('should works properly',()=>{ | ||
@@ -93,0 +110,0 @@ let OtherStruct=struct({ |
Sorry, the diff of this file is not supported yet
65470
2194
259