New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rusted

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rusted - npm Package Compare versions

Comparing version 0.3.1 to 0.4.0

lib/trait.js

24

lib/impl.js

@@ -11,16 +11,26 @@ 'use strict';

var _trait = require('./trait');
var _util = require('./util');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var impl = function impl(to, what) {
var setter = to.prototype ? function (name, fn) {
to.prototype[name] = function () {
var impl = function impl() /*trait?, target, block*/{
var trait = arguments[0] instanceof _trait.Trait ? arguments[0] : null,
target = trait ? arguments[1] : arguments[0],
block = trait ? arguments[2] : arguments[1];
var setter = target.prototype ? function (name, fn) {
(0, _util.is_static_method)(fn) ? target[name] = fn : target.prototype[name] = function () {
return fn.apply({}, [this].concat(Array.prototype.slice.call(arguments, 0)));
};
} : (0, _panic2.default)('Cannot implementate to ' + to);
} : (0, _panic2.default)('Cannot implementate to ' + target);
for (var name in what) {
setter(name, what[name]);
}
trait ? Object.keys(trait).forEach(function (name) {
block && name in block ? setter(name, block[name]) : (0, _util.is_empty_function)(trait[name]) ? (0, _panic2.default)('A selected trait needs to implementate method [' + name + '] !') : setter(name, trait[name]);
}) : Object.keys(block).forEach(function (name) {
setter(name, block[name]);
});
};
exports.default = impl;

@@ -15,2 +15,6 @@ 'use strict';

var _trait = require('./trait');
var _trait2 = _interopRequireDefault(_trait);
var _panic = require('./panic');

@@ -45,4 +49,5 @@

impl: _impl2.default,
trait: _trait2.default,
panic: _panic2.default,
type: _type2.default
};
{
"name": "rusted",
"version": "0.3.1",
"version": "0.4.0",
"description": "Rust like enum, Result and Option for javascript",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -16,4 +16,4 @@ [![npm version](https://badge.fury.io/js/rusted.svg)](https://badge.fury.io/js/rusted)

- [x] `impl`
- [ ] static method
- [ ] `trait`
- [x] static method (associated function)
- [x] `trait`
- [ ] `iter`

@@ -20,0 +20,0 @@ - [x] type alias

import panic from './panic';
let impl=(to,what)=>{
let setter=to.prototype
import {Trait} from './trait';
import {is_static_method,is_empty_function} from './util';
let impl=function(/*trait?, target, block*/){
let trait=arguments[0] instanceof Trait
? arguments[0]
: null ,
target=trait
? arguments[1]
: arguments[0] ,
block=trait
? arguments[2]
: arguments[1] ;
let setter=target.prototype
? (name,fn)=>{
to.prototype[name]=function(){
return fn.apply({},[this].concat(Array.prototype.slice.call(arguments,0)));
};
is_static_method(fn)
? (target[name]=fn)
: (target.prototype[name]=function(){
return fn.apply({},[this].concat(Array.prototype.slice.call(arguments,0)));
});
}
: panic(`Cannot implementate to ${to}`) ;
: panic(`Cannot implementate to ${target}`) ;
for(let name in what){
setter(name,what[name]);
}
trait
? Object.keys(trait).forEach((name)=>{
block && name in block
? setter(name,block[name])
: (is_empty_function(trait[name])
? panic(`A selected trait needs to implementate method [${name}] !`)
: setter(name,trait[name])
);
})
: Object.keys(block).forEach((name)=>{
setter(name,block[name]);
});
};
export default impl;
import Enum from './enum';
import match from './match';
import impl from './impl';
import trait from './trait';
import panic from './panic';

@@ -22,4 +23,5 @@ import struct from './struct';

impl,
trait,
panic,
type
};

@@ -7,2 +7,4 @@ let expect=require('chai').expect;

import impl from '../src/impl';
import type from '../src/type';
import trait from '../src/trait';

@@ -60,2 +62,5 @@ describe('impl.js',()=>{

});
},
fuga(){
return Foo.Foo;
}

@@ -66,2 +71,3 @@ });

expect(foo.hoge(5)).to.equal(5);
expect(Foo.fuga().hoge(3)).to.equal(3);
});

@@ -76,2 +82,5 @@ it('should works properly for struct',()=>{

return `(${self.x},${self.y})`;
},
from_array(arr){
return Position({x:arr[0],y:arr[1]});
}

@@ -84,4 +93,91 @@ });

expect(pos.toString()).to.equal('(0,0)');
expect(Position.from_array([2,3]).toString()).to.equal('(2,3)');
});
it('should sets static method to constructor directly (associated function)',()=>{
let Foo=struct({
x:type.i32,
y:type.i32
});
impl(Foo,{
new(x,y){
return Foo({x:x||0,y:y||0});
}
});
expect(Foo).to.have.property('new');
expect(Foo.new(0,0)).to.be.instanceof(Foo);
});
it('should works on primitive type properly',()=>{
impl(Array,{
foo(el){
return el.reverse();
},
bar(self){
return self.map(n=>n*2).join(',');
}
});
expect(Array.foo([1,2,3]).bar()).to.equal('6,4,2');
});
it('should works with trait',()=>{
let FooTrait=trait({
foo(){
return true;
},
hoge(self){
return true;
},
new(){
},
print(self){}
});
let Bar=struct({
x:type.i32,
y:type.i32
});
impl(FooTrait,Bar,{
new(){
return Bar({x:0,y:0});
},
print(self){
return `${self.x},${self.y}`;
}
});
expect(Bar.foo()).to.be.true;
let bar=Bar({
x:0,y:0
});
expect(bar.hoge()).to.be.true;
expect(bar.print()).to.equal('0,0');
expect(Bar.new()).to.be.instanceof(Bar);
});
it('should panic when passed block not fully implement trait interface',()=>{
let FooTrait=trait({
foo(){}
});
let Bar=struct({});
expect(()=>{
impl(FooTrait,Bar);
}).to.throw();
});
it('should ignore unnecessary methods',()=>{
let FooTrait=trait({
foo(){}
});
let Bar=struct({});
impl(FooTrait,Bar,{
foo(){
return true;
},
bar(){
return true;
}
});
expect(Bar).not.to.have.property('bar');
});
});
});

@@ -11,3 +11,3 @@ import {expect} from 'chai';

'Enum','struct',
'match','impl',
'match','impl','trait',
'panic'

@@ -14,0 +14,0 @@ ];

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc