index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict"
  2. var wcwidth = require('../')
  3. var test = require('tape')
  4. test('handles regular strings', function(t) {
  5. t.strictEqual(wcwidth('abc'), 3)
  6. t.end()
  7. })
  8. test('handles multibyte strings', function(t) {
  9. t.strictEqual(wcwidth('字的模块'), 8)
  10. t.end()
  11. })
  12. test('handles multibyte characters mixed with regular characters', function(t) {
  13. t.strictEqual(wcwidth('abc 字的模块'), 12)
  14. t.end()
  15. })
  16. test('ignores control characters e.g. \\n', function(t) {
  17. t.strictEqual(wcwidth('abc\n字的模块\ndef'), 14)
  18. t.end()
  19. })
  20. test('ignores bad input', function(t) {
  21. t.strictEqual(wcwidth(''), 0)
  22. t.strictEqual(wcwidth(3), 0)
  23. t.strictEqual(wcwidth({}), 0)
  24. t.strictEqual(wcwidth([]), 0)
  25. t.strictEqual(wcwidth(), 0)
  26. t.end()
  27. })
  28. test('ignores nul (charcode 0)', function(t) {
  29. t.strictEqual(wcwidth(String.fromCharCode(0)), 0)
  30. t.end()
  31. })
  32. test('ignores nul mixed with chars', function(t) {
  33. t.strictEqual(wcwidth('a' + String.fromCharCode(0) + '\n字的'), 5)
  34. t.end()
  35. })
  36. test('can have custom value for nul', function(t) {
  37. t.strictEqual(wcwidth.config({
  38. nul: 10
  39. })(String.fromCharCode(0) + 'a字的'), 15)
  40. t.end()
  41. })
  42. test('can have custom control char value', function(t) {
  43. t.strictEqual(wcwidth.config({
  44. control: 1
  45. })('abc\n字的模块\ndef'), 16)
  46. t.end()
  47. })
  48. test('negative custom control chars == -1', function(t) {
  49. t.strictEqual(wcwidth.config({
  50. control: -1
  51. })('abc\n字的模块\ndef'), -1)
  52. t.end()
  53. })