# trunk

### 说明

&#x20;将源数组根据指定大小`分片`

### 源码

<https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/array/trunk>

### 用法

{% code title="trunk.test.ts" %}

```typescript
import { trunk } from "../../utility/array/trunk";

describe('trunk', () => {
  test('trunk should return an empty array when receive an empty array', () => {
    const received: number[] = [];
    const expected = 0;

    const result = trunk<number>(received);

    expect(result.length).toBe(expected);
  });

  test('trunk should return the divided array by default size', () => {
    const received: number[] = [1, 2, 3, 4, 5, 6, 7];
    const expected = {
      length: 7,
      value: [[1], [2], [3], [4], [5], [6], [7]],
    };

    const result = trunk<number>(received);

    for (const [i, v] of result.entries()) {
      for (const [ii, vv] of v.entries()) {
        expect(vv).toBe(expected.value[i][ii]);
      }
      expect(v.length).toBe(expected.value[i].length);
    }
    expect(result.length).toBe(expected.length);
  });

  test('trunk should return the divided array by customized size', () => {
    const received: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    const expected = {
      s2: [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]],
      s5: [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]],
      s7: [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10]],
      s10: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]],
    };

    const p2 = trunk<number>(received, 2);
    const p5 = trunk<number>(received, 5);
    const p7 = trunk<number>(received, 7);
    const p10 = trunk<number>(received, 10);

    for (const [i, v] of p2.entries()) {
      for (const [ii, vv] of v.entries()) {
        expect(vv).toBe(expected.s2[i][ii]);
      }
      expect(v.length).toBe(expected.s2[i].length);
    }

    for (const [i, v] of p5.entries()) {
      for (const [ii, vv] of v.entries()) {
        expect(vv).toBe(expected.s5[i][ii]);
      }
      expect(v.length).toBe(expected.s5[i].length);
    }

    for (const [i, v] of p7.entries()) {
      for (const [ii, vv] of v.entries()) {
        expect(vv).toBe(expected.s7[i][ii]);
      }
      expect(v.length).toBe(expected.s7[i].length);
    }

    for (const [i, v] of p10.entries()) {
      for (const [ii, vv] of v.entries()) {
        expect(vv).toBe(expected.s10[i][ii]);
      }
      expect(v.length).toBe(expected.s10[i].length);
    }
  });
});
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ddzy.gitbook.io/ts-utility-plugins-docs/utility/utility-array/trunk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
