Files
shields/lib/svg-badge-parser.js
Paul Melnikow 66d444aa40 Clean up our callback style and enforce no exclusive tests and remove an exclusive test (#1900)
We use arrow functions in most places; this enforces it.

Passing arrow functions to Mocha is discouraged: https://mochajs.org/#arrow-functions

This was a mix of autofixes and hand adjustments.
2018-08-12 20:45:43 -04:00

37 lines
804 B
JavaScript

'use strict'
const nodeifySync = require('./nodeify-sync')
const leadingWhitespace = /(?:\r\n\s*|\r\s*|\n\s*)/g
const getValue = />([^<>]+)<\/text><\/g>/
function valueFromSvgBadge(svg) {
if (typeof svg !== 'string') {
throw TypeError('Parameter should be a string')
}
const stripped = svg.replace(leadingWhitespace, '')
const match = getValue.exec(stripped)
if (match) {
return match[1]
} else {
throw Error(`Can't get value from SVG:\n${svg}`)
}
}
// Get data from a svg-style badge.
// cb: function(err, string)
function fetchFromSvg(request, url, cb) {
request(url, (err, res, buffer) => {
if (err !== null) {
cb(err)
} else {
nodeifySync(() => valueFromSvgBadge(buffer), cb)
}
})
}
module.exports = {
valueFromSvgBadge,
fetchFromSvg,
}