Coder Social home page Coder Social logo

mysticatea / eaw Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 1.0 44 KB

The Node.js module to calculate the width of east Asian characters.

License: MIT License

JavaScript 100.00%
javascript unicode text east-asian-characters eastasianwidth npm npm-module npm-package web node nodejs

eaw's Introduction

eaw

npm version Downloads/month Build Status Coverage Status Dependency Status

The Node.js module to calculate the width of east Asian characters. (based on Unicode 9.0.0)

The script of this module is generated with EastAsianWidth.txt that Unicode Character Database provides.

The generator script is used to maintain this module as following to latest Unicode.

πŸ’Ώ Installation

This module can be installed by npm.

$ npm install --save eaw
  • It requires Node.js >=4.0.0.
  • It can be used in browsers by browserify-like tools.

πŸ“– Usage

CLI

This module provides a CLI command: eaw

The command calculates the width of east Asian characters for each line.

Usage: eaw [OPTIONS]

    It calculates the width of given east Asian characters.
    It reads the characters from stdin or `--text` option.

    OPTIONS:
        -s, --split <NUMBER> ... It splits the given text by LF to make the
                                 width of each line shorter than the given
                                 number.
        -t, --text <TEXT> ...... The characters that eaw processes instead of
                                 stdin.

Example:
    $ eaw --text "hello, δΈ–η•Œ"
    11
    $ cat hello.txt | eaw
    11
    $ cat hello.txt | eaw --split 4
    hell
    o,
    δΈ–η•Œ

Node.js

This module provides Node.js module.

const eaw = require("eaw")

console.log(eaw.isNarrowCharacter("A"))              // β†’ true
console.log(eaw.isNarrowCharacter("あ"))             // β†’ false
console.log(eaw.getWidth("A"))                       // β†’ 1
console.log(eaw.getWidth("あ"))                      // β†’ 2
console.log(eaw.getWidth("hello, δΈ–η•Œ"))             // β†’ 11
console.log(eaw.getWidth(["hello, δΈ–η•Œ", "🌟❀"]))   // β†’ [11, 4]
console.log(eaw.split("hello, δΈ–η•Œ", 4))             // β†’ ["hell", "o, ", "δΈ–η•Œ"]
console.log(eaw.split(["hello, δΈ–η•Œ", "πŸŒŸβ€πŸ‘"], 4)) // β†’ ["hell", "o, ", "δΈ–η•Œ", "🌟❀", "πŸ‘"]

process.stdin
    .pipe(eaw.createWidthStream())
    .pipe(process.stdout)

process.stdin
    .pipe(eaw.createSplitStream(4))
    .pipe(process.stdout)

eaw.isNarrowCharacter(character: string): boolean

It checks whether the given character is a narrow character or not.

This function checks the first code point if the argument has 2 or more characters.

eaw.getWidth(characters: string, widthOfWideCharacters?: number = 2): number
eaw.getWidth(characters: string[], widthOfWideCharacters?: number = 2): number[]
eaw.getWidth(characters: Iterable<string>, widthOfWideCharacters?: number = 2): Iterable<number>

It returns the total width of the given characters.

eaw.split(characters: string, maxPerLine: number, widthOfWideCharacters?: number = 2): string[]
eaw.split(characters: string[], maxPerLine: number, widthOfWideCharacters?: number = 2): string[]
eaw.split(characters: Iterable<string>, maxPerLine: number, widthOfWideCharacters?: number = 2): Iterable<string>

It splits the given characters to make the width of each line shorter than the given number.

eaw.createWidthStream(widthOfWideCharacters?: number = 2): stream.Transform

It returns the transform stream to calculate the width of each line.

eaw.createSplitStream(maxPerLine: number, widthOfWideCharacters?: number = 2): stream.Transform

It returns the transform stream to split the given characters to make the width of each line shorter than the given number.

πŸ“° Changelog

See GitHub Releases

πŸ’ͺ Contributing

Welcom your contributing!

Please use GitHub's issues/PRs.

Development tools

  • npm test runs tests.
  • npm run build generates the script from EastAsianWidth.txt.
  • npm run clean removes the coverage result of the last npm test command.
  • npm run coverage opens the coverage of the last npm test command by browsers.
  • npm run lint runs ESLint.
  • npm run watch runs tests for each file change.

eaw's People

Contributors

mysticatea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.