diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
commit | de98e0b232509d5f40c135d540a70e415272ff85 (patch) | |
tree | a79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/fbjs/lib/UnicodeBidiDirection.js.flow | |
parent | e0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff) |
node_modules
Diffstat (limited to 'node_modules/fbjs/lib/UnicodeBidiDirection.js.flow')
-rw-r--r-- | node_modules/fbjs/lib/UnicodeBidiDirection.js.flow | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/node_modules/fbjs/lib/UnicodeBidiDirection.js.flow b/node_modules/fbjs/lib/UnicodeBidiDirection.js.flow new file mode 100644 index 000000000..7d3964b95 --- /dev/null +++ b/node_modules/fbjs/lib/UnicodeBidiDirection.js.flow @@ -0,0 +1,112 @@ +/** + * Copyright (c) 2013-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @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;
\ No newline at end of file |