Have ideas to improve npm?Join in the discussion! »

    spiral-array

    1.0.9 • Public • Published

    Traverse a 2d array from the centre in a spiral

    (+ other useful functions)

    👀 Displaying the data

    const array = generateArray(3);
    printArrayTable(array)
    Traversing 9 cells
    ┌─────────┬───┬───┬───┐
    │ (index) │ 0 │ 1 │ 2 │
    ├─────────┼───┼───┼───┤
    │    0    │ 1 │ 2 │ 3 │
    │    1    │ 4 │ 5 │ 6 │
    │    2    │ 7 │ 8 │ 9 │
    └─────────┴───┴───┴───┘

    🎯 Finding the centre of an array

    const centre = locateCentre(array)
    centre { contents: 5, coordinates: [ 11 ] }

    🧭 Traversing the array

    const results = spiral(array);
    results {
      contents: [
        56321,
        4789
      ],
      coordinates: [
        [ 11 ], [ 12 ],
        [ 02 ], [ 01 ],
        [ 00 ], [ 10 ],
        [ 20 ], [ 21 ],
        [ 22 ]
      ]
    }

    🔍 Find the next cell

    const cell = nextCell(array, [0,2], 'l');
    cell {
      contents: [2],
      coordinates: [ [ 01 ] ]
    }

    💻 API

    type

    There are two types of response that can be returned from some functions.

    1. Contents of the cell
    2. Coordinates of the cell

    If type is not specified, both contents and coordinates will be returned.

    You can import these functions into your project:

    module.exports = {
        spiral: (array, type) => traverseArray(array, type),
        generateArray: (size) => generateArray(size),
        printArrayTable: (array) => console.table(array),
        locateCentre: (array, type) => locateCentre(array, type),
        nextCell: (array, currentCell, direction, type) => nextCell(array, currentCell, direction, type)
    };

    ℹ️ Help

    Node.js heap out of memory

    When dealing with very large arrays (around 1000 x 1000 or higher) more memory can be allocated to your node process. See: https://stackoverflow.com/questions/38558989/node-js-heap-out-of-memory

    Install

    npm i spiral-array

    DownloadsWeekly Downloads

    1

    Version

    1.0.9

    License

    ISC

    Unpacked Size

    5.94 kB

    Total Files

    3

    Last publish

    Collaborators

    • avatar