Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

173 lignes
7.0 KiB

  1. // ArrayList
  2. const { clone } = require('../core/util');
  3. // Create a new ArrayList
  4. exports.create = function(options, name) {
  5. const value = this.parseOptional(options.value, 'object', []);
  6. if (!name) throw Error('arraylist.create: name is required.');
  7. if (!this.req.arrays) {
  8. this.req.arrays = {};
  9. }
  10. this.req.arrays[name] = Array.isArray(value) ? clone(value) : [];
  11. };
  12. // Return the ArrayList as array value
  13. exports.value = function(options) {
  14. const ref = this.parseRequired(options.ref, 'string', 'arraylist.value: ref is required.');
  15. if (!this.req.arrays) throw Error('arraylist.value: No ArrayList are created.');
  16. if (!this.req.arrays[ref]) throw Error(`arraylist.value: ArrayList ${ref} not found.`);
  17. return clone(this.req.arrays[ref]);
  18. };
  19. // Return the size of the ArrayList
  20. exports.size = function(options) {
  21. const ref = this.parseRequired(options.ref, 'string', 'arraylist.size: ref is required.');
  22. if (!this.req.arrays) throw Error('arraylist.size: No arraylists are created.');
  23. if (!this.req.arrays[ref]) throw Error(`arraylist.size: ArrayList ${ref} not found.`);
  24. return this.req.arrays[ref].length;
  25. };
  26. // Return the value at a specific index
  27. exports.get = function(options) {
  28. const ref = this.parseRequired(options.ref, 'string', 'arraylist.get: ref is required.');
  29. const index = this.parseRequired(options.index, 'number', 'arraylist.get: index is required.');
  30. if (!this.req.arrays) throw Error('arraylist.get: No ArrayList are created.');
  31. if (!this.req.arrays[ref]) throw Error(`arraylist.get: ArrayList ${ref} not found.`);
  32. return clone(this.req.arrays[ref][index]);
  33. };
  34. // Add a value to the ArrayList
  35. exports.add = function(options) {
  36. const ref = this.parseRequired(options.ref, 'string', 'arraylist.add: ref is required.');
  37. const value = this.parseRequired(options.value, '*', 'arraylist.add: value is required.');
  38. if (!this.req.arrays) throw Error('arraylist.add: No ArrayList are created.');
  39. if (!this.req.arrays[ref]) throw Error(`arraylist.add: ArrayList ${ref} not found.`);
  40. this.req.arrays[ref].push(clone(value));
  41. };
  42. // Add an array of values to the ArrayList
  43. exports.addAll = function(options) {
  44. const ref = this.parseRequired(options.ref, 'string', 'arraylist.addAll: ref is required.');
  45. const value = this.parseRequired(options.value, 'object', 'arraylist.addAll: value is required.');
  46. if (!this.req.arrays) throw Error('arraylist.addAll: No ArrayList are created.');
  47. if (!this.req.arrays[ref]) throw Error(`arraylist.addAll: ArrayList ${ref} not found.`);
  48. if (!Array.isArray(value)) throw Error('arraylist.addAll: Value must be an array.')
  49. for (let val of value) {
  50. this.req.arrays[ref].push(clone(val));
  51. }
  52. };
  53. // Update a value at a specific index
  54. exports.set = function(options) {
  55. const ref = this.parseRequired(options.ref, 'string', 'arraylist.set: ref is required.');
  56. const index = this.parseRequired(options.index, 'number', 'arraylist.set: index is required.');
  57. const value = this.parseRequired(options.value, '*', 'arraylist.set: value is required.');
  58. if (!this.req.arrays) throw Error('arraylist.set: No ArrayList are created.');
  59. if (!this.req.arrays[ref]) throw Error(`arraylist.set: ArrayList ${ref} not found.`);
  60. this.req.arrays[ref][index] = clone(value);
  61. };
  62. // Remove the first occurrence of a specific value
  63. exports.remove = function(options) {
  64. const ref = this.parseRequired(options.ref, 'string', 'arraylist.remove: ref is required.');
  65. const value = this.parseRequired(options.value, '*', 'arraylist.remove: value is required.');
  66. if (!this.req.arrays) throw Error('arraylist.remove: No ArrayList are created.');
  67. if (!this.req.arrays[ref]) throw Error(`arraylist.remove: ArrayList ${ref} not found.`);
  68. const index = this.req.arrays[ref].indexOf(value);
  69. if (index == -1) throw Error('arraylist.remove: Value does not exist in the ArrayList.');
  70. this.req.arrays[ref].splice(index, 1);
  71. };
  72. // Remove a value from the ArrayList at a specific index
  73. exports.removeAt = function(options) {
  74. const ref = this.parseRequired(options.ref, 'string', 'arraylist.removeAt: ref is required.');
  75. const index = this.parseRequired(options.index, 'number', 'arraylist.removeAt: index is required.');
  76. if (!this.req.arrays) throw Error('arraylist.removeAt: No ArrayList are created.');
  77. if (!this.req.arrays[ref]) throw Error(`arraylist.removeAt: ArrayList ${ref} not found.`);
  78. this.req.arrays[ref].splice(index, 1);
  79. };
  80. // Clear all values from the ArrayList
  81. exports.clear = function(options) {
  82. const ref = this.parseRequired(options.ref, 'string', 'arraylist.clear: ref is required.');
  83. if (!this.req.arrays) throw Error('arraylist.clear: No ArrayList are created.');
  84. if (!this.req.arrays[ref]) throw Error(`arraylist.clear: ArrayList ${ref} not found.`);
  85. this.req.arrays[ref] = [];
  86. };
  87. // Sort the ArrayList
  88. exports.sort = function(options) {
  89. const ref = this.parseRequired(options.ref, 'string', 'arraylist.sort: ref is required.');
  90. const prop = this.parseOptional(options.prop, 'string', null);
  91. const desc = this.parseOptional(options.desc, 'boolean', false);
  92. if (!this.req.arrays) throw Error('arraylist.sort: No ArrayList are created.');
  93. if (!this.req.arrays[ref]) throw Error(`arraylist.sort: ArrayList ${ref} not found.`);
  94. this.req.arrays[ref].sort((a, b) => {
  95. if (prop) {
  96. a = a[prop];
  97. b = b[prop];
  98. }
  99. if (a == b) return 0;
  100. if (a < b) return desc ? 1 : -1;
  101. return desc ? -1 : 1;
  102. });
  103. };
  104. // Return the index of the first occurrence of a specific value
  105. exports.indexOf = function(options) {
  106. const ref = this.parseRequired(options.ref, 'string', 'arraylist.indexOf: ref is required.');
  107. const value = this.parseRequired(options.value, '*', 'arraylist.indexOf: value is required.');
  108. if (!this.req.arrays) throw Error('arraylist.indexOf: No ArrayList are created.');
  109. if (!this.req.arrays[ref]) throw Error(`arraylist.indexOf: ArrayList ${ref} not found.`);
  110. return this.req.arrays[ref].indexOf(value);
  111. };
  112. // Return true if the ArrayList contains a specific value
  113. exports.contains = function(options) {
  114. const ref = this.parseRequired(options.ref, 'string', 'arraylist.contains: ref is required.');
  115. const value = this.parseRequired(options.value, '*', 'arraylist.contains: value is required.');
  116. if (!this.req.arrays) throw Error('arraylist.contains: No ArrayList are created.');
  117. if (!this.req.arrays[ref]) throw Error(`arraylist.contains: ArrayList ${ref} not found.`);
  118. return this.req.arrays[ref].includes(value);
  119. };
  120. // Return true when ArrayList is empty
  121. exports.isEmpty = function(options) {
  122. const ref = this.parseRequired(options.ref, 'string', 'arraylist.isEmpty: ref is required.');
  123. if (!this.req.arrays) throw Error('arraylist.isEmpty: No ArrayList are created.');
  124. if (!this.req.arrays[ref]) throw Error(`arraylist.isEmpty: ArrayList ${ref} not found.`);
  125. return !this.req.arrays[ref].length;
  126. };