项目作者: suddi

项目描述 :
NPM string formatting module for string beautification (https://www.npmjs.com/package/string-formatting)
高级语言: JavaScript
项目地址: git://github.com/suddi/string-formatting.git
创建时间: 2017-01-17T10:28:02Z
项目社区:https://github.com/suddi/string-formatting

开源协议:MIT License

下载


string-formatting

CircleCI
codecov
Codacy Badge
npm
npm
David
David
license

coverage

String formatting module for string beautification, splits string optimally over multiple lines.
Can be used for formatting addresses.

Installation

  1. npm install --save string-formatting

API

  1. const StringFormatter = require('string-formatting');
  2. StringFormatting.apply(<string-to-be-formatted>, <options>);

Default configuration, can be overriden with user-defined options:

  1. {
  2. numLines: 1, // number of lines to format to (numLines and lengthOfLine have overlapping use, please see below)
  3. lengthOfLine: [255], // length for each line as a number or length for each specific line in an Array (numLines and lengthOfLine have overlapping use, please see below)
  4. firstLineRequired: true, // whether the first line in the array must have a value, else fail
  5. splitTokenRegex: / /, // regex to split the string with
  6. mergeToken: ' ' // string to merge the string with in case multiple words join on the same line
  7. };

NOTE: When both numLines defined and lengthOfLine is defined as an Array (where each line can have multiple lengths).
string-formatting requires that the numLines and the length of lengthOfLine Array be the same.
This is because in the scenario where lengthOfLine is defined per line, numLines is an extraneous value and can be omitted.

Usage

  1. const StringFormatter = require('string-formatting');
  2. const output = StringFormatting.apply('Hello World!', {
  3. lengthOfLine: [5, 6] // the first line is allowed to have a maximum length of 5, the second line, a maximum length of 6
  4. });
  5. console.log(output);
  6. // ['Hello', 'World!']
  7. const output = StringFormatting.apply('Hello World! I am Node.js', {
  8. numLines: 2,
  9. lengthOfLine: 12
  10. });
  11. console.log(output);
  12. // ['Hello World!', 'I am Node.js']
  13. const output = StringFormatting.apply('Hello World!', {
  14. lengthOfLine: [2, 100],
  15. firstLineRequired: false
  16. });
  17. console.log(output);
  18. // ['', 'Hello World!']
  19. const output = StringFormatting.apply('Hello World!', {
  20. lengthOfLine: [4, 6],
  21. splitTokenRegex: /[aeiou]/,
  22. mergeToken: ';'
  23. });
  24. console.log(output);
  25. // ['H;ll', 'W;rld!']

For more workable examples, please see fixtures.