greedy-wrap
Advanced tools
Comparing version 0.0.1 to 0.1.1
14
index.js
'use strict'; | ||
var stringLength = require('string-length'); | ||
var ansiRegex = require('ansi-regex'); | ||
module.exports = function (str, opts) { | ||
@@ -14,3 +17,4 @@ var recursiveSpaceLeft = 0; | ||
var regex = /\S+|\s?/g; | ||
var ansi = ansiRegex().toString().split('/')[1]; | ||
var regex = new RegExp(ansi + "|\\S+|\\s?", "g"); | ||
var words = str.match(regex); | ||
@@ -29,3 +33,3 @@ var spaceLeft = width; | ||
if (current.length > width) { | ||
if (stringLength(current) > width) { | ||
var overflow = wrap( current.substr(spaceLeft), { width: width } ); | ||
@@ -35,8 +39,8 @@ result += current.substr(0, spaceLeft) + linebreak + overflow; | ||
} else { | ||
if (current.length > spaceLeft) { | ||
if (stringLength(current) > spaceLeft) { | ||
result = result + linebreak + current; | ||
spaceLeft = width - current.length; | ||
spaceLeft = width - stringLength(current); | ||
} else { | ||
result += current; | ||
spaceLeft -= current.length; | ||
spaceLeft -= stringLength(current); | ||
} | ||
@@ -43,0 +47,0 @@ } |
{ | ||
"name": "greedy-wrap", | ||
"version": "0.0.1", | ||
"version": "0.1.1", | ||
"description": "A recursive greedy algorithm implementation for the word wrap process.", | ||
@@ -22,5 +22,11 @@ "license": "MIT", | ||
"keywords": [ | ||
"word-wrap", "word wrap", "greedy", "greedy-wrap" | ||
"word-wrap", | ||
"word wrap", | ||
"greedy", | ||
"greedy-wrap" | ||
], | ||
"dependencies": {}, | ||
"dependencies": { | ||
"ansi-regex": "^2.0.0", | ||
"string-length": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
@@ -27,0 +33,0 @@ "mocha": "*" |
# greedy-wrap [![Build Status](https://travis-ci.org/danilosampaio/greedy-wrap.svg?branch=master)](https://travis-ci.org/danilosampaio/greedy-wrap) | ||
> A recursive greedy algorithm implementation for the word wrap process. | ||
> It also correctly calculate the width of the words containing ansi escape codes. | ||
@@ -28,2 +29,3 @@ | ||
greedyWrap('Supercalifragilisticexpialidocious', {width: 10}); | ||
@@ -37,2 +39,12 @@ | ||
ious | ||
greedyWrap('\u001b[1mthis\u001b[22m is a text with only \u001b[1mshort\u001b[22m words.', {width: 10}) | ||
//=> Result: | ||
this is a | ||
text with | ||
only short | ||
words. | ||
``` | ||
@@ -39,0 +51,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4200
42
74
2
+ Addedansi-regex@^2.0.0
+ Addedstring-length@^1.0.1
+ Addedansi-regex@2.1.1(transitive)
+ Addedstring-length@1.0.1(transitive)
+ Addedstrip-ansi@3.0.1(transitive)