object-refs
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -67,10 +67,34 @@ 'use strict'; | ||
* @param {Object} element the element to add | ||
* @param {Number} optional index to add element to | ||
* (possibly moving other elements around) | ||
*/ | ||
Object.defineProperty(collection, 'add', { | ||
value: function(element) { | ||
value: function(element, idx) { | ||
if (!this.contains(element)) { | ||
this.push(element); | ||
var currentIdx = this.indexOf(element); | ||
// set inverse | ||
if (typeof idx === 'undefined') { | ||
if (currentIdx !== -1) { | ||
// element already in collection (!) | ||
return; | ||
} | ||
// add to end of array, as no idx is specified | ||
idx = this.length; | ||
} | ||
// handle already in collection | ||
if (currentIdx !== -1) { | ||
// remove element from currentIdx | ||
this.splice(currentIdx, 1); | ||
} | ||
// add element at idx | ||
this.splice(idx, 0, element); | ||
if (currentIdx === -1) { | ||
// set inverse, unless element was | ||
// in collection already | ||
refs.set(element, inverseProperty, target); | ||
@@ -77,0 +101,0 @@ } |
{ | ||
"name": "object-refs", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Minimal bi-directional object references for JavaScript", | ||
"scripts": { | ||
"dev": "grunt auto-test", | ||
"test": "grunt test" | ||
@@ -7,0 +8,0 @@ }, |
# object-refs | ||
[![Build Status](https://travis-ci.org/bpmn-io/object-refs.svg)](https://travis-ci.org/bpmn-io/object-refs) | ||
[![Build Status](https://travis-ci.org/bpmn-io/object-refs.svg?branch=master)](https://travis-ci.org/bpmn-io/object-refs) | ||
@@ -10,2 +10,2 @@ A minimal implementation of bi-directional object references for JavaScript. | ||
MIT | ||
MIT |
@@ -47,2 +47,85 @@ 'use strict'; | ||
var a = {}; | ||
var b = { id: 'B' }; | ||
var c = { id: 'C' }; | ||
refs.bind(a, 'foos'); | ||
// when | ||
a.foos.add(b); | ||
a.foos.add(c); | ||
// then | ||
expect(a.foos).to.eql([ b, c ]); | ||
}); | ||
it('should add at idx', function() { | ||
// given | ||
var a = {}; | ||
var b = { id: 'B' }; | ||
var c = { id: 'C' }; | ||
var d = { id: 'D' }; | ||
refs.bind(a, 'foos'); | ||
a.foos.add(b); | ||
a.foos.add(c); | ||
// when | ||
a.foos.add(d, 1); | ||
// then | ||
expect(a.foos).to.eql([ b, d, c ]); | ||
}); | ||
it('should move to front with idx', function() { | ||
// given | ||
var a = {}; | ||
var b = { id: 'B' }; | ||
var c = { id: 'C' }; | ||
var d = { id: 'D' }; | ||
refs.bind(a, 'foos'); | ||
a.foos.add(b); | ||
a.foos.add(c); | ||
a.foos.add(d); | ||
// when | ||
a.foos.add(c, 0); | ||
// then | ||
expect(a.foos).to.eql([ c, b, d ]); | ||
}); | ||
it('should move to back with idx', function() { | ||
// given | ||
var a = {}; | ||
var b = { id: 'B' }; | ||
var c = { id: 'C' }; | ||
var d = { id: 'D' }; | ||
refs.bind(a, 'foos'); | ||
a.foos.add(b); | ||
a.foos.add(c); | ||
a.foos.add(d); | ||
// when | ||
a.foos.add(b, 2); | ||
// then | ||
expect(a.foos).to.eql([ c, d, b ]); | ||
}); | ||
it('should inverse add', function() { | ||
// given | ||
var a = {}; | ||
var b = {}; | ||
@@ -56,3 +139,3 @@ | ||
// then | ||
expect(a.foos.contains(b)).to.equal(true); | ||
expect(b.bar).to.equal(a); | ||
}); | ||
@@ -78,2 +161,3 @@ | ||
describe('#remove', function() { | ||
@@ -80,0 +164,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22029
725
11