From 60abaea6be6f87d1473d7b32cfe644e8106bbe36 Mon Sep 17 00:00:00 2001 From: isaacs Date: Sun, 4 Aug 2019 16:36:14 -0700 Subject: [PATCH] feat: give these objects a name It's much less friendly to use this library (and those that depend on it, like npm-package-arg) when it's not clear whether the returned value is a member of a class (which likely has some useful methods) or just an object full of data that the user is expected to know what to do with. This is especially vexing since some of this sort of data is saved to various lock and config files, so _sometimes_ a thing that looks suspiciously like a GitHost object actually is just a plain old JS object. --- git-host.js | 4 ++-- test/basic.js | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/git-host.js b/git-host.js index 9086edd..d6dd9c0 100644 --- a/git-host.js +++ b/git-host.js @@ -17,7 +17,8 @@ var extend = Object.assign || function _extend (target, source) { return target } -var GitHost = module.exports = function (type, user, auth, project, committish, defaultRepresentation, opts) { +module.exports = GitHost +function GitHost (type, user, auth, project, committish, defaultRepresentation, opts) { var gitHostInfo = this gitHostInfo.type = type Object.keys(gitHosts[type]).forEach(function (key) { @@ -30,7 +31,6 @@ var GitHost = module.exports = function (type, user, auth, project, committish, gitHostInfo.default = defaultRepresentation gitHostInfo.opts = opts || {} } -GitHost.prototype = {} GitHost.prototype.hash = function () { return this.committish ? '#' + this.committish : '' diff --git a/test/basic.js b/test/basic.js index 5428e07..e2c6bdc 100644 --- a/test/basic.js +++ b/test/basic.js @@ -3,6 +3,9 @@ var HostedGit = require('../index') var test = require('tap').test test('basic', function (t) { + const h = HostedGit.fromUrl('github:user/project') + t.is(h.constructor, HostedGit) + t.is(h.constructor.name, 'GitHost') t.is(HostedGit.fromUrl('https://google.com'), undefined, 'null on failure') t.is(HostedGit.fromUrl('https://github.com/abc/def').getDefaultRepresentation(), 'https', 'match https urls') t.is(HostedGit.fromUrl('https://github.com/abc/def/').getDefaultRepresentation(), 'https', 'match URLs with a trailing slash')