项目作者: luizbills

项目描述 :
Simple Grid data structure
高级语言: JavaScript
项目地址: git://github.com/luizbills/grid.git
创建时间: 2014-02-22T20:59:46Z
项目社区:https://github.com/luizbills/grid

开源协议:MIT License

下载


GRID

Simple grid data structure.


Install

  1. npm install grid-data

Usage

  1. // create a empty 2x2 grid
  2. const g = new Grid(2, 2)
  3. // set values
  4. g.set(0, 0, "first cell") // zero-based index
  5. g.set(1, 1, "last cell")
  6. // get values
  7. console.log(g.get(0, 0)) // => "first cell"
  8. console.log(g.get(1, 1)) // => "last cell"
  9. // check a position
  10. console.log(g.has(0, 1)) // => false
  11. // create a 2x2 grid from a Array
  12. const ga = Grid.fromArray(2, 2, [1, 2, 3, 4])
  13. console.log(ga.get(0, 0)) // => 1
  14. console.log(ga.get(0, 1)) // => 2
  15. console.log(ga.get(1, 0)) // => 3
  16. console.log(ga.get(1, 1)) // => 4
  17. // export the grid as Array
  18. console.log(ga.toArray()); // => [1,2,3,4]
  19. // export the grid as String
  20. console.log(ga.toString()); // => 1,2,3,4
  21. // use forEach to interact
  22. ga.forEach(function (value, x, y) {
  23. // do something...
  24. })
  25. // sanitizes a X position with .clipX (or use .clipY for Y positions)
  26. console.log(ga.clipX(5)) // => 1
  27. console.log(ga.clipY(-1)) // => 0
  28. // clone a grid
  29. const cloned = ga.clone()
  30. // clear all stored values
  31. ga.clear()
  32. // public properties
  33. g.width
  34. g.height
  35. g.length // === g.width * g.height

License

MIT