helpers.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // #<{(|
  2. // * Escapes `"` charachters from string
  3. // |)}>#
  4. // function escapeString(str: string): string {
  5. // return str.replace('"', '\"');
  6. // }
  7. //
  8. // #<{(|
  9. // * Determines if a value is an object
  10. // |)}>#
  11. // export function isObject(value: any): boolean {
  12. // var type = typeof value;
  13. // return !!value && (type === 'object');
  14. // }
  15. //
  16. // #<{(|
  17. // * Gets constructor name of an object.
  18. // * From http://stackoverflow.com/a/332429
  19. // *
  20. // |)}>#
  21. // export function getObjectName(object: Object): string {
  22. // if (object === undefined) {
  23. // return '';
  24. // }
  25. // if (object === null) {
  26. // return 'Object';
  27. // }
  28. // if (typeof object === 'object' && !object.constructor) {
  29. // return 'Object';
  30. // }
  31. //
  32. // const funcNameRegex = /function ([^(]*)/;
  33. // const results = (funcNameRegex).exec((object).constructor.toString());
  34. // if (results && results.length > 1) {
  35. // return results[1];
  36. // } else {
  37. // return '';
  38. // }
  39. // }
  40. //
  41. // #<{(|
  42. // * Gets type of an object. Returns "null" for null objects
  43. // |)}>#
  44. // export function getType(object: Object): string {
  45. // if (object === null) { return 'null'; }
  46. // return typeof object;
  47. // }
  48. //
  49. // #<{(|
  50. // * Generates inline preview for a JavaScript object based on a value
  51. // |)}>#
  52. // export function getValuePreview (object: Object, value: string): string {
  53. // var type = getType(object);
  54. //
  55. // if (type === 'null' || type === 'undefined') { return type; }
  56. //
  57. // if (type === 'string') {
  58. // value = '"' + escapeString(value) + '"';
  59. // }
  60. // if (type === 'function'){
  61. //
  62. // // Remove content of the function
  63. // return object.toString()
  64. // .replace(/[\r\n]/g, '')
  65. // .replace(/\{.*\}/, '') + '{…}';
  66. // }
  67. // return value;
  68. // }
  69. //
  70. // #<{(|
  71. // * Generates inline preview for a JavaScript object
  72. // |)}>#
  73. // export function getPreview(object: string): string {
  74. // let value = '';
  75. // if (isObject(object)) {
  76. // value = getObjectName(object);
  77. // if (Array.isArray(object)) {
  78. // value += '[' + object.length + ']';
  79. // }
  80. // } else {
  81. // value = getValuePreview(object, object);
  82. // }
  83. // return value;
  84. // }
  85. //
  86. // #<{(|
  87. // * Generates a prefixed CSS class name
  88. // |)}>#
  89. // export function cssClass(className: string): string {
  90. // return `json-formatter-${className}`;
  91. // }
  92. //
  93. // #<{(|
  94. // * Creates a new DOM element wiht given type and class
  95. // * TODO: move me to helpers
  96. // |)}>#
  97. // export function createElement(type: string, className?: string, content?: Element|string): Element {
  98. // const el = document.createElement(type);
  99. // if (className) {
  100. // el.classList.add(cssClass(className));
  101. // }
  102. // if (content !== undefined) {
  103. // if (content instanceof Node) {
  104. // el.appendChild(content);
  105. // } else {
  106. // el.appendChild(document.createTextNode(String(content)));
  107. // }
  108. // }
  109. // return el;
  110. // }