wordwrapjs
Advanced tools
Comparing version 0.1.0 to 0.2.0
"use strict"; | ||
var arrayify = require("array-back"); | ||
/** | ||
Word wrapping, with a few features. | ||
Word wrapping, with a few features. Namely the capability to ignore certain text patterns when wrapping (e.g. to prevent ansi escape sequences breaking layout in the terminal.) | ||
@module wordwrapjs | ||
@example | ||
Wrap some sick bars in a 20 character column. | ||
```js | ||
> wrap = require("wordwrapjs") | ||
> bars = "I'm rapping. I'm rapping. I'm rap rap rapping. I'm rap rap rap rap rappity rapping." | ||
> result = wrap(bars, { width: 20 }) | ||
``` | ||
`result` now looks like this: | ||
``` | ||
I'm rapping. I'm | ||
rapping. I'm rap rap | ||
rapping. I'm rap rap | ||
rap rap rappity | ||
rapping. | ||
``` | ||
*/ | ||
@@ -11,20 +30,35 @@ module.exports = wrap; | ||
/** | ||
@param {string} | ||
@param {object} | ||
@param {string} - the input text to wrap | ||
@param [options] {object} - optional config | ||
@param [options.width=30] {number} - the max column width in characters | ||
@param [options.ignore] {RegExp | RegExp[]} - one or more patterns to be ignored when sizing the newly wrapped lines. For example `ignore: /\u001b.*?m/g` will ignore unprintable ansi escape sequences. | ||
@param [options.newLine=os.EOL] {string} - the desired new line character to use, defaults to [os.EOL](https://nodejs.org/api/os.html#os_os_eol). | ||
@return {string} | ||
@alias module:wordwrapjs | ||
*/ | ||
function wrap(text, options){ | ||
// if (!(this instanceof WordWrap)) return new WordWrap(text, options); | ||
options = options || {}; | ||
options.width = options.width || 30; | ||
options.newLine = options.newLine || "\n"; | ||
var w = text.split(/\s+/); | ||
var words = text.split(/\s+/); | ||
var total = 0; | ||
var result = w.reduce(function(prev, curr){ | ||
total += curr.length + (prev ? 1 : 0); | ||
return words.reduce(function(prev, curr){ | ||
var currLength = curr.length; | ||
if (options.ignore){ | ||
var ignore = arrayify(options.ignore); | ||
var withoutIgnored = curr; | ||
ignore.forEach(function(pattern){ | ||
withoutIgnored = withoutIgnored.replace(pattern, ""); | ||
}); | ||
currLength = withoutIgnored.length; | ||
} | ||
total += currLength + (prev ? 1 : 0); | ||
if (total > options.width) { | ||
total = curr.length; | ||
if (prev) prev += "\n"; | ||
total = currLength; | ||
if (prev) prev += options.newLine; | ||
return prev + curr; | ||
@@ -36,4 +70,2 @@ } else { | ||
}, ""); | ||
return result; | ||
} |
{ | ||
"name": "wordwrapjs", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "0.1.0", | ||
"description": "wordwrapjs", | ||
"version": "0.2.0", | ||
"description": "Word-wrapping for javascript.", | ||
"repository": "https://github.com/75lb/wordwrapjs.git", | ||
@@ -29,3 +29,6 @@ "license": "MIT", | ||
"tape": "^4.2.0" | ||
}, | ||
"dependencies": { | ||
"array-back": "^1.0.2" | ||
} | ||
} |
@@ -8,16 +8,37 @@ [![view on npm](http://img.shields.io/npm/v/wordwrapjs.svg)](https://www.npmjs.org/package/wordwrapjs) | ||
## wordwrapjs | ||
Word wrapping, with a few features. | ||
Word wrapping, with a few features. Namely the capability to ignore certain text patterns when wrapping (e.g. to prevent ansi escape sequences breaking layout in the terminal.) | ||
<a name="module_wordwrapjs..wrap"></a> | ||
### wordwrapjs~wrap(text, options) | ||
**Kind**: inner method of <code>[wordwrapjs](#module_wordwrapjs)</code> | ||
**Example** | ||
Wrap some sick bars in a 20 character column. | ||
| Param | Type | | ||
| --- | --- | | ||
| text | <code>string</code> | | ||
| options | <code>object</code> | | ||
```js | ||
> wrap = require("wordwrapjs") | ||
> bars = "I'm rapping. I'm rapping. I'm rap rap rapping. I'm rap rap rap rap rappity rapping." | ||
> result = wrap(bars, { width: 20 }) | ||
``` | ||
`result` now looks like this: | ||
``` | ||
I'm rapping. I'm | ||
rapping. I'm rap rap | ||
rapping. I'm rap rap | ||
rap rap rappity | ||
rapping. | ||
``` | ||
<a name="exp_module_wordwrapjs--wrap"></a> | ||
### wrap(text, [options]) ⇒ <code>string</code> ⏏ | ||
**Kind**: Exported function | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| text | <code>string</code> | | the input text to wrap | | ||
| [options] | <code>object</code> | | optional config | | ||
| [options.width] | <code>number</code> | <code>30</code> | the max column width in characters | | ||
| [options.ignore] | <code>RegExp</code> | <code>Array.<RegExp></code> | | one or more patterns to be ignored when sizing the newly wrapped lines. For example `ignore: /\u001b.*?m/g` will ignore unprintable ansi escape sequences. | | ||
| [options.newLine] | <code>string</code> | <code>"os.EOL"</code> | the desired new line character to use, defaults to [os.EOL](https://nodejs.org/api/os.html#os_os_eol). | | ||
* * * | ||
© 2015 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). |
var test = require("tape"); | ||
var wrap = require("../"); | ||
// test("first", function(t){ | ||
// | ||
// }); | ||
var bars = "I'm rapping. I'm rapping. I'm rap rap rapping. I'm rap rap rap rap rappity rapping."; | ||
console.dir(wrap("75lb", { width: 3} )); | ||
test("simple", function(t){ | ||
t.strictEqual( | ||
wrap(bars), | ||
"I'm rapping. I'm rapping. I'm\nrap rap rapping. I'm rap rap\nrap rap rappity rapping." | ||
); | ||
t.end(); | ||
}); | ||
test("width", function(t){ | ||
t.strictEqual( | ||
wrap(bars, { width: 3 }), | ||
'I\'m\nrapping.\nI\'m\nrapping.\nI\'m\nrap\nrap\nrapping.\nI\'m\nrap\nrap\nrap\nrap\nrappity\nrapping.' | ||
); | ||
t.end(); | ||
}); | ||
test("ignore", function(t){ | ||
t.strictEqual( | ||
wrap(bars, { ignore: "I'm" }), | ||
'I\'m rapping. I\'m rapping. I\'m rap rap\nrapping. I\'m rap rap rap rap\nrappity rapping.' | ||
); | ||
t.end(); | ||
}); | ||
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
7400
9
84
44
1
+ Addedarray-back@^1.0.2
+ Addedarray-back@1.0.4(transitive)
+ Addedtypical@2.6.1(transitive)