Skip to content

Commit

Permalink
Merge branch 'hotfix/fix_instanceOf' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Oct 22, 2015
2 parents cc022f0 + 3949465 commit 2ff0fc6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
5 changes: 2 additions & 3 deletions es5/lib/jsonApiModelFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ var _dovima = require("dovima");
var _dovima2 = _interopRequireDefault(_dovima);

function convertModel(model) {
//HACK: this is a temporal hack to work around version issues with dovima
if (model && typeof model.toJSON === "function" && model.constructor.name === "Model" || model instanceof _dovima2["default"]) {
if (model && typeof model.toJSON === "function" || model instanceof _dovima2["default"]) {
var attributes = model.toJSON();
var id = attributes.id;
delete attributes.id; //so it's just on the root
Expand All @@ -28,7 +27,7 @@ function convertModel(model) {
}

function JsonApiModelFormatter(models) {
if (Array.isArray(models) || models.constructor.name === "Array"
if (Array.isArray(models) || models && models.length >= 0
//HACK: this is a temporal hack to work around version issues with dovima
|| models.constructor.name === "Collection") {
return models.map(convertModel);
Expand Down
43 changes: 43 additions & 0 deletions es5/spec/jsonApiModelFormatter.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
Expand Down Expand Up @@ -62,4 +64,45 @@ describe("jsonApiModelFormatter", function () {
(0, _libJsonApiModelFormatterJs2["default"])(userAttributes);
}).should["throw"]("The object provided to be formatted as json is not a dovima Model / Collection.");
});

describe("function object conflicts", function () {
it("should work correctly if the object has a toJSON function", function () {
var result = "some result";

var Apple = (function () {
function Apple() {
_classCallCheck(this, Apple);
}

_createClass(Apple, [{
key: "toJSON",
value: function toJSON() {
return result;
}
}]);

return Apple;
})();

var apple = new Apple();
(0, _libJsonApiModelFormatterJs2["default"])(apple).attributes.should.equal(result);
});

it("should work correctly with a unknown collection that extends the array", function () {
var Apples = (function (_Array) {
function Apples() {
_classCallCheck(this, Apples);

_get(Object.getPrototypeOf(Apples.prototype), "constructor", this).apply(this, arguments);
}

_inherits(Apples, _Array);

return Apples;
})(Array);

var apples = new Apples();
(0, _libJsonApiModelFormatterJs2["default"])(apples).should.eql([]);
});
});
});
5 changes: 2 additions & 3 deletions es6/lib/jsonApiModelFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import Model from "dovima";
import {Collection} from "dovima";

function convertModel(model) {
//HACK: this is a temporal hack to work around version issues with dovima
if((model && typeof (model.toJSON) === "function" && model.constructor.name === "Model")
if((model && typeof (model.toJSON) === "function")
|| model instanceof Model) {
let attributes = model.toJSON();
const id = attributes.id;
Expand All @@ -20,7 +19,7 @@ function convertModel(model) {

export default function JsonApiModelFormatter(models) {
if(Array.isArray(models)
|| (models.constructor.name === "Array")
|| (models && models.length >= 0)
//HACK: this is a temporal hack to work around version issues with dovima
|| (models.constructor.name === "Collection")) {
return models.map(convertModel);
Expand Down
21 changes: 21 additions & 0 deletions es6/spec/jsonApiModelFormatter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,25 @@ describe("jsonApiModelFormatter", () => {
jsonApiModelFormatter(userAttributes);
}.should.throw("The object provided to be formatted as json is not a dovima Model / Collection.");
});

describe("function object conflicts", () => {
it("should work correctly if the object has a toJSON function", () => {
const result = "some result";
class Apple {
toJSON() {
return result;
}
}

const apple = new Apple();
jsonApiModelFormatter(apple).attributes.should.equal(result);
});

it("should work correctly with a unknown collection that extends the array", () => {
class Apples extends Array {}

const apples = new Apples();
jsonApiModelFormatter(apples).should.eql([]);
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonapi-model-formatter",
"version": "0.1.9",
"version": "0.1.10",
"description": "JSON API formatter for dovima models.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 2ff0fc6

Please sign in to comment.