aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2019-02-03 14:58:04 -1000
committercel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2019-02-03 14:58:04 -1000
commitda132772a40f9291abbc5fd997591e32d9f65268 (patch)
treef86be0097dd74ad8e1a240a7324da1f63a7e6745
parent72e77600465d68edfe3b23abc3509ed1a76a11e4 (diff)
downloadpatchfoo-da132772a40f9291abbc5fd997591e32d9f65268.tar.gz
patchfoo-da132772a40f9291abbc5fd997591e32d9f65268.zip
Move truncate function into utils
-rw-r--r--lib/render-msg.js27
-rw-r--r--lib/render.js7
-rw-r--r--lib/util.js5
3 files changed, 17 insertions, 22 deletions
diff --git a/lib/render-msg.js b/lib/render-msg.js
index 17fc36d..2720751 100644
--- a/lib/render-msg.js
+++ b/lib/render-msg.js
@@ -454,13 +454,8 @@ RenderMsg.prototype.getMsgName = function (id, cb) {
}
}
-function truncate(str, len) {
- str = String(str)
- return str.length > len ? str.substr(0, len) + '...' : str
-}
-
function title(str) {
- return truncate(mdInline(str), 72)
+ return u.truncate(mdInline(str), 72)
}
RenderMsg.prototype.title = function (cb) {
@@ -503,7 +498,7 @@ RenderMsg.prototype.title1 = function (cb) {
if (err) return cb(err)
var name = about.name || about.title
|| (about.description && mdInline(about.description))
- if (name) return cb(null, truncate(name, 72))
+ if (name) return cb(null, u.truncate(name, 72))
self.message(function (err, el) {
if (err) return cb(err)
cb(null, '%' + title(h('div', el).textContent))
@@ -523,7 +518,7 @@ RenderMsg.prototype.link = function (link, cb) {
var ref = u.linkDest(link)
if (!ref) return cb(null, '')
self.getName(ref, function (err, name) {
- if (err) name = truncate(ref, 10)
+ if (err) name = u.truncate(ref, 10)
cb(null, h('a', {href: self.toUrl(ref)}, name))
})
}
@@ -595,7 +590,7 @@ RenderMsg.prototype.about = function (cb) {
return this.wrapMini([
isSelf ?
'self-identifies as ' :
- ['identifies ', h('a', {href: this.toUrl(this.c.about)}, truncate(this.c.about, 10)), ' as '],
+ ['identifies ', h('a', {href: this.toUrl(this.c.about)}, u.truncate(this.c.about, 10)), ' as '],
h('ins', String(this.c.name))
], cb)
}
@@ -606,9 +601,9 @@ RenderMsg.prototype.about = function (cb) {
isSelf ?
public ? 'is okay with being hosted publicly'
: 'wishes to not to be hosted publicly'
- : public ? ['thinks ', h('a', {href: this.toUrl(this.c.about)}, truncate(this.c.about, 10)),
+ : public ? ['thinks ', h('a', {href: this.toUrl(this.c.about)}, u.truncate(this.c.about, 10)),
' should be hosted publicly ']
- : ['wishes ', h('a', {href: this.toUrl(this.c.about)}, truncate(this.c.about, 10)),
+ : ['wishes ', h('a', {href: this.toUrl(this.c.about)}, u.truncate(this.c.about, 10)),
' to not be hosted publicly']
], cb)
}
@@ -655,7 +650,7 @@ RenderMsg.prototype.about = function (cb) {
this.c.review ? 'reviews' : 'describes', ' ',
!this.c.about ? ''
: showComputedName ? this.link1(this.c.about, done())
- : h('a', {href: this.toUrl(this.c.about)}, truncate(this.c.about, 10)),
+ : h('a', {href: this.toUrl(this.c.about)}, u.truncate(this.c.about, 10)),
': '
],
this.c.name ? [h('ins', String(this.c.name)), ' '] : '',
@@ -785,7 +780,7 @@ RenderMsg.prototype.pubOwnerConfirm = function (cb) {
var announcement = this.c.announcement
this.wrap([
'confirms pub ownership announcement ',
- h('a', {href: this.toUrl(announcement)}, truncate(announcement, 10)),
+ h('a', {href: this.toUrl(announcement)}, u.truncate(announcement, 10)),
this.c.address ? [
'. address: ',
h('input', {disabled: 'disabled', value: String(this.c.address), style: 'width:100%'})
@@ -811,7 +806,7 @@ RenderMsg.prototype.userInvite = function (cb) {
isValid ? 'valid ' : 'invalid ',
'user invite',
hostLink ? [' from ', hostLink] : '',
- h('a', {href: self.toUrl(invite)}, truncate(invite, 10)),
+ h('a', {href: self.toUrl(invite)}, u.truncate(invite, 10)),
], cb)
}
*/
@@ -1288,7 +1283,7 @@ RenderMsg.prototype.npmPackages = function (cb) {
var done = multicb({pluck: 1, spread: true})
var elCb = done()
function renderIdLink(id) {
- return [h('a', {href: self.toUrl(id)}, truncate(id, 8)), ' ']
+ return [h('a', {href: self.toUrl(id)}, u.truncate(id, 8)), ' ']
}
var singlePkg = self.c.mentions
&& self.c.mentions.length === 1
@@ -2100,7 +2095,7 @@ RenderMsg.prototype.tagTitle = function (cb) {
if (err) return cb(err)
var name = about.name || about.title
|| (about.description && mdInline(about.description))
- cb(null, name ? name.replace(/^%/, '') : truncate(self.msg.key, 8))
+ cb(null, name ? name.replace(/^%/, '') : u.truncate(self.msg.key, 8))
})
}
var done = multicb({pluck: 1, spread: true})
diff --git a/lib/render.js b/lib/render.js
index b4ca804..0e2f95f 100644
--- a/lib/render.js
+++ b/lib/render.js
@@ -419,18 +419,13 @@ Render.prototype.getName = function (id, cb) {
}
}
-function truncate(str, len) {
- str = String(str)
- return str.length > len ? str.substr(0, len) + '...' : str
-}
-
Render.prototype.getNameLink = function (id, opts, cb) {
if (!cb && typeof opts === 'function') cb = opts, opts = null
var length = opts && opts.length || Infinity
var self = this
self.getName(id, function (err, name) {
if (err) return cb(err)
- cb(null, h('a', {href: self.toUrl(id)}, truncate(name, length)))
+ cb(null, h('a', {href: self.toUrl(id)}, u.truncate(name, length)))
})
}
diff --git a/lib/util.js b/lib/util.js
index 5bd5f11..4fdbc24 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -261,3 +261,8 @@ u.estimateMessageSize = function (content) {
return JSON.stringify(draftMsg, null, 2).length
}
+
+u.truncate = function (str, len) {
+ str = String(str)
+ return str.length > len ? str.substr(0, len) + '...' : str
+}