Skip to content

Commit

Permalink
add remove api, bump to v1.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterEB committed Mar 14, 2017
1 parent 285d529 commit a396a6f
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/object_instance.js
Expand Up @@ -79,11 +79,13 @@ ObjectInstance.prototype.init = function (resrcs, setup) {

ObjectInstance.prototype.has = function (rid) {
var ridkey = validateRid(rid) && utils.getRidKey(this.oid, rid);

return this.hasOwnProperty(ridkey);
};

ObjectInstance.prototype.get = function (rid) {
var ridkey = validateRid(rid) && utils.getRidKey(this.oid, rid);

return this[ridkey];
};

Expand All @@ -92,7 +94,9 @@ ObjectInstance.prototype.set = function (rid, value) {
throw new TypeError('Resource cannot be a function or undefined.');

var ridkey = validateRid(rid) && utils.getRidKey(this.oid, rid);

this[ridkey] = value;

return this;
};

Expand Down Expand Up @@ -216,7 +220,7 @@ ObjectInstance.prototype.read = function (rid, opt, callback) {

// if we are here, rsc is a plain object
if (!rsc._isCb)
return opt.restrict ? restrictRead(rsc, callback) : utils.invokeCbNextTick(null, _.omit(rsc, [ '_isCb' ], callback));
return opt.restrict ? restrictRead(rsc, callback) : utils.invokeCbNextTick(null, _.omit(rsc, [ '_isCb' ]), callback);

// rsc should be read from users callback
if (_.isFunction(rsc.exec)) // an exec resource cannot be read, so checks for it first
Expand Down
18 changes: 16 additions & 2 deletions lib/smartobject.js
Expand Up @@ -63,6 +63,21 @@ SmartObject.prototype.create = function (oid, iid) {
/*************************************************************************************************/
/*** Public Methods: Synchronous ***/
/*************************************************************************************************/
SmartObject.prototype.remove = function (oid, iid) {
var objInst;

objInst = this.findObjectInstance(oid, iid);

if (objInst) {
objInst.clear();
objInst = null;
delete this.findObject(oid)[iid];
return true;
}

return false;
};

SmartObject.prototype.objectList = function () {
var objList = [];

Expand Down Expand Up @@ -99,8 +114,7 @@ SmartObject.prototype.has = function (oid, iid, rid) {
objInst = obj[iid];
has = !!objInst;
if (has && !_.isUndefined(rid)) {
resrc = objInst.get(rid);
has = !_.isUndefined(resrc);
has = objInst.has(rid);
}
}
return has;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "smartobject",
"version": "1.4.2",
"version": "1.4.3",
"description": "A Smart Object Class that helps you with creating IPSO Smart Objects in your JavaScript applications",
"main": "index.js",
"scripts": {
Expand Down
74 changes: 72 additions & 2 deletions test/smartObject.functional.test.js
Expand Up @@ -29,6 +29,19 @@ describe('Smart Object - Functional Check', function () {
expect(typeof smartObj.temperature[0].sensorValue.read).to.be.eql('function');
expect(smartObj.temperature[0].sensorValue._isCb).to.be.true;
});

it('should add a Multi-Resource', function () {
smartObj.init('multiResource', 0, {
5700: {
0: 'x',
1: 'y',
2: 'z'
}
});

expect(smartObj.multiResource[0][5700][0]).to.be.eql('x');
expect(smartObj.multiResource[0][5700]._isCb).to.be.false;
});
});

describe('#.create()', function () {
Expand All @@ -39,9 +52,22 @@ describe('Smart Object - Functional Check', function () {
});
});

describe('#.remove()', function () {
it('should remove an Object Instance', function () {
smartObj.init(3303, 2, {
5700: 20
});
expect(smartObj.temperature[2].sensorValue).to.be.eql(20);

smartObj.remove(3303, 2);
expect(smartObj.temperature[2]).to.be.eql(undefined);
expect(smartObj.findObjectInstance(3303, 2)).to.be.eql(undefined);
});
});

describe('#.objectList()', function () {
it('should return objectList', function () {
expect(smartObj.objectList()).to.be.eql([{ oid: 3303, iid: [ 0, 1 ]}]);
expect(smartObj.objectList()).to.be.eql([{ oid: 3303, iid: [ 0, 1 ]}, { oid: 'multiResource', iid: [0] }]);
});
});

Expand Down Expand Up @@ -128,6 +154,15 @@ describe('Smart Object - Functional Check', function () {
1: {
units: 'f'
}
},
multiResource: {
0: {
5700: {
0: 'x',
1: 'y',
2: 'z'
}
}
}
};

Expand Down Expand Up @@ -174,6 +209,15 @@ describe('Smart Object - Functional Check', function () {
1: {
units: 'f'
}
},
multiResource: {
0: {
5700: {
0: 'x',
1: 'y',
2: 'z'
}
}
}
};

Expand Down Expand Up @@ -377,6 +421,19 @@ describe('Smart Object - Functional Check', function () {
done();
});
});

it('should pass the read value when Resources is a Multi-Resource', function (done) {
var obj = {
0: 'x',
1: 'y',
2: 'z'
};

smartObj.read('multiResource', 0, 5700, function (err, result) {
if (_.isEqual(result, obj))
done();
});
});
});

describe('#.write()', function () {
Expand Down Expand Up @@ -449,8 +506,21 @@ describe('Smart Object - Functional Check', function () {
done();
});
});
});

it('should write Resource and pass the write value when Resources is a Multi-Resource', function (done) {
var obj = {
0: 'a',
1: 'b',
2: 'c'
};

smartObj.write('multiResource', 0, 5700, obj, function (err, result) {
if (_.isEqual(result, obj))
done();
});
});
});

describe('#.exec()', function () {
it('should exec Resource and pass true through its second argument', function (done) {
smartObj.exec(3303, 0, 5704, [ 20 ], function (err, result) {
Expand Down
33 changes: 33 additions & 0 deletions test/smartObject.signature.test.js
Expand Up @@ -121,6 +121,39 @@ describe('Smart Object - Signature Check', function () {
});
});

describe('#.remove()', function () {
it('should be a function', function () {
expect(smartObj.remove).to.be.a('function');
});

it('should throw TypeError if oid is not a string or a number', function () {
expect(function () { return smartObj.remove(); }).to.throw(TypeError);
expect(function () { return smartObj.remove(undefined); }).to.throw(TypeError);
expect(function () { return smartObj.remove(null); }).to.throw(TypeError);
expect(function () { return smartObj.remove(NaN); }).to.throw(TypeError);
expect(function () { return smartObj.remove([]); }).to.throw(TypeError);
expect(function () { return smartObj.remove({}); }).to.throw(TypeError);
expect(function () { return smartObj.remove(true); }).to.throw(TypeError);
expect(function () { return smartObj.remove(new Date()); }).to.throw(TypeError);
expect(function () { return smartObj.remove(function () {}); }).to.throw(TypeError);
});

it('should throw TypeError if iid is not a string or a number', function () {
expect(function () { return smartObj.remove(3304); }).to.throw(TypeError);
expect(function () { return smartObj.remove(3304, undefined); }).to.throw(TypeError);
expect(function () { return smartObj.remove(3304, null); }).to.throw(TypeError);
expect(function () { return smartObj.remove(3304, NaN); }).to.throw(TypeError);
expect(function () { return smartObj.remove(3304, []); }).to.throw(TypeError);
expect(function () { return smartObj.remove(3304, {}); }).to.throw(TypeError);
expect(function () { return smartObj.remove(3304, true); }).to.throw(TypeError);
expect(function () { return smartObj.remove(3304, new Date()); }).to.throw(TypeError);
expect(function () { return smartObj.remove(3304, function () {}); }).to.throw(TypeError);

expect(function () { return smartObj.remove(3304, 2); }).not.to.throw(TypeError);
expect(function () { return smartObj.remove(3304, 'b'); }).not.to.throw(TypeError);
});
});

describe('#.objectList()', function () {
it('should be a function', function () {
expect(smartObj.objectList).to.be.a('function');
Expand Down

0 comments on commit a396a6f

Please sign in to comment.