util.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. const nameStartChar = ':A-Za-z_\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD';
  3. const nameChar = nameStartChar + '\\-.\\d\\u00B7\\u0300-\\u036F\\u203F-\\u2040';
  4. export const nameRegexp = '[' + nameStartChar + '][' + nameChar + ']*';
  5. const regexName = new RegExp('^' + nameRegexp + '$');
  6. export function getAllMatches(string, regex) {
  7. const matches = [];
  8. let match = regex.exec(string);
  9. while (match) {
  10. const allmatches = [];
  11. allmatches.startIndex = regex.lastIndex - match[0].length;
  12. const len = match.length;
  13. for (let index = 0; index < len; index++) {
  14. allmatches.push(match[index]);
  15. }
  16. matches.push(allmatches);
  17. match = regex.exec(string);
  18. }
  19. return matches;
  20. }
  21. export const isName = function(string) {
  22. const match = regexName.exec(string);
  23. return !(match === null || typeof match === 'undefined');
  24. }
  25. export function isExist(v) {
  26. return typeof v !== 'undefined';
  27. }
  28. export function isEmptyObject(obj) {
  29. return Object.keys(obj).length === 0;
  30. }
  31. /**
  32. * Copy all the properties of a into b.
  33. * @param {*} target
  34. * @param {*} a
  35. */
  36. export function merge(target, a, arrayMode) {
  37. if (a) {
  38. const keys = Object.keys(a); // will return an array of own properties
  39. const len = keys.length; //don't make it inline
  40. for (let i = 0; i < len; i++) {
  41. if (arrayMode === 'strict') {
  42. target[keys[i]] = [ a[keys[i]] ];
  43. } else {
  44. target[keys[i]] = a[keys[i]];
  45. }
  46. }
  47. }
  48. }
  49. /* exports.merge =function (b,a){
  50. return Object.assign(b,a);
  51. } */
  52. export function getValue(v) {
  53. if (exports.isExist(v)) {
  54. return v;
  55. } else {
  56. return '';
  57. }
  58. }
  59. // const fakeCall = function(a) {return a;};
  60. // const fakeCallNoReturn = function() {};