Skip to content

Instantly share code, notes, and snippets.

@ponsfrilus
Created June 6, 2018 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ponsfrilus/309c4e44e9e3e6bb42c6172d0f9f895c to your computer and use it in GitHub Desktop.
Save ponsfrilus/309c4e44e9e3e6bb42c6172d0f9f895c to your computer and use it in GitHub Desktop.
// ANSI Terminal stlye \u001B[1municorn\u001B[22m
// ZWJ https://en.wikipedia.org/wiki/Zero-width_joiner 👶
// UNICODE ☆ 🤓 え 术
const testStrings = [
'2189☆ 404⑂ nodejs-learning-guide (@chyingp/Ruby)',
'Nodejs学习笔记以及经验总结,公众号"程序猿小卡"',
'🤓 Build your own (insert technology here)',
'后端架构师技术图谱',
'☆🤓端',
'☆ 🤓 端',
'\u001b[1m古\u001b[22m',
'\u001B[1municorn\u001B[22m',
'👶🏽', // zwj chars
'あいうえお',
'アイウエオ',
'※脚注',
]
const testLength = [
'49',
'46',
'42',
'18',
'5',
'9',
'2',
'7',
'2',
'10',
'5',
'5',
]
// npm i utfstring string-width string-length strip-ansi power-assert-util-string-width
var utfstring = require( 'utfstring' ) // https://www.npmjs.com/package/utfstring
var stringWidth = require( 'string-width' ) // https://www.npmjs.com/package/string-width
var stringLength = require( 'string-length' ) // https://www.npmjs.com/package/string-length
var stripAnsi = require( 'strip-ansi' ) // https://www.npmjs.com/package/strip-ansi
var _ = require( 'lodash' ) // https://github.com/lodash/lodash/blob/4.13.1-npm/_stringToArray.js
var paWidth = require( 'power-assert-util-string-width' ) // https://www.npmjs.com/package/power-assert-util-string-width
testStrings.forEach((e, i) => {
console.log('================================================================')
console.log('---------+---------+---------+---------+---------+---------+ raw:')
console.log(e)
console.log('------------------------------------------------------ js length:')
console.log(e.length)
console.log('------------------------------------------------------ utfstring:')
console.log(utfstring.visual.length(e))
console.log(utfstring.length(e))
console.log('--------------------------------------------------- string-width:')
console.log(stringWidth(e))
console.log('-------------------------------------------------- string-length:')
console.log(stringLength(e))
console.log('----------------------------------------------------- strip-ansi:')
console.log(stripAnsi(e).length)
console.log('--------------------------------------------------------- lodash:')
console.log(_.size(e))
console.log('-------------------------------------------------------- paWidth:')
console.log(paWidth(e))
console.log(paWidth.narrow(e))
console.log('-----------------------------------------------------------------')
console.log('EXPECTED: ' + testLength[i])
console.log('================================================================\n')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment