From a82a259159859bebe216cb71159e5de09877d88b Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Thu, 29 Aug 2024 23:29:44 +0100 Subject: [PATCH] factor reduce-flatten, reduce-without and regex-repo out of the project --- helpers/ddata.js | 29 ++++++++++++++------- helpers/helpers.js | 3 +-- package-lock.json | 58 ----------------------------------------- package.json | 11 +++++--- test/api.js | 2 +- test/ddata/parseLink.js | 20 +++++++------- 6 files changed, 40 insertions(+), 83 deletions(-) diff --git a/helpers/ddata.js b/helpers/ddata.js index 3bfca97..5727874 100644 --- a/helpers/ddata.js +++ b/helpers/ddata.js @@ -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 @@ -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({ @@ -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 = ':' @@ -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)) } @@ -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' } }) diff --git a/helpers/helpers.js b/helpers/helpers.js index 308ca63..d5da595 100644 --- a/helpers/helpers.js +++ b/helpers/helpers.js @@ -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. @@ -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) diff --git a/package-lock.json b/package-lock.json index 7682204..666ef1b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,10 +16,7 @@ "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" }, @@ -398,15 +395,6 @@ "url": "https://github.com/sponsors/isaacs" } }, - "node_modules/reduce-flatten": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/reduce-flatten/-/reduce-flatten-3.0.1.tgz", - "integrity": "sha512-bYo+97BmUUOzg09XwfkwALt4PQH1M5L0wzKerBt6WLm3Fhdd43mMS89HiT1B9pJIqko/6lWx3OnV4J9f2Kqp5Q==", - "license": "MIT", - "engines": { - "node": ">=8" - } - }, "node_modules/reduce-unique": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/reduce-unique/-/reduce-unique-2.0.1.tgz", @@ -416,52 +404,6 @@ "node": ">=6" } }, - "node_modules/reduce-without": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/reduce-without/-/reduce-without-1.0.1.tgz", - "integrity": "sha512-zQv5y/cf85sxvdrKPlfcRzlDn/OqKFThNimYmsS3flmkioKvkUGn2Qg9cJVoQiEvdxFGLE0MQER/9fZ9sUqdxg==", - "license": "MIT", - "dependencies": { - "test-value": "^2.0.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/reduce-without/node_modules/array-back": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/array-back/-/array-back-1.0.4.tgz", - "integrity": "sha512-1WxbZvrmyhkNoeYcizokbmh5oiOCIfyvGtcqbK3Ls1v1fKcquzxnQSceOx6tzq7jmai2kFLWIpGND2cLhH6TPw==", - "license": "MIT", - "dependencies": { - "typical": "^2.6.0" - }, - "engines": { - "node": ">=0.12.0" - } - }, - "node_modules/reduce-without/node_modules/test-value": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/test-value/-/test-value-2.1.0.tgz", - "integrity": "sha512-+1epbAxtKeXttkGFMTX9H42oqzOTufR1ceCF+GYA5aOmvaPq9wd4PUS8329fn2RRLGNeUkgRLnVpycjx8DsO2w==", - "license": "MIT", - "dependencies": { - "array-back": "^1.0.3", - "typical": "^2.6.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/regex-repo": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/regex-repo/-/regex-repo-5.0.0.tgz", - "integrity": "sha512-6sVOLr5uq9AxMiRh9Eu9ip66kk4viqnOk8ZYqWihDmXIRZqLqEv1DKyDLwVyOCwaErjhXmBKvAYwtQ9cC2GDSQ==", - "license": "Apache-2.0", - "engines": { - "node": ">=18.0.0" - } - }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", diff --git a/package.json b/package.json index 392aec6..4ffdf9a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/api.js b/test/api.js index 11a8c18..4e83311 100644 --- a/test/api.js +++ b/test/api.js @@ -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 } diff --git a/test/ddata/parseLink.js b/test/ddata/parseLink.js index 840223f..b38adc3 100644 --- a/test/ddata/parseLink.js +++ b/test/ddata/parseLink.js @@ -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 = [ { @@ -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}', @@ -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}', @@ -298,7 +298,7 @@ const cleverLinksResults = [ original: '{@linkplain symbolThirteen}', caption: 'symbolThirteen', url: 'symbolThirteen', - format: 'plain', + format: 'plain' }, { original: '{@linkcode proto://fourteen.asbf}', @@ -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)) {