unicode.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import {
  6. isLowSurrogateHalf,
  7. isHighSurrogateHalf,
  8. isCombiningMark,
  9. isInsideSurrogatePair,
  10. isInsideCombinedSymbol
  11. } from '../src/unicode';
  12. describe( 'utils', () => {
  13. describe( 'unicode', () => {
  14. describe( 'isHighSurrogateHalf', () => {
  15. it( 'should return true if given character is a high surrogate half', () => {
  16. // '𨭎'.charCodeAt( 0 ); // 55394 --> high surrogate half.
  17. // '𨭎'.charCodeAt( 1 ); // 57166 --> low surrogate half.
  18. expect( isHighSurrogateHalf( String.fromCharCode( 55394 ) ) ).to.be.true;
  19. } );
  20. it( 'should return false if given character is a low surrogate half', () => {
  21. // '𨭎'.charCodeAt( 0 ); // 55394 --> high surrogate half.
  22. // '𨭎'.charCodeAt( 1 ); // 57166 --> low surrogate half.
  23. expect( isHighSurrogateHalf( String.fromCharCode( 57166 ) ) ).to.be.false;
  24. } );
  25. it( 'should return false if given character is not a surrogate half', () => {
  26. expect( isHighSurrogateHalf( 'a' ) ).to.be.false;
  27. expect( isHighSurrogateHalf( '𨭎' ) ).to.be.false;
  28. } );
  29. it( 'should return false for falsy values', () => {
  30. expect( isHighSurrogateHalf( null ) ).to.be.false;
  31. expect( isHighSurrogateHalf( undefined ) ).to.be.false;
  32. expect( isHighSurrogateHalf( false ) ).to.be.false;
  33. expect( isHighSurrogateHalf( 0 ) ).to.be.false;
  34. } );
  35. } );
  36. describe( 'isLowSurrogateHalf', () => {
  37. it( 'should return true if given character is a low surrogate half', () => {
  38. // '𨭎'.charCodeAt( 0 ); // 55394 --> high surrogate half.
  39. // '𨭎'.charCodeAt( 1 ); // 57166 --> low surrogate half.
  40. expect( isLowSurrogateHalf( String.fromCharCode( 57166 ) ) ).to.be.true;
  41. } );
  42. it( 'should return false if given character is a high surrogate half', () => {
  43. // '𨭎'.charCodeAt( 0 ); // 55394 --> high surrogate half.
  44. // '𨭎'.charCodeAt( 1 ); // 57166 --> low surrogate half.
  45. expect( isLowSurrogateHalf( String.fromCharCode( 55394 ) ) ).to.be.false;
  46. } );
  47. it( 'should return false if given character is not a surrogate half', () => {
  48. expect( isLowSurrogateHalf( 'a' ) ).to.be.false;
  49. expect( isLowSurrogateHalf( '𨭎' ) ).to.be.false;
  50. } );
  51. it( 'should return false for falsy values', () => {
  52. expect( isLowSurrogateHalf( null ) ).to.be.false;
  53. expect( isLowSurrogateHalf( undefined ) ).to.be.false;
  54. expect( isLowSurrogateHalf( false ) ).to.be.false;
  55. expect( isLowSurrogateHalf( 0 ) ).to.be.false;
  56. } );
  57. } );
  58. describe( 'isCombiningMark', () => {
  59. it( 'should return true if given character is a combining mark', () => {
  60. expect( isCombiningMark( '̣' ) ).to.be.true;
  61. } );
  62. it( 'should return false if given character is not a combining mark', () => {
  63. expect( isCombiningMark( 'a' ) ).to.be.false;
  64. expect( isCombiningMark( 'q̣' ) ).to.be.false;
  65. } );
  66. it( 'should return false for falsy values', () => {
  67. expect( isCombiningMark( null ) ).to.be.false;
  68. expect( isCombiningMark( undefined ) ).to.be.false;
  69. expect( isCombiningMark( false ) ).to.be.false;
  70. expect( isCombiningMark( 0 ) ).to.be.false;
  71. } );
  72. } );
  73. describe( 'isInsideSurrogatePair', () => {
  74. const testString = 'a𨭎b';
  75. it( 'should return true if given offset in a string is inside a surrogate pair', () => {
  76. expect( isInsideSurrogatePair( testString, 2 ) ).to.be.true;
  77. } );
  78. it( 'should return false if given offset in a string is not inside a surrogate pair', () => {
  79. expect( isInsideSurrogatePair( testString, 1 ) ).to.be.false;
  80. expect( isInsideSurrogatePair( testString, 3 ) ).to.be.false;
  81. } );
  82. it( 'should return false if given offset in a string is at the string beginning or end', () => {
  83. expect( isInsideSurrogatePair( testString, 0 ) ).to.be.false;
  84. expect( isInsideSurrogatePair( testString, 4 ) ).to.be.false;
  85. } );
  86. it( 'should return false if given offset is between two surrogate pairs', () => {
  87. expect( isInsideSurrogatePair( '𨭎𨭎', 2 ) ).to.be.false;
  88. } );
  89. } );
  90. describe( 'isInsideCombinedSymbol', () => {
  91. const testString = 'aa̻̐ͩbb';
  92. it( 'should return true if given offset in a string is inside a symbol with combining mark', () => {
  93. expect( isInsideCombinedSymbol( testString, 2 ) ).to.be.true;
  94. expect( isInsideCombinedSymbol( testString, 3 ) ).to.be.true;
  95. expect( isInsideCombinedSymbol( testString, 4 ) ).to.be.true;
  96. } );
  97. it( 'should return false if given offset in a string is not inside a symbol with combining mark', () => {
  98. expect( isInsideCombinedSymbol( testString, 1 ) ).to.be.false;
  99. expect( isInsideCombinedSymbol( testString, 5 ) ).to.be.false;
  100. } );
  101. it( 'should return false if given offset in a string is at the string beginning or end', () => {
  102. expect( isInsideCombinedSymbol( testString, 0 ) ).to.be.false;
  103. expect( isInsideCombinedSymbol( testString, 6 ) ).to.be.false;
  104. } );
  105. } );
  106. } );
  107. } );