utils.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 typeof value === 'function' && toString.call(value) === '[object Function]';
  37. };
  38. }
  39. export {isFunction};
  40. /* eslint-enable func-style */
  41. /* istanbul ignore next */
  42. export const isArray = Array.isArray || function(value) {
  43. return (value && typeof value === 'object') ? toString.call(value) === '[object Array]' : false;
  44. };
  45. // Older IE versions do not directly support indexOf so we must implement our own, sadly.
  46. export function indexOf(array, value) {
  47. for (let i = 0, len = array.length; i < len; i++) {
  48. if (array[i] === value) {
  49. return i;
  50. }
  51. }
  52. return -1;
  53. }
  54. export function escapeExpression(string) {
  55. if (typeof string !== 'string') {
  56. // don't escape SafeStrings, since they're already safe
  57. if (string && string.toHTML) {
  58. return string.toHTML();
  59. } else if (string == null) {
  60. return '';
  61. } else if (!string) {
  62. return string + '';
  63. }
  64. // Force a string conversion as this will be done by the append regardless and
  65. // the regex test will do this transparently behind the scenes, causing issues if
  66. // an object's to string has escaped characters in it.
  67. string = '' + string;
  68. }
  69. if (!possible.test(string)) { return string; }
  70. return string.replace(badChars, escapeChar);
  71. }
  72. export function isEmpty(value) {
  73. if (!value && value !== 0) {
  74. return true;
  75. } else if (isArray(value) && value.length === 0) {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. }
  81. export function createFrame(object) {
  82. let frame = extend({}, object);
  83. frame._parent = object;
  84. return frame;
  85. }
  86. export function blockParams(params, ids) {
  87. params.path = ids;
  88. return params;
  89. }
  90. export function appendContextPath(contextPath, id) {
  91. return (contextPath ? contextPath + '.' : '') + id;
  92. }