Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

format-javascript-comment

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

format-javascript-comment - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

48

lib/formatCommentLines.js
'use babel';
const fairLines = require('fair-lines');
const MAX_TOTAL_LINE_LENGTH = 80;

@@ -10,3 +12,2 @@ const JSDOC_TAGS = [

/**
* First, hacky implementation. But the tests pass :).
* @param {array} lines

@@ -18,2 +19,4 @@ * @return {array} The formatted lines.

// Remember the whitespace before the symbol,
// after the symbol and the symbol itself.
let whitespaceBefore = lines[0].match(/^\s*/);

@@ -23,9 +26,7 @@ let symbol = lines[0].match(/\/\/|\*/)[0];

let comment = lines
// Remove whitespace and symbol
.map(line =>
line
.replace(new RegExp('^' + whitespaceBefore + '\\' + symbol), '')
.trimLeft()
).join(' ');
// Remove whitespace and symbol
let comment = lines.map(line => line
.replace(new RegExp('^' + whitespaceBefore + '\\' + symbol), '')
.trimLeft()
).join(' ');

@@ -37,20 +38,21 @@ let maxLineLength = MAX_TOTAL_LINE_LENGTH

let parts = comment.split(' ');
let jsdocGroup = '(' + JSDOC_TAGS.join('|') + ')';
let splitLineRegex = new RegExp(`${jsdocGroup}.*?(?=${jsdocGroup}|$)`, 'g');
let newLines = [];
let curLine = -1;
let result = comment.replace(splitLineRegex, match => {
newLines.push(match);
return '';
});
while (parts.length > 0) {
let curPart = parts[0];
let advanceLine = newLines[curLine] === undefined
|| newLines[curLine].length + curPart.length > maxLineLength
|| JSDOC_TAGS.includes(curPart);
if (advanceLine) {
curLine++;
newLines[curLine] = '';
}
newLines[curLine] += curPart + ' ';
parts.splice(0, 1);
}
if (newLines.length === 0) newLines.push(comment);
else newLines.unshift(result);
// Insert comment symbol
newLines = newLines.map(line => fairLines.balanced(line, {
width: maxLineLength
}));
// Flatten the two dimensional array
newLines = newLines.concat.apply([], newLines);
// Insert comment symbol with the whitespace
newLines = newLines.map(line =>

@@ -57,0 +59,0 @@ whitespaceBefore + symbol + whitespaceAfter + line.trimRight()

{
"name": "format-javascript-comment",
"main": "./lib/format-comment",
"version": "0.1.3",
"version": "0.1.4",
"author": "Jan Amann <jan@amann.me>",

@@ -16,3 +16,8 @@ "description": "Format your JavaScript comments",

},
"dependencies": {},
"scripts": {
"lint": "eslint ."
},
"dependencies": {
"fair-lines": "^1.0.0"
},
"devDependencies": {

@@ -19,0 +24,0 @@ "babel-eslint": "^6.0.0",

# Format JavaScript comment for Atom
> Reformats your multiline comments into beautiful, maximum 80 character long lines, with no gaps or irregularities. ✨
> Reformats your multiline comments into beautiful, maximum 80 character long lines with [minimum raggedness](https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap#Minimum_raggedness). ✨

@@ -8,5 +8,13 @@ ## What?

```js
// Lorem ipsum dolor sit amet, consectetur
// adipisicing elit, sed do eiusmod tempor incididunt ut labore
// et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in.
/**
* If it keeps on rainin', levee's goin' to break. When the levee breaks I'll have no place to stay.
* @param {number} itKeepsOnRaining
* @param {string} [targetCity = 'Chicago'] It's got what it takes to make a mountain man leave his home.
* Oh, well, oh, well, oh, well.
* @return {string}
*/
function whatToDo(itKeepsOnRaining, targetCity = 'Chicago') {
if (itKeepsOnRaining) return `Levee's goin' to break; Going to ${targetCity}.`;
else return 'Sit on the levee and moan';
}
```

@@ -16,6 +24,14 @@

```js
// Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
// tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
// quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
// consequat. Duis aute irure dolor in.
/**
* If it keeps on rainin', levee's goin' to break.
* When the levee breaks I'll have no place to stay.
* @param {number} itKeepsOnRaining
* @param {string} [targetCity = 'Chicago'] It's got what it takes to
* make a mountain man leave his home. Oh, well, oh, well, oh, well.
* @return {string}
*/
function whatToDo(itKeepsOnRaining, targetCity = 'Chicago') {
if (itKeepsOnRaining) return `Levee's goin' to break; Going to ${targetCity}.`;
else return 'Sit on the levee and moan';
}
```

@@ -39,3 +55,3 @@

- Better way to find the selection automatically if no selection is provided
- Distribute words consistently across lines
- Configurable line length
- Retain blank spaces

@@ -0,1 +1,2 @@

/* eslint-disable max-len */
'use babel';

@@ -11,4 +12,4 @@

])).toEqual([
'// Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod',
'// tempor incididunt ut labore et dolore magna aliqua.'
'// Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed',
'// do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
]);

@@ -21,6 +22,6 @@ });

])).toEqual([
'// Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod',
'// tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,',
'// quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo',
'// consequat. Duis aute irure'
'// Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed',
'// do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
'// Ut enim ad minim veniam, quis nostrud exercitation ullamco',
'// laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure'
]);

@@ -70,3 +71,3 @@ });

describe('\* comments', () => {
describe('\\* comments', () => {
it('turns one line into two lines', () => {

@@ -76,4 +77,4 @@ expect(formatCommentLines([

])).toEqual([
'* Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod',
'* tempor incididunt ut labore et dolore magna aliqua.'
'* Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed',
'* do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
]);

@@ -86,6 +87,6 @@ });

])).toEqual([
' * Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod',
' * tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,',
' * quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo',
' * consequat. Duis aute irure'
' * Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed',
' * do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
' * Ut enim ad minim veniam, quis nostrud exercitation ullamco',
' * laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure'
]);

@@ -104,13 +105,12 @@ });

expect(formatCommentLines([
' * Description',
' * @param {string} test Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do.',
' * @param {string} [test = 2] Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
' * Some more description.'
' * Description',
' * @param {string} test Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do.',
' * @param {string} [test = 2] Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
])).toEqual([
' * Description',
' * @param {string} test Lorem ipsum dolor sit amet, consectetur adipisicing',
' * elit, sed do.',
' * @param {string} [test = 2] Lorem ipsum dolor sit amet, consectetur',
' * adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna',
' * aliqua. * Some more description.'
' * Description',
' * @param {string} test Lorem ipsum dolor sit',
' * amet, consectetur adipisicing elit, sed do.',
' * @param {string} [test = 2] Lorem ipsum dolor sit',
' * amet, consectetur adipisicing elit, sed do eiusmod',
' * tempor incididunt ut labore et dolore magna aliqua.'
]);

@@ -123,4 +123,4 @@ });

])).toEqual([
' * This is an email: jan@amann.me. I\'m currently @ home. It\'s also possible to',
' * write it like @home if I want'
' * This is an email: jan@amann.me. I\'m currently @ home.',
' * It\'s also possible to write it like @home if I want'
]);

@@ -127,0 +127,0 @@ });

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc