@samitouri / QOS-React-2 / commits / 580e2f56d5

Add isArray in devTools utils (#22438)

Behnam Mohammadi committed Sep 28, 2021 at 00:47 UTC 580e2f56d54b6f7868a98d6bd5ce07bfaa3cdbe2
2 files changed +17 -4
packages/react-devtools-shared/src/isArray.js new
+12
@@ -0,0 +1,12 @@
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 + * @flow
8 + */
9 +
10 +const isArray = Array.isArray;
11 +
12 +export default isArray;
packages/react-devtools-shared/src/utils.js
+5 -4
@@ -48,6 +48,7 @@ import {
48 } from 'react-devtools-shared/src/types';
49 import {localStorageGetItem, localStorageSetItem} from './storage';
50 import {meta} from './hydration';
51 +import isArray from './isArray';
52
53 import type {ComponentFilter, ElementType} from './types';
54 import type {LRUCache} from 'react-devtools-shared/src/types';
@@ -475,7 +476,7 @@ export function deletePathInObject(
476 if (object != null) {
477 const parent = getInObject(object, path.slice(0, length - 1));
478 if (parent) {
478 - if (Array.isArray(parent)) {
479 + if (isArray(parent)) {
480 parent.splice(((last: any): number), 1);
481 } else {
482 delete parent[last];
@@ -496,7 +497,7 @@ export function renamePathInObject(
497 const lastOld = oldPath[length - 1];
498 const lastNew = newPath[length - 1];
499 parent[lastNew] = parent[lastOld];
499 - if (Array.isArray(parent)) {
500 + if (isArray(parent)) {
501 parent.splice(((lastOld: any): number), 1);
502 } else {
503 delete parent[lastOld];
@@ -580,7 +581,7 @@ export function getDataType(data: Object): DataType {
581 return 'number';
582 }
583 case 'object':
583 - if (Array.isArray(data)) {
584 + if (isArray(data)) {
585 return 'array';
586 } else if (ArrayBuffer.isView(data)) {
587 return hasOwnProperty.call(data.constructor, 'BYTES_PER_ELEMENT')
@@ -799,7 +800,7 @@ export function formatDataForPreview(
800 // To mimic their behavior, detect if we've been given an entries tuple.
801 // Map(2) {"abc" => 123, "def" => 123}
802 // Set(2) {"abc", 123}
802 - if (Array.isArray(entryOrEntries)) {
803 + if (isArray(entryOrEntries)) {
804 const key = formatDataForPreview(entryOrEntries[0], true);
805 const value = formatDataForPreview(entryOrEntries[1], false);
806 formatted += `${key} => ${value}`;