-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show object summary everywhere
- Loading branch information
Showing
5 changed files
with
122 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { getText, IS_IMMUTABLE_KEY } from "./utils"; | ||
import React from "react"; | ||
import { base16Theme } from "./base16theme"; | ||
|
||
export const getItemString = ( | ||
type: string, | ||
data: any, | ||
dataTypeKey: string | symbol | undefined, | ||
isWideLayout: boolean, | ||
isDiff?: boolean | ||
) => ( | ||
<span | ||
style={{ | ||
color: base16Theme.base04, | ||
}} | ||
> | ||
{data[IS_IMMUTABLE_KEY] ? "Immutable" : ""} | ||
{dataTypeKey && data[dataTypeKey] ? `${data[dataTypeKey] as string} ` : ""} | ||
{getText(type, data, isWideLayout, isDiff)} | ||
</span> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { isCollection, isIndexed, isKeyed } from "immutable"; | ||
|
||
export const IS_IMMUTABLE_KEY = "@@__IS_IMMUTABLE__@@"; | ||
|
||
function isIterable(obj: any) { | ||
return ( | ||
obj !== null && | ||
typeof obj === "object" && | ||
!Array.isArray(obj) && | ||
typeof obj[window.Symbol.iterator] === "function" | ||
); | ||
} | ||
|
||
function isImmutable(value: any) { | ||
return isKeyed(value) || isIndexed(value) || isCollection(value); | ||
} | ||
|
||
function getShortTypeString(val: any, diff: boolean | undefined) { | ||
if (diff && Array.isArray(val)) { | ||
val = val[val.length === 2 ? 1 : 0]; | ||
} | ||
|
||
if (isIterable(val) && !isImmutable(val)) { | ||
return "(…)"; | ||
} else if (Array.isArray(val)) { | ||
return val.length > 0 ? "[…]" : "[]"; | ||
} else if (val === null) { | ||
return "null"; | ||
} else if (val === undefined) { | ||
return "undef"; | ||
} else if (typeof val === "object") { | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
return Object.keys(val as {}).length > 0 ? "{…}" : "{}"; | ||
} else if (typeof val === "function") { | ||
return "fn"; | ||
} else if (typeof val === "string") { | ||
return `"${val.substr(0, 10) + (val.length > 10 ? "…" : "")}"`; | ||
} else if (typeof val === "symbol") { | ||
return "symbol"; | ||
} else { | ||
return val; | ||
} | ||
} | ||
|
||
export function getText( | ||
type: string, | ||
data: any, | ||
isWideLayout: boolean, | ||
isDiff: boolean | undefined | ||
) { | ||
if (type === "Object") { | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
const keys = Object.keys(data as {}); | ||
if (!isWideLayout) return keys.length ? "{…}" : "{}"; | ||
|
||
const str = keys | ||
.slice(0, 3) | ||
.map( | ||
(key) => `${key}: ${getShortTypeString(data[key], isDiff) as string}` | ||
) | ||
.concat(keys.length > 3 ? ["…"] : []) | ||
.join(", "); | ||
|
||
return `{ ${str} }`; | ||
} else if (type === "Array") { | ||
if (!isWideLayout) return data.length ? "[…]" : "[]"; | ||
|
||
const str = data | ||
.slice(0, 4) | ||
.map((val: any) => getShortTypeString(val, isDiff)) | ||
.concat(data.length > 4 ? ["…"] : []) | ||
.join(", "); | ||
|
||
return `[${str as string}]`; | ||
} else { | ||
return type; | ||
} | ||
} |