mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-07-12 11:16:09 +09:00
Clean up the oopsie. Rebuild the blank exercises.
This commit is contained in:
+59
-17
@@ -1,8 +1,24 @@
|
||||
'use strict'
|
||||
var gitHosts = require('./git-host-info.js')
|
||||
var extend = Object.assign || require('util')._extend
|
||||
/* eslint-disable node/no-deprecated-api */
|
||||
|
||||
var GitHost = module.exports = function (type, user, auth, project, committish, defaultRepresentation, opts) {
|
||||
// copy-pasta util._extend from node's source, to avoid pulling
|
||||
// the whole util module into peoples' webpack bundles.
|
||||
/* istanbul ignore next */
|
||||
var extend = Object.assign || function _extend (target, source) {
|
||||
// Don't do anything if source isn't an object
|
||||
if (source === null || typeof source !== 'object') return target
|
||||
|
||||
var keys = Object.keys(source)
|
||||
var i = keys.length
|
||||
while (i--) {
|
||||
target[keys[i]] = source[keys[i]]
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -15,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 : ''
|
||||
@@ -24,27 +39,43 @@ GitHost.prototype.hash = function () {
|
||||
GitHost.prototype._fill = function (template, opts) {
|
||||
if (!template) return
|
||||
var vars = extend({}, opts)
|
||||
vars.path = vars.path ? vars.path.replace(/^[/]+/g, '') : ''
|
||||
opts = extend(extend({}, this.opts), opts)
|
||||
var self = this
|
||||
Object.keys(this).forEach(function (key) {
|
||||
if (self[key] != null && vars[key] == null) vars[key] = self[key]
|
||||
})
|
||||
var rawAuth = vars.auth
|
||||
var rawComittish = vars.committish
|
||||
var rawcommittish = vars.committish
|
||||
var rawFragment = vars.fragment
|
||||
var rawPath = vars.path
|
||||
var rawProject = vars.project
|
||||
Object.keys(vars).forEach(function (key) {
|
||||
vars[key] = encodeURIComponent(vars[key])
|
||||
var value = vars[key]
|
||||
if ((key === 'path' || key === 'project') && typeof value === 'string') {
|
||||
vars[key] = value.split('/').map(function (pathComponent) {
|
||||
return encodeURIComponent(pathComponent)
|
||||
}).join('/')
|
||||
} else {
|
||||
vars[key] = encodeURIComponent(value)
|
||||
}
|
||||
})
|
||||
vars['auth@'] = rawAuth ? rawAuth + '@' : ''
|
||||
vars['#fragment'] = rawFragment ? '#' + this.hashformat(rawFragment) : ''
|
||||
vars.fragment = vars.fragment ? vars.fragment : ''
|
||||
vars['#path'] = rawPath ? '#' + this.hashformat(rawPath) : ''
|
||||
vars['/path'] = vars.path ? '/' + vars.path : ''
|
||||
vars.projectPath = rawProject.split('/').map(encodeURIComponent).join('/')
|
||||
if (opts.noCommittish) {
|
||||
vars['#committish'] = ''
|
||||
vars['/tree/committish'] = ''
|
||||
vars['/comittish'] = ''
|
||||
vars.comittish = ''
|
||||
vars['/committish'] = ''
|
||||
vars.committish = ''
|
||||
} else {
|
||||
vars['#committish'] = rawComittish ? '#' + rawComittish : ''
|
||||
vars['#committish'] = rawcommittish ? '#' + rawcommittish : ''
|
||||
vars['/tree/committish'] = vars.committish
|
||||
? '/' + vars.treepath + '/' + vars.committish
|
||||
: ''
|
||||
? '/' + vars.treepath + '/' + vars.committish
|
||||
: ''
|
||||
vars['/committish'] = vars.committish ? '/' + vars.committish : ''
|
||||
vars.committish = vars.committish || 'master'
|
||||
}
|
||||
@@ -67,8 +98,19 @@ GitHost.prototype.sshurl = function (opts) {
|
||||
return this._fill(this.sshurltemplate, opts)
|
||||
}
|
||||
|
||||
GitHost.prototype.browse = function (opts) {
|
||||
return this._fill(this.browsetemplate, opts)
|
||||
GitHost.prototype.browse = function (P, F, opts) {
|
||||
if (typeof P === 'string') {
|
||||
if (typeof F !== 'string') {
|
||||
opts = F
|
||||
F = null
|
||||
}
|
||||
return this._fill(this.browsefiletemplate, extend({
|
||||
fragment: F,
|
||||
path: P
|
||||
}, opts))
|
||||
} else {
|
||||
return this._fill(this.browsetemplate, P)
|
||||
}
|
||||
}
|
||||
|
||||
GitHost.prototype.docs = function (opts) {
|
||||
@@ -95,14 +137,13 @@ GitHost.prototype.path = function (opts) {
|
||||
return this._fill(this.pathtemplate, opts)
|
||||
}
|
||||
|
||||
GitHost.prototype.tarball = function (opts) {
|
||||
GitHost.prototype.tarball = function (opts_) {
|
||||
var opts = extend({}, opts_, { noCommittish: false })
|
||||
return this._fill(this.tarballtemplate, opts)
|
||||
}
|
||||
|
||||
GitHost.prototype.file = function (P, opts) {
|
||||
return this._fill(this.filetemplate, extend({
|
||||
path: P.replace(/^[/]+/g, '')
|
||||
}, opts))
|
||||
return this._fill(this.filetemplate, extend({ path: P }, opts))
|
||||
}
|
||||
|
||||
GitHost.prototype.getDefaultRepresentation = function () {
|
||||
@@ -110,5 +151,6 @@ GitHost.prototype.getDefaultRepresentation = function () {
|
||||
}
|
||||
|
||||
GitHost.prototype.toString = function (opts) {
|
||||
return (this[this.default] || this.sshurl).call(this, opts)
|
||||
if (this.default && typeof this[this.default] === 'function') return this[this.default](opts)
|
||||
return this.sshurl(opts)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user