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/UnicodeBidiService.js.flow | |
parent | e0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff) |
node_modules
Diffstat (limited to 'node_modules/fbjs/lib/UnicodeBidiService.js.flow')
-rw-r--r-- | node_modules/fbjs/lib/UnicodeBidiService.js.flow | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/node_modules/fbjs/lib/UnicodeBidiService.js.flow b/node_modules/fbjs/lib/UnicodeBidiService.js.flow new file mode 100644 index 000000000..236a2f3dd --- /dev/null +++ b/node_modules/fbjs/lib/UnicodeBidiService.js.flow @@ -0,0 +1,97 @@ +/** + * 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 UnicodeBidiService + * @typechecks + * @flow + */ + +/** + * Stateful API for text direction detection + * + * This class can be used in applications where you need to detect the + * direction of a sequence of text blocks, where each direction shall be used + * as the fallback direction for the next one. + * + * NOTE: A default direction, if not provided, is set based on the global + * direction, as defined by `UnicodeBidiDirection`. + * + * == Example == + * ``` + * var UnicodeBidiService = require('UnicodeBidiService'); + * + * var bidiService = new UnicodeBidiService(); + * + * ... + * + * bidiService.reset(); + * for (var para in paragraphs) { + * var dir = bidiService.getDirection(para); + * ... + * } + * ``` + * + * Part of our implementation of Unicode Bidirectional Algorithm (UBA) + * Unicode Standard Annex #9 (UAX9) + * http://www.unicode.org/reports/tr9/ + */ + +'use strict'; + +const UnicodeBidi = require('./UnicodeBidi'); +const UnicodeBidiDirection = require('./UnicodeBidiDirection'); + +const invariant = require('./invariant'); + +import type { BidiDirection } from './UnicodeBidiDirection'; + +class UnicodeBidiService { + + _defaultDir: BidiDirection; + _lastDir: BidiDirection; + + /** + * Stateful class for paragraph direction detection + * + * @param defaultDir Default direction of the service + */ + constructor(defaultDir: ?BidiDirection) { + if (!defaultDir) { + defaultDir = UnicodeBidiDirection.getGlobalDir(); + } else { + invariant(UnicodeBidiDirection.isStrong(defaultDir), 'Default direction must be a strong direction (LTR or RTL)'); + } + this._defaultDir = defaultDir; + this.reset(); + } + + /** + * Reset the internal state + * + * Instead of creating a new instance, you can just reset() your instance + * everytime you start a new loop. + */ + reset(): void { + this._lastDir = this._defaultDir; + } + + /** + * Returns the direction of a block of text, and remembers it as the + * fall-back direction for the next paragraph. + * + * @param str A text block, e.g. paragraph, table cell, tag + * @return The resolved direction + */ + getDirection(str: string): BidiDirection { + this._lastDir = UnicodeBidi.getDirection(str, this._lastDir); + return this._lastDir; + } + +} + +module.exports = UnicodeBidiService;
\ No newline at end of file |