aboutsummaryrefslogtreecommitdiff
path: root/node_modules/fbjs/lib/UnicodeBidiDirection.js.flow
blob: 1301fedd8e45ef857b281d9dded3c7d65ac3396c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
 * Copyright (c) 2013-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @providesModule UnicodeBidiDirection
 * @typechecks
 * @flow
 */

/**
 * Constants to represent text directionality
 *
 * Also defines a *global* direciton, to be used in bidi algorithms as a
 * default fallback direciton, when no better direction is found or provided.
 *
 * NOTE: Use `setGlobalDir()`, or update `initGlobalDir()`, to set the initial
 *       global direction value based on the application.
 *
 * Part of the implementation of Unicode Bidirectional Algorithm (UBA)
 * Unicode Standard Annex #9 (UAX9)
 * http://www.unicode.org/reports/tr9/
 */

'use strict';

const invariant = require('./invariant');

export type BidiDirection = 'LTR' | 'RTL' | 'NEUTRAL';
export type HTMLDir = 'ltr' | 'rtl';

const NEUTRAL = 'NEUTRAL'; // No strong direction
const LTR = 'LTR'; // Left-to-Right direction
const RTL = 'RTL'; // Right-to-Left direction

let globalDir: ?BidiDirection = null;

// == Helpers ==

/**
 * Check if a directionality value is a Strong one
 */
function isStrong(dir: BidiDirection): boolean {
  return dir === LTR || dir === RTL;
}

/**
 * Get string value to be used for `dir` HTML attribute or `direction` CSS
 * property.
 */
function getHTMLDir(dir: BidiDirection): HTMLDir {
  invariant(isStrong(dir), '`dir` must be a strong direction to be converted to HTML Direction');
  return dir === LTR ? 'ltr' : 'rtl';
}

/**
 * Get string value to be used for `dir` HTML attribute or `direction` CSS
 * property, but returns null if `dir` has same value as `otherDir`.
 * `null`.
 */
function getHTMLDirIfDifferent(dir: BidiDirection, otherDir: BidiDirection): ?HTMLDir {
  invariant(isStrong(dir), '`dir` must be a strong direction to be converted to HTML Direction');
  invariant(isStrong(otherDir), '`otherDir` must be a strong direction to be converted to HTML Direction');
  return dir === otherDir ? null : getHTMLDir(dir);
}

// == Global Direction ==

/**
 * Set the global direction.
 */
function setGlobalDir(dir: BidiDirection): void {
  globalDir = dir;
}

/**
 * Initialize the global direction
 */
function initGlobalDir(): void {
  setGlobalDir(LTR);
}

/**
 * Get the global direction
 */
function getGlobalDir(): BidiDirection {
  if (!globalDir) {
    this.initGlobalDir();
  }
  invariant(globalDir, 'Global direction not set.');
  return globalDir;
}

const UnicodeBidiDirection = {
  // Values
  NEUTRAL,
  LTR,
  RTL,
  // Helpers
  isStrong,
  getHTMLDir,
  getHTMLDirIfDifferent,
  // Global Direction
  setGlobalDir,
  initGlobalDir,
  getGlobalDir
};

module.exports = UnicodeBidiDirection;