From 66f747c219c492e9450d2d5b7bf8fb5c74d57dc2 Mon Sep 17 00:00:00 2001 From: Mikola Lysenko Date: Tue, 9 Apr 2013 20:30:11 -0500 Subject: [PATCH] adding files --- .gitignore | 15 +++++++++++++++ README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ dup.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ test/test.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 dup.js create mode 100644 test/test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d1b0b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +lib-cov +*.seed +*.log +*.csv +*.dat +*.out +*.pid +*.gz + +pids +logs +results + +npm-debug.log +node_modules/* \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..8e18b8a --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# dup +Initializes an n-dimensional array to a constant value. + +## Install + + npm install dup + +## Example + +```javascript +var dup = require("dup") + +console.log(dup(5)) + +//Prints: +// +// [0, 0, 0, 0, 0] +// + + +console.log(dup([3,2], 1)) + +//Prints: +// +// [[1, 1], +// [1, 1], +// [1, 1]] +// +``` + +## `require("dup")(shape[, value])` +Initializes an array to a constant value + +* `shape` Either an array of dimensions or a scalar, representing the shape of the array to create. +* `value` The value to initialize the array with. Default is 0 + +**Returns** An n-dimensional array with the given shape and value + +## Rationale +I often find myself using [numeric.js'](http://www.numericjs.com/) `rep` method in my projects. However, for many things pulling in all of numeric.js is total overkill, especially for code that ultimately needs to run on the browser. So I decided to make this library which just implements the `rep` and nothing else in the spirit of promoting modularity by smaller and more numerous packages. + +# Credits +(c) 2013 Mikola Lysenko. MIT License \ No newline at end of file diff --git a/dup.js b/dup.js new file mode 100644 index 0000000..295044c --- /dev/null +++ b/dup.js @@ -0,0 +1,49 @@ +"use strict" + +function dupe_array(count, value, i) { + var c = count[i]|0 + if(c <= 0) { + return [] + } + var result = new Array(c), j + if(i === count.length-1) { + for(j=0; j 0) { + return dupe_number(count|0, value) + } + break + case "object": + if(typeof (count.length) === "number") { + return dupe_array(count, value, 0) + } + break + } + return [] +} + +module.exports = dupe \ No newline at end of file diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..f75c752 --- /dev/null +++ b/test/test.js @@ -0,0 +1,29 @@ +var dup = require("../dup.js") + +require("tap").test("dup", function(t) { + + t.equals(dup().length, 0) + + var a = dup(5) + t.equals(a.length, 5) + for(var i=0; i<5; ++i) { + t.equals(a[i], 0) + } + + var b = dup([3,2], 1) + t.equals(b.length, 3) + for(var i=0; i<3; ++i) { + var c = b[i] + t.equals(c.length, 2) + t.equals(c[0], 1) + t.equals(c[1], 1) + if(i > 0) { + t.assert(b[i] !== b[i-1]) + } + } + + var c = dup([], 100) + t.equals(c.length, 0) + + t.end() +}) \ No newline at end of file