Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ooflorent committed Jan 2, 2015
0 parents commit 3a4e39f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.js]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
indent_size = 4
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Florent Cailhol

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# speedr-array2

> Two-dimensional array
## Install

```sh
$ npm install --save speedr-array2
```

## Usage

```js
import {Array2} from 'speedr-array2'

const Tiles = {
SAND: 0,
TREE: 1,
WALL: 2
}

// Constructors
let map1 = new Array2(10, 10)
let map2 = new Array2(80, 50, Tiles.SAND)
let map3 = new Array2(40, 40, (x, y) => x * y)

// Access
map3.get(4, 9) // 36
map3.set(4, 9, 12)
```

## License

MIT © Florent Cailhol
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "speedr-array2",
"version": "1.0.0",
"description": "Two-dimensional array",
"author": "Florent Cailhol <[email protected]>",
"repository": "speedr/array2",
"keywords": [
"speedr",
"array",
"array2"
],
"main": "lib/index.js",
"files": [
"lib"
],
"scripts": {
"build": "6to5 --source-maps-inline --out-dir lib src"
},
"devDependencies": {
"6to5": "^2.2.0"
},
"license": "MIT"
}
25 changes: 25 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export class Array2 {
constructor(w, h, initValue) {
this.w = w
this.h = h
this.e = new Array(w * h)

let fn = typeof initValue == 'function' ? initValue : () => initValue

for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
this.set(x, y, fn(x, y))
}
}
}

get(x, y) {
if (x < 0 || y < 0 || x >= this.w || y >= this.h) return null
return this.e[y * this.w + x]
}

set(x, y, val) {
if (x < 0 || y < 0 || x >= this.w || y >= this.h) return
this.e[y * this.w + x] = val
}
}

0 comments on commit 3a4e39f

Please sign in to comment.