+64
-30
@@ -6,3 +6,6 @@ 'use strict'; | ||
| }); | ||
| exports.parseWords = parseWords; | ||
| exports.wrap = wrap; | ||
| exports.square = square; | ||
| exports.unwrap = unwrap; | ||
@@ -41,5 +44,3 @@ var _graphemeSplitter = require('grapheme-splitter'); | ||
| */ | ||
| function square(str) { | ||
| var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
| function parseWords(str) { | ||
| var chars = splitter.splitGraphemes(str); | ||
@@ -49,17 +50,15 @@ var words = []; | ||
| var charCount = 0; | ||
| { | ||
| var word = []; | ||
| for (var i = 0; i < chars.length; i++) { | ||
| var char = chars[i]; | ||
| if (!isWhiteSpace(char)) { | ||
| charCount++; | ||
| word.push(char); | ||
| if (i < chars.length - 1) continue; | ||
| } | ||
| var word = []; | ||
| for (var i = 0; i < chars.length; i++) { | ||
| var char = chars[i]; | ||
| if (!isWhiteSpace(char)) { | ||
| charCount++; | ||
| word.push(char); | ||
| if (i < chars.length - 1) continue; | ||
| } | ||
| if (word.length) { | ||
| longestWordLength = Math.max(longestWordLength, word.length); | ||
| words.push(word); | ||
| word = []; | ||
| } | ||
| if (word.length) { | ||
| longestWordLength = Math.max(longestWordLength, word.length); | ||
| words.push(word); | ||
| word = []; | ||
| } | ||
@@ -69,16 +68,15 @@ } | ||
| if (!words.length) return ''; | ||
| return { words: words, charCount: charCount, longestWordLength: longestWordLength }; | ||
| } | ||
| var target = Math.max(opts.longWordForcesRect ? longestWordLength : 0, Math.ceil(Math.sqrt(charCount)) // chances are an extra line will be needed, so round up to columns | ||
| ); | ||
| function wrapToTarget(words, target) { | ||
| var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
| DEBUG && console.log({ str: str, target: target, charCount: charCount, longestWordLength: longestWordLength }, words); | ||
| var output = []; | ||
| var line = []; //words[0]; | ||
| for (var _i = 0; _i < words.length; _i++) { | ||
| var _word = words[_i]; | ||
| DEBUG && console.log({ word: _word, line: line }, line.length + 1 + _word.length); | ||
| var line = []; | ||
| for (var i = 0; i < words.length; i++) { | ||
| var word = words[i]; | ||
| DEBUG && console.log({ word: word, line: line }, line.length + 1 + word.length); | ||
| if (line.length + 1 + _word.length > target && line.length) { | ||
| if (line.length + 1 + word.length > target && line.length) { | ||
| output.push(line.join('')); | ||
@@ -89,5 +87,5 @@ line.length = 0; | ||
| if (line.length) line.push(' '); | ||
| line.push.apply(line, _toConsumableArray(_word)); | ||
| line.push.apply(line, _toConsumableArray(word)); | ||
| if (_i === words.length - 1) output.push(line.join('')); | ||
| if (i === words.length - 1) output.push(line.join('')); | ||
| } | ||
@@ -97,3 +95,39 @@ | ||
| return output.join('\n'); | ||
| return output.join(opts.lineDelimeter || '\n'); | ||
| } | ||
| function wrap(str) { | ||
| var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
| var _parseWords = parseWords(str), | ||
| words = _parseWords.words; | ||
| return wrapToTarget(words, opts.width || 80, opts); | ||
| } | ||
| function square(str) { | ||
| var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
| var _parseWords2 = parseWords(str), | ||
| words = _parseWords2.words, | ||
| charCount = _parseWords2.charCount, | ||
| longestWordLength = _parseWords2.longestWordLength; | ||
| var target = Math.max(opts.longWordForcesRect ? longestWordLength : 0, Math.ceil(Math.sqrt(charCount)) // chances are an extra line will be needed, so round up to columns | ||
| ); | ||
| DEBUG && console.log({ str: str, target: target, charCount: charCount, longestWordLength: longestWordLength }, words); | ||
| return wrapToTarget(words, target, opts); | ||
| } | ||
| function unwrap(str) { | ||
| var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
| var _parseWords3 = parseWords(str), | ||
| words = _parseWords3.words; | ||
| return words.map(function (word) { | ||
| return word.join(''); | ||
| }).join(' '); | ||
| } |
+42
-25
@@ -54,3 +54,3 @@ <!DOCTYPE html> | ||
| */ | ||
| export function square(str, opts = {}) { | ||
| export function parseWords(str) { | ||
| const chars = splitter.splitGraphemes(str); | ||
@@ -60,17 +60,15 @@ const words = []; | ||
| let charCount = 0; | ||
| { | ||
| let word = []; | ||
| for (let i = 0; i < chars.length; i++) { | ||
| const char = chars[i]; | ||
| if (!isWhiteSpace(char)) { | ||
| charCount++; | ||
| word.push(char); | ||
| if (i < chars.length - 1) continue; | ||
| } | ||
| let word = []; | ||
| for (let i = 0; i < chars.length; i++) { | ||
| const char = chars[i]; | ||
| if (!isWhiteSpace(char)) { | ||
| charCount++; | ||
| word.push(char); | ||
| if (i < chars.length - 1) continue; | ||
| } | ||
| if (word.length) { | ||
| longestWordLength = Math.max(longestWordLength, word.length); | ||
| words.push(word); | ||
| word = []; | ||
| } | ||
| if (word.length) { | ||
| longestWordLength = Math.max(longestWordLength, word.length); | ||
| words.push(word); | ||
| word = []; | ||
| } | ||
@@ -80,13 +78,8 @@ } | ||
| if (!words.length) return ''; | ||
| return {words, charCount, longestWordLength}; | ||
| } | ||
| const target = Math.max( | ||
| opts.longWordForcesRect ? longestWordLength : 0, | ||
| Math.ceil(Math.sqrt(charCount)) // chances are an extra line will be needed, so round up to columns | ||
| ); | ||
| DEBUG && console.log({str, target, charCount, longestWordLength}, words); | ||
| function wrapToTarget(words, target, opts = {}) { | ||
| const output = []; | ||
| const line = []; //words[0]; | ||
| const line = []; | ||
| for (let i = 0; i < words.length; i++) { | ||
@@ -109,4 +102,28 @@ const word = words[i]; | ||
| return output.join('\n'); | ||
| return output.join(opts.lineDelimeter || '\n'); | ||
| } | ||
| export function wrap(str, opts = {}) { | ||
| const {words} = parseWords(str); | ||
| return wrapToTarget(words, opts.width || 80, opts); | ||
| } | ||
| export function square(str, opts = {}) { | ||
| const {words, charCount, longestWordLength} = parseWords(str); | ||
| const target = Math.max( | ||
| opts.longWordForcesRect ? longestWordLength : 0, | ||
| Math.ceil(Math.sqrt(charCount)) // chances are an extra line will be needed, so round up to columns | ||
| ); | ||
| DEBUG && console.log({str, target, charCount, longestWordLength}, words); | ||
| return wrapToTarget(words, target, opts); | ||
| } | ||
| export function unwrap(str, opts = {}) { | ||
| const {words} = parseWords(str); | ||
| return words.map(word => word.join('')).join(' '); | ||
| } | ||
| </code></pre> | ||
@@ -113,0 +130,0 @@ </article> |
@@ -152,3 +152,3 @@ <!DOCTYPE html> | ||
| <h4 class="name" id=".square"><span class="type-signature">(static) </span>square<span class="signature">()</span><span class="type-signature"></span></h4> | ||
| <h4 class="name" id=".parseWords"><span class="type-signature">(static) </span>parseWords<span class="signature">()</span><span class="type-signature"></span></h4> | ||
@@ -155,0 +155,0 @@ |
+1
-1
| { | ||
| "name": "wrap-words", | ||
| "version": "0.0.3", | ||
| "version": "0.0.4", | ||
| "description": "Word wrap.", | ||
@@ -5,0 +5,0 @@ "main": "dist/", |
Sorry, the diff of this file is too big to display
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
1166372
0.21%2463
2.07%