object-refs
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -33,20 +33,21 @@ 'use strict'; | ||
jasmine_node: { | ||
options: { | ||
specNameMatcher: '.*Spec', | ||
jUnit: { | ||
report: true, | ||
savePath : 'tmp/reports/jasmine', | ||
useDotNotation: true, | ||
consolidate: true | ||
} | ||
}, | ||
all: [ '<%= config.tests %>/spec/' ] | ||
mochaTest: { | ||
test: { | ||
options: { | ||
reporter: 'spec', | ||
require: [ | ||
'./test/expect.js' | ||
] | ||
}, | ||
src: ['test/**/*.js'] | ||
} | ||
}, | ||
watch: { | ||
test: { | ||
files: [ '<%= config.sources %>/**/*.js', '<%= config.tests %>/**/*.js' ], | ||
tasks: [ 'jasmine_node' ] | ||
tasks: [ 'test' ] | ||
} | ||
}, | ||
jsdoc: { | ||
@@ -66,6 +67,6 @@ dist: { | ||
grunt.registerTask('test', [ 'jasmine_node' ]); | ||
grunt.registerTask('auto-test', [ 'jasmine_node', 'watch:test' ]); | ||
grunt.registerTask('test', [ 'mochaTest' ]); | ||
grunt.registerTask('auto-test', [ 'test', 'watch:test' ]); | ||
grunt.registerTask('default', [ 'jshint', 'test', 'jsdoc' ]); | ||
}; |
module.exports = require('./lib/refs'); | ||
module.exports.Collection = require('./lib/collection'); |
@@ -7,7 +7,4 @@ 'use strict'; | ||
* | ||
* @classdesc A change and inverse-reference aware collection with set semantics. | ||
* | ||
* @class RefsCollection | ||
*/ | ||
function RefsCollection() { } | ||
@@ -38,14 +35,16 @@ /** | ||
*/ | ||
collection.remove = function(element) { | ||
var idx = this.indexOf(element); | ||
if (idx !== -1) { | ||
this.splice(idx, 1); | ||
Object.defineProperty(collection, 'remove', { | ||
value: function(element) { | ||
var idx = this.indexOf(element); | ||
if (idx !== -1) { | ||
this.splice(idx, 1); | ||
// unset inverse | ||
refs.unset(element, inverseProperty, target); | ||
// unset inverse | ||
refs.unset(element, inverseProperty, target); | ||
} | ||
return element; | ||
} | ||
}); | ||
return element; | ||
}; | ||
/** | ||
@@ -58,5 +57,7 @@ * Returns true if the collection contains the given element | ||
*/ | ||
collection.contains = function(element) { | ||
return this.indexOf(element) !== -1; | ||
}; | ||
Object.defineProperty(collection, 'contains', { | ||
value: function(element) { | ||
return this.indexOf(element) !== -1; | ||
} | ||
}); | ||
@@ -70,12 +71,20 @@ /** | ||
*/ | ||
collection.add = function(element) { | ||
Object.defineProperty(collection, 'add', { | ||
value: function(element) { | ||
if (!this.contains(element)) { | ||
this.push(element); | ||
if (!this.contains(element)) { | ||
this.push(element); | ||
// set inverse | ||
refs.set(element, inverseProperty, target); | ||
// set inverse | ||
refs.set(element, inverseProperty, target); | ||
} | ||
} | ||
}; | ||
}); | ||
// a simple marker, identifying this element | ||
// as being a refs collection | ||
Object.defineProperty(collection, '__refs_collection', { | ||
value: true | ||
}); | ||
return collection; | ||
@@ -85,2 +94,8 @@ } | ||
module.exports.extend = extend; | ||
function isExtended(collection) { | ||
return collection.__refs_collection === true; | ||
} | ||
module.exports.extend = extend; | ||
module.exports.isExtended = isExtended; |
@@ -9,3 +9,2 @@ 'use strict'; | ||
function defineCollectionProperty(ref, property, target) { | ||
@@ -140,2 +139,13 @@ Object.defineProperty(target, property.name, { | ||
Refs.prototype.ensureRefsCollection = function(target, property) { | ||
var collection = target[property.name]; | ||
if (!Collection.isExtended(collection)) { | ||
defineCollectionProperty(this, property, target); | ||
} | ||
return collection; | ||
}; | ||
Refs.prototype.ensureBound = function(target, property) { | ||
@@ -153,3 +163,3 @@ if (!hasOwnProperty(target, property)) { | ||
if (property.collection) { | ||
target[property.name].remove(value); | ||
this.ensureRefsCollection(target, property).remove(value); | ||
} else { | ||
@@ -167,3 +177,3 @@ target[property.name] = undefined; | ||
if (property.collection) { | ||
target[property.name].add(value); | ||
this.ensureRefsCollection(target, property).add(value); | ||
} else { | ||
@@ -170,0 +180,0 @@ target[property.name] = value; |
{ | ||
"name": "object-refs", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Minimal bi-directional object references for JavaScript", | ||
@@ -27,2 +27,3 @@ "scripts": { | ||
"devDependencies": { | ||
"chai": "^1.9.1", | ||
"grunt": "~0.4.2", | ||
@@ -33,6 +34,6 @@ "grunt-contrib-watch": "~0.5.0", | ||
"grunt-jsdoc": "~0.5.1", | ||
"grunt-jasmine-node": "~0.2.1", | ||
"grunt-release": "^0.7.0", | ||
"grunt-mocha-test": "^0.11.0", | ||
"grunt-release": "^0.13.0", | ||
"load-grunt-tasks": "~0.3.0" | ||
} | ||
} |
# object-refs | ||
[![Build Status](https://travis-ci.org/bpmn-io/object-refs.svg)](https://travis-ci.org/bpmn-io/object-refs) | ||
A minimal implementation of bi-directional object references for JavaScript. | ||
@@ -4,0 +6,0 @@ |
@@ -5,9 +5,6 @@ 'use strict'; | ||
var hasOwnProperty = require('./helper').hasOwnProperty, | ||
expectArraysEqual = require('./helper').expectArraysEqual; | ||
describe('collection api', function() { | ||
var refs = Refs({ name: 'foos', collection: true }, { name: 'bar' }); | ||
var refs = new Refs({ name: 'foos', collection: true }, { name: 'bar' }); | ||
@@ -26,3 +23,3 @@ | ||
// then | ||
expect(a.foos.contains({})).toBe(false); | ||
expect(a.foos.contains({})).to.equal(false); | ||
}); | ||
@@ -40,3 +37,3 @@ | ||
// then | ||
expect(a.foos.contains(b)).toBe(true); | ||
expect(a.foos.contains(b)).to.equal(true); | ||
}); | ||
@@ -61,3 +58,3 @@ | ||
// then | ||
expect(a.foos.contains(b)).toBe(true); | ||
expect(a.foos.contains(b)).to.equal(true); | ||
}); | ||
@@ -78,3 +75,3 @@ | ||
// then | ||
expect(b.bar).toBe(a); | ||
expect(b.bar).to.equal(a); | ||
}); | ||
@@ -99,4 +96,4 @@ | ||
// then | ||
expectArraysEqual(a.foos, [ ]); | ||
expect(b.bar).toBe(undefined); | ||
expect(a.foos).to.eql([ ]); | ||
expect(b.bar).not.to.exist; | ||
@@ -103,0 +100,0 @@ }); |
'use strict'; | ||
var hasOwnProperty = require('./helper').hasOwnProperty; | ||
var hasProperty = require('./has-property'); | ||
@@ -19,4 +19,4 @@ | ||
// then | ||
expect(a.value).not.toBeDefined(); | ||
expect(hasOwnProperty(a, 'foo')).toBe(true); | ||
expect(a.value).not.to.exist; | ||
expect(hasProperty(a, 'foo')).to.equal(true); | ||
}); | ||
@@ -39,5 +39,5 @@ | ||
// then | ||
expect(hasOwnProperty(a, 'foo')).toBe(false); | ||
expect(hasProperty(a, 'foo')).to.equal(false); | ||
}); | ||
}); |
@@ -15,7 +15,9 @@ 'use strict'; | ||
return; | ||
if (true) { | ||
return; | ||
} | ||
it('refs access', function() { | ||
var refs = Refs({ name: 'foos', collection: true }, { name: 'bar' }); | ||
var refs = new Refs({ name: 'foos', collection: true }, { name: 'bar' }); | ||
@@ -22,0 +24,0 @@ var foos = []; |
@@ -5,4 +5,3 @@ 'use strict'; | ||
var hasOwnProperty = require('./helper').hasOwnProperty, | ||
expectArraysEqual = require('./helper').expectArraysEqual; | ||
var hasProperty = require('./has-property'); | ||
@@ -16,3 +15,3 @@ | ||
var refs = Refs({ name: 'foo' }, { name: 'bar' }); | ||
var refs = new Refs({ name: 'foo' }, { name: 'bar' }); | ||
@@ -30,3 +29,3 @@ | ||
// then | ||
expect(a.foo).toBe(b); | ||
expect(a.foo).to.equal(b); | ||
}); | ||
@@ -44,4 +43,4 @@ | ||
// then | ||
expect(a.foo).not.toBeDefined(); | ||
expect(hasOwnProperty(a, 'foo')).toBe(true); | ||
expect(a.foo).not.to.exist; | ||
expect(hasProperty(a, 'foo')).to.equal(true); | ||
}); | ||
@@ -60,3 +59,3 @@ | ||
var json = JSON.stringify(a); | ||
expect(json).toBe('{}'); | ||
expect(json).to.equal('{}'); | ||
}); | ||
@@ -68,3 +67,3 @@ | ||
// given | ||
var enumerableRefs = Refs({ name: 'foo', enumerable: true }, { name: 'bar' }); | ||
var enumerableRefs = new Refs({ name: 'foo', enumerable: true }, { name: 'bar' }); | ||
@@ -78,3 +77,3 @@ var a = {}; | ||
var json = JSON.stringify(a); | ||
expect(json).toBe('{"foo":{}}'); | ||
expect(json).to.equal('{"foo":{}}'); | ||
}); | ||
@@ -95,4 +94,4 @@ | ||
// then | ||
expect(a.foo).toBe(b); | ||
expect(b.bar).toBe(a); | ||
expect(a.foo).to.equal(b); | ||
expect(b.bar).to.equal(a); | ||
}); | ||
@@ -113,4 +112,4 @@ | ||
expect(a.foo).toBe(null); | ||
expect(b.bar).toBe(undefined); | ||
expect(a.foo).to.equal(null); | ||
expect(b.bar).to.equal(undefined); | ||
}); | ||
@@ -131,3 +130,3 @@ | ||
// then | ||
expect(a.foo).toBe(undefined); | ||
expect(a.foo).to.equal(undefined); | ||
}); | ||
@@ -146,3 +145,3 @@ | ||
delete a.foo; | ||
}).toThrow('Cannot delete property \'foo\' of #<Object>'); | ||
}).to.throw('Cannot delete property \'foo\' of #<Object>'); | ||
}); | ||
@@ -155,3 +154,3 @@ | ||
var refs = Refs({ name: 'foos', collection: true }, { name: 'bar' }); | ||
var refs = new Refs({ name: 'foos', collection: true }, { name: 'bar' }); | ||
@@ -169,3 +168,3 @@ | ||
var json = JSON.stringify(a); | ||
expect(json).toBe('{}'); | ||
expect(json).to.equal('{}'); | ||
}); | ||
@@ -177,3 +176,3 @@ | ||
// given | ||
var enumerableRefs = Refs({ name: 'foos', enumerable: true, collection: true }, { name: 'bar' }); | ||
var enumerableRefs = new Refs({ name: 'foos', enumerable: true, collection: true }, { name: 'bar' }); | ||
@@ -187,3 +186,3 @@ var a = {}; | ||
var json = JSON.stringify(a); | ||
expect(json).toBe('{"foos":[{}]}'); | ||
expect(json).to.equal('{"foos":[{}]}'); | ||
}); | ||
@@ -202,3 +201,3 @@ | ||
// then | ||
expectArraysEqual(a.foos, [ b ]); | ||
expect(a.foos).to.eql([ b ]); | ||
}); | ||
@@ -216,3 +215,3 @@ | ||
// then | ||
expectArraysEqual(a.foos, []); | ||
expect(a.foos).to.eql([]); | ||
}); | ||
@@ -235,3 +234,3 @@ | ||
// then | ||
expectArraysEqual(a.foos, [ b1, b2 ]); | ||
expect(a.foos).to.eql([ b1, b2 ]); | ||
}); | ||
@@ -256,7 +255,7 @@ | ||
// then | ||
expectArraysEqual(a.foos, [ b2 ]); | ||
expect(a.foos).to.eql([ b2 ]); | ||
}); | ||
it('should transitively define properties', function() { | ||
it('should transitively bind one property', function() { | ||
@@ -273,5 +272,21 @@ // given | ||
// then | ||
expectArraysEqual(a.foos, []); | ||
expect(a.foos).to.eql([]); | ||
}); | ||
it('should transitively bind many property', function() { | ||
// given | ||
var b = { }; | ||
var a = { foos: [ 'a' ] }; | ||
refs.bind(b, 'bar'); | ||
// when | ||
b.bar = a; | ||
// then | ||
expect(a.foos).to.eql([ 'a', b ]); | ||
}); | ||
}); | ||
@@ -282,3 +297,3 @@ | ||
var refs = Refs({ name: 'foos', collection: true }, { name: 'bars', collection: true }); | ||
var refs = new Refs({ name: 'foos', collection: true }, { name: 'bars', collection: true }); | ||
@@ -294,3 +309,3 @@ | ||
// then | ||
expectArraysEqual(b.bars, []); | ||
expect(b.bars).to.eql([]); | ||
}); | ||
@@ -310,3 +325,3 @@ | ||
// then | ||
expectArraysEqual(b.bars, [ a ]); | ||
expect(b.bars).to.eql([ a ]); | ||
}); | ||
@@ -330,6 +345,6 @@ | ||
// then | ||
expect(b2.bars.contains(a2)).toBe(true); | ||
expect(b2.bars.contains(a2)).to.equal(true); | ||
expectArraysEqual(b1.bars, [ a1 ]); | ||
expectArraysEqual(b2.bars, [ a1, a2 ]); | ||
expect(b1.bars).to.eql([ a1 ]); | ||
expect(b2.bars).to.eql([ a1, a2 ]); | ||
}); | ||
@@ -355,4 +370,4 @@ | ||
// then | ||
expectArraysEqual(b1.bars, [ a1 ]); | ||
expectArraysEqual(b2.bars, [ a2 ]); | ||
expect(b1.bars).to.eql([ a1 ]); | ||
expect(b2.bars).to.eql([ a2 ]); | ||
}); | ||
@@ -373,6 +388,6 @@ | ||
// then | ||
expectArraysEqual(a.foos, []); | ||
expectArraysEqual(b.bars, []); | ||
expect(a.foos).to.eql([]); | ||
expect(b.bars).to.eql([]); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
16
651
10
20004
9