UnicodeBidiDirection.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @typechecks
  8. *
  9. */
  10. /**
  11. * Constants to represent text directionality
  12. *
  13. * Also defines a *global* direciton, to be used in bidi algorithms as a
  14. * default fallback direciton, when no better direction is found or provided.
  15. *
  16. * NOTE: Use `setGlobalDir()`, or update `initGlobalDir()`, to set the initial
  17. * global direction value based on the application.
  18. *
  19. * Part of the implementation of Unicode Bidirectional Algorithm (UBA)
  20. * Unicode Standard Annex #9 (UAX9)
  21. * http://www.unicode.org/reports/tr9/
  22. */
  23. 'use strict';
  24. var invariant = require('./invariant');
  25. var NEUTRAL = 'NEUTRAL'; // No strong direction
  26. var LTR = 'LTR'; // Left-to-Right direction
  27. var RTL = 'RTL'; // Right-to-Left direction
  28. var globalDir = null;
  29. // == Helpers ==
  30. /**
  31. * Check if a directionality value is a Strong one
  32. */
  33. function isStrong(dir) {
  34. return dir === LTR || dir === RTL;
  35. }
  36. /**
  37. * Get string value to be used for `dir` HTML attribute or `direction` CSS
  38. * property.
  39. */
  40. function getHTMLDir(dir) {
  41. !isStrong(dir) ? process.env.NODE_ENV !== 'production' ? invariant(false, '`dir` must be a strong direction to be converted to HTML Direction') : invariant(false) : void 0;
  42. return dir === LTR ? 'ltr' : 'rtl';
  43. }
  44. /**
  45. * Get string value to be used for `dir` HTML attribute or `direction` CSS
  46. * property, but returns null if `dir` has same value as `otherDir`.
  47. * `null`.
  48. */
  49. function getHTMLDirIfDifferent(dir, otherDir) {
  50. !isStrong(dir) ? process.env.NODE_ENV !== 'production' ? invariant(false, '`dir` must be a strong direction to be converted to HTML Direction') : invariant(false) : void 0;
  51. !isStrong(otherDir) ? process.env.NODE_ENV !== 'production' ? invariant(false, '`otherDir` must be a strong direction to be converted to HTML Direction') : invariant(false) : void 0;
  52. return dir === otherDir ? null : getHTMLDir(dir);
  53. }
  54. // == Global Direction ==
  55. /**
  56. * Set the global direction.
  57. */
  58. function setGlobalDir(dir) {
  59. globalDir = dir;
  60. }
  61. /**
  62. * Initialize the global direction
  63. */
  64. function initGlobalDir() {
  65. setGlobalDir(LTR);
  66. }
  67. /**
  68. * Get the global direction
  69. */
  70. function getGlobalDir() {
  71. if (!globalDir) {
  72. this.initGlobalDir();
  73. }
  74. !globalDir ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Global direction not set.') : invariant(false) : void 0;
  75. return globalDir;
  76. }
  77. var UnicodeBidiDirection = {
  78. // Values
  79. NEUTRAL: NEUTRAL,
  80. LTR: LTR,
  81. RTL: RTL,
  82. // Helpers
  83. isStrong: isStrong,
  84. getHTMLDir: getHTMLDir,
  85. getHTMLDirIfDifferent: getHTMLDirIfDifferent,
  86. // Global Direction
  87. setGlobalDir: setGlobalDir,
  88. initGlobalDir: initGlobalDir,
  89. getGlobalDir: getGlobalDir
  90. };
  91. module.exports = UnicodeBidiDirection;