usage.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var hasOwnProperty = Object.prototype.hasOwnProperty;
  2. function buildMap(list, caseInsensitive) {
  3. var map = Object.create(null);
  4. if (!Array.isArray(list)) {
  5. return null;
  6. }
  7. for (var i = 0; i < list.length; i++) {
  8. var name = list[i];
  9. if (caseInsensitive) {
  10. name = name.toLowerCase();
  11. }
  12. map[name] = true;
  13. }
  14. return map;
  15. }
  16. function buildList(data) {
  17. if (!data) {
  18. return null;
  19. }
  20. var tags = buildMap(data.tags, true);
  21. var ids = buildMap(data.ids);
  22. var classes = buildMap(data.classes);
  23. if (tags === null &&
  24. ids === null &&
  25. classes === null) {
  26. return null;
  27. }
  28. return {
  29. tags: tags,
  30. ids: ids,
  31. classes: classes
  32. };
  33. }
  34. function buildIndex(data) {
  35. var scopes = false;
  36. if (data.scopes && Array.isArray(data.scopes)) {
  37. scopes = Object.create(null);
  38. for (var i = 0; i < data.scopes.length; i++) {
  39. var list = data.scopes[i];
  40. if (!list || !Array.isArray(list)) {
  41. throw new Error('Wrong usage format');
  42. }
  43. for (var j = 0; j < list.length; j++) {
  44. var name = list[j];
  45. if (hasOwnProperty.call(scopes, name)) {
  46. throw new Error('Class can\'t be used for several scopes: ' + name);
  47. }
  48. scopes[name] = i + 1;
  49. }
  50. }
  51. }
  52. return {
  53. whitelist: buildList(data),
  54. blacklist: buildList(data.blacklist),
  55. scopes: scopes
  56. };
  57. }
  58. module.exports = {
  59. buildIndex: buildIndex
  60. };