Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow .toJSON() function on objects to be used #153

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ language: node_js
node_js:
- "4"
- "6"
- "8"
script:
- >
if [ "$TRAVIS_EVENT_TYPE" == "cron" ] && node --version | grep v6\. ; then
if [ "$TRAVIS_EVENT_TYPE" == "cron" ] && node --version | grep --invert-match v4\. ; then
./bin/build.sh test:live;
else
echo "skipping live tests";
Expand Down
4 changes: 4 additions & 0 deletions lib/database/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ class DataNode {
return new DataNode({'.value': value, '.priority': priority});
}

if (typeof value === 'object' && typeof value.toJSON === 'function') {
return this.from(value.toJSON(), priority, now);
}

if (!isObject(value) && !Array.isArray(value)) {
throw new Error(`Invalid data node type: ${value} (${typeof value})`);
}
Expand Down
36 changes: 35 additions & 1 deletion test/spec/lib/database/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,41 @@ describe('store', function() {
});
});

[new Date(), /foo/].forEach(function(v) {
it('should let .toJSON() be used as value', function() {
class Something {
constructor() {
Object.defineProperty(this, 'key', {value: 'definedValue', enumberable: false});
Object.defineProperty(this, 'key2', {value: 'definedValue'});
}
toJSON() {
return {key: 'val'};
}
}

expect(store.create(new Something()).$value()).to.eql({key: 'val'});
expect(store.create({v: new Something()}).$value()).to.eql({v: {key: 'val'}});
});

it('should not let classes without .toJSON() be used as value', function() {
class Something {
constructor() {
Object.defineProperty(this, 'key', {value: 'definedValue', enumberable: false});
Object.defineProperty(this, 'key2', {value: 'definedValue'});
}
}

expect(() => store.create(new Something())).to.throw();
expect(() => store.create({v: new Something()})).to.throw();
});

[new Date()].forEach(function(v) {
it(`should let ${v.constructor.name} be used as value`, function() {
expect(store.create(v).$value()).to.eql(v.toJSON());
expect(store.create({v}).$value()).to.eql({v: v.toJSON()});
});
});

[/foo/].forEach(function(v) {
it(`should not let ${v.constructor.name} be used as value`, function() {
expect(() => store.create(v)).to.throw();
expect(() => store.create({v})).to.throw();
Expand Down
8 changes: 7 additions & 1 deletion test/spec/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ describe('util', function() {

describe('setFirebaseData', function() {

it('should throw on invalid data', function() {
it('should not throw on invalid data', function() {
expect(
() => util.setFirebaseData(new Date())
).to.not.throw();
});

it('should throw on invalid data', function() {
expect(
() => util.setFirebaseData(/regex/)
).to.throw();
});

Expand Down