utils.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const escape = {
  2. '&': '&',
  3. '<': '&lt;',
  4. '>': '&gt;',
  5. '"': '&quot;',
  6. "'": '&#x27;',
  7. '`': '&#x60;',
  8. '=': '&#x3D;'
  9. };
  10. const badChars = /[&<>"'`=]/g,
  11. possible = /[&<>"'`=]/;
  12. function escapeChar(chr) {
  13. return escape[chr];
  14. }
  15. export function extend(obj /* , ...source */) {
  16. for (let i = 1; i < arguments.length; i++) {
  17. for (let key in arguments[i]) {
  18. if (Object.prototype.hasOwnProperty.call(arguments[i], key)) {
  19. obj[key] = arguments[i][key];
  20. }
  21. }
  22. }
  23. return obj;
  24. }
  25. export let toString = Object.prototype.toString;
  26. // Sourced from lodash
  27. // https://github.com/bestiejs/lodash/blob/master/LICENSE.txt
  28. /* eslint-disable func-style */
  29. let isFunction = function(value) {
  30. return typeof value === 'function';
  31. };
  32. // fallback for older versions of Chrome and Safari
  33. /* istanbul ignore next */
  34. if (isFunction(/x/)) {
  35. isFunction = function(value) {
  36. return (
  37. typeof value === 'function' &&
  38. toString.call(value) === '[object Function]'
  39. );
  40. };
  41. }
  42. export { isFunction };
  43. /* eslint-enable func-style */
  44. /* istanbul ignore next */
  45. export const isArray =
  46. Array.isArray ||
  47. function(value) {
  48. return value && typeof value === 'object'
  49. ? toString.call(value) === '[object Array]'
  50. : false;
  51. };
  52. // Older IE versions do not directly support indexOf so we must implement our own, sadly.
  53. export function indexOf(array, value) {
  54. for (let i = 0, len = array.length; i < len; i++) {
  55. if (array[i] === value) {
  56. return i;
  57. }
  58. }
  59. return -1;
  60. }
  61. export function escapeExpression(string) {
  62. if (typeof string !== 'string') {
  63. // don't escape SafeStrings, since they're already safe
  64. if (string && string.toHTML) {
  65. return string.toHTML();
  66. } else if (string == null) {
  67. return '';
  68. } else if (!string) {
  69. return string + '';
  70. }
  71. // Force a string conversion as this will be done by the append regardless and
  72. // the regex test will do this transparently behind the scenes, causing issues if
  73. // an object's to string has escaped characters in it.
  74. string = '' + string;
  75. }
  76. if (!possible.test(string)) {
  77. return string;
  78. }
  79. return string.replace(badChars, escapeChar);
  80. }
  81. export function isEmpty(value) {
  82. if (!value && value !== 0) {
  83. return true;
  84. } else if (isArray(value) && value.length === 0) {
  85. return true;
  86. } else {
  87. return false;
  88. }
  89. }
  90. export function createFrame(object) {
  91. let frame = extend({}, object);
  92. frame._parent = object;
  93. return frame;
  94. }
  95. export function blockParams(params, ids) {
  96. params.path = ids;
  97. return params;
  98. }
  99. export function appendContextPath(contextPath, id) {
  100. return (contextPath ? contextPath + '.' : '') + id;
  101. }