util.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. const nameRegexp = '[' + nameStartChar + '][' + nameChar + ']*'
  5. const regexName = new RegExp('^' + nameRegexp + '$');
  6. const getAllMatches = function(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. const isName = function(string) {
  22. const match = regexName.exec(string);
  23. return !(match === null || typeof match === 'undefined');
  24. };
  25. exports.isExist = function(v) {
  26. return typeof v !== 'undefined';
  27. };
  28. exports.isEmptyObject = function(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. exports.merge = function(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. exports.getValue = function(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() {};
  61. exports.isName = isName;
  62. exports.getAllMatches = getAllMatches;
  63. exports.nameRegexp = nameRegexp;