From 71fa8578af4204308c56901a06579b46c7dcce4f Mon Sep 17 00:00:00 2001 From: Miguel Angel Asencio Hurtado Date: Fri, 4 Sep 2015 22:22:55 -0500 Subject: [PATCH 1/4] chore(pad-array): create initial structure --- .gitignore | 2 ++ .travis.yml | 5 +++++ History.md | 4 ++++ README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 36 ++++++++++++++++++++++++++++++++++++ src/index.js | 22 ++++++++++++++++++++++ test/test.js | 13 +++++++++++++ 7 files changed, 130 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 History.md create mode 100644 package.json create mode 100644 src/index.js create mode 100644 test/test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..34977ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +.idea \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c73310b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - "0.12" + - "iojs" +sudo: false \ No newline at end of file diff --git a/History.md b/History.md new file mode 100644 index 0000000..da83882 --- /dev/null +++ b/History.md @@ -0,0 +1,4 @@ +0.0.0 / HEAD +============ + +* first release \ No newline at end of file diff --git a/README.md b/README.md index 5ce66d3..e0f4fff 100644 --- a/README.md +++ b/README.md @@ -1 +1,49 @@ # pad-array + +[![NPM version][npm-image]][npm-url] +[![build status][travis-image]][travis-url] +[![David deps][david-image]][david-url] +[![npm download][download-image]][download-url] + +Function to pads an array in Javascript + +This code is based in the MATLAB's code [padarray](http://www.mathworks.com/help/images/ref/padarray.html) + +## Installation + +`npm install ml-pad-array` + +## Methods + +### pad-array(data, [options]) + +Pads the array `data` given the current `options`. + +__Options__ +* padsize: +* padval: +* direction: + +## Test + +```bash +$ npm install +$ npm test +``` + +## Authors + +- [Miguel Asencio](https://github.com/maasencioh) + +## License + +[MIT](./LICENSE) + +[npm-image]: https://img.shields.io/npm/v/ml-pad-array.svg?style=flat-square +[npm-url]: https://npmjs.org/package/ml-pad-array +[travis-image]: https://img.shields.io/travis/mljs/pad-array/master.svg?style=flat-square +[travis-url]: https://travis-ci.org/mljs/pad-array +[david-image]: https://img.shields.io/david/mljs/pad-array.svg?style=flat-square +[david-url]: https://david-dm.org/mljs/pad-array +[download-image]: https://img.shields.io/npm/dm/ml-pad-array.svg?style=flat-square +[download-url]: https://npmjs.org/package/ml-pad-array diff --git a/package.json b/package.json new file mode 100644 index 0000000..119a4f3 --- /dev/null +++ b/package.json @@ -0,0 +1,36 @@ +{ + "name": "ml-pad-array", + "version": "0.0.0", + "description": "Function to pads an array in Javascript", + "main": "src/index.js", + "directories": { + "lib": "src", + "test": "test" + }, + "scripts": { + "test": "mocha --require should --reporter mocha-better-spec-reporter --recursive" + }, + "repository": { + "type": "git", + "url": "https://github.com/mljs/pad-array" + }, + "keywords": [ + "pad", + "array", + "utils" + ], + "author": { + "name": "Miguel Asencio", + "mail": "maasencioh@gmail.com" + }, + "license": "MIT", + "bugs": { + "url": "https://github.com/mljs/pad-array/issues" + }, + "homepage": "https://github.com/mljs/pad-array", + "devDependencies": { + "mocha": "latest", + "mocha-better-spec-reporter": "latest", + "should": "latest" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..a3cd22d --- /dev/null +++ b/src/index.js @@ -0,0 +1,22 @@ +'use strict'; + +var defaultOptions = { + padsize: 1, + padval: 0, + direction: 'both' +}; + +/** + * Pads and array + * @param {Array } data + * @param {object} options + */ +function padArray (data, options) { + options = options || {}; + for (var o in defaultOptions) + if (!(options[o])) + options[o] = defaultOptions[o]; + return data; +} + +module.exports = padArray; diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..b1a06b7 --- /dev/null +++ b/test/test.js @@ -0,0 +1,13 @@ +'use strict'; + +var padArray = require('..'); + +describe('Pad array test', function () { + + it('Default test', function () { + var data = new Array(200); + for (var i = 0; i < data.length; i++) + data[i] = Math.sin(i); + var model = padArray(data); + }); +}); \ No newline at end of file From fa798a93ce88bb62fa3d3368dedb99dd0361a58a Mon Sep 17 00:00:00 2001 From: Miguel Angel Asencio Hurtado Date: Sun, 6 Sep 2015 17:42:49 -0500 Subject: [PATCH 2/4] feat(pad-array): implement method for array case --- README.md | 40 ++++++++++++++++----- package.json | 3 +- src/index.js | 98 ++++++++++++++++++++++++++++++++++++++++++++++++---- test/test.js | 47 ++++++++++++++++++++++--- 4 files changed, 169 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index e0f4fff..fc55855 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![David deps][david-image]][david-url] [![npm download][download-image]][download-url] -Function to pads an array in Javascript +Function to fill an array in Javascript This code is based in the MATLAB's code [padarray](http://www.mathworks.com/help/images/ref/padarray.html) @@ -13,16 +13,40 @@ This code is based in the MATLAB's code [padarray](http://www.mathworks.com/help `npm install ml-pad-array` -## Methods - -### pad-array(data, [options]) +## pad-array(data, [options]) -Pads the array `data` given the current `options`. +Pads the array `data` given the current options`. __Options__ -* padsize: -* padval: -* direction: +* padsize: Defines the number of fields that will be expanded. The default value is 1. The possible type values are: + * Number: If the value is just a number it will expand in all directions with that value. + * 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` +* 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: + * `'circular'`: Pad with circular repetition of elements within the dimension. + * `'replicate'`: Pad by repeating border elements of array. In this case the `padsize` shouldn't be bigger than the dimensions. + * `'symmetric'`: Pad array with mirror reflections of itself. In this case the `padsize` shouldn't be bigger than the dimensions. + +## Examples + +```js +var data = [1, 2, 3, 4]; + +// default case +var default_case = padArray(data); +default_case === [0, 1, 2, 3, 4, 0]; + +// circular case +var circular_case = padArray(data, {padsize: 5, padval: 'circular'}); +circular_case === [4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1]; + +// replicate case +var replicate_case = padArray(data, {padsize: 3, padval: 'replicate'}); +replicate_case === [1, 1, 1, 1, 2, 3, 4, 4, 4, 4]; + +// symmetric case +var symmetric_case = padArray(data, {padsize: 3, padval: 'symmetric'}); +symmetric_case === [3, 2, 1, 1, 2, 3, 4, 4, 3, 2]; +``` ## Test diff --git a/package.json b/package.json index 119a4f3..1ca36d3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ml-pad-array", "version": "0.0.0", - "description": "Function to pads an array in Javascript", + "description": "Function to fill an array in Javascript", "main": "src/index.js", "directories": { "lib": "src", @@ -16,6 +16,7 @@ }, "keywords": [ "pad", + "fill", "array", "utils" ], diff --git a/src/index.js b/src/index.js index a3cd22d..80e5d80 100644 --- a/src/index.js +++ b/src/index.js @@ -2,10 +2,91 @@ var defaultOptions = { padsize: 1, - padval: 0, - direction: 'both' + padval: 0 }; +/** + * Case when the entry is an array + * @param data + * @param options + * @returns {Array} + */ +function arrayCase(data, options) { + var len = data.length; + if (options.padsize[0] === undefined) + options.padsize = [options.padsize, options.padsize]; + + var ans = new Array(len + options.padsize[0] + options.padsize[1]); + + // circular option + if (options.padval === 'circular') { + for (var i = 0; i < (len + options.padsize[0] + options.padsize[1]); i++) { + if (i < options.padsize[0]) + ans[i] = data[((len - (options.padsize[0] % len)) + i) % len]; + else if (i < (options.padsize[0] + len)) + ans[i] = data[i - options.padsize[0]]; + else + ans[i] = data[(i - options.padsize[0]) % len]; + } + } + + // replicate option + else if (options.padval === 'replicate') { + if ((options.padsize[0] > len) || (options.padsize[1] > len)) + throw new RangeError('expanded value should not be bigger than the data length'); + for (var j = 0; j < (len + options.padsize[0] + options.padsize[1]); j++) { + if (j < options.padsize[0]) + ans[j] = data[0]; + else if (j < (options.padsize[0] + len)) + ans[j] = data[j - options.padsize[0]]; + else + ans[j] = data[len - 1]; + } + } + + // symmetric option + else if (options.padval === 'symmetric') { + if ((options.padsize[0] > len) || (options.padsize[1] > len)) + throw new RangeError('expanded value should not be bigger than the data length'); + for (var k = 0; k < (len + options.padsize[0] + options.padsize[1]); k++) { + if (k < options.padsize[0]) + ans[k] = data[options.padsize[0] - 1 - k]; + else if (k < (options.padsize[0] + len)) + ans[k] = data[k - options.padsize[0]]; + else + ans[k] = data[2*len + options.padsize[0] - k - 1]; + } + } + + // default option + else { + for (var l = 0; l < (len + options.padsize[0] + options.padsize[1]); l++) { + if (l < options.padsize[0]) + ans[l] = options.padval; + else if (l < (options.padsize[0] + len)) + ans[l] = data[l - options.padsize[0]]; + else + ans[l] = options.padval; + } + } + + return ans; +} + +/** + * Case when the entry is a matrix + * @param data + * @param options + * @returns {Array} + */ +function matrixCase(data, options) { + var row = data.length; + var col = data[0].length; + if (options.padsize[0] === undefined) + options.padsize = [options.padsize, options.padsize, options.padsize, options.padsize]; + throw new Error('matrix not supported yet, sorry'); +} + /** * Pads and array * @param {Array } data @@ -13,10 +94,15 @@ var defaultOptions = { */ function padArray (data, options) { options = options || {}; - for (var o in defaultOptions) - if (!(options[o])) - options[o] = defaultOptions[o]; - return data; + options.padsize = options.padsize || defaultOptions.padsize; + options.padval = options.padval || defaultOptions.padval; + + if (data[0] === undefined) + throw new TypeError('data should be an array'); + else if (data[0][0] === undefined) + return arrayCase(data, options); + else + return matrixCase(data, options); } module.exports = padArray; diff --git a/test/test.js b/test/test.js index b1a06b7..d1ddd22 100644 --- a/test/test.js +++ b/test/test.js @@ -2,12 +2,51 @@ var padArray = require('..'); -describe('Pad array test', function () { +describe('Array test', function () { it('Default test', function () { - var data = new Array(200); - for (var i = 0; i < data.length; i++) - data[i] = Math.sin(i); + var data = [1, 2, 3, 4]; var model = padArray(data); + model[0].should.equal(0); + }); + + it('Replicate test', function () { + var data = [1, 2, 3, 4]; + var options = { + padsize: 3, + padval: 'replicate' + }; + var model = padArray(data, options); + model[0].should.equal(1); + }); + + it('Circular test', function () { + var data = [1, 2, 3, 4]; + var options = { + padsize: 5, + padval: 'circular' + }; + var model = padArray(data, options); + model[0].should.equal(4); + }); + + it('Symmetric test', function () { + var data = [1, 2, 3, 4]; + var options = { + padsize: 3, + padval: 'symmetric' + }; + var model = padArray(data, options); + model[0].should.equal(3); + }); + + it('Numeric test', function () { + var data = [1, 2, 3, 4]; + var options = { + padsize: 3, + padval: 8 + }; + var model = padArray(data, options); + model[0].should.equal(8); }); }); \ No newline at end of file From f2995c56b07a7d0b8f6d3f76d5190b6090081a9c Mon Sep 17 00:00:00 2001 From: Miguel Angel Asencio Hurtado Date: Mon, 7 Sep 2015 10:13:13 -0500 Subject: [PATCH 3/4] docs(pad-array): update README docs --- README.md | 5 +---- package.json | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fc55855..23fe2cb 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ [![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] -[![David deps][david-image]][david-url] [![npm download][download-image]][download-url] Function to fill an array in Javascript @@ -15,7 +14,7 @@ This code is based in the MATLAB's code [padarray](http://www.mathworks.com/help ## pad-array(data, [options]) -Pads the array `data` given the current options`. +Pads the `data` array given the current options` (returns a new array). __Options__ * padsize: Defines the number of fields that will be expanded. The default value is 1. The possible type values are: @@ -67,7 +66,5 @@ $ npm test [npm-url]: https://npmjs.org/package/ml-pad-array [travis-image]: https://img.shields.io/travis/mljs/pad-array/master.svg?style=flat-square [travis-url]: https://travis-ci.org/mljs/pad-array -[david-image]: https://img.shields.io/david/mljs/pad-array.svg?style=flat-square -[david-url]: https://david-dm.org/mljs/pad-array [download-image]: https://img.shields.io/npm/dm/ml-pad-array.svg?style=flat-square [download-url]: https://npmjs.org/package/ml-pad-array diff --git a/package.json b/package.json index 1ca36d3..4496f6f 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ ], "author": { "name": "Miguel Asencio", - "mail": "maasencioh@gmail.com" + "email": "maasencioh@gmail.com" }, "license": "MIT", "bugs": { From 4ac1c798dff6a6b793841a55e9ae5e611d0bb6e3 Mon Sep 17 00:00:00 2001 From: Miguel Angel Asencio Hurtado Date: Mon, 7 Sep 2015 10:23:53 -0500 Subject: [PATCH 4/4] chore(arrayCase): uses isArray, opt. conditions and no diff vars --- README.md | 2 +- src/index.js | 52 ++++++++++++++++++++++++++++------------------------ 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 23fe2cb..9cf3f55 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ This code is based in the MATLAB's code [padarray](http://www.mathworks.com/help ## pad-array(data, [options]) -Pads the `data` array given the current options` (returns a new array). +Pads the `data` array given the current `options` (returns a new array). __Options__ * padsize: Defines the number of fields that will be expanded. The default value is 1. The possible type values are: diff --git a/src/index.js b/src/index.js index 80e5d80..b5b7e46 100644 --- a/src/index.js +++ b/src/index.js @@ -17,10 +17,12 @@ function arrayCase(data, options) { options.padsize = [options.padsize, options.padsize]; var ans = new Array(len + options.padsize[0] + options.padsize[1]); + var cond = len + options.padsize[0] + options.padsize[1]; + var i; // circular option if (options.padval === 'circular') { - for (var i = 0; i < (len + options.padsize[0] + options.padsize[1]); i++) { + for (i = 0; i < cond; i++) { if (i < options.padsize[0]) ans[i] = data[((len - (options.padsize[0] % len)) + i) % len]; else if (i < (options.padsize[0] + len)) @@ -34,13 +36,13 @@ function arrayCase(data, options) { else if (options.padval === 'replicate') { if ((options.padsize[0] > len) || (options.padsize[1] > len)) throw new RangeError('expanded value should not be bigger than the data length'); - for (var j = 0; j < (len + options.padsize[0] + options.padsize[1]); j++) { - if (j < options.padsize[0]) - ans[j] = data[0]; - else if (j < (options.padsize[0] + len)) - ans[j] = data[j - options.padsize[0]]; + for (i = 0; i < cond; i++) { + if (i < options.padsize[0]) + ans[i] = data[0]; + else if (i < (options.padsize[0] + len)) + ans[i] = data[i - options.padsize[0]]; else - ans[j] = data[len - 1]; + ans[i] = data[len - 1]; } } @@ -48,25 +50,25 @@ function arrayCase(data, options) { else if (options.padval === 'symmetric') { if ((options.padsize[0] > len) || (options.padsize[1] > len)) throw new RangeError('expanded value should not be bigger than the data length'); - for (var k = 0; k < (len + options.padsize[0] + options.padsize[1]); k++) { - if (k < options.padsize[0]) - ans[k] = data[options.padsize[0] - 1 - k]; - else if (k < (options.padsize[0] + len)) - ans[k] = data[k - options.padsize[0]]; + for (i = 0; i < cond; i++) { + if (i < options.padsize[0]) + ans[i] = data[options.padsize[0] - 1 - i]; + else if (i < (options.padsize[0] + len)) + ans[i] = data[i - options.padsize[0]]; else - ans[k] = data[2*len + options.padsize[0] - k - 1]; + ans[i] = data[2*len + options.padsize[0] - i - 1]; } } // default option else { - for (var l = 0; l < (len + options.padsize[0] + options.padsize[1]); l++) { - if (l < options.padsize[0]) - ans[l] = options.padval; - else if (l < (options.padsize[0] + len)) - ans[l] = data[l - options.padsize[0]]; + for (i = 0; i < cond; i++) { + if (i < options.padsize[0]) + ans[i] = options.padval; + else if (i < (options.padsize[0] + len)) + ans[i] = data[i - options.padsize[0]]; else - ans[l] = options.padval; + ans[i] = options.padval; } } @@ -97,12 +99,14 @@ function padArray (data, options) { options.padsize = options.padsize || defaultOptions.padsize; options.padval = options.padval || defaultOptions.padval; - if (data[0] === undefined) - throw new TypeError('data should be an array'); - else if (data[0][0] === undefined) - return arrayCase(data, options); + if (Array.isArray(data)) { + if (Array.isArray(data[0])) + return matrixCase(data, options); + else + return arrayCase(data, options); + } else - return matrixCase(data, options); + throw new TypeError('data should be an array'); } module.exports = padArray;