Skip to content

Commit

Permalink
Record: add support for Symbol type
Browse files Browse the repository at this point in the history
  • Loading branch information
oskargustafsson committed May 12, 2017
1 parent c71142b commit 38ebebf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// JSHint Default Configuration File (as on JSHint website)
// See http://jshint.com/docs/ for more details

"esversion": 6,

"maxerr" : 50, // {int} Maximum error before stopping

// Enforcing
Expand Down
17 changes: 12 additions & 5 deletions dist/dev/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@
break;
}
} else {
if (val instanceof anOkType) {
isValueOk = true;
break;
if (anOkType === Symbol) {
if ('symbol' == typeof val) {
isValueOk = true;
break;
}
} else {
if (val instanceof anOkType) {
isValueOk = true;
break;
}
}
}
}
Expand All @@ -57,8 +64,8 @@
}
}
if (!isValueOk) {
var typeNames = propSchema.type.map(function(val) {
return val instanceof Function ? val.name : typeof val;
var typeNames = propSchema.type.map(function(aType) {
return aType instanceof Function ? aType.name : typeof aType;
});
throw 'Property ' + propName + ' must be of type [' + typeNames.join(', ') + '], it can not be assigned a value of type ' + typeof val;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bff-lib",
"author": "Oskar Gustafsson",
"version": "0.6.3",
"version": "0.6.4",
"license": "MIT",
"description": "Basic Front-end Foundation - modules for JS front-end development",
"keywords": [
Expand Down
9 changes: 7 additions & 2 deletions src/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,20 @@
isValueOk = true;
break;
}
} else if (anOkType === Symbol) {
if (typeof val === 'symbol') {
isValueOk = true;
break;
}
} else if (val instanceof anOkType) {
isValueOk = true;
break;
}
}

if (!isValueOk) {
var typeNames = propSchema.type.map(function (val) {
return val instanceof Function ? val.name : typeof val;
var typeNames = propSchema.type.map(function (aType) {
return aType instanceof Function ? aType.name : typeof aType;
});

throw 'Property ' + propName + ' must be of type [' + typeNames.join(', ') +
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,15 @@ define(function (require) {
expect(function () { new Record({ race: false }); }).to.throw();
},

'allows Symbol type': function () {
var human = Symbol('human');
var Record = AbstractRecord.withProperties({
race: Symbol,
});
expect(new Record({ race: human }).race).to.equal(human);
expect(function () { new Record({ race: 'human' }); }).to.throw();
},

},

'default values': {
Expand Down

0 comments on commit 38ebebf

Please sign in to comment.