Skip to content

Commit 95ada11

Browse files
committed
Merge pull request #2 from mljs/pad
Array implementation
2 parents 96bf888 + 4ac1c79 commit 95ada11

7 files changed

Lines changed: 281 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.idea

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- "0.12"
4+
- "iojs"
5+
sudo: false

History.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0.0.0 / HEAD
2+
============
3+
4+
* first release

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,70 @@
11
# pad-array
2+
3+
[![NPM version][npm-image]][npm-url]
4+
[![build status][travis-image]][travis-url]
5+
[![npm download][download-image]][download-url]
6+
7+
Function to fill an array in Javascript
8+
9+
This code is based in the MATLAB's code [padarray](http://www.mathworks.com/help/images/ref/padarray.html)
10+
11+
## Installation
12+
13+
`npm install ml-pad-array`
14+
15+
## pad-array(data, [options])
16+
17+
Pads the `data` array given the current `options` (returns a new array).
18+
19+
__Options__
20+
* padsize: Defines the number of fields that will be expanded. The default value is 1. The possible type values are:
21+
* Number: If the value is just a number it will expand in all directions with that value.
22+
* Array of numbers: It will expand in each direction given the values, for the array case the two values are `left and right` and for the matrix case are `left, up, right, down`
23+
* padval: Determine how to fill the values, if the value don't match with the next strings, the new values are going to be filled with that value. The default value is 0. The special strings are:
24+
* `'circular'`: Pad with circular repetition of elements within the dimension.
25+
* `'replicate'`: Pad by repeating border elements of array. In this case the `padsize` shouldn't be bigger than the dimensions.
26+
* `'symmetric'`: Pad array with mirror reflections of itself. In this case the `padsize` shouldn't be bigger than the dimensions.
27+
28+
## Examples
29+
30+
```js
31+
var data = [1, 2, 3, 4];
32+
33+
// default case
34+
var default_case = padArray(data);
35+
default_case === [0, 1, 2, 3, 4, 0];
36+
37+
// circular case
38+
var circular_case = padArray(data, {padsize: 5, padval: 'circular'});
39+
circular_case === [4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1];
40+
41+
// replicate case
42+
var replicate_case = padArray(data, {padsize: 3, padval: 'replicate'});
43+
replicate_case === [1, 1, 1, 1, 2, 3, 4, 4, 4, 4];
44+
45+
// symmetric case
46+
var symmetric_case = padArray(data, {padsize: 3, padval: 'symmetric'});
47+
symmetric_case === [3, 2, 1, 1, 2, 3, 4, 4, 3, 2];
48+
```
49+
50+
## Test
51+
52+
```bash
53+
$ npm install
54+
$ npm test
55+
```
56+
57+
## Authors
58+
59+
- [Miguel Asencio](https://github.com/maasencioh)
60+
61+
## License
62+
63+
[MIT](./LICENSE)
64+
65+
[npm-image]: https://img.shields.io/npm/v/ml-pad-array.svg?style=flat-square
66+
[npm-url]: https://npmjs.org/package/ml-pad-array
67+
[travis-image]: https://img.shields.io/travis/mljs/pad-array/master.svg?style=flat-square
68+
[travis-url]: https://travis-ci.org/mljs/pad-array
69+
[download-image]: https://img.shields.io/npm/dm/ml-pad-array.svg?style=flat-square
70+
[download-url]: https://npmjs.org/package/ml-pad-array

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "ml-pad-array",
3+
"version": "0.0.0",
4+
"description": "Function to fill an array in Javascript",
5+
"main": "src/index.js",
6+
"directories": {
7+
"lib": "src",
8+
"test": "test"
9+
},
10+
"scripts": {
11+
"test": "mocha --require should --reporter mocha-better-spec-reporter --recursive"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/mljs/pad-array"
16+
},
17+
"keywords": [
18+
"pad",
19+
"fill",
20+
"array",
21+
"utils"
22+
],
23+
"author": {
24+
"name": "Miguel Asencio",
25+
"email": "maasencioh@gmail.com"
26+
},
27+
"license": "MIT",
28+
"bugs": {
29+
"url": "https://github.com/mljs/pad-array/issues"
30+
},
31+
"homepage": "https://github.com/mljs/pad-array",
32+
"devDependencies": {
33+
"mocha": "latest",
34+
"mocha-better-spec-reporter": "latest",
35+
"should": "latest"
36+
}
37+
}

