Skip to content

common: support checking custom types #1529

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

Merged
merged 3 commits into from
Aug 26, 2016
Merged
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
26 changes: 26 additions & 0 deletions packages/common/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,29 @@ function createLimiter(makeRequestFn, options) {
}

util.createLimiter = createLimiter;

function isCustomType(unknown, module) {
function getConstructorName(obj) {
return obj.constructor && obj.constructor.name.toLowerCase();
}

var moduleNameParts = module.split('/');

var parentModuleName = moduleNameParts[0] && moduleNameParts[0].toLowerCase();
var subModuleName = moduleNameParts[1] && moduleNameParts[1].toLowerCase();

if (subModuleName && getConstructorName(unknown) !== subModuleName) {
return false;
}

var walkingModule = unknown;
do {

This comment was marked as spam.

This comment was marked as spam.

if (getConstructorName(walkingModule) === parentModuleName) {
return true;
}
} while ((walkingModule = walkingModule.parent));

return false;
}

util.isCustomType = isCustomType;
31 changes: 31 additions & 0 deletions packages/common/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1370,4 +1370,35 @@ describe('common/util', function() {
});
});
});

describe('isCustomType', function() {
function PubSub() {}

function MiddleLayer() {
this.parent = new PubSub();
}

function Subscription() {
this.parent = new MiddleLayer();
}

var pubsub = new PubSub();
var subscription = new Subscription();

it('should match a Service type by constructor names', function() {
assert(util.isCustomType(pubsub, 'pubsub'));
});

it('should match a ServiceObject type by constructor names', function() {
assert(util.isCustomType(subscription, 'pubsub'));
assert(util.isCustomType(subscription, 'pubsub/subscription'));

assert(util.isCustomType(subscription, 'middlelayer'));
assert(util.isCustomType(subscription, 'middlelayer/subscription'));
});

it('should support any casing', function() {
assert(util.isCustomType(subscription, 'PubSub/Subscription'));
});
});
});