113 lines
3.1 KiB
JavaScript
113 lines
3.1 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.getSuggestedArrayLiteralLength = getSuggestedArrayLiteralLength;
|
|
exports.commonAncestorOf = commonAncestorOf;
|
|
exports.getOrDefault = getOrDefault;
|
|
|
|
var _invariant = require("../invariant.js");
|
|
|
|
var _invariant2 = _interopRequireDefault(_invariant);
|
|
|
|
var _index = require("../methods/index.js");
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
/**
|
|
* Get index property list length by searching array properties list for the max index key value plus 1.
|
|
*/
|
|
/**
|
|
* Copyright (c) 2017-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.
|
|
*/
|
|
|
|
function getSuggestedArrayLiteralLength(realm, val) {
|
|
(0, _invariant2.default)((0, _index.IsArray)(realm, val));
|
|
|
|
var length = 0;
|
|
var _iteratorNormalCompletion = true;
|
|
var _didIteratorError = false;
|
|
var _iteratorError = undefined;
|
|
|
|
try {
|
|
for (var _iterator = val.properties.keys()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
var key = _step.value;
|
|
|
|
if ((0, _index.IsArrayIndex)(realm, key) && Number(key) >= length) {
|
|
length = Number(key) + 1;
|
|
}
|
|
}
|
|
} catch (err) {
|
|
_didIteratorError = true;
|
|
_iteratorError = err;
|
|
} finally {
|
|
try {
|
|
if (!_iteratorNormalCompletion && _iterator.return) {
|
|
_iterator.return();
|
|
}
|
|
} finally {
|
|
if (_didIteratorError) {
|
|
throw _iteratorError;
|
|
}
|
|
}
|
|
}
|
|
|
|
return length;
|
|
}
|
|
|
|
function commonAncestorOf(node1, node2) {
|
|
if (node1 === node2) return node1;
|
|
// First get the path length to the root node for both nodes while also checking if
|
|
// either node is the parent of the other.
|
|
var n1 = node1,
|
|
n2 = node2,
|
|
count1 = 0,
|
|
count2 = 0;
|
|
while (true) {
|
|
var p1 = n1 && n1.getParent();
|
|
var p2 = n2 && n2.getParent();
|
|
if (p1 === node2) return node2;
|
|
if (p2 === node1) return node1;
|
|
if (p1) count1++;
|
|
if (p2) count2++;
|
|
if (!p1 && !p2) break;
|
|
n1 = p1;
|
|
n2 = p2;
|
|
}
|
|
// Now shorten the longest path to the same length as the shorter path
|
|
n1 = node1;
|
|
while (count1 > count2) {
|
|
(0, _invariant2.default)(n1 !== undefined);
|
|
n1 = n1.getParent();
|
|
count1--;
|
|
}
|
|
n2 = node2;
|
|
while (count1 < count2) {
|
|
(0, _invariant2.default)(n2 !== undefined);
|
|
n2 = n2.getParent();
|
|
count2--;
|
|
}
|
|
// Now run up both paths in tandem, stopping at the first common entry
|
|
while (n1 !== n2) {
|
|
(0, _invariant2.default)(n1 !== undefined);
|
|
n1 = n1.getParent();
|
|
(0, _invariant2.default)(n2 !== undefined);
|
|
n2 = n2.getParent();
|
|
}
|
|
return n1;
|
|
}
|
|
|
|
// Gets map[key] with default value provided by defaultFn
|
|
function getOrDefault(map, key, defaultFn) {
|
|
var value = map.get(key);
|
|
if (value === undefined) map.set(key, value = defaultFn());
|
|
(0, _invariant2.default)(value !== undefined);
|
|
return value;
|
|
}
|
|
//# sourceMappingURL=utils.js.map
|