项目作者: slammayjammay

项目描述 :
Combines or cancels adjacent elements of an array
高级语言: JavaScript
项目地址: git://github.com/slammayjammay/array-element-combiner.git
创建时间: 2017-07-20T01:46:38Z
项目社区:https://github.com/slammayjammay/array-element-combiner

开源协议:MIT License

下载


array-element-combiner

Traverses an array, comparing every two adjacent elements. Takes a compare callback, which determines whether to combine elements; and a combine callback, which determines the value of the combination of elements. In the returned array, the combined value will be stored and the two original elements will be deleted.

A possible use case might include an array holding a set of ordered instructions that can be shortened in the case of duplicative or opposite instructions. See the example.

Usage

  1. $ npm install --save array-element-combiner
  1. const combiner = require('array-element-combiner');
  2. let array = [1, 2, 3, 4, 5];
  3. const options = {
  4. cancel(value) { /* ... */ },
  5. compare(a, b) { /* ... */ },
  6. combine(a, b,) { /* ... */ },
  7. ignore(a, b) { /* ... */ }
  8. };
  9. array = combiner(array, options); // returns new array
  10. console.log(array);

Example" class="reference-link">Example

Let’s say you have a set of directions that include goForward, turnRight, turnLeft, and turnAround. You also have an array that has a randomized set of directions. The array can possibly be shortened, because some elements can cancel out (turnRight and turnLeft), while others can be combined (two turnRight‘s).

  1. const directions = [
  2. 'goForward',
  3. 'turnRight',
  4. 'goForward',
  5. 'turnLeft',
  6. 'turnLeft',
  7. 'turnAround',
  8. 'goForward'
  9. ];

Now we need to provide logic for how the directions will be combined or cancelled. For example, two turnRights combine to make a turnAround; a turnAround and turnRight make turnLeft; a turnLeft and turnRight cancel out, etc.

  1. const options = {
  2. compare(a, b) {
  3. // can be combined if they both include a 'turn'
  4. return a.includes('turn') && b.includes('turn');
  5. },
  6. combine(a, b) {
  7. // we know that a and b both include 'turn' because they passed the
  8. // `compare` callback
  9. const aDir = a.includes('right') ? 1 : a.includes('left') ? -1 : 2;
  10. const bDir = b.includes('right') ? 1 : b.includes('left') ? -1 : 2;
  11. let totalDir = aDir + bDir;
  12. // Two `turnAround`s or a `turnLeft` and `turnRight` will cancel out. Without
  13. // providing a cancel callback, an empty string will be inserted into the
  14. // returned array.
  15. if (totalDir === 0 || totalDir === 4) {
  16. return '';
  17. }
  18. // A `turnAround` and a `turnRight` make a left.
  19. if (totalDir === 3) {
  20. totalDir = -1;
  21. }
  22. const dirString = totalDir === 1 ? 'Right' : totalDir === 2 ? 'Around' : 'Left'
  23. return `turn${dirString}`;
  24. },
  25. cancel(value) {
  26. return value === '';
  27. }
  28. };

Output:

  1. /* ... */
  2. const output = combiner(directions, options);
  3. console.log(output);
  4. // [
  5. // 'goForward',
  6. // 'turnRight',
  7. // 'goForward',
  8. // 'goForward'
  9. // ]