1.0.1 • Published 3 years ago

@msikma/stringwrap-cjk v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

MIT license

stringwrap-cjk

This is a string wrapping library with support for CJK language wrapping rules. It's intended to be used for command line utilities.

CJK languages (Chinese, Japanese and Korean, plus historic Vietnamese) use wide characters that get displayed as two columns wide in the terminal. Several

Here's an example of it in action using a mixture of Latin and Japanese text:

const { stringWrapCJK } = require('@msikma/stringwrap-cjk')

const input = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempor risus id nibh mollis sollicitudin. 10分だけ付き合おう。もっと時間が必要だ。いい手品師になろうと本気で思っている。村人たちは皆、行方不明になった猫を探すために山の中へでかけた。`
const wrapped = stringWrapCJK(input, { maxWidth: 42 })
const separator = '-'.repeat(42)

console.log(separator)
console.log(wrapped)
console.log(separator)

This prints the following to the console (with separator lines used to indicate what the maximum width should be):

Latin text is wrapped by splitting the words by whitespace, and Japanese text is split per-character.

Usage

To install:

yarn add @msikma/stringwrap-cjk

Find this package on npm.

Options

The following options can be passed:

OptionTypeDescription
maxWidthint, strMaximum width of the output. This can be either a number value or a percentage such as "100%". If a percentage is given, the value is calculated based on the terminal size.
indentAmountint, arrAmount of indentation. This is either an integer or an array of two integers; with the array, a left and right indent can be added.
indentCharstrThe character used to indent the string.
indentTypestrThere are two types of indent: either the indent string is repeated, or the indent string is cropped to the desired width. Must be "repeat" or "crop".
lineCallbackfuncCallback function that runs on each line as it's split. Performed before any line trimming is done. If the callback function returns null or undefined, the line is skipped entirely.
newlineBeforeLongWordsboolWhether to insert a newline before a long word that exceed the maximum width.
newlineCharstrThe character used to create newlines.
whitespaceCharstrCharacter to convert all whitespace to, if whitespaceNormalize is true.
whitespaceMaintainWideSpaceboolWhether to maintain CJK wide space characters even when whitespaceNormalize is true.
whitespaceNormalizeboolWhether to normalize all whitespace characters to the same one.
whitespaceTrimboolWhether to trim the end of the result string.
textDirectionstrEither "ltr" (left to right) or "rtl" (right to left).
breakLongWordsboolWhether to break really long words (longer than the available space) up into chunks.
padToMaxWidthboolWhether to pad all lines to the full (visual) line width. Padding is done using the whitespace character, and is on if there is a right indent.
throwOnErrorboolIf any of the passed options are invalid, revert to using the defaults rather than throwing. On by default.
useVisualWidthboolWhether to determine string length based on the visual width of the characters. This means wide characters (such as CJK wide characters or emoji) count double.

Known issues

  • The textDirection option doesn't work very well yet and isn't included in the tests. All it does right now is change the way the padding works.

License

© MIT license.

1.0.1

3 years ago

1.0.0

3 years ago