src/index.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
'use strict';
2+
3+
var defaultOptions = {
4+
padsize: 1,
5+
padval: 0
6+
};
7+
8+
/**
9+
* Case when the entry is an array
10+
* @param data
11+
* @param options
12+
* @returns {Array}
13+
*/
14+
function arrayCase(data, options) {
15+
var len = data.length;
16+
if (options.padsize[0] === undefined)
17+
options.padsize = [options.padsize, options.padsize];
18+
19+
var ans = new Array(len + options.padsize[0] + options.padsize[1]);
20+
var cond = len + options.padsize[0] + options.padsize[1];
21+
var i;
22+
23+
// circular option
24+
if (options.padval === 'circular') {
25+
for (i = 0; i < cond; i++) {
26+
if (i < options.padsize[0])
27+
ans[i] = data[((len - (options.padsize[0] % len)) + i) % len];
28+
else if (i < (options.padsize[0] + len))
29+
ans[i] = data[i - options.padsize[0]];
30+
else
31+
ans[i] = data[(i - options.padsize[0]) % len];
32+
}
33+
}
34+
35+
// replicate option
36+
else if (options.padval === 'replicate') {
37+
if ((options.padsize[0] > len) || (options.padsize[1] > len))
38+
throw new RangeError('expanded value should not be bigger than the data length');
39+
for (i = 0; i < cond; i++) {
40+
if (i < options.padsize[0])
41+
ans[i] = data[0];
42+
else if (i < (options.padsize[0] + len))
43+
ans[i] = data[i - options.padsize[0]];
44+
else
45+
ans[i] = data[len - 1];
46+
}
47+
}
48+
49+
// symmetric option
50+
else if (options.padval === 'symmetric') {
51+
if ((options.padsize[0] > len) || (options.padsize[1] > len))
52+
throw new RangeError('expanded value should not be bigger than the data length');
53+
for (i = 0; i < cond; i++) {
54+
if (i < options.padsize[0])
55+
ans[i] = data[options.padsize[0] - 1 - i];
56+
else if (i < (options.padsize[0] + len))
57+
ans[i] = data[i - options.padsize[0]];
58+
else
59+
ans[i] = data[2*len + options.padsize[0] - i - 1];
60+
}
61+
}
62+
63+
// default option
64+
else {
65+
for (i = 0; i < cond; i++) {
66+
if (i < options.padsize[0])
67+
ans[i] = options.padval;
68+
else if (i < (options.padsize[0] + len))
69+
ans[i] = data[i - options.padsize[0]];
70+
else
71+
ans[i] = options.padval;
72+
}
73+
}
74+
75+
return ans;
76+
}
77+
78+
/**
79+
* Case when the entry is a matrix
80+
* @param data
81+
* @param options
82+
* @returns {Array}
83+
*/
84+
function matrixCase(data, options) {
85+
var row = data.length;
86+
var col = data[0].length;
87+
if (options.padsize[0] === undefined)
88+
options.padsize = [options.padsize, options.padsize, options.padsize, options.padsize];
89+
throw new Error('matrix not supported yet, sorry');
90+
}
91+
92+
/**
93+
* Pads and array
94+
* @param {Array <number>} data
95+
* @param {object} options
96+
*/
97+
function padArray (data, options) {
98+
options = options || {};
99+
options.padsize = options.padsize || defaultOptions.padsize;
100+
options.padval = options.padval || defaultOptions.padval;
101+
102+
if (Array.isArray(data)) {
103+
if (Array.isArray(data[0]))
104+
return matrixCase(data, options);
105+
else
106+
return arrayCase(data, options);
107+
}
108+
else
109+
throw new TypeError('data should be an array');
110+
}
111+
112+
module.exports = padArray;

test/test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
var padArray = require('..');
4+
5+
describe('Array test', function () {
6+
7+
it('Default test', function () {
8+
var data = [1, 2, 3, 4];
9+
var model = padArray(data);
10+
model[0].should.equal(0);
11+
});
12+
13+
it('Replicate test', function () {
14+
var data = [1, 2, 3, 4];
15+
var options = {
16+
padsize: 3,
17+
padval: 'replicate'
18+
};
19+
var model = padArray(data, options);
20+
model[0].should.equal(1);
21+
});
22+
23+
it('Circular test', function () {
24+
var data = [1, 2, 3, 4];
25+
var options = {
26+
padsize: 5,
27+
padval: 'circular'
28+
};
29+
var model = padArray(data, options);
30+
model[0].should.equal(4);
31+
});
32+
33+
it('Symmetric test', function () {
34+
var data = [1, 2, 3, 4];
35+
var options = {
36+
padsize: 3,
37+
padval: 'symmetric'
38+
};
39+
var model = padArray(data, options);
40+
model[0].should.equal(3);
41+
});
42+
43+
it('Numeric test', function () {
44+
var data = [1, 2, 3, 4];
45+
var options = {
46+
padsize: 3,
47+
padval: 8
48+
};
49+
var model = padArray(data, options);
50+
model[0].should.equal(8);
51+
});
52+
});

0 commit comments

Comments
 (0)