Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object-refs

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-refs - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

32

lib/collection.js

@@ -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 @@ }

3

package.json
{
"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 @@

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