Skip to content

Commit

Permalink
isDbjsObject and validDbjsObject
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jan 22, 2014
1 parent 4353bac commit 4812827
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
6 changes: 3 additions & 3 deletions is-dbjs-nested-object.js
@@ -1,11 +1,11 @@
'use strict';

var startsWith = require('es5-ext/string/#/starts-with')
, isDbjsKind = require('./is-dbjs-kind');
var startsWith = require('es5-ext/string/#/starts-with')
, isDbjsObject = require('./is-dbjs-object');

module.exports = function (value/*, owner*/) {
var owner = arguments[1];
if (!isDbjsKind(value)) return false;
if (!isDbjsObject(value)) return false;
if (!value.owner || !value.owner.__id__) return false;
if (!startsWith.call(value.__id__, value.owner.__id__ + '/')) return false;
if (owner == null) return true;
Expand Down
8 changes: 8 additions & 0 deletions is-dbjs-object.js
@@ -0,0 +1,8 @@
'use strict';

var hasOwnProperty = Object.prototype.hasOwnProperty;

module.exports = function (value) {
return (value && hasOwnProperty.call(value, '__id__') &&
(value._kind_ === 'object')) || false;
};
19 changes: 19 additions & 0 deletions test/is-dbjs-object.js
@@ -0,0 +1,19 @@
'use strict';

var Database = require('../');

module.exports = function (t, a) {
var db = new Database(), obj = new db.Object();

a(t(), false, "Undefined");
a(t(null), false, "Null");
a(t('raz'), false, "String");
a(t(2342), false, "Number");
a(t(new Date()), false, "Date");
a(t(new db.DateTime()), false, "Date out of database");
a(t({}), false, "Plain object");
a(t(obj), true, "Db object");
a(t(obj.$getOwn('test')), false, "Db descriptor");
a(t(obj.$getOwn('test').$getOwn('raz')), false, "Db descriptor's descriptor");
a(t(obj._getMultiple_('test').$getOwn('raz')), false, "Db item");
};
23 changes: 23 additions & 0 deletions test/valid-dbjs-object.js
@@ -0,0 +1,23 @@
'use strict';

var Database = require('../');

module.exports = function (t, a) {
var db = new Database(), obj = new db.Object(), desc, item;

a.throws(function () { t(); }, TypeError, "Undefined");
a.throws(function () { t(null); }, TypeError, "Null");
a.throws(function () { t('raz'); }, TypeError, "String");
a.throws(function () { t(2342); }, TypeError, "Number");
a.throws(function () { t(new Date()); }, TypeError, "Date");
a.throws(function () { t(new db.DateTime()); }, TypeError,
"Date out of database");
a.throws(function () { t({}); }, TypeError, "Plain object");
a(t(obj), obj, "Db object");
desc = obj.$getOwn('test');
a.throws(function () { t(desc); }, TypeError, "Db descriptor");
desc = desc.$getOwn('raz');
a.throws(function () { t(desc); }, TypeError, "Db descriptor's descriptor");
item = obj._getMultiple_('test').$getOwn('raz');
a.throws(function () { t(item); }, TypeError, "Db item");
};
8 changes: 8 additions & 0 deletions valid-dbjs-object.js
@@ -0,0 +1,8 @@
'use strict';

var isDbjsObject = require('./is-dbjs-object');

module.exports = function (value) {
if (isDbjsObject(value)) return value;
throw new TypeError(value + " is not dbjs object");
};

0 comments on commit 4812827

Please sign in to comment.