123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- function escapeString(str: string): string {
- return str.replace('"', '\"');
- }
- export function isObject(value: any): boolean {
- var type = typeof value;
- return !!value && (type == 'object');
- }
- export function getObjectName(object: Object):string {
- if (object === undefined) {
- return '';
- }
- if (object === null) {
- return 'Object';
- }
- if (typeof object === 'object' && !object.constructor) {
- return 'Object';
- }
- const funcNameRegex = /function ([^(]*)/;
- const results = (funcNameRegex).exec((object).constructor.toString());
- if (results && results.length > 1) {
- return results[1];
- } else {
- return '';
- }
- }
- export function getType(object: Object): string {
- if (object === null) { return 'null'; }
- return typeof object;
- }
- export function getValuePreview (object: Object, value: string): string {
- var type = getType(object);
- if (type === 'null' || type === 'undefined') { return type; }
- if (type === 'string') {
- value = '"' + escapeString(value) + '"';
- }
- if (type === 'function'){
-
- return object.toString()
- .replace(/[\r\n]/g, '')
- .replace(/\{.*\}/, '') + '{…}';
- }
- return value;
- }
- export function getPreview(object: string): string {
- let value = '';
- if (isObject(object)) {
- value = getObjectName(object);
- if (Array.isArray(object))
- value += '[' + object.length + ']';
- } else {
- value = getValuePreview(object, object);
- }
- return value;
- }
- export function cssClass(className:string): string {
- return `json-formatter-${className}`;
- }
- export function createElement(type: string, className?: string, content?: Element|string): Element {
- const el = document.createElement(type);
- if (className) {
- el.classList.add(cssClass(className));
- }
- if (content !== undefined) {
- if (content instanceof Node) {
- el.appendChild(content);
- } else {
- el.appendChild(document.createTextNode(String(content)));
- }
- }
- return el;
- }
|