UNPKG

973 BJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
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 * @format
8 *
9 * @emails oncall+draft_js
10 */
11'use strict';
12
13var UnicodeUtils = require("fbjs/lib/UnicodeUtils");
14
15var substr = UnicodeUtils.substr;
16/**
17 * Convert to native JavaScript string lengths to determine ranges.
18 */
19
20function decodeEntityRanges(text, ranges) {
21 var entities = Array(text.length).fill(null);
22
23 if (ranges) {
24 ranges.forEach(function (range) {
25 // Using Unicode-enabled substrings converted to JavaScript lengths,
26 // fill the output array with entity keys.
27 var start = substr(text, 0, range.offset).length;
28 var end = start + substr(text, range.offset, range.length).length;
29
30 for (var ii = start; ii < end; ii++) {
31 entities[ii] = range.key;
32 }
33 });
34 }
35
36 return entities;
37}
38
39module.exports = decodeEntityRanges;
\No newline at end of file