Skip to content

Commit

Permalink
factor reduce-flatten, reduce-without and regex-repo out of the project
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Aug 29, 2024
1 parent 6c33d89 commit a82a259
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 83 deletions.
29 changes: 20 additions & 9 deletions helpers/ddata.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ const handlebars = require('handlebars')
const { marked } = require('marked')
const objectGet = require('object-get')
const where = require('test-value').where
const flatten = require('reduce-flatten')
const state = require('../lib/state')
const urlRe = require('regex-repo').urlRe

let malformedDataWarningIssued = false

function isValidURL (url) {
try {
new URL(url)
return true
} catch (err) {
if (err.code === 'ERR_INVALID_URL') {
return false
} else {
throw err
}
}
}

/**
* ddata is a collection of handlebars helpers for working with the documentation data output by [jsdoc-parse](https://github.com/75lb/jsdoc-parse).
* @module
Expand Down Expand Up @@ -268,7 +279,7 @@ function returnSig2 (options) {
if (typeNames.length) {
return options.fn({
symbol: '⇒',
types: typeNames.reduce(flatten, [])
types: typeNames.flat()
})
} else {
return options.fn({
Expand Down Expand Up @@ -338,7 +349,7 @@ function sig (options) {
return name
})
if (typeNames.length) {
data.returnTypes = typeNames.reduce(flatten, [])
data.returnTypes = typeNames.flat()
}
} else if ((this.type || this.kind === 'namespace') && this.kind !== 'event') {
data.returnSymbol = ':'
Expand Down Expand Up @@ -649,7 +660,7 @@ function parseLink (text, dmdOptions = {}) {
original: matches[0],
caption: matches[3],
url: matches[2],
format: matches[1]
format: matches[1]
})
text = text.replace(matches[0], ' '.repeat(matches[0].length))
}
Expand Down Expand Up @@ -677,13 +688,13 @@ function parseLink (text, dmdOptions = {}) {
results.forEach((result) => {
const format = result.format
if (format === undefined) {
result.format = format // if tag is @linkplain or @linkcode, then that determines the format
result.format = format || // if tag is @linkplain or @linkcode, then that determines the format
// else, if 'clever-links' is true, then if the link is a URL, it's plain, otherwise code format
|| (dmdOptions['clever-links'] && (urlRe.test(result.url) ? 'plain' : 'code'))
(dmdOptions['clever-links'] && (isValidURL(result.url) ? 'plain' : 'code')) ||
// else, if 'monospace-links' is true, then all links are code format
|| (dmdOptions['monospace-links'] && 'code')
(dmdOptions['monospace-links'] && 'code') ||
// else, it's a plain
|| 'plain'
'plain'
}
})

Expand Down
3 changes: 1 addition & 2 deletions helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const handlebars = require('handlebars')
const util = require('util')
const commonSequence = require('common-sequence')
const unique = require('reduce-unique')
const without = require('reduce-without')

/**
A library of helpers used exclusively by dmd.. dmd also registers helpers from ddata.
Expand Down Expand Up @@ -202,7 +201,7 @@ function _groupBy (identifiers, groupByFields) {
})
.map(function (i) { return i[group] })
.reduce(unique, [])
if (groupValues.length <= 1) groupByFields = groupByFields.reduce(without(group), [])
if (groupValues.length <= 1) groupByFields = groupByFields.filter(g => g !== group)
})
identifiers = _addGroup(identifiers, groupByFields)

Expand Down
58 changes: 0 additions & 58 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,21 @@
"handlebars": "^4.7.8",
"marked": "^14.1.0",
"object-get": "^2.1.1",
"reduce-flatten": "^3.0.1",
"reduce-unique": "^2.0.1",
"reduce-without": "^1.0.1",
"regex-repo": "^5.0.0",
"test-value": "^3.0.0",
"walk-back": "^5.1.1"
},
"devDependencies": {
"dmd-plugin-example": "^0.1.0"
},
"peerDependencies": {
"@75lb/nature": "latest"
},
"peerDependenciesMeta": {
"@75lb/nature": {
"optional": true
}
},
"standard": {
"ignore": [
"example",
Expand Down
2 changes: 1 addition & 1 deletion test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test.set('Dmd issue #89 - Max callstack size exceeded bug', async function () {
}
]
const options = {
files: [ 'foo.js' ],
files: ['foo.js'],
template: '{{#class name="Foo"}}{{>docs}}{{/class}}\n',
noCache: true
}
Expand Down
20 changes: 10 additions & 10 deletions test/ddata/parseLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ test.set('multiple {@link(plain/code) someSymbol Caption here}', function () {
})

// {@link symbol catption} style
const allLinksText = 'blah {@linkplain thingOne Caption one} blah {@linkcode ftp://url-two.tld Caption two} whatever {@link thingThree Caption three} !@ {@link https://url-four.com Caption four} ok '
const allLinksText = 'blah {@linkplain thingOne Caption one} blah {@linkcode ftp://url-two.tld Caption two} whatever {@link thingThree Caption three} !@ {@link https://url-four.com Caption four} ok ' +
// {@link symbol|caption} style
+ '{@linkplain thingFive|caption five} nah {@linkcode git://url-six.com|caption six} ??? {@link thingSeven|caption seven} {@link https://url-eight.net|caption eight} @typedef '
'{@linkplain thingFive|caption five} nah {@linkcode git://url-six.com|caption six} ??? {@link thingSeven|caption seven} {@link https://url-eight.net|caption eight} @typedef ' +
// [caption]{@link symbol} style
+ '[caption nine]{@linkplain symbolNine} ach [caption ten]{@linkcode http://url.ten.com} 2434 [caption eleven]{@link symbolEleven} http://foo.com [caption twelve]{@link http://url.12.com} whawha'
'[caption nine]{@linkplain symbolNine} ach [caption ten]{@linkcode http://url.ten.com} 2434 [caption eleven]{@link symbolEleven} http://foo.com [caption twelve]{@link http://url.12.com} whawha' +
// {@link symbol} style
+ '{@linkplain symbolThirteen} fee {@linkcode proto://fourteen.asbf} blb {@link symbolFifteen} geez {@link telnet://16.123.123.123}'
'{@linkplain symbolThirteen} fee {@linkcode proto://fourteen.asbf} blb {@link symbolFifteen} geez {@link telnet://16.123.123.123}'

const cleverLinksResults = [
{
Expand All @@ -238,7 +238,7 @@ const cleverLinksResults = [
original: '{@link thingThree Caption three}',
caption: 'Caption three',
url: 'thingThree',
format: 'code',
format: 'code'
},
{
original: '{@link https://url-four.com Caption four}',
Expand All @@ -262,7 +262,7 @@ const cleverLinksResults = [
original: '{@link thingSeven|caption seven}',
caption: 'caption seven',
url: 'thingSeven',
format: 'code',
format: 'code'
},
{
original: '{@link https://url-eight.net|caption eight}',
Expand Down Expand Up @@ -298,7 +298,7 @@ const cleverLinksResults = [
original: '{@linkplain symbolThirteen}',
caption: 'symbolThirteen',
url: 'symbolThirteen',
format: 'plain',
format: 'plain'
},
{
original: '{@linkcode proto://fourteen.asbf}',
Expand All @@ -320,15 +320,15 @@ const cleverLinksResults = [
}
]

test.set("'clever-links' true, 'monospace-links' undefined", function() {
test.set("'clever-links' true, 'monospace-links' undefined", function () {
a.deepEqual(ddata.parseLink(allLinksText, { 'clever-links': true }), cleverLinksResults)
})

test.set("'clever-links' true overrides 'monospace-links' true", function() {
test.set("'clever-links' true overrides 'monospace-links' true", function () {
a.deepEqual(ddata.parseLink(allLinksText, { 'clever-links': true, 'monospace-links': true }), cleverLinksResults)
})

test.set("'monospace-links' set all {@link}s to 'code' format", function() {
test.set("'monospace-links' set all {@link}s to 'code' format", function () {
const monospaceLinkResults = cleverLinksResults.map((result) => {
const newResult = Object.assign({}, result)
if (!/@link(?:code|plain)/.test(result.original)) {
Expand Down

0 comments on commit a82a259

Please sign in to comment.