helpers.ts 2.5 KB

